@sodax/wallet-sdk-react 1.5.7-beta → 2.0.0-rc.10
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/README.md +107 -142
- package/dist/XConnector-12q0OVe5.d.ts +146 -0
- package/dist/chunk-7V7O3Q7Y.mjs +62 -0
- package/dist/chunk-C6M34IVL.mjs +86 -0
- package/dist/chunk-DQTYAMKF.mjs +1609 -0
- package/dist/chunk-FSOGMSJH.mjs +39 -0
- package/dist/chunk-IFXZQW4C.mjs +98 -0
- package/dist/chunk-JQ4H4GJ5.mjs +100 -0
- package/dist/chunk-LKSSME2J.mjs +31 -0
- package/dist/chunk-MWWVB7TD.mjs +123 -0
- package/dist/chunk-NAKCAL2M.mjs +32 -0
- package/dist/chunk-OPYSVPRW.mjs +144 -0
- package/dist/chunk-QMXBY3UI.mjs +1 -0
- package/dist/chunk-TACW7Z4D.mjs +31 -0
- package/dist/chunk-WPZOLGVB.mjs +148 -0
- package/dist/chunk-X7BHR7WS.mjs +200 -0
- package/dist/chunk-Z5GXDHGL.mjs +190 -0
- package/dist/config-DEsqgrG1.d.ts +151 -0
- package/dist/index.d.ts +768 -1498
- package/dist/index.mjs +453 -2005
- package/dist/xchains/bitcoin/index.d.ts +125 -0
- package/dist/xchains/bitcoin/index.mjs +14 -0
- package/dist/xchains/evm/index.d.ts +39 -0
- package/dist/xchains/evm/index.mjs +3 -0
- package/dist/xchains/icon/index.d.ts +37 -0
- package/dist/xchains/icon/index.mjs +5 -0
- package/dist/xchains/injective/index.d.ts +35 -0
- package/dist/xchains/injective/index.mjs +3 -0
- package/dist/xchains/near/index.d.ts +34 -0
- package/dist/xchains/near/index.mjs +4 -0
- package/dist/xchains/solana/index.d.ts +26 -0
- package/dist/xchains/solana/index.mjs +5 -0
- package/dist/xchains/stacks/index.d.ts +42 -0
- package/dist/xchains/stacks/index.mjs +3 -0
- package/dist/xchains/stellar/index.d.ts +44 -0
- package/dist/xchains/stellar/index.mjs +4 -0
- package/dist/xchains/sui/index.d.ts +37 -0
- package/dist/xchains/sui/index.mjs +5 -0
- package/docs/ADDING_A_NEW_CHAIN.md +440 -0
- package/docs/ARCHITECTURE.md +291 -0
- package/docs/BATCH_OPERATIONS.md +267 -0
- package/docs/CHAIN_DETECTION.md +216 -0
- package/docs/CONFIGURE_PROVIDER.md +360 -0
- package/docs/CONNECTORS.md +247 -0
- package/docs/CONNECT_FLOW.md +276 -0
- package/docs/EVM_SWITCH_CHAIN.md +161 -0
- package/docs/SIGN_MESSAGE.md +213 -0
- package/docs/SUB_PATH_EXPORTS.md +218 -0
- package/docs/WALLETCONNECT.md +154 -0
- package/docs/WALLET_MODAL.md +331 -0
- package/docs/WALLET_PROVIDER_BRIDGE.md +226 -0
- package/package.json +56 -22
- package/dist/index.cjs +0 -2147
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -1579
- package/dist/index.mjs.map +0 -1
- package/src/Hydrate.ts +0 -65
- package/src/SodaxWalletProvider.tsx +0 -97
- package/src/actions/getXChainType.ts +0 -8
- package/src/actions/getXService.ts +0 -33
- package/src/actions/index.ts +0 -2
- package/src/assets/wallets/hana.svg +0 -6
- package/src/assets/wallets/havah.svg +0 -76
- package/src/assets/wallets/keplr.svg +0 -30
- package/src/assets/wallets/metamask.svg +0 -60
- package/src/assets/wallets/phantom.svg +0 -4
- package/src/assets/wallets/sui.svg +0 -20
- package/src/core/XConnector.ts +0 -54
- package/src/core/XService.ts +0 -85
- package/src/core/index.ts +0 -2
- package/src/hooks/index.ts +0 -11
- package/src/hooks/useEthereumChainId.ts +0 -44
- package/src/hooks/useEvmSwitchChain.ts +0 -91
- package/src/hooks/useWalletProvider.ts +0 -206
- package/src/hooks/useXAccount.ts +0 -51
- package/src/hooks/useXAccounts.ts +0 -56
- package/src/hooks/useXBalances.ts +0 -65
- package/src/hooks/useXConnect.ts +0 -118
- package/src/hooks/useXConnection.ts +0 -72
- package/src/hooks/useXConnectors.ts +0 -72
- package/src/hooks/useXDisconnect.ts +0 -73
- package/src/hooks/useXService.ts +0 -8
- package/src/hooks/useXSignMessage.ts +0 -82
- package/src/index.ts +0 -19
- package/src/types/index.ts +0 -22
- package/src/useXWagmiStore.ts +0 -116
- package/src/utils/index.ts +0 -21
- package/src/xchains/bitcoin/BitcoinXConnector.ts +0 -34
- package/src/xchains/bitcoin/BitcoinXService.ts +0 -40
- package/src/xchains/bitcoin/OKXXConnector.ts +0 -117
- package/src/xchains/bitcoin/UnisatXConnector.ts +0 -117
- package/src/xchains/bitcoin/XverseXConnector.ts +0 -232
- package/src/xchains/bitcoin/index.ts +0 -7
- package/src/xchains/bitcoin/useBitcoinXConnectors.ts +0 -14
- package/src/xchains/evm/EvmXConnector.ts +0 -27
- package/src/xchains/evm/EvmXService.ts +0 -211
- package/src/xchains/evm/index.ts +0 -3
- package/src/xchains/icon/IconHanaXConnector.ts +0 -39
- package/src/xchains/icon/IconXService.ts +0 -117
- package/src/xchains/icon/actions.ts +0 -28
- package/src/xchains/icon/iconex/index.tsx +0 -46
- package/src/xchains/icon/index.ts +0 -2
- package/src/xchains/injective/InjectiveXConnector.ts +0 -60
- package/src/xchains/injective/InjectiveXService.ts +0 -62
- package/src/xchains/injective/actions.ts +0 -32
- package/src/xchains/injective/index.ts +0 -2
- package/src/xchains/near/NearXConnector.ts +0 -42
- package/src/xchains/near/NearXService.ts +0 -46
- package/src/xchains/near/useNearXConnectors.ts +0 -23
- package/src/xchains/solana/SolanaXConnector.ts +0 -26
- package/src/xchains/solana/SolanaXService.ts +0 -46
- package/src/xchains/solana/index.ts +0 -2
- package/src/xchains/stacks/StacksXConnector.ts +0 -63
- package/src/xchains/stacks/StacksXService.ts +0 -59
- package/src/xchains/stacks/constants.ts +0 -42
- package/src/xchains/stacks/index.ts +0 -4
- package/src/xchains/stacks/useStacksXConnectors.ts +0 -7
- package/src/xchains/stellar/CustomSorobanServer.ts +0 -93
- package/src/xchains/stellar/StellarWalletsKitXConnector.ts +0 -53
- package/src/xchains/stellar/StellarXService.ts +0 -93
- package/src/xchains/stellar/actions.ts +0 -24
- package/src/xchains/stellar/index.tsx +0 -2
- package/src/xchains/stellar/useStellarXConnectors.ts +0 -21
- package/src/xchains/stellar/utils.ts +0 -49
- package/src/xchains/sui/SuiXConnector.ts +0 -28
- package/src/xchains/sui/SuiXService.ts +0 -66
- package/src/xchains/sui/index.ts +0 -2
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import { useCallback, useMemo } from 'react';
|
|
2
|
-
import { useAccount, useSwitchChain } from 'wagmi';
|
|
3
|
-
import { baseChainInfo, type ChainId } from '@sodax/types';
|
|
4
|
-
import { getXChainType } from '@/actions';
|
|
5
|
-
import type { InjectiveXService } from '@/xchains/injective';
|
|
6
|
-
import { useXService } from '@/hooks/useXService';
|
|
7
|
-
import useEthereumChainId from './useEthereumChainId';
|
|
8
|
-
import { mainnet } from 'viem/chains';
|
|
9
|
-
import { Wallet } from '@injectivelabs/wallet-base';
|
|
10
|
-
|
|
11
|
-
interface UseEvmSwitchChainReturn {
|
|
12
|
-
isWrongChain: boolean;
|
|
13
|
-
handleSwitchChain: () => void;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Hook to handle EVM chain switching functionality
|
|
18
|
-
*
|
|
19
|
-
* @param expectedXChainId - The target chain ID to switch to (e.g. '0xa.optimism', '0x89.polygon')
|
|
20
|
-
* @returns {Object} Object containing:
|
|
21
|
-
* - isWrongChain: boolean indicating if current chain differs from expected chain
|
|
22
|
-
* - handleSwitchChain: function to trigger chain switch to expected chain
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
* ```tsx
|
|
26
|
-
* function ChainSwitchButton({ targetChain }: { targetChain: ChainId }) {
|
|
27
|
-
* const { isWrongChain, handleSwitchChain } = useEvmSwitchChain(targetChain);
|
|
28
|
-
*
|
|
29
|
-
* return (
|
|
30
|
-
* <Button onClick={handleSwitchChain} disabled={!isWrongChain}>
|
|
31
|
-
* Switch Network
|
|
32
|
-
* </Button>
|
|
33
|
-
* );
|
|
34
|
-
* }
|
|
35
|
-
* ```
|
|
36
|
-
*/
|
|
37
|
-
|
|
38
|
-
export const switchEthereumChain = async () => {
|
|
39
|
-
const metamaskProvider = (window as any).ethereum as any;
|
|
40
|
-
|
|
41
|
-
return await Promise.race([
|
|
42
|
-
metamaskProvider.request({
|
|
43
|
-
method: 'wallet_switchEthereumChain',
|
|
44
|
-
params: [{ chainId: '0x1' }],
|
|
45
|
-
}),
|
|
46
|
-
new Promise<void>(resolve =>
|
|
47
|
-
metamaskProvider.on('change', ({ chain }: { chain: { id: number } }) => {
|
|
48
|
-
if (chain?.id === 1) {
|
|
49
|
-
resolve();
|
|
50
|
-
}
|
|
51
|
-
}),
|
|
52
|
-
),
|
|
53
|
-
]);
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
export const useEvmSwitchChain = (expectedXChainId: ChainId): UseEvmSwitchChainReturn => {
|
|
57
|
-
const xChainType = getXChainType(expectedXChainId);
|
|
58
|
-
const expectedChainId = baseChainInfo[expectedXChainId].chainId as number;
|
|
59
|
-
|
|
60
|
-
const injectiveXService = useXService('INJECTIVE') as unknown as InjectiveXService;
|
|
61
|
-
const ethereumChainId = useEthereumChainId();
|
|
62
|
-
|
|
63
|
-
const { chainId } = useAccount();
|
|
64
|
-
const isWrongChain = useMemo(() => {
|
|
65
|
-
return (
|
|
66
|
-
(xChainType === 'EVM' && chainId !== expectedChainId) ||
|
|
67
|
-
(xChainType === 'INJECTIVE' &&
|
|
68
|
-
injectiveXService &&
|
|
69
|
-
injectiveXService.walletStrategy.getWallet() === Wallet.Metamask &&
|
|
70
|
-
ethereumChainId !== mainnet.id)
|
|
71
|
-
);
|
|
72
|
-
}, [xChainType, chainId, expectedChainId, ethereumChainId, injectiveXService]);
|
|
73
|
-
|
|
74
|
-
const { switchChain } = useSwitchChain();
|
|
75
|
-
|
|
76
|
-
const handleSwitchChain = useCallback(() => {
|
|
77
|
-
if (xChainType === 'INJECTIVE') {
|
|
78
|
-
switchEthereumChain();
|
|
79
|
-
} else {
|
|
80
|
-
switchChain({ chainId: expectedChainId });
|
|
81
|
-
}
|
|
82
|
-
}, [switchChain, expectedChainId, xChainType]);
|
|
83
|
-
|
|
84
|
-
return useMemo(
|
|
85
|
-
() => ({
|
|
86
|
-
isWrongChain,
|
|
87
|
-
handleSwitchChain,
|
|
88
|
-
}),
|
|
89
|
-
[isWrongChain, handleSwitchChain],
|
|
90
|
-
);
|
|
91
|
-
};
|
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
ChainId,
|
|
3
|
-
IEvmWalletProvider,
|
|
4
|
-
IIconWalletProvider,
|
|
5
|
-
IInjectiveWalletProvider,
|
|
6
|
-
INearWalletProvider,
|
|
7
|
-
ISolanaWalletProvider,
|
|
8
|
-
IStacksWalletProvider,
|
|
9
|
-
IStellarWalletProvider,
|
|
10
|
-
ISuiWalletProvider,
|
|
11
|
-
IBitcoinWalletProvider,
|
|
12
|
-
} from '@sodax/types';
|
|
13
|
-
import { useMemo } from 'react';
|
|
14
|
-
import { BitcoinXService } from '../xchains/bitcoin/BitcoinXService';
|
|
15
|
-
import type { BitcoinXConnector } from '../xchains/bitcoin/BitcoinXConnector';
|
|
16
|
-
import {
|
|
17
|
-
EvmWalletProvider,
|
|
18
|
-
IconWalletProvider,
|
|
19
|
-
SuiWalletProvider,
|
|
20
|
-
InjectiveWalletProvider,
|
|
21
|
-
StellarWalletProvider,
|
|
22
|
-
SolanaWalletProvider,
|
|
23
|
-
NearWalletProvider,
|
|
24
|
-
StacksWalletProvider,
|
|
25
|
-
} from '@sodax/wallet-sdk-core';
|
|
26
|
-
import { getXChainType } from '../actions';
|
|
27
|
-
import { usePublicClient, useWalletClient } from 'wagmi';
|
|
28
|
-
import { type SolanaXService, type StellarXService, useXAccount, useXService } from '..';
|
|
29
|
-
import type { SuiXService } from '../xchains/sui/SuiXService';
|
|
30
|
-
import { CHAIN_INFO, SupportedChainId } from '../xchains/icon/IconXService';
|
|
31
|
-
import type { InjectiveXService } from '../xchains/injective/InjectiveXService';
|
|
32
|
-
import type { NearXService } from '../xchains/near/NearXService';
|
|
33
|
-
import { useXConnection } from './useXConnection';
|
|
34
|
-
import { useXConnectors } from './useXConnectors';
|
|
35
|
-
import type { StacksXConnector } from '../xchains/stacks';
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Hook to get the appropriate wallet provider based on the chain type.
|
|
39
|
-
* Supports EVM, SUI, ICON, INJECTIVE, STELLAR, SOLANA and NEAR chains.
|
|
40
|
-
*
|
|
41
|
-
* @param {ChainId | undefined} spokeChainId - The chain ID to get the wallet provider for. Can be any valid ChainId value.
|
|
42
|
-
* @returns {EvmWalletProvider | SuiWalletProvider | IconWalletProvider | InjectiveWalletProvider | undefined}
|
|
43
|
-
* The appropriate wallet provider instance for the given chain ID, or undefined if:
|
|
44
|
-
* - No chain ID is provided
|
|
45
|
-
* - Chain type is not supported
|
|
46
|
-
* - Required wallet provider options are not available
|
|
47
|
-
*
|
|
48
|
-
* @example
|
|
49
|
-
* ```tsx
|
|
50
|
-
* // Get wallet provider for a specific chain
|
|
51
|
-
* const walletProvider = useWalletProvider('sui');
|
|
52
|
-
* ```
|
|
53
|
-
*/
|
|
54
|
-
export function useWalletProvider(
|
|
55
|
-
spokeChainId: ChainId | undefined,
|
|
56
|
-
):
|
|
57
|
-
| IEvmWalletProvider
|
|
58
|
-
| ISuiWalletProvider
|
|
59
|
-
| IIconWalletProvider
|
|
60
|
-
| IInjectiveWalletProvider
|
|
61
|
-
| IStellarWalletProvider
|
|
62
|
-
| ISolanaWalletProvider
|
|
63
|
-
| IBitcoinWalletProvider
|
|
64
|
-
| INearWalletProvider
|
|
65
|
-
| IStacksWalletProvider
|
|
66
|
-
| undefined {
|
|
67
|
-
const xChainType = getXChainType(spokeChainId);
|
|
68
|
-
// EVM-specific hooks
|
|
69
|
-
const evmPublicClient = usePublicClient();
|
|
70
|
-
|
|
71
|
-
const { data: evmWalletClient } = useWalletClient();
|
|
72
|
-
|
|
73
|
-
// Cross-chain hooks
|
|
74
|
-
const xService = useXService(getXChainType(spokeChainId));
|
|
75
|
-
const xAccount = useXAccount(spokeChainId);
|
|
76
|
-
const stacksConnection = useXConnection('STACKS');
|
|
77
|
-
const stacksConnectors = useXConnectors('STACKS');
|
|
78
|
-
const xConnection = useXConnection(xChainType);
|
|
79
|
-
|
|
80
|
-
return useMemo(() => {
|
|
81
|
-
switch (xChainType) {
|
|
82
|
-
case 'EVM': {
|
|
83
|
-
if (!evmWalletClient) {
|
|
84
|
-
return undefined;
|
|
85
|
-
}
|
|
86
|
-
if (!evmPublicClient) {
|
|
87
|
-
return undefined;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return new EvmWalletProvider({
|
|
91
|
-
walletClient: evmWalletClient,
|
|
92
|
-
publicClient: evmPublicClient,
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
case 'SUI': {
|
|
97
|
-
const suiXService = xService as SuiXService;
|
|
98
|
-
const { client, wallet, account } = {
|
|
99
|
-
client: suiXService.suiClient,
|
|
100
|
-
wallet: suiXService.suiWallet,
|
|
101
|
-
account: suiXService.suiAccount,
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
return new SuiWalletProvider({ client, wallet, account });
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
case 'ICON': {
|
|
108
|
-
const { walletAddress, rpcUrl } = {
|
|
109
|
-
walletAddress: xAccount.address,
|
|
110
|
-
rpcUrl: CHAIN_INFO[SupportedChainId.MAINNET].APIEndpoint,
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
return new IconWalletProvider({
|
|
114
|
-
walletAddress: walletAddress as `hx${string}` | undefined,
|
|
115
|
-
rpcUrl: rpcUrl as `http${string}`,
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
case 'INJECTIVE': {
|
|
120
|
-
const injectiveXService = xService as InjectiveXService;
|
|
121
|
-
if (!injectiveXService) {
|
|
122
|
-
return undefined;
|
|
123
|
-
// throw new Error('InjectiveXService is not initialized');
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
return new InjectiveWalletProvider({
|
|
127
|
-
msgBroadcaster: injectiveXService.msgBroadcaster,
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
case 'STELLAR': {
|
|
132
|
-
const stellarXService = xService as StellarXService;
|
|
133
|
-
if (!stellarXService.walletsKit) {
|
|
134
|
-
return undefined;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return new StellarWalletProvider({
|
|
138
|
-
type: 'BROWSER_EXTENSION',
|
|
139
|
-
walletsKit: stellarXService.walletsKit,
|
|
140
|
-
network: 'PUBLIC',
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
case 'SOLANA': {
|
|
145
|
-
const solanaXService = xService as SolanaXService;
|
|
146
|
-
|
|
147
|
-
if (!solanaXService.wallet) {
|
|
148
|
-
return undefined;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
if (!solanaXService.connection) {
|
|
152
|
-
return undefined;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
return new SolanaWalletProvider({
|
|
156
|
-
wallet: solanaXService.wallet,
|
|
157
|
-
endpoint: solanaXService.connection.rpcEndpoint,
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
case 'BITCOIN': {
|
|
162
|
-
if (!xConnection?.xConnectorId) return undefined;
|
|
163
|
-
const connector = BitcoinXService.getInstance().getXConnectorById(xConnection.xConnectorId) as
|
|
164
|
-
| BitcoinXConnector
|
|
165
|
-
| undefined;
|
|
166
|
-
if (!connector) return undefined;
|
|
167
|
-
// Recreate from window extension object — works after page reload without reconnect
|
|
168
|
-
return connector.recreateWalletProvider(xConnection.xAccount);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
case 'NEAR': {
|
|
172
|
-
const nearXService = xService as NearXService;
|
|
173
|
-
if (!nearXService.walletSelector) {
|
|
174
|
-
return undefined;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
return new NearWalletProvider({ wallet: nearXService.walletSelector });
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
case 'STACKS': {
|
|
181
|
-
const address = xAccount.address;
|
|
182
|
-
if (!address) {
|
|
183
|
-
return undefined;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
const activeStacksConnector = stacksConnectors.find(c => c.id === stacksConnection?.xConnectorId) as
|
|
187
|
-
| StacksXConnector
|
|
188
|
-
| undefined;
|
|
189
|
-
|
|
190
|
-
return new StacksWalletProvider({ address, provider: activeStacksConnector?.getProvider() });
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
default:
|
|
194
|
-
return undefined;
|
|
195
|
-
}
|
|
196
|
-
}, [
|
|
197
|
-
xChainType,
|
|
198
|
-
evmPublicClient,
|
|
199
|
-
evmWalletClient,
|
|
200
|
-
xService,
|
|
201
|
-
xAccount,
|
|
202
|
-
stacksConnection,
|
|
203
|
-
stacksConnectors,
|
|
204
|
-
xConnection,
|
|
205
|
-
]);
|
|
206
|
-
}
|
package/src/hooks/useXAccount.ts
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { useMemo } from 'react';
|
|
2
|
-
|
|
3
|
-
import { ChainTypeArr, type ChainId, type ChainType } from '@sodax/types';
|
|
4
|
-
|
|
5
|
-
import type { XAccount } from '../types';
|
|
6
|
-
import { useXConnection } from './useXConnection';
|
|
7
|
-
import { getXChainType } from '../actions';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Hook to get the current connected account for a specific blockchain
|
|
11
|
-
*
|
|
12
|
-
* @param chainIdentifier - The blockchain identifier (either chain type like 'EVM' or chain ID like '0xa86a.avax')
|
|
13
|
-
* @returns {XAccount} The current connected account, or undefined if no account is connected
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```ts
|
|
17
|
-
* // Using ChainType (preferred)
|
|
18
|
-
* const { address } = useXAccount('EVM');
|
|
19
|
-
*
|
|
20
|
-
* // Using ChainId
|
|
21
|
-
* const { address } = useXAccount('0xa86a.avax');
|
|
22
|
-
*
|
|
23
|
-
* // Returns: { address: string | undefined, xChainType: ChainType | undefined }
|
|
24
|
-
* ```
|
|
25
|
-
*/
|
|
26
|
-
function isChainType(chainIdentifier: ChainType | ChainId): chainIdentifier is ChainType {
|
|
27
|
-
return ChainTypeArr.includes(chainIdentifier as ChainType);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function useXAccount(chainIdentifier?: ChainType | ChainId): XAccount {
|
|
31
|
-
const resolvedChainType: ChainType | undefined = chainIdentifier
|
|
32
|
-
? isChainType(chainIdentifier)
|
|
33
|
-
? chainIdentifier
|
|
34
|
-
: getXChainType(chainIdentifier as ChainId)
|
|
35
|
-
: undefined;
|
|
36
|
-
|
|
37
|
-
const xConnection = useXConnection(resolvedChainType);
|
|
38
|
-
|
|
39
|
-
const xAccount = useMemo((): XAccount => {
|
|
40
|
-
if (!resolvedChainType) {
|
|
41
|
-
return {
|
|
42
|
-
address: undefined,
|
|
43
|
-
xChainType: undefined,
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return xConnection?.xAccount || { address: undefined, xChainType: resolvedChainType };
|
|
48
|
-
}, [resolvedChainType, xConnection]);
|
|
49
|
-
|
|
50
|
-
return xAccount;
|
|
51
|
-
}
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { useMemo } from 'react';
|
|
2
|
-
|
|
3
|
-
import type { ChainType } from '@sodax/types';
|
|
4
|
-
import { useCurrentAccount } from '@mysten/dapp-kit';
|
|
5
|
-
import { useWallet } from '@solana/wallet-adapter-react';
|
|
6
|
-
import { useAccount } from 'wagmi';
|
|
7
|
-
|
|
8
|
-
import type { XAccount } from '../types';
|
|
9
|
-
import { useXWagmiStore } from '../useXWagmiStore';
|
|
10
|
-
|
|
11
|
-
export function useXAccounts() {
|
|
12
|
-
const xChainTypes = useXWagmiStore(state => Object.keys(state.xServices));
|
|
13
|
-
const xConnections = useXWagmiStore(state => state.xConnections);
|
|
14
|
-
const { address: evmAddress } = useAccount();
|
|
15
|
-
const suiAccount = useCurrentAccount();
|
|
16
|
-
const solanaWallet = useWallet();
|
|
17
|
-
|
|
18
|
-
const xAccounts = useMemo(() => {
|
|
19
|
-
const result: Partial<Record<ChainType, XAccount>> = {};
|
|
20
|
-
for (const xChainType of xChainTypes) {
|
|
21
|
-
const xConnection = xConnections[xChainType];
|
|
22
|
-
|
|
23
|
-
if (xConnection?.xAccount) {
|
|
24
|
-
result[xChainType] = xConnection.xAccount;
|
|
25
|
-
} else {
|
|
26
|
-
result[xChainType] = {
|
|
27
|
-
address: undefined,
|
|
28
|
-
xChainType,
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (evmAddress) {
|
|
34
|
-
result['EVM'] = {
|
|
35
|
-
address: evmAddress,
|
|
36
|
-
xChainType: 'EVM',
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
if (suiAccount) {
|
|
40
|
-
result['SUI'] = {
|
|
41
|
-
address: suiAccount.address,
|
|
42
|
-
xChainType: 'SUI',
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
if (solanaWallet.publicKey) {
|
|
46
|
-
result['SOLANA'] = {
|
|
47
|
-
address: solanaWallet.publicKey.toString(),
|
|
48
|
-
xChainType: 'SOLANA',
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return result;
|
|
53
|
-
}, [xChainTypes, xConnections, evmAddress, suiAccount, solanaWallet]);
|
|
54
|
-
|
|
55
|
-
return xAccounts;
|
|
56
|
-
}
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import { getXChainType } from '@/actions';
|
|
2
|
-
import { type UseQueryResult, useQuery } from '@tanstack/react-query';
|
|
3
|
-
import type { ChainId, XToken } from '@sodax/types';
|
|
4
|
-
import { useXService } from './useXService';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Hook to fetch token balances for multiple tokens on a specific chain
|
|
8
|
-
*
|
|
9
|
-
* @param params - Query parameters object
|
|
10
|
-
* @param params.xChainId - Chain identifier (e.g. '0xa86a.avax', '0x1.base', '0x2.bsc', '0x89.polygon', '0x1.optimism')
|
|
11
|
-
* @param params.xTokens - Array of token objects to fetch balances for.
|
|
12
|
-
* @param params.address - Wallet address to fetch balances for. If undefined, returns empty object
|
|
13
|
-
*
|
|
14
|
-
* @returns UseQueryResult containing an object mapping token addresses to their balances as bigints.
|
|
15
|
-
* The balances are denominated in the token's smallest unit (e.g. wei for ETH).
|
|
16
|
-
* Returns empty object if wallet is not connected or service is unavailable.
|
|
17
|
-
*/
|
|
18
|
-
/**
|
|
19
|
-
* @example
|
|
20
|
-
* ```tsx
|
|
21
|
-
* // Example usage in a component
|
|
22
|
-
* function TokenBalances({ tokens }: { tokens: XToken[] }) {
|
|
23
|
-
* const { address } = useXAccount('EVM');
|
|
24
|
-
* const { data: balances } = useXBalances({
|
|
25
|
-
* xChainId: '0xa86a.avax',
|
|
26
|
-
* xTokens: tokens,
|
|
27
|
-
* address,
|
|
28
|
-
* });
|
|
29
|
-
*
|
|
30
|
-
* return (
|
|
31
|
-
* <div>
|
|
32
|
-
* {tokens.map(token => (
|
|
33
|
-
* <div key={token.address}>
|
|
34
|
-
* {token.symbol}: {formatUnits(balances?.[token.address] || 0n, token.decimals)}
|
|
35
|
-
* </div>
|
|
36
|
-
* ))}
|
|
37
|
-
* </div>
|
|
38
|
-
* );
|
|
39
|
-
* }
|
|
40
|
-
* ```
|
|
41
|
-
*/
|
|
42
|
-
|
|
43
|
-
export function useXBalances({
|
|
44
|
-
xChainId,
|
|
45
|
-
xTokens,
|
|
46
|
-
address,
|
|
47
|
-
}: { xChainId: ChainId; xTokens: readonly XToken[]; address: string | undefined }): UseQueryResult<{
|
|
48
|
-
[key: string]: bigint;
|
|
49
|
-
}> {
|
|
50
|
-
const xService = useXService(getXChainType(xChainId));
|
|
51
|
-
return useQuery({
|
|
52
|
-
queryKey: ['xBalances', xChainId, xTokens.map(x => x.symbol), address],
|
|
53
|
-
queryFn: async () => {
|
|
54
|
-
if (!xService) {
|
|
55
|
-
return {};
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const balances = await xService.getBalances(address, xTokens);
|
|
59
|
-
|
|
60
|
-
return balances;
|
|
61
|
-
},
|
|
62
|
-
enabled: !!xService && !!address && (xTokens?.length ?? 0) > 0,
|
|
63
|
-
refetchInterval: 5_000,
|
|
64
|
-
});
|
|
65
|
-
}
|
package/src/hooks/useXConnect.ts
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
import type { XAccount } from '@/types';
|
|
2
|
-
import { useConnectWallet } from '@mysten/dapp-kit';
|
|
3
|
-
import { useWallet } from '@solana/wallet-adapter-react';
|
|
4
|
-
import { useMutation, type UseMutationResult } from '@tanstack/react-query';
|
|
5
|
-
import { useConnect } from 'wagmi';
|
|
6
|
-
import type { XConnector } from '../core/XConnector';
|
|
7
|
-
import { useXWagmiStore } from '../useXWagmiStore';
|
|
8
|
-
import type { EvmXConnector } from '../xchains/evm';
|
|
9
|
-
import type { SolanaXConnector } from '../xchains/solana';
|
|
10
|
-
import type { SuiXConnector } from '../xchains/sui';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Hook for connecting to various blockchain wallets across different chains
|
|
14
|
-
*
|
|
15
|
-
* Handles connection logic for EVM, SUI, Solana and other supported chains.
|
|
16
|
-
* Sets up wallet connections and stores connection state in XWagmiStore.
|
|
17
|
-
*
|
|
18
|
-
* @param {void} - No parameters required
|
|
19
|
-
* @returns {UseMutationResult<XAccount | undefined, Error, XConnector>} Mutation result containing:
|
|
20
|
-
* - mutateAsync: Function to connect a wallet
|
|
21
|
-
* - isPending: Boolean indicating if connection is in progress
|
|
22
|
-
* - error: Any error that occurred
|
|
23
|
-
* - data: Connected account data if successful
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* ```ts
|
|
27
|
-
* const { mutateAsync: connect, isPending } = useXConnect();
|
|
28
|
-
*
|
|
29
|
-
* const handleConnect = async (connector: XConnector) => {
|
|
30
|
-
* try {
|
|
31
|
-
* await connect(connector);
|
|
32
|
-
* } catch (err) {
|
|
33
|
-
* console.error(err);
|
|
34
|
-
* }
|
|
35
|
-
* };
|
|
36
|
-
* ```
|
|
37
|
-
*/
|
|
38
|
-
export function useXConnect(): UseMutationResult<XAccount | undefined, Error, XConnector> {
|
|
39
|
-
const setXConnection = useXWagmiStore(state => state.setXConnection);
|
|
40
|
-
|
|
41
|
-
const { connectAsync: evmConnectAsync } = useConnect();
|
|
42
|
-
const { mutateAsync: suiConnectAsync } = useConnectWallet();
|
|
43
|
-
|
|
44
|
-
const { select, connect } = useWallet();
|
|
45
|
-
|
|
46
|
-
return useMutation({
|
|
47
|
-
mutationFn: async (xConnector: XConnector) => {
|
|
48
|
-
const xChainType = xConnector.xChainType;
|
|
49
|
-
let xAccount: XAccount | undefined;
|
|
50
|
-
|
|
51
|
-
switch (xChainType) {
|
|
52
|
-
case 'EVM':
|
|
53
|
-
await evmConnectAsync({ connector: (xConnector as EvmXConnector).connector });
|
|
54
|
-
break;
|
|
55
|
-
case 'SUI':
|
|
56
|
-
await suiConnectAsync({ wallet: (xConnector as SuiXConnector).wallet });
|
|
57
|
-
break;
|
|
58
|
-
case 'SOLANA': {
|
|
59
|
-
const walletName = (xConnector as SolanaXConnector).wallet.adapter.name;
|
|
60
|
-
|
|
61
|
-
select(walletName);
|
|
62
|
-
|
|
63
|
-
const adapter = (xConnector as SolanaXConnector).wallet.adapter;
|
|
64
|
-
|
|
65
|
-
if (!adapter) throw new Error('No adapter found for Solana wallet');
|
|
66
|
-
|
|
67
|
-
if (walletName === 'MetaMask') {
|
|
68
|
-
await new Promise<void>((resolve, reject) => {
|
|
69
|
-
const timeout = setTimeout(() => {
|
|
70
|
-
cleanup();
|
|
71
|
-
reject(new Error('Wallet connection timeout'));
|
|
72
|
-
}, 30000);
|
|
73
|
-
|
|
74
|
-
const handleConnect = () => {
|
|
75
|
-
cleanup();
|
|
76
|
-
resolve();
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
const handleError = (error: Error) => {
|
|
80
|
-
cleanup();
|
|
81
|
-
reject(error);
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
const cleanup = () => {
|
|
85
|
-
clearTimeout(timeout);
|
|
86
|
-
adapter.off('connect', handleConnect);
|
|
87
|
-
adapter.off('error', handleError);
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
adapter.on('connect', handleConnect);
|
|
91
|
-
adapter.on('error', handleError);
|
|
92
|
-
|
|
93
|
-
connect().catch(err => {
|
|
94
|
-
cleanup();
|
|
95
|
-
reject(err);
|
|
96
|
-
});
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
break;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
default:
|
|
104
|
-
xAccount = await xConnector.connect();
|
|
105
|
-
break;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if (xAccount) {
|
|
109
|
-
setXConnection(xConnector.xChainType, {
|
|
110
|
-
xAccount,
|
|
111
|
-
xConnectorId: xConnector.id,
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
return xAccount;
|
|
116
|
-
},
|
|
117
|
-
});
|
|
118
|
-
}
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import type { ChainType } from '@sodax/types';
|
|
2
|
-
import { useCurrentAccount, useCurrentWallet } from '@mysten/dapp-kit';
|
|
3
|
-
import { useWallet } from '@solana/wallet-adapter-react';
|
|
4
|
-
import { useMemo } from 'react';
|
|
5
|
-
import { useAccount, useConnections } from 'wagmi';
|
|
6
|
-
import type { XConnection } from '../types';
|
|
7
|
-
import { useXWagmiStore } from '../useXWagmiStore';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Hook for accessing connection details for a specific blockchain
|
|
11
|
-
*
|
|
12
|
-
* Retrieves the current connection state for the specified chain type,
|
|
13
|
-
* including the connected account and connector ID.
|
|
14
|
-
*
|
|
15
|
-
* @param {ChainType} xChainType - The type of blockchain to get connection details for
|
|
16
|
-
* @returns {XConnection | undefined} Connection details including account and connector ID, or undefined if not connected
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```ts
|
|
20
|
-
* const connection = useXConnection('EVM');
|
|
21
|
-
*
|
|
22
|
-
* if (connection) {
|
|
23
|
-
* console.log('Connected account:', connection.xAccount.address);
|
|
24
|
-
* console.log('Using connector:', connection.xConnectorId);
|
|
25
|
-
* }
|
|
26
|
-
* ```
|
|
27
|
-
*/
|
|
28
|
-
export function useXConnection(xChainType: ChainType | undefined): XConnection | undefined {
|
|
29
|
-
const xConnection = useXWagmiStore(state => (xChainType ? state.xConnections?.[xChainType] : undefined));
|
|
30
|
-
|
|
31
|
-
const evmConnections = useConnections();
|
|
32
|
-
const { address: evmAddress } = useAccount();
|
|
33
|
-
const suiAccount = useCurrentAccount();
|
|
34
|
-
const suiCurrentWallet = useCurrentWallet();
|
|
35
|
-
const solanaWallet = useWallet();
|
|
36
|
-
|
|
37
|
-
const xConnection2 = useMemo(() => {
|
|
38
|
-
if (!xChainType) {
|
|
39
|
-
return undefined;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
switch (xChainType) {
|
|
43
|
-
case 'EVM':
|
|
44
|
-
return {
|
|
45
|
-
xAccount: { address: evmAddress as string, xChainType },
|
|
46
|
-
xConnectorId: evmConnections?.[0]?.connector.id,
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
case 'SUI':
|
|
50
|
-
if (suiCurrentWallet.currentWallet && suiCurrentWallet.connectionStatus === 'connected') {
|
|
51
|
-
return {
|
|
52
|
-
xAccount: { address: suiAccount?.address, xChainType },
|
|
53
|
-
xConnectorId: suiCurrentWallet.currentWallet.name,
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
return undefined;
|
|
57
|
-
|
|
58
|
-
case 'SOLANA':
|
|
59
|
-
if (solanaWallet.connected) {
|
|
60
|
-
return {
|
|
61
|
-
xAccount: { address: solanaWallet.publicKey?.toString(), xChainType },
|
|
62
|
-
xConnectorId: `${solanaWallet.wallet?.adapter.name}`,
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
return undefined;
|
|
66
|
-
default:
|
|
67
|
-
return xConnection;
|
|
68
|
-
}
|
|
69
|
-
}, [xChainType, xConnection, evmAddress, suiAccount, evmConnections, suiCurrentWallet, solanaWallet]);
|
|
70
|
-
|
|
71
|
-
return xConnection2;
|
|
72
|
-
}
|