Skip to content

Commit

Permalink
feat: hook and utility class to easily deal with planck denominated n…
Browse files Browse the repository at this point in the history
…umber
  • Loading branch information
tien committed Jun 14, 2024
1 parent 400a09e commit ef4c144
Show file tree
Hide file tree
Showing 14 changed files with 842 additions and 18 deletions.
37 changes: 31 additions & 6 deletions apps/example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,31 @@ import {
useQuery,
useQueryWithRefresh,
useWallets,
useChainSpecData,
} from "@reactive-dot/react";
import { DenominatedNumber } from "@reactive-dot/utils";
import { formatDistance } from "date-fns";
import { Binary } from "polkadot-api";
import { Suspense, useState, useTransition } from "react";
import { ErrorBoundary, type FallbackProps } from "react-error-boundary";

const useNativeTokenNumberWithPlanck = (planck: bigint) => {
const chainSpecData = useChainSpecData();

return new DenominatedNumber(
planck,
chainSpecData.properties.tokenDecimals,
chainSpecData.properties.tokenSymbol,
);
};

const PendingRewards = (props: { address: string; rewards: bigint }) => (
<li>
{props.address}:{" "}
{useNativeTokenNumberWithPlanck(props.rewards).toLocaleString()}
</li>
);

const PendingPoolRewards = () => {
const accounts = useAccounts();

Expand Down Expand Up @@ -47,9 +66,11 @@ const PendingPoolRewards = () => {
</button>
<ul>
{pendingRewards.map((rewards, index) => (
<li key={index}>
{accounts.at(index)?.address}: {rewards.toLocaleString()} planck
</li>
<PendingRewards
key={index}
address={accounts.at(index)?.address ?? ""}
rewards={rewards}
/>
))}
</ul>
</article>
Expand Down Expand Up @@ -111,19 +132,23 @@ const Query = () => {
</article>
<article>
<h4>Total issuance</h4>
<p>{totalIssuance.toLocaleString()} planck</p>
<p>{useNativeTokenNumberWithPlanck(totalIssuance).toLocaleString()}</p>
</article>
<article>
<h4>Bonding duration</h4>
<p>{formatDistance(0, bondingDurationMs)}</p>
</article>
<article>
<h4>Total value staked</h4>
<p>{totalStaked?.toLocaleString()} planck</p>
<p>
{useNativeTokenNumberWithPlanck(totalStaked ?? 0n).toLocaleString()}
</p>
</article>
<article>
<h4>Total value locked in nomination Pools</h4>
<p>{totalValueLocked.toLocaleString()} planck</p>
<p>
{useNativeTokenNumberWithPlanck(totalValueLocked).toLocaleString()}
</p>
</article>
<article>
<h4>First 4 pools</h4>
Expand Down
3 changes: 2 additions & 1 deletion nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
"cache": true
},
"test": {
"dependsOn": ["^build"],
"cache": true
}
},
"parallel": 5
"parallel": 6
}
3 changes: 3 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,8 @@
"@walletconnect/universal-provider": {
"optional": true
}
},
"dependencies": {
"@reactive-dot/utils": "workspace:^"
}
}
3 changes: 0 additions & 3 deletions packages/eslint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
"private": true,
"type": "module",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"devDependencies": {
"@eslint/compat": "^1.0.3",
"@eslint/js": "^9.4.0",
Expand Down
23 changes: 23 additions & 0 deletions packages/react/src/hooks/useChainSpecData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ChainIdContext } from "../context.js";
import { chainSpecDataAtomFamily } from "../stores/client.js";
import type { ChainHookOptions } from "./types.js";
import { ReDotError } from "@reactive-dot/core";
import { useAtomValue } from "jotai";
import { useContext } from "react";

/**
* Hook for fetching the [JSON-RPC spec](https://paritytech.github.io/json-rpc-interface-spec/api/chainSpec.html).
*
* @param options - Additional options
* @returns The [JSON-RPC spec](https://paritytech.github.io/json-rpc-interface-spec/api/chainSpec.html)
*/
export const useChainSpecData = (options?: ChainHookOptions) => {
const contextChainId = useContext(ChainIdContext);
const chainId = options?.chainId ?? contextChainId;

if (chainId === undefined) {
throw new ReDotError("No chain Id provided");
}

return useAtomValue(chainSpecDataAtomFamily(chainId));
};
1 change: 1 addition & 0 deletions packages/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export {
export type { ChainHookOptions } from "./hooks/types.js";
export { useAccounts } from "./hooks/useAccounts.js";
export { useBlock } from "./hooks/useBlock.js";
export { useChainSpecData } from "./hooks/useChainSpecData.js";
export { useClient } from "./hooks/useClient.js";
export { useMutation } from "./hooks/useMutation.js";
export { useQuery, useQueryWithRefresh } from "./hooks/useQuery.js";
Expand Down
1 change: 1 addition & 0 deletions packages/utils/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
4 changes: 4 additions & 0 deletions packages/utils/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import recommended from "@reactive-dot/eslint-config";
import tseslint from "typescript-eslint";

export default tseslint.config(...recommended);
29 changes: 29 additions & 0 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@reactive-dot/utils",
"version": "0.0.0",
"type": "module",
"files": [
"src",
"build"
],
"exports": {
".": {
"default": "./build/index.js",
"types": "./src/index.ts"
}
},
"scripts": {
"dev": "tsc --build --watch",
"build": "rm -rf build && tsc --build",
"lint": "eslint src",
"test": "vitest run"
},
"devDependencies": {
"@reactive-dot/eslint-config": "workspace:^",
"@tsconfig/recommended": "^1.0.6",
"@tsconfig/strictest": "^2.0.5",
"eslint": "^9.4.0",
"typescript": "^5.4.5",
"vitest": "^1.6.0"
}
}
Loading

0 comments on commit ef4c144

Please sign in to comment.