Skip to content

Commit 9b80685

Browse files
committed
Merge remote-tracking branch 'origin/main' into feature/create-typink
2 parents 098bc8a + c6bc16d commit 9b80685

31 files changed

+263
-312
lines changed

assets/networks/alephzero.png

6.39 KB
Loading

assets/networks/alephzero.svg

Lines changed: 8 additions & 11 deletions
Loading

assets/networks/pop-network.png

25.3 KB
Loading

assets/networks/pop-network.svg

Lines changed: 8 additions & 27 deletions
Loading

assets/wallets/polkadot-js-logo.png

7.56 KB
Loading

assets/wallets/polkadot-js-logo.svg

Lines changed: 9 additions & 1 deletion
Loading

assets/wallets/subwallet-logo.png

34 KB
Loading

assets/wallets/subwallet-logo.svg

Lines changed: 8 additions & 81 deletions
Loading

assets/wallets/talisman-logo.png

11.4 KB
Loading

assets/wallets/talisman-logo.svg

Lines changed: 8 additions & 14 deletions
Loading

e2e/zombienet/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
"e2e:test": "npx vitest --watch=false --no-file-parallelism"
88
},
99
"dependencies": {
10-
"@dedot/chaintypes": "^0.49.0",
11-
"@polkadot/keyring": "^13.2.3",
12-
"@polkadot/types": "^15.3.1",
13-
"@polkadot/util-crypto": "^13.2.3",
10+
"@dedot/chaintypes": "^0.52.0",
11+
"@polkadot/keyring": "^13.3.1",
12+
"@polkadot/types": "^15.4.1",
13+
"@polkadot/util-crypto": "^13.3.1",
1414
"dedot": "^0.6.1",
1515
"happy-dom": "^15.11.7",
1616
"typink": "workspace:*"

examples/demo-subconnect/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
},
1313
"dependencies": {
1414
"@chakra-ui/icons": "2.2.5",
15-
"@chakra-ui/react": "^2.10.4",
15+
"@chakra-ui/react": "^2.10.5",
1616
"@chakra-ui/system": "^2.6.2",
17-
"@dedot/chaintypes": "^0.48.0",
17+
"@dedot/chaintypes": "^0.52.0",
1818
"@emotion/react": "^11.14.0",
1919
"@emotion/styled": "^11.14.0",
2020
"@esbuild-plugins/node-globals-polyfill": "^0.2.3",

examples/demo-subconnect/src/components/AccountSelection.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Box, Button, Flex, Menu, MenuButton, MenuDivider, MenuItem, MenuList, Text } from '@chakra-ui/react';
22
import { useEffect, useMemo } from 'react';
33
import ConnectedWallet from '@/components/dialog/ConnectedWallet.tsx';
4-
import { formatBalance, shortenAddress } from '@/utils/string.ts';
5-
import { useBalances, useTypink } from 'typink';
4+
import { shortenAddress } from '@/utils/string.ts';
5+
import { formatBalance, useBalances, useTypink } from 'typink';
66
import { useApp } from '@/providers/AppProvider.tsx';
77
import { useConnectWallet } from '@subwallet-connect/react';
88

@@ -59,9 +59,7 @@ export default function AccountSelection() {
5959
<Text fontWeight='500'>{one.name}</Text>
6060

6161
<Text fontSize='xs'>Address: {shortenAddress(one.address)}</Text>
62-
<Text fontSize='xs'>
63-
Balance: {formatBalance(balances[one.address]?.free) || '0'} {network.symbol}
64-
</Text>
62+
<Text fontSize='xs'>Balance: {formatBalance(balances[one.address]?.free, network)}</Text>
6563
</Flex>
6664
</MenuItem>
6765
))}

examples/demo-subconnect/src/components/Psp22Board.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { Box, Button, Divider, Heading } from '@chakra-ui/react';
22
import PendingText from '@/components/shared/PendingText.tsx';
3-
import { formatBalance } from '@/utils/string.ts';
3+
import {} from '@/utils/string.ts';
44
import { ContractId } from 'contracts/deployments';
55
import { Psp22ContractApi } from 'contracts/types/psp22';
6-
import { useContract, useContractQuery, useContractTx, useTypink } from 'typink';
6+
import { formatBalance, useContract, useContractQuery, useContractTx, useTypink } from 'typink';
77
import { txToaster } from '@/utils/txToaster.tsx';
88
import { ConnectWalletButton } from '@/components/shared/ConnectWalletButton.tsx';
99

1010
export default function Psp22Board() {
1111
const { contract } = useContract<Psp22ContractApi>(ContractId.PSP22);
12-
const { defaultCaller, connectedAccount } = useTypink();
12+
const { defaultCaller, connectedAccount, network } = useTypink();
1313
const mintTx = useContractTx(contract, 'psp22MintableMint');
1414

1515
const { data: tokenName, isLoading: loadingTokenName } = useContractQuery({
@@ -98,15 +98,15 @@ export default function Psp22Board() {
9898
<Box mb={2}>
9999
Total Supply:{' '}
100100
<PendingText fontWeight='600' isLoading={loadingTotalSupply}>
101-
{formatBalance(totalSupply, tokenDecimal)} {tokenSymbol}
101+
{formatBalance(totalSupply, network)}
102102
</PendingText>
103103
</Box>
104104
<Divider my={4} />
105105
<Box>
106106
My Balance:{' '}
107107
{connectedAccount ? (
108108
<PendingText fontWeight='600' isLoading={loadingBalance}>
109-
{formatBalance(myBalance, tokenDecimal)} {tokenSymbol}
109+
{formatBalance(myBalance, network)}
110110
</PendingText>
111111
) : (
112112
<ConnectWalletButton buttonProps={{ size: 'xs' }} />

examples/demo-subconnect/src/utils/string.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,3 @@ export const shortenAddress = (address?: string): string => {
1414

1515
return `${address.substring(0, 6)}...${address.substring(length - 6, length)}`;
1616
};
17-
18-
export const formatBalance = (balance: bigint | undefined, decimal: number = 10): string => {
19-
if (typeof balance !== 'bigint') return '';
20-
21-
return (parseFloat(balance.toString()) / Math.pow(10, decimal)).toString();
22-
};

examples/demo/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
},
1313
"dependencies": {
1414
"@chakra-ui/icons": "2.2.5",
15-
"@chakra-ui/react": "^2.10.4",
15+
"@chakra-ui/react": "^2.10.5",
1616
"@chakra-ui/system": "^2.6.2",
17-
"@dedot/chaintypes": "^0.48.0",
17+
"@dedot/chaintypes": "^0.52.0",
1818
"@emotion/react": "^11.14.0",
1919
"@emotion/styled": "^11.14.0",
2020
"dedot": "^0.6.1",

examples/demo/src/components/AccountSelection.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { Box, Button, Flex, Menu, MenuButton, MenuDivider, MenuItem, MenuList, T
22
import { useEffect, useMemo } from 'react';
33
import ConnectedWallet from '@/components/dialog/ConnectedWallet.tsx';
44
import WalletSelection, { ButtonStyle } from '@/components/dialog/WalletSelection.tsx';
5-
import { formatBalance, shortenAddress } from '@/utils/string.ts';
6-
import { useBalances, useTypink } from 'typink';
5+
import { shortenAddress } from '@/utils/string.ts';
6+
import { formatBalance, useBalances, useTypink } from 'typink';
77

88
export default function AccountSelection() {
99
const { accounts, connectedAccount, setConnectedAccount, disconnect, network } = useTypink();
@@ -51,9 +51,7 @@ export default function AccountSelection() {
5151
<Text fontWeight='500'>{one.name}</Text>
5252

5353
<Text fontSize='xs'>Address: {shortenAddress(one.address)}</Text>
54-
<Text fontSize='xs'>
55-
Balance: {formatBalance(balances[one.address]?.free) || '0'} {network.symbol}
56-
</Text>
54+
<Text fontSize='xs'>Balance: {formatBalance(balances[one.address]?.free, network)}</Text>
5755
</Flex>
5856
</MenuItem>
5957
))}

examples/demo/src/components/Psp22Board.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ import { Box, Button, Checkbox, Divider, Heading, Input } from '@chakra-ui/react
22
import { useRef, useState } from 'react';
33
import WalletSelection from '@/components/dialog/WalletSelection.tsx';
44
import PendingText from '@/components/shared/PendingText.tsx';
5-
import { formatBalance } from '@/utils/string.ts';
65
import { ContractId } from 'contracts/deployments';
76
import { Psp22ContractApi } from 'contracts/types/psp22';
8-
import { useContract, useContractQuery, useContractTx, usePSP22Balance, useTypink } from 'typink';
7+
import { useContract, useContractQuery, useContractTx, usePSP22Balance, useTypink, formatBalance } from 'typink';
98
import { txToaster } from '@/utils/txToaster.tsx';
109

1110
export default function Psp22Board() {
1211
const { contract } = useContract<Psp22ContractApi>(ContractId.PSP22);
13-
const { connectedAccount } = useTypink();
12+
const { connectedAccount, network } = useTypink();
1413
const mintTx = useContractTx(contract, 'psp22MintableMint');
1514
const inputAddressRef = useRef<HTMLInputElement>(null);
1615
const [address, setAddress] = useState('');
@@ -108,7 +107,7 @@ export default function Psp22Board() {
108107
<Box mb={2}>
109108
Total Supply:{' '}
110109
<PendingText fontWeight='600' isLoading={loadingTotalSupply}>
111-
{formatBalance(totalSupply, tokenDecimal)} {tokenSymbol}
110+
{formatBalance(totalSupply, network)}
112111
</PendingText>
113112
</Box>
114113
<Divider my={4} />
@@ -126,7 +125,7 @@ export default function Psp22Board() {
126125
<Box mt={4}>
127126
Balance:{' '}
128127
<PendingText fontWeight='600' isLoading={loadingAnotherBalance}>
129-
{formatBalance(addressBalance, tokenDecimal)} {tokenSymbol}
128+
{formatBalance(addressBalance, network)}
130129
</PendingText>
131130
</Box>
132131
)}
@@ -136,7 +135,7 @@ export default function Psp22Board() {
136135
My Balance:{' '}
137136
{connectedAccount ? (
138137
<PendingText fontWeight='600' isLoading={loadingBalance}>
139-
{formatBalance(myBalance, tokenDecimal)} {tokenSymbol}
138+
{formatBalance(myBalance, network)}
140139
</PendingText>
141140
) : (
142141
<WalletSelection buttonProps={{ size: 'xs' }} />

examples/demo/src/utils/string.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,3 @@ export const shortenAddress = (address?: string): string => {
1414

1515
return `${address.substring(0, 6)}...${address.substring(length - 6, length)}`;
1616
};
17-
18-
export const formatBalance = (balance: bigint | undefined, decimal: number = 10): string => {
19-
if (typeof balance !== 'bigint') return '';
20-
21-
return (parseFloat(balance.toString()) / Math.pow(10, decimal)).toString();
22-
};

lerna.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
3-
"version": "0.0.1-alpha.2",
3+
"version": "0.0.1-alpha.4",
44
"npmClient": "yarn",
55
"granularPathspec": false
66
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
},
2525
"devDependencies": {
2626
"@testing-library/dom": "^10.4.0",
27-
"@testing-library/react": "^16.1.0",
27+
"@testing-library/react": "^16.2.0",
2828
"@testing-library/react-hooks": "^8.0.1",
2929
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
3030
"husky": "^8.0.3",

packages/typink/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typink",
3-
"version": "0.0.1-alpha.2",
3+
"version": "0.0.1-alpha.4",
44
"description": "Fully-typed react hooks to interact with ink! smart contract powered by Dedot!",
55
"author": "Thang X. Vu <thang@dedot.dev>",
66
"main": "src/index.ts",

packages/typink/src/hooks/internal/useInitializeClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export function useInitializeClient(network?: NetworkInfo, options?: UseClientOp
6161
setReady(false);
6262

6363
// TODO support light-client
64-
const provider: JsonRpcProvider = new WsProvider(network.provider);
64+
const provider: JsonRpcProvider = new WsProvider(network.providers[0]);
6565

6666
if (network.jsonRpcApi === JsonRpcApi.LEGACY) {
6767
setClient(await LegacyClient.new({ provider, cacheMetadata }));

packages/typink/src/networks/development.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export const development: NetworkInfo = {
44
id: 'development',
55
name: 'Development',
66
logo: `https://avatars.githubusercontent.com/u/47530779?s=200&v=4`,
7-
provider: 'ws://127.0.0.1:9944',
7+
providers: ['ws://127.0.0.1:9944'],
88
symbol: 'UNIT',
99
decimals: 12,
1010
};

packages/typink/src/networks/mainnet.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,29 @@ export const alephZero: NetworkInfo = {
44
id: 'alephzero',
55
name: 'Aleph Zero',
66
logo: 'https://raw.githubusercontent.com/dedotdev/typink/refs/heads/main/assets/networks/alephzero.svg',
7-
provider: 'wss://ws.azero.dev',
7+
providers: ['wss://ws.azero.dev'],
88
symbol: 'AZERO',
99
decimals: 12,
1010
jsonRpcApi: JsonRpcApi.LEGACY,
11+
subscanUrl: 'https://alephzero.subscan.io',
1112
};
1213

1314
export const astar: NetworkInfo = {
1415
id: 'astar',
1516
name: 'Astar',
1617
logo: 'https://raw.githubusercontent.com/dedotdev/typink/refs/heads/main/assets/networks/astar.png',
17-
provider: 'wss://rpc.astar.network',
18+
providers: ['wss://rpc.astar.network'],
1819
symbol: 'ASTR',
1920
decimals: 18,
21+
subscanUrl: 'https://astar.subscan.io',
2022
};
2123

2224
export const shiden: NetworkInfo = {
2325
id: 'astar_shiden',
2426
name: 'Shiden',
2527
logo: 'https://raw.githubusercontent.com/dedotdev/typink/refs/heads/main/assets/networks/shiden.png',
26-
provider: 'wss://rpc.shiden.astar.network',
28+
providers: ['wss://rpc.shiden.astar.network'],
2729
symbol: 'SDN',
2830
decimals: 18,
31+
subscanUrl: 'https://shiden.subscan.io',
2932
};

packages/typink/src/networks/testnet.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,32 @@ export const popTestnet: NetworkInfo = {
44
id: 'pop_testnet',
55
name: 'POP Testnet',
66
logo: 'https://raw.githubusercontent.com/dedotdev/typink/refs/heads/main/assets/networks/pop-network.svg',
7-
provider: 'wss://rpc1.paseo.popnetwork.xyz',
7+
providers: ['wss://rpc1.paseo.popnetwork.xyz'],
88
symbol: 'PAS',
99
decimals: 10,
1010
faucetUrl: 'https://onboard.popnetwork.xyz',
11+
pjsUrl: 'https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Frpc1.paseo.popnetwork.xyz',
1112
};
1213

1314
export const alephZeroTestnet: NetworkInfo = {
1415
id: 'alephzero_testnet',
1516
name: 'Aleph Zero Testnet',
1617
logo: 'https://raw.githubusercontent.com/dedotdev/typink/refs/heads/main/assets/networks/alephzero.svg',
17-
provider: 'wss://ws.test.azero.dev',
18+
providers: ['wss://ws.test.azero.dev'],
1819
symbol: 'TZERO',
1920
decimals: 12,
2021
faucetUrl: 'https://faucet.test.azero.dev',
2122
jsonRpcApi: JsonRpcApi.LEGACY,
23+
subscanUrl: 'https://alephzero-testnet.subscan.io',
2224
};
2325

2426
export const shibuyaTestnet: NetworkInfo = {
2527
id: 'astar_shibuya',
2628
name: 'Shibuya',
2729
logo: 'https://raw.githubusercontent.com/dedotdev/typink/refs/heads/main/assets/networks/shiden.png',
28-
provider: 'wss://rpc.shibuya.astar.network',
30+
providers: ['wss://rpc.shibuya.astar.network'],
2931
symbol: 'SBY',
3032
decimals: 18,
3133
faucetUrl: 'https://docs.astar.network/docs/build/environment/faucet',
34+
subscanUrl: 'https://shibuya.subscan.io',
3235
};

packages/typink/src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ export interface NetworkInfo {
3434
id: NetworkId;
3535
name: string;
3636
logo: string;
37-
provider: string;
37+
providers: string[];
3838
symbol: string;
3939
decimals: number;
4040
subscanUrl?: string;
41+
pjsUrl?: string;
4142
faucetUrl?: string;
4243
jsonRpcApi?: JsonRpcApi; // default to new
4344
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { describe, it, expect } from 'vitest';
2+
import { formatBalance } from '../formatBalance.js';
3+
import { TypinkError } from '../errors.js';
4+
5+
describe('formatBalance', () => {
6+
it('should work probably (withAll = true)', () => {
7+
expect(formatBalance(1e12, { decimals: 12, symbol: 'AZERO', withAll: true })).toEqual('1.00 AZERO');
8+
expect(formatBalance(1e12, { decimals: 13, symbol: 'AZERO', withAll: true })).toEqual('0.10 AZERO');
9+
expect(formatBalance(12_023_172_837_123, { decimals: 12, withAll: true })).toEqual('12.023172837123');
10+
expect(formatBalance('1', { decimals: 12, withAll: true })).toEqual('0.000000000001');
11+
expect(formatBalance(1e12, { decimals: 13, withAll: true })).toEqual('0.10');
12+
expect(formatBalance(-10000, { decimals: 4, withAll: true })).toEqual('-1.00');
13+
expect(formatBalance('-10200', { decimals: 4, withAll: true })).toEqual('-1.02');
14+
expect(formatBalance(-1e12, { decimals: 12, symbol: 'AZERO', withAll: true })).toEqual('-1.00 AZERO');
15+
expect(formatBalance(12_172_837, { decimals: 1, withAll: true })).toEqual('1,217,283.70');
16+
});
17+
18+
it('should work probably (withAll = false)', () => {
19+
expect(formatBalance(1e12, { decimals: 12, symbol: 'AZERO' })).toEqual('1.00 AZERO');
20+
expect(formatBalance(1e12, { decimals: 13, symbol: 'AZERO' })).toEqual('0.10 AZERO');
21+
expect(formatBalance(12_023_172_837_123, { decimals: 12 })).toEqual('12.0231');
22+
expect(formatBalance('1', { decimals: 12 })).toEqual('0.00');
23+
expect(formatBalance(1e12, { decimals: 13 })).toEqual('0.10');
24+
expect(formatBalance(-10000, { decimals: 4 })).toEqual('-1.00');
25+
expect(formatBalance('-10200', { decimals: 4 })).toEqual('-1.02');
26+
expect(formatBalance(-1e12, { decimals: 12, symbol: 'AZERO' })).toEqual('-1.00 AZERO');
27+
});
28+
29+
it('should work probably with locale', () => {
30+
expect(formatBalance(12_172_837, { decimals: 1, locale: 'en' })).toEqual('1,217,283.70');
31+
expect(formatBalance(12_172_837, { decimals: 1, locale: 'sl' })).toEqual('1.217.283,70');
32+
expect(formatBalance(12_172_837, { decimals: 1, locale: 'sl-si' })).toEqual('1.217.283,70');
33+
expect(formatBalance(12_172_837, { decimals: 1, locale: 'it-it' })).toEqual('1.217.283,70');
34+
expect(formatBalance(12_172_837, { decimals: 1, locale: 'ja-jp' })).toEqual('1,217,283.70');
35+
});
36+
37+
it('should throw error if input has bad chars', () => {
38+
expect(() => formatBalance('1.000000000001', { decimals: 12 })).toThrow(
39+
new TypinkError('Invalid value: 1.000000000001 at position 1, bigint was expected'),
40+
);
41+
expect(() => formatBalance('1,2', { decimals: 12, symbol: 'AZERO' })).toThrow(
42+
new TypinkError('Invalid value: 1,2 at position 1, bigint was expected'),
43+
);
44+
});
45+
46+
it('should throw error if decimals is invalid', () => {
47+
expect(() => formatBalance('1', { decimals: 12.2, symbol: 'AZERO' })).toThrow(
48+
new TypinkError('Invalid decimals: 12.2, an positive integer is expected'),
49+
);
50+
expect(() => formatBalance('1', { decimals: -12, symbol: 'AZERO' })).toThrow(
51+
new TypinkError('Invalid decimals: -12, an positive integer is expected'),
52+
);
53+
});
54+
});

0 commit comments

Comments
 (0)