@dodoex/wallet-web3-react 0.0.1-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/babel.config.js +9 -0
- package/dist/index.js +72 -0
- package/dist/types/ClientProvider.d.ts +2 -0
- package/dist/types/LangProvider.d.ts +11 -0
- package/dist/types/WalletConnect/AccountPage.d.ts +11 -0
- package/dist/types/WalletConnect/ActivityList.d.ts +20 -0
- package/dist/types/WalletConnect/ConnectAlchemy/index.d.ts +6 -0
- package/dist/types/WalletConnect/ConnectAlchemy/useConnectAlchemy.d.ts +12 -0
- package/dist/types/WalletConnect/ConnectLedger/ErrorDialog.d.ts +4 -0
- package/dist/types/WalletConnect/ConnectLedger/LoadingDialog.d.ts +3 -0
- package/dist/types/WalletConnect/ConnectLedger/LockedDialog.d.ts +4 -0
- package/dist/types/WalletConnect/ConnectLedger/ProtocolDialog.d.ts +6 -0
- package/dist/types/WalletConnect/ConnectLedger/SelectAddressDialog.d.ts +8 -0
- package/dist/types/WalletConnect/ConnectLedger/SelectPathDialog.d.ts +5 -0
- package/dist/types/WalletConnect/ConnectLedger/helper.d.ts +2 -0
- package/dist/types/WalletConnect/ConnectLedger/index.d.ts +6 -0
- package/dist/types/WalletConnect/ConnectPage.d.ts +9 -0
- package/dist/types/WalletConnect/HasBalanceTokenList.d.ts +4 -0
- package/dist/types/WalletConnect/ReceiveTokenPage.d.ts +4 -0
- package/dist/types/WalletConnect/SendTokenPage.d.ts +5 -0
- package/dist/types/WalletConnect/WalletDialog.d.ts +6 -0
- package/dist/types/WalletConnectProvider.d.ts +72 -0
- package/dist/types/components/AddressWithLinkAndCopy.d.ts +28 -0
- package/dist/types/components/Dialog.d.ts +15 -0
- package/dist/types/components/TokenLogo.d.ts +26 -0
- package/dist/types/components/WalletTag.d.ts +7 -0
- package/dist/types/constants/localstorage.d.ts +6 -0
- package/dist/types/hooks/useConnectWalet.d.ts +30 -0
- package/dist/types/hooks/useFetchFiatPrice.d.ts +3 -0
- package/dist/types/hooks/useFetchTokensBalance.d.ts +14 -0
- package/dist/types/hooks/useHasBalanceTokenList.d.ts +21 -0
- package/dist/types/hooks/useTransactionList.d.ts +272 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/utils/formatter.d.ts +20 -0
- package/dist/types/utils/time.d.ts +3 -0
- package/dist/types/utils/utils.d.ts +2 -0
- package/lingui.config.ts +13 -0
- package/locales/en.po +251 -0
- package/locales/en.ts +1 -0
- package/locales/zh.po +249 -0
- package/locales/zh.ts +1 -0
- package/package.json +68 -0
- package/rollup.config.mjs +64 -0
- package/src/ClientProvider.tsx +15 -0
- package/src/LangProvider.tsx +32 -0
- package/src/WalletConnect/AccountPage.tsx +535 -0
- package/src/WalletConnect/ActivityList.tsx +597 -0
- package/src/WalletConnect/ConnectAlchemy/index.tsx +246 -0
- package/src/WalletConnect/ConnectAlchemy/useConnectAlchemy.ts +101 -0
- package/src/WalletConnect/ConnectLedger/ErrorDialog.tsx +61 -0
- package/src/WalletConnect/ConnectLedger/LoadingDialog.tsx +106 -0
- package/src/WalletConnect/ConnectLedger/LockedDialog.tsx +54 -0
- package/src/WalletConnect/ConnectLedger/ProtocolDialog.tsx +61 -0
- package/src/WalletConnect/ConnectLedger/SelectAddressDialog.tsx +326 -0
- package/src/WalletConnect/ConnectLedger/SelectPathDialog.tsx +68 -0
- package/src/WalletConnect/ConnectLedger/helper.ts +14 -0
- package/src/WalletConnect/ConnectLedger/index.tsx +89 -0
- package/src/WalletConnect/ConnectPage.tsx +459 -0
- package/src/WalletConnect/HasBalanceTokenList.tsx +201 -0
- package/src/WalletConnect/ReceiveTokenPage.tsx +143 -0
- package/src/WalletConnect/SendTokenPage.tsx +249 -0
- package/src/WalletConnect/WalletDialog.tsx +72 -0
- package/src/WalletConnectProvider.tsx +54 -0
- package/src/components/AddressWithLinkAndCopy.tsx +200 -0
- package/src/components/Dialog.tsx +155 -0
- package/src/components/TokenLogo.tsx +165 -0
- package/src/components/WalletTag.tsx +113 -0
- package/src/constants/localstorage.ts +22 -0
- package/src/hooks/useConnectWalet.ts +144 -0
- package/src/hooks/useFetchFiatPrice.ts +51 -0
- package/src/hooks/useFetchTokensBalance.ts +52 -0
- package/src/hooks/useHasBalanceTokenList.ts +157 -0
- package/src/hooks/useTransactionList.ts +87 -0
- package/src/index.tsx +4 -0
- package/src/utils/formatter.ts +102 -0
- package/src/utils/time.ts +21 -0
- package/src/utils/utils.ts +8 -0
- package/tsconfig.json +22 -0
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import WalletWeb3, { useWalletStore, Wallet } from '@dodoex/wallet-web3';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
|
|
4
|
+
const SETTINGS_ACCOUNT_RISK_CONFIRM = '0.settings:account:risk-confirm';
|
|
5
|
+
|
|
6
|
+
export interface WalletItem {
|
|
7
|
+
title: string;
|
|
8
|
+
icon: string;
|
|
9
|
+
currentType: string;
|
|
10
|
+
onClick: (
|
|
11
|
+
providerPackageOptions?: Partial<WalletWeb3['providerConfig']>,
|
|
12
|
+
) => void;
|
|
13
|
+
disabled?: boolean;
|
|
14
|
+
isLastConnection: boolean;
|
|
15
|
+
isInstalled?: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const useConnectWallet = ({
|
|
19
|
+
walletWeb3,
|
|
20
|
+
matchTestChain,
|
|
21
|
+
showOtherInjectWallet,
|
|
22
|
+
selectedChainId,
|
|
23
|
+
hasTermsOfServiceLink,
|
|
24
|
+
}: {
|
|
25
|
+
walletWeb3?: WalletWeb3;
|
|
26
|
+
/** Match the test chain and display the first bit of showName and chainIds as the matching test chain. If it is false or not transmitted, the main network information will still be displayed. */
|
|
27
|
+
matchTestChain?: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* No need to display when switching wallets
|
|
30
|
+
*/
|
|
31
|
+
showOtherInjectWallet?: boolean;
|
|
32
|
+
selectedChainId?: number;
|
|
33
|
+
hasTermsOfServiceLink?: boolean;
|
|
34
|
+
}) => {
|
|
35
|
+
const activeWalletType = useWalletStore((state) => state.walletType);
|
|
36
|
+
|
|
37
|
+
const { walletList } = useWalletListByNetwork(
|
|
38
|
+
selectedChainId,
|
|
39
|
+
walletWeb3,
|
|
40
|
+
showOtherInjectWallet,
|
|
41
|
+
);
|
|
42
|
+
const [userReadAndChecked, setUserReadAndChecked] = React.useState<boolean>(
|
|
43
|
+
hasTermsOfServiceLink
|
|
44
|
+
? localStorage.getItem(SETTINGS_ACCOUNT_RISK_CONFIRM) === '1' || true
|
|
45
|
+
: true,
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
const handleChangeUserReadAndChecked = (val: boolean) => {
|
|
49
|
+
localStorage.setItem(SETTINGS_ACCOUNT_RISK_CONFIRM, val ? '1' : '0');
|
|
50
|
+
setUserReadAndChecked(val);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
walletList,
|
|
55
|
+
userReadAndChecked,
|
|
56
|
+
handleChangeUserReadAndChecked,
|
|
57
|
+
activeWalletType,
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export const connectToWallet = async (
|
|
62
|
+
walletWeb3: WalletWeb3 | undefined,
|
|
63
|
+
wallet: Wallet,
|
|
64
|
+
config?: Partial<WalletWeb3['providerConfig']>,
|
|
65
|
+
) => {
|
|
66
|
+
if (!walletWeb3 || !walletWeb3.connectWalletBefore(wallet)) return;
|
|
67
|
+
const providerConfig = {
|
|
68
|
+
...config,
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
return walletWeb3.connectToWallet(wallet, providerConfig);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export function useWalletListByNetwork(
|
|
75
|
+
chainId?: number,
|
|
76
|
+
walletWeb3?: WalletWeb3,
|
|
77
|
+
showOtherInjectWallet?: boolean,
|
|
78
|
+
closeSingleWalletConnectModal?: () => void,
|
|
79
|
+
) {
|
|
80
|
+
const [walletList, setWalletList] = React.useState<WalletItem[]>([]);
|
|
81
|
+
const disabledWalletKeyList = useWalletStore(
|
|
82
|
+
(state) => state.disabledWalletKeyList,
|
|
83
|
+
);
|
|
84
|
+
const eip6963WalletList = useWalletStore((state) => state.eip6963WalletList);
|
|
85
|
+
|
|
86
|
+
React.useEffect(() => {
|
|
87
|
+
const computed = async () => {
|
|
88
|
+
if (walletWeb3 && chainId !== undefined) {
|
|
89
|
+
const walletListOrigin = walletWeb3.getWalletList(chainId, {
|
|
90
|
+
eip6963WalletList,
|
|
91
|
+
});
|
|
92
|
+
const list = [] as WalletItem[];
|
|
93
|
+
let len = walletListOrigin.length;
|
|
94
|
+
for (let i = 0; i < len; i++) {
|
|
95
|
+
const item = walletListOrigin[i];
|
|
96
|
+
let isInstalled = false;
|
|
97
|
+
if (item.injected) {
|
|
98
|
+
isInstalled = item.injected();
|
|
99
|
+
}
|
|
100
|
+
if (!isInstalled && item.isInstalled) {
|
|
101
|
+
isInstalled = await item.isInstalled?.();
|
|
102
|
+
}
|
|
103
|
+
list.push({
|
|
104
|
+
title: item.showName,
|
|
105
|
+
icon: item.logo,
|
|
106
|
+
currentType: item.type,
|
|
107
|
+
onClick: (providerPackageOptions) => {
|
|
108
|
+
try {
|
|
109
|
+
return connectToWallet(
|
|
110
|
+
walletWeb3,
|
|
111
|
+
item,
|
|
112
|
+
providerPackageOptions,
|
|
113
|
+
);
|
|
114
|
+
} catch (error) {
|
|
115
|
+
if (closeSingleWalletConnectModal) {
|
|
116
|
+
closeSingleWalletConnectModal();
|
|
117
|
+
}
|
|
118
|
+
throw error;
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
disabled: disabledWalletKeyList.includes(
|
|
122
|
+
`${item.type}-${item.showName}`,
|
|
123
|
+
),
|
|
124
|
+
isLastConnection:
|
|
125
|
+
item.type === walletWeb3.connectController.getLastConnectType(),
|
|
126
|
+
isInstalled,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
setWalletList(list);
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
computed();
|
|
133
|
+
}, [
|
|
134
|
+
walletWeb3,
|
|
135
|
+
chainId,
|
|
136
|
+
showOtherInjectWallet,
|
|
137
|
+
disabledWalletKeyList,
|
|
138
|
+
eip6963WalletList,
|
|
139
|
+
]);
|
|
140
|
+
|
|
141
|
+
return {
|
|
142
|
+
walletList,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { ChainId, platformIdMap, RestApiRequests } from '@dodoex/api';
|
|
2
|
+
import { useQuery } from '@tanstack/react-query';
|
|
3
|
+
import { TokenInfo } from '../components/TokenLogo';
|
|
4
|
+
import { setTokenFiatPriceList } from '../constants/localstorage';
|
|
5
|
+
import { useWalletConnectContext } from '../WalletConnectProvider';
|
|
6
|
+
|
|
7
|
+
export function getFiatPriceQueryKeys(tokens?: TokenInfo[]) {
|
|
8
|
+
return ['fetch', 'fetchFiatPrice', tokens];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function useFetchFiatPrice(tokens: TokenInfo[]) {
|
|
12
|
+
const { encryptFiatPriceToken, restApiRequests: restApiRequestsProps } =
|
|
13
|
+
useWalletConnectContext();
|
|
14
|
+
return useQuery({
|
|
15
|
+
enabled: !!tokens.length,
|
|
16
|
+
queryKey: getFiatPriceQueryKeys(tokens),
|
|
17
|
+
queryFn: async () => {
|
|
18
|
+
const token = encryptFiatPriceToken?.() || '';
|
|
19
|
+
const restApiRequests = restApiRequestsProps ?? new RestApiRequests();
|
|
20
|
+
const path = `/frontend-v2-price-api/current/batch`;
|
|
21
|
+
const { result } = await restApiRequests.postJson(
|
|
22
|
+
path,
|
|
23
|
+
{
|
|
24
|
+
networks: tokens.map(
|
|
25
|
+
(token) => platformIdMap[token.chainId as ChainId],
|
|
26
|
+
),
|
|
27
|
+
addresses: tokens.map((token) => token.address),
|
|
28
|
+
symbols: tokens.map((token) => token.symbol),
|
|
29
|
+
isCache: true,
|
|
30
|
+
},
|
|
31
|
+
undefined,
|
|
32
|
+
{
|
|
33
|
+
headers: {
|
|
34
|
+
'pass-key': token,
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
);
|
|
38
|
+
const resultMap = new Map<string, number>();
|
|
39
|
+
if (result.data) {
|
|
40
|
+
result.data.forEach((item: { price: string; address: string }) => {
|
|
41
|
+
const tokenUSD = Number(item.price);
|
|
42
|
+
if (!Number.isNaN(tokenUSD) && tokenUSD > 0) {
|
|
43
|
+
resultMap.set(item.address, tokenUSD);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
setTokenFiatPriceList(Object.fromEntries(resultMap.entries()));
|
|
47
|
+
}
|
|
48
|
+
return resultMap;
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { getFetchERC20BalanceOfQueryOptions } from '@dodoex/dodo-contract-request';
|
|
2
|
+
import { useQueries } from '@tanstack/react-query';
|
|
3
|
+
import { TokenInfo } from '../components/TokenLogo';
|
|
4
|
+
|
|
5
|
+
type TokenInfoMap = Map<string, bigint>;
|
|
6
|
+
|
|
7
|
+
export default function useFetchTokensBalance({
|
|
8
|
+
account,
|
|
9
|
+
tokenList,
|
|
10
|
+
blockNumber,
|
|
11
|
+
skip,
|
|
12
|
+
}: {
|
|
13
|
+
account: string | undefined;
|
|
14
|
+
tokenList?: TokenInfo[];
|
|
15
|
+
blockNumber?: number;
|
|
16
|
+
skip?: boolean;
|
|
17
|
+
}) {
|
|
18
|
+
const tokensQueries = useQueries({
|
|
19
|
+
queries: (tokenList ?? []).map((token) => {
|
|
20
|
+
const query = getFetchERC20BalanceOfQueryOptions(
|
|
21
|
+
skip ? undefined : token.chainId,
|
|
22
|
+
token?.address,
|
|
23
|
+
account,
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
queryKey: blockNumber
|
|
28
|
+
? [...query.queryKey, blockNumber]
|
|
29
|
+
: query.queryKey,
|
|
30
|
+
enabled: query.enabled && !skip,
|
|
31
|
+
queryFn: query.queryFn,
|
|
32
|
+
};
|
|
33
|
+
}),
|
|
34
|
+
combine: (results) => {
|
|
35
|
+
const tokenInfoMap = new Map() as TokenInfoMap;
|
|
36
|
+
results.forEach((result, i) => {
|
|
37
|
+
const token = tokenList?.[i];
|
|
38
|
+
if (result.data && token) {
|
|
39
|
+
tokenInfoMap.set(`${token.chainId}-${token.address}`, result.data);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
return {
|
|
43
|
+
tokenInfoMap,
|
|
44
|
+
data: results.map((result) => result.data),
|
|
45
|
+
isPending: results.some((result) => result.isPending),
|
|
46
|
+
isLoading: results.some((result) => result.isLoading),
|
|
47
|
+
};
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
return tokensQueries;
|
|
52
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import BigNumber from 'bignumber.js';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { useQueryClient } from '@tanstack/react-query';
|
|
4
|
+
import { isEqualWith } from 'lodash';
|
|
5
|
+
import { useWalletConnectContext } from '../WalletConnectProvider';
|
|
6
|
+
import useFetchTokensBalance from './useFetchTokensBalance';
|
|
7
|
+
import { useFetchFiatPrice } from './useFetchFiatPrice';
|
|
8
|
+
import { getFetchERC20HelperIsERC20QueryOptions } from '@dodoex/dodo-contract-request';
|
|
9
|
+
import { getTokenFiatPriceList } from '../constants/localstorage';
|
|
10
|
+
import { TokenInfo } from '../components/TokenLogo';
|
|
11
|
+
|
|
12
|
+
interface TokenResult extends TokenInfo {
|
|
13
|
+
balance: BigNumber;
|
|
14
|
+
}
|
|
15
|
+
export function useHasBalanceTokenList({
|
|
16
|
+
account,
|
|
17
|
+
chainId,
|
|
18
|
+
visible,
|
|
19
|
+
}: {
|
|
20
|
+
account: string | undefined;
|
|
21
|
+
chainId: number;
|
|
22
|
+
visible: boolean;
|
|
23
|
+
}) {
|
|
24
|
+
const queryClient = useQueryClient();
|
|
25
|
+
const [hasBalanceTokenList, setHasBalanceTokenList] = React.useState<
|
|
26
|
+
Array<TokenResult>
|
|
27
|
+
>([]);
|
|
28
|
+
const fiatPriceQuery = useFetchFiatPrice(hasBalanceTokenList);
|
|
29
|
+
|
|
30
|
+
const { tokenList } = useWalletConnectContext();
|
|
31
|
+
const fetchTokenQuery = useFetchTokensBalance({
|
|
32
|
+
account,
|
|
33
|
+
tokenList,
|
|
34
|
+
skip: !visible,
|
|
35
|
+
});
|
|
36
|
+
React.useEffect(() => {
|
|
37
|
+
let time: NodeJS.Timeout;
|
|
38
|
+
const commonKey = getFetchERC20HelperIsERC20QueryOptions(
|
|
39
|
+
chainId,
|
|
40
|
+
undefined,
|
|
41
|
+
account,
|
|
42
|
+
undefined,
|
|
43
|
+
).queryKey.filter((item) => !!item);
|
|
44
|
+
const unSubscribe = queryClient.getQueryCache().subscribe((event) => {
|
|
45
|
+
try {
|
|
46
|
+
const isNotMatch = commonKey.some(
|
|
47
|
+
(key) => !event.query.queryKey.includes(key),
|
|
48
|
+
);
|
|
49
|
+
if (!isNotMatch) {
|
|
50
|
+
clearTimeout(time);
|
|
51
|
+
time = setTimeout(() => {
|
|
52
|
+
const tokenQueriesData = queryClient.getQueriesData<bigint>({
|
|
53
|
+
queryKey: commonKey,
|
|
54
|
+
});
|
|
55
|
+
let newHasBalanceTokenList = [] as TokenResult[];
|
|
56
|
+
const hasBalanceAddressSet = new Set<string>();
|
|
57
|
+
tokenQueriesData.forEach((value) => {
|
|
58
|
+
const balance = value[1];
|
|
59
|
+
const queryKey = value[0];
|
|
60
|
+
const token = tokenList.find(
|
|
61
|
+
(token) =>
|
|
62
|
+
`${token.chainId}-${token.address}` ===
|
|
63
|
+
`${queryKey[1]}-${queryKey[2]}`,
|
|
64
|
+
);
|
|
65
|
+
if (
|
|
66
|
+
token &&
|
|
67
|
+
balance &&
|
|
68
|
+
balance > 0 &&
|
|
69
|
+
!hasBalanceAddressSet.has(token.address) &&
|
|
70
|
+
token.decimals !== undefined
|
|
71
|
+
) {
|
|
72
|
+
hasBalanceAddressSet.add(token.address);
|
|
73
|
+
newHasBalanceTokenList.push({
|
|
74
|
+
...token,
|
|
75
|
+
balance: new BigNumber(balance.toString()).div(
|
|
76
|
+
10 ** token.decimals,
|
|
77
|
+
),
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
newHasBalanceTokenList = newHasBalanceTokenList.sort((a, b) =>
|
|
82
|
+
a.balance.gt(b.balance) ? -1 : 1,
|
|
83
|
+
);
|
|
84
|
+
if (
|
|
85
|
+
!isEqualWith(
|
|
86
|
+
newHasBalanceTokenList,
|
|
87
|
+
hasBalanceTokenList,
|
|
88
|
+
(newValue, oldValue, key) => {
|
|
89
|
+
if (key === 'balance' && BigNumber.isBigNumber(newValue)) {
|
|
90
|
+
return newValue.isEqualTo(oldValue);
|
|
91
|
+
}
|
|
92
|
+
return undefined;
|
|
93
|
+
},
|
|
94
|
+
)
|
|
95
|
+
) {
|
|
96
|
+
setHasBalanceTokenList(newHasBalanceTokenList);
|
|
97
|
+
}
|
|
98
|
+
}, 100);
|
|
99
|
+
}
|
|
100
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
101
|
+
} catch (error) {}
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
return () => {
|
|
105
|
+
unSubscribe();
|
|
106
|
+
clearTimeout(time);
|
|
107
|
+
};
|
|
108
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
109
|
+
}, [queryClient, account, chainId]);
|
|
110
|
+
|
|
111
|
+
const tokenLoading = fetchTokenQuery.isLoading || fetchTokenQuery.isPending;
|
|
112
|
+
|
|
113
|
+
const hasBalanceList = React.useMemo(() => {
|
|
114
|
+
return hasBalanceTokenList
|
|
115
|
+
.map((token) => {
|
|
116
|
+
const fiatPriceBalance = fiatPriceQuery.data?.get(token.address)
|
|
117
|
+
? token.balance.times(fiatPriceQuery.data?.get(token.address) ?? 0)
|
|
118
|
+
: undefined;
|
|
119
|
+
return {
|
|
120
|
+
...token,
|
|
121
|
+
fiatPriceBalance,
|
|
122
|
+
};
|
|
123
|
+
})
|
|
124
|
+
.sort((a, b) => {
|
|
125
|
+
if (a.fiatPriceBalance && b.fiatPriceBalance) {
|
|
126
|
+
return a.fiatPriceBalance.gt(b.fiatPriceBalance) ? -1 : 1;
|
|
127
|
+
}
|
|
128
|
+
const cacheFiatPriceObject = getTokenFiatPriceList();
|
|
129
|
+
const aFiatPrice = cacheFiatPriceObject[a.address];
|
|
130
|
+
const bFiatPrice = cacheFiatPriceObject[b.address];
|
|
131
|
+
if (!aFiatPrice || !bFiatPrice) return a.balance.gt(b.balance) ? -1 : 1;
|
|
132
|
+
return a.balance.times(aFiatPrice).gt(b.balance.times(bFiatPrice))
|
|
133
|
+
? -1
|
|
134
|
+
: 1;
|
|
135
|
+
});
|
|
136
|
+
}, [hasBalanceTokenList, fiatPriceQuery.data]);
|
|
137
|
+
|
|
138
|
+
const allFiatPriceBalance = React.useMemo(() => {
|
|
139
|
+
let result = new BigNumber(0);
|
|
140
|
+
hasBalanceList.forEach((token) => {
|
|
141
|
+
if (token.fiatPriceBalance) {
|
|
142
|
+
result = result.plus(token.fiatPriceBalance);
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
return result;
|
|
146
|
+
}, [hasBalanceList]);
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
tokenLoading: !!tokenLoading && !hasBalanceList.length,
|
|
150
|
+
hasBalanceList,
|
|
151
|
+
fiatPriceQuery,
|
|
152
|
+
allFiatPriceBalance,
|
|
153
|
+
allFiatPriceBalanceLoading:
|
|
154
|
+
(fiatPriceQuery.isLoading && !!hasBalanceTokenList.length) ||
|
|
155
|
+
tokenLoading,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { GraphQLRequests, SystemApi } from '@dodoex/api';
|
|
2
|
+
import { useInfiniteQuery } from '@tanstack/react-query';
|
|
3
|
+
import React from 'react';
|
|
4
|
+
import { useWalletConnectContext } from '../WalletConnectProvider';
|
|
5
|
+
|
|
6
|
+
export enum StateText {
|
|
7
|
+
Running = 'pending',
|
|
8
|
+
Success = 'success',
|
|
9
|
+
Failed = 'failed',
|
|
10
|
+
Warning = 'reset',
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type NoticeTransactionList = NonNullable<
|
|
14
|
+
NonNullable<
|
|
15
|
+
ReturnType<
|
|
16
|
+
NonNullable<
|
|
17
|
+
typeof SystemApi.graphql.fetchNoticeCenterTransactionList['__apiType']
|
|
18
|
+
>
|
|
19
|
+
>['notice_center_transactionList']
|
|
20
|
+
>['list']
|
|
21
|
+
>;
|
|
22
|
+
|
|
23
|
+
export function useTransactionList({
|
|
24
|
+
account,
|
|
25
|
+
chainId,
|
|
26
|
+
}: {
|
|
27
|
+
account: string | undefined;
|
|
28
|
+
chainId?: number;
|
|
29
|
+
}) {
|
|
30
|
+
const { graphQLRequests } = useWalletConnectContext();
|
|
31
|
+
const infiniteQueryOptions = (
|
|
32
|
+
graphQLRequests as GraphQLRequests
|
|
33
|
+
)?.getInfiniteQuery(
|
|
34
|
+
SystemApi.graphql.fetchNoticeCenterTransactionList,
|
|
35
|
+
'page',
|
|
36
|
+
{
|
|
37
|
+
where: {
|
|
38
|
+
limit: 10,
|
|
39
|
+
user: account,
|
|
40
|
+
chainId,
|
|
41
|
+
refreshNow: true,
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
) ?? {
|
|
45
|
+
queryKey: [1],
|
|
46
|
+
queryFn: () => {
|
|
47
|
+
// empty
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
const fetchQuery = useInfiniteQuery({
|
|
51
|
+
...infiniteQueryOptions,
|
|
52
|
+
enabled: !!account && !!graphQLRequests,
|
|
53
|
+
initialPageParam: 1,
|
|
54
|
+
getNextPageParam: (item) => {
|
|
55
|
+
const { page, limit, count } = item.notice_center_transactionList ?? {};
|
|
56
|
+
if (!page || !limit || !count) return null;
|
|
57
|
+
let totalPage = Math.floor(count / limit);
|
|
58
|
+
if (count % limit) {
|
|
59
|
+
totalPage += 1;
|
|
60
|
+
}
|
|
61
|
+
if (page >= totalPage) return null;
|
|
62
|
+
return page + 1;
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const [list, pendingList] = React.useMemo(() => {
|
|
67
|
+
const list = [] as NoticeTransactionList;
|
|
68
|
+
|
|
69
|
+
fetchQuery.data?.pages?.forEach((page) => {
|
|
70
|
+
page.notice_center_transactionList?.list?.forEach((item) => {
|
|
71
|
+
if (!item?.extend?.safeTxHash) {
|
|
72
|
+
list.push(item);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
const pendingList =
|
|
77
|
+
list.filter((item) => item?.extend.status === StateText.Running) ?? [];
|
|
78
|
+
|
|
79
|
+
return [list, pendingList];
|
|
80
|
+
}, [fetchQuery.data]);
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
...fetchQuery,
|
|
84
|
+
list,
|
|
85
|
+
pendingList,
|
|
86
|
+
};
|
|
87
|
+
}
|
package/src/index.tsx
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import BigNumber from 'bignumber.js';
|
|
2
|
+
|
|
3
|
+
const kilo = 1000;
|
|
4
|
+
const million = 1000000;
|
|
5
|
+
// const billion = 1000000000;
|
|
6
|
+
function getNegative(num: number) {
|
|
7
|
+
return new BigNumber(num).negated();
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* format to short number, like: -0.12 -> 0, 0.0000123->0.000012, 123.234 -> 123.23, 1234.12 -> 1.23K, 1000000.123->1.00M
|
|
11
|
+
* @param n
|
|
12
|
+
*/
|
|
13
|
+
export function formatShortNumber(n?: BigNumber, showDecimals = 4): string {
|
|
14
|
+
if (!n || n.isNaN()) {
|
|
15
|
+
return '-';
|
|
16
|
+
}
|
|
17
|
+
if (n.eq(0)) {
|
|
18
|
+
return '0';
|
|
19
|
+
}
|
|
20
|
+
if (n.lte(0.000001) && n.gte(-0.000001)) {
|
|
21
|
+
return n.toExponential(2);
|
|
22
|
+
}
|
|
23
|
+
if (n.lt(1) && n.gt(-1)) {
|
|
24
|
+
return formatReadableNumber({ input: n, showDecimals });
|
|
25
|
+
}
|
|
26
|
+
if (n.lt(kilo) && n.gt(getNegative(kilo))) {
|
|
27
|
+
return formatReadableNumber({ input: n, showDecimals });
|
|
28
|
+
}
|
|
29
|
+
if (n.lt(million) && n.gt(getNegative(million))) {
|
|
30
|
+
return `${formatReadableNumber({ input: n.div(kilo), showDecimals: 2 })}K`;
|
|
31
|
+
}
|
|
32
|
+
return `${formatReadableNumber({
|
|
33
|
+
input: n.div(million),
|
|
34
|
+
showDecimals: 2,
|
|
35
|
+
})}M`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* format to readable number, like: 0.00 -> 0, 1.00 -> 1, 1.235 -> 1.23, 1.230 -> 1.23
|
|
40
|
+
* @param input
|
|
41
|
+
*/
|
|
42
|
+
export function formatReadableNumber({
|
|
43
|
+
input,
|
|
44
|
+
showDecimals = 4,
|
|
45
|
+
showPrecisionDecimals = 2,
|
|
46
|
+
exponentialDecimalsAmount = 8,
|
|
47
|
+
showIntegerOnly = false,
|
|
48
|
+
showDecimalsOnly = false,
|
|
49
|
+
noGroupSeparator = false,
|
|
50
|
+
roundingMode = BigNumber.ROUND_DOWN,
|
|
51
|
+
}: {
|
|
52
|
+
input: BigNumber | number | string;
|
|
53
|
+
showDecimals?: number;
|
|
54
|
+
showIntegerOnly?: boolean;
|
|
55
|
+
showDecimalsOnly?: boolean;
|
|
56
|
+
showPrecisionDecimals?: number;
|
|
57
|
+
exponentialDecimalsAmount?: number;
|
|
58
|
+
noGroupSeparator?: boolean;
|
|
59
|
+
roundingMode?: BigNumber.RoundingMode;
|
|
60
|
+
}): string {
|
|
61
|
+
const source = new BigNumber(input);
|
|
62
|
+
if (source.isNaN()) {
|
|
63
|
+
return '-';
|
|
64
|
+
}
|
|
65
|
+
let amount = source.dp(showDecimals, roundingMode);
|
|
66
|
+
if (amount.eq(0) && (source.gt(0) || source.lt(0))) {
|
|
67
|
+
const significantDigits = showPrecisionDecimals ?? showDecimals;
|
|
68
|
+
amount = source.sd(significantDigits, BigNumber.ROUND_DOWN);
|
|
69
|
+
const amountDP = amount.dp();
|
|
70
|
+
if (
|
|
71
|
+
amountDP &&
|
|
72
|
+
amountDP > exponentialDecimalsAmount &&
|
|
73
|
+
!showDecimalsOnly &&
|
|
74
|
+
!showIntegerOnly
|
|
75
|
+
) {
|
|
76
|
+
return amount.toExponential();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
amount = source.sd(
|
|
80
|
+
showPrecisionDecimals ?? showDecimals,
|
|
81
|
+
BigNumber.ROUND_DOWN,
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
if (showIntegerOnly) {
|
|
85
|
+
amount = amount.integerValue(BigNumber.ROUND_DOWN);
|
|
86
|
+
}
|
|
87
|
+
if (showDecimalsOnly) {
|
|
88
|
+
amount = amount.minus(amount.integerValue(BigNumber.ROUND_DOWN));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (noGroupSeparator) {
|
|
92
|
+
return amount.toFormat({
|
|
93
|
+
decimalSeparator: '.',
|
|
94
|
+
groupSeparator: '',
|
|
95
|
+
groupSize: 3,
|
|
96
|
+
secondaryGroupSize: 0,
|
|
97
|
+
fractionGroupSeparator: '',
|
|
98
|
+
fractionGroupSize: 0,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
return amount.toFormat();
|
|
102
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import dayjs from 'dayjs';
|
|
2
|
+
|
|
3
|
+
export function formatReadableTimeAgo({ time }: { time: number }) {
|
|
4
|
+
if (!time) {
|
|
5
|
+
return '';
|
|
6
|
+
}
|
|
7
|
+
const start = dayjs(time);
|
|
8
|
+
const end = dayjs();
|
|
9
|
+
const diffHours = end.diff(start, 'h');
|
|
10
|
+
if (diffHours > 24) {
|
|
11
|
+
return start.format('YYYY/MM/DD HH:MM');
|
|
12
|
+
}
|
|
13
|
+
if (diffHours >= 1) {
|
|
14
|
+
return `${diffHours}h ago`;
|
|
15
|
+
}
|
|
16
|
+
const diffMinute = end.diff(start, 'm');
|
|
17
|
+
if (diffMinute >= 1) {
|
|
18
|
+
return `${diffMinute}min ago`;
|
|
19
|
+
}
|
|
20
|
+
return `${end.diff(start, 's')}s`;
|
|
21
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"allowJs": true,
|
|
4
|
+
"skipLibCheck": true,
|
|
5
|
+
"strict": true,
|
|
6
|
+
"forceConsistentCasingInFileNames": true,
|
|
7
|
+
"noEmit": false,
|
|
8
|
+
"esModuleInterop": true,
|
|
9
|
+
"module": "esnext",
|
|
10
|
+
"moduleResolution": "node",
|
|
11
|
+
"resolveJsonModule": true,
|
|
12
|
+
"isolatedModules": true,
|
|
13
|
+
"downlevelIteration": true,
|
|
14
|
+
"newLine": "CRLF",
|
|
15
|
+
"declaration": true,
|
|
16
|
+
"declarationDir": "./dist/types",
|
|
17
|
+
"jsx": "preserve",
|
|
18
|
+
"target": "es2016"
|
|
19
|
+
},
|
|
20
|
+
"exclude": ["node_modules"],
|
|
21
|
+
"include": ["src/**/*.d.ts", "src/**/*.ts", "src/**/*.tsx"]
|
|
22
|
+
}
|