@sodax/wallet-sdk-react 1.5.7-beta → 2.0.0-rc.2

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.
Files changed (211) hide show
  1. package/README.md +103 -145
  2. package/ai-exported/AGENTS.md +122 -0
  3. package/ai-exported/integration/README.md +102 -0
  4. package/ai-exported/integration/ai-rules.md +136 -0
  5. package/ai-exported/integration/architecture.md +181 -0
  6. package/ai-exported/integration/examples/01-minimal-evm.tsx +75 -0
  7. package/ai-exported/integration/examples/02-multi-chain-modal.tsx +169 -0
  8. package/ai-exported/integration/examples/03-nextjs-app-router.tsx +99 -0
  9. package/ai-exported/integration/examples/04-walletconnect-setup.tsx +89 -0
  10. package/ai-exported/integration/examples/README.md +29 -0
  11. package/ai-exported/integration/recipes/batch-operations.md +223 -0
  12. package/ai-exported/integration/recipes/bridge-to-sdk.md +164 -0
  13. package/ai-exported/integration/recipes/chain-detection.md +254 -0
  14. package/ai-exported/integration/recipes/connect-button.md +156 -0
  15. package/ai-exported/integration/recipes/multi-chain-modal.md +199 -0
  16. package/ai-exported/integration/recipes/setup.md +158 -0
  17. package/ai-exported/integration/recipes/sign-message.md +137 -0
  18. package/ai-exported/integration/recipes/sub-path-imports.md +95 -0
  19. package/ai-exported/integration/recipes/switch-chain.md +141 -0
  20. package/ai-exported/integration/recipes/walletconnect-setup.md +139 -0
  21. package/ai-exported/integration/reference/api-surface.md +175 -0
  22. package/ai-exported/integration/reference/chain-support.md +78 -0
  23. package/ai-exported/integration/reference/connectors.md +74 -0
  24. package/ai-exported/integration/reference/hooks.md +204 -0
  25. package/ai-exported/integration/reference/wallet-brands.md +106 -0
  26. package/ai-exported/migration/README.md +49 -0
  27. package/ai-exported/migration/ai-rules.md +144 -0
  28. package/ai-exported/migration/breaking-changes.md +305 -0
  29. package/ai-exported/migration/checklist.md +159 -0
  30. package/ai-exported/migration/recipes/connect-button.md +166 -0
  31. package/ai-exported/migration/recipes/multi-chain-modal.md +244 -0
  32. package/ai-exported/migration/recipes/ssr-setup.md +162 -0
  33. package/ai-exported/migration/recipes/walletconnect-migration.md +168 -0
  34. package/ai-exported/migration/reference/components.md +73 -0
  35. package/ai-exported/migration/reference/config.md +307 -0
  36. package/ai-exported/migration/reference/hooks.md +278 -0
  37. package/ai-exported/migration/reference/imports.md +157 -0
  38. package/dist/XConnector-B9YQTVJ4.d.ts +146 -0
  39. package/dist/chunk-2BOUGCJ7.mjs +150 -0
  40. package/dist/chunk-2BOUGCJ7.mjs.map +1 -0
  41. package/dist/chunk-66BAUK56.mjs +202 -0
  42. package/dist/chunk-66BAUK56.mjs.map +1 -0
  43. package/dist/chunk-7ULB6DW4.mjs +102 -0
  44. package/dist/chunk-7ULB6DW4.mjs.map +1 -0
  45. package/dist/chunk-BKJB527E.mjs +125 -0
  46. package/dist/chunk-BKJB527E.mjs.map +1 -0
  47. package/dist/chunk-BXJLBR4G.mjs +88 -0
  48. package/dist/chunk-BXJLBR4G.mjs.map +1 -0
  49. package/dist/chunk-E5IAZ7E6.mjs +186 -0
  50. package/dist/chunk-E5IAZ7E6.mjs.map +1 -0
  51. package/dist/chunk-MAQ47Q52.mjs +33 -0
  52. package/dist/chunk-MAQ47Q52.mjs.map +1 -0
  53. package/dist/chunk-MXZVF5HR.mjs +34 -0
  54. package/dist/chunk-MXZVF5HR.mjs.map +1 -0
  55. package/dist/chunk-N5A2TMF6.mjs +33 -0
  56. package/dist/chunk-N5A2TMF6.mjs.map +1 -0
  57. package/dist/chunk-NY7U7OJW.mjs +64 -0
  58. package/dist/chunk-NY7U7OJW.mjs.map +1 -0
  59. package/dist/chunk-PJLEJVAU.mjs +140 -0
  60. package/dist/chunk-PJLEJVAU.mjs.map +1 -0
  61. package/dist/chunk-PLCA4ZDJ.mjs +1585 -0
  62. package/dist/chunk-PLCA4ZDJ.mjs.map +1 -0
  63. package/dist/chunk-TZMKDXFA.mjs +3 -0
  64. package/dist/chunk-TZMKDXFA.mjs.map +1 -0
  65. package/dist/chunk-X2MHIWXO.mjs +100 -0
  66. package/dist/chunk-X2MHIWXO.mjs.map +1 -0
  67. package/dist/chunk-XZ7CHO2S.mjs +41 -0
  68. package/dist/chunk-XZ7CHO2S.mjs.map +1 -0
  69. package/dist/config-OlnzyEUE.d.ts +146 -0
  70. package/dist/index.cjs +2784 -1594
  71. package/dist/index.cjs.map +1 -1
  72. package/dist/index.d.ts +768 -1498
  73. package/dist/index.mjs +463 -2004
  74. package/dist/index.mjs.map +1 -1
  75. package/dist/xchains/bitcoin/index.cjs +1927 -0
  76. package/dist/xchains/bitcoin/index.cjs.map +1 -0
  77. package/dist/xchains/bitcoin/index.d.ts +125 -0
  78. package/dist/xchains/bitcoin/index.mjs +16 -0
  79. package/dist/xchains/bitcoin/index.mjs.map +1 -0
  80. package/dist/xchains/evm/index.cjs +316 -0
  81. package/dist/xchains/evm/index.cjs.map +1 -0
  82. package/dist/xchains/evm/index.d.ts +39 -0
  83. package/dist/xchains/evm/index.mjs +5 -0
  84. package/dist/xchains/evm/index.mjs.map +1 -0
  85. package/dist/xchains/icon/index.cjs +311 -0
  86. package/dist/xchains/icon/index.cjs.map +1 -0
  87. package/dist/xchains/icon/index.d.ts +37 -0
  88. package/dist/xchains/icon/index.mjs +7 -0
  89. package/dist/xchains/icon/index.mjs.map +1 -0
  90. package/dist/xchains/injective/index.cjs +223 -0
  91. package/dist/xchains/injective/index.cjs.map +1 -0
  92. package/dist/xchains/injective/index.d.ts +35 -0
  93. package/dist/xchains/injective/index.mjs +5 -0
  94. package/dist/xchains/injective/index.mjs.map +1 -0
  95. package/dist/xchains/near/index.cjs +190 -0
  96. package/dist/xchains/near/index.cjs.map +1 -0
  97. package/dist/xchains/near/index.d.ts +34 -0
  98. package/dist/xchains/near/index.mjs +6 -0
  99. package/dist/xchains/near/index.mjs.map +1 -0
  100. package/dist/xchains/solana/index.cjs +186 -0
  101. package/dist/xchains/solana/index.cjs.map +1 -0
  102. package/dist/xchains/solana/index.d.ts +26 -0
  103. package/dist/xchains/solana/index.mjs +7 -0
  104. package/dist/xchains/solana/index.mjs.map +1 -0
  105. package/dist/xchains/stacks/index.cjs +240 -0
  106. package/dist/xchains/stacks/index.cjs.map +1 -0
  107. package/dist/xchains/stacks/index.d.ts +36 -0
  108. package/dist/xchains/stacks/index.mjs +5 -0
  109. package/dist/xchains/stacks/index.mjs.map +1 -0
  110. package/dist/xchains/stellar/index.cjs +322 -0
  111. package/dist/xchains/stellar/index.cjs.map +1 -0
  112. package/dist/xchains/stellar/index.d.ts +44 -0
  113. package/dist/xchains/stellar/index.mjs +6 -0
  114. package/dist/xchains/stellar/index.mjs.map +1 -0
  115. package/dist/xchains/sui/index.cjs +248 -0
  116. package/dist/xchains/sui/index.cjs.map +1 -0
  117. package/dist/xchains/sui/index.d.ts +37 -0
  118. package/dist/xchains/sui/index.mjs +7 -0
  119. package/dist/xchains/sui/index.mjs.map +1 -0
  120. package/docs/ADDING_A_NEW_CHAIN.md +440 -0
  121. package/docs/ARCHITECTURE.md +291 -0
  122. package/docs/BATCH_OPERATIONS.md +267 -0
  123. package/docs/CHAIN_DETECTION.md +216 -0
  124. package/docs/CONFIGURE_PROVIDER.md +360 -0
  125. package/docs/CONNECTORS.md +247 -0
  126. package/docs/CONNECT_FLOW.md +276 -0
  127. package/docs/EVM_SWITCH_CHAIN.md +161 -0
  128. package/docs/SIGN_MESSAGE.md +213 -0
  129. package/docs/SUB_PATH_EXPORTS.md +246 -0
  130. package/docs/WALLETCONNECT.md +154 -0
  131. package/docs/WALLET_MODAL.md +331 -0
  132. package/docs/WALLET_PROVIDER_BRIDGE.md +226 -0
  133. package/package.json +37 -12
  134. package/skills/SKILLS.md +84 -0
  135. package/skills/bridge-to-sdk.md +148 -0
  136. package/skills/connect-button.md +116 -0
  137. package/skills/evm-only-walletconnect.md +111 -0
  138. package/skills/multi-chain-modal.md +178 -0
  139. package/skills/setup.md +107 -0
  140. package/dist/index.d.cts +0 -1579
  141. package/src/Hydrate.ts +0 -65
  142. package/src/SodaxWalletProvider.tsx +0 -97
  143. package/src/actions/getXChainType.ts +0 -8
  144. package/src/actions/getXService.ts +0 -33
  145. package/src/actions/index.ts +0 -2
  146. package/src/assets/wallets/hana.svg +0 -6
  147. package/src/assets/wallets/havah.svg +0 -76
  148. package/src/assets/wallets/keplr.svg +0 -30
  149. package/src/assets/wallets/metamask.svg +0 -60
  150. package/src/assets/wallets/phantom.svg +0 -4
  151. package/src/assets/wallets/sui.svg +0 -20
  152. package/src/core/XConnector.ts +0 -54
  153. package/src/core/XService.ts +0 -85
  154. package/src/core/index.ts +0 -2
  155. package/src/hooks/index.ts +0 -11
  156. package/src/hooks/useEthereumChainId.ts +0 -44
  157. package/src/hooks/useEvmSwitchChain.ts +0 -91
  158. package/src/hooks/useWalletProvider.ts +0 -206
  159. package/src/hooks/useXAccount.ts +0 -51
  160. package/src/hooks/useXAccounts.ts +0 -56
  161. package/src/hooks/useXBalances.ts +0 -65
  162. package/src/hooks/useXConnect.ts +0 -118
  163. package/src/hooks/useXConnection.ts +0 -72
  164. package/src/hooks/useXConnectors.ts +0 -72
  165. package/src/hooks/useXDisconnect.ts +0 -73
  166. package/src/hooks/useXService.ts +0 -8
  167. package/src/hooks/useXSignMessage.ts +0 -82
  168. package/src/index.ts +0 -19
  169. package/src/types/index.ts +0 -22
  170. package/src/useXWagmiStore.ts +0 -116
  171. package/src/utils/index.ts +0 -21
  172. package/src/xchains/bitcoin/BitcoinXConnector.ts +0 -34
  173. package/src/xchains/bitcoin/BitcoinXService.ts +0 -40
  174. package/src/xchains/bitcoin/OKXXConnector.ts +0 -117
  175. package/src/xchains/bitcoin/UnisatXConnector.ts +0 -117
  176. package/src/xchains/bitcoin/XverseXConnector.ts +0 -232
  177. package/src/xchains/bitcoin/index.ts +0 -7
  178. package/src/xchains/bitcoin/useBitcoinXConnectors.ts +0 -14
  179. package/src/xchains/evm/EvmXConnector.ts +0 -27
  180. package/src/xchains/evm/EvmXService.ts +0 -211
  181. package/src/xchains/evm/index.ts +0 -3
  182. package/src/xchains/icon/IconHanaXConnector.ts +0 -39
  183. package/src/xchains/icon/IconXService.ts +0 -117
  184. package/src/xchains/icon/actions.ts +0 -28
  185. package/src/xchains/icon/iconex/index.tsx +0 -46
  186. package/src/xchains/icon/index.ts +0 -2
  187. package/src/xchains/injective/InjectiveXConnector.ts +0 -60
  188. package/src/xchains/injective/InjectiveXService.ts +0 -62
  189. package/src/xchains/injective/actions.ts +0 -32
  190. package/src/xchains/injective/index.ts +0 -2
  191. package/src/xchains/near/NearXConnector.ts +0 -42
  192. package/src/xchains/near/NearXService.ts +0 -46
  193. package/src/xchains/near/useNearXConnectors.ts +0 -23
  194. package/src/xchains/solana/SolanaXConnector.ts +0 -26
  195. package/src/xchains/solana/SolanaXService.ts +0 -46
  196. package/src/xchains/solana/index.ts +0 -2
  197. package/src/xchains/stacks/StacksXConnector.ts +0 -63
  198. package/src/xchains/stacks/StacksXService.ts +0 -59
  199. package/src/xchains/stacks/constants.ts +0 -42
  200. package/src/xchains/stacks/index.ts +0 -4
  201. package/src/xchains/stacks/useStacksXConnectors.ts +0 -7
  202. package/src/xchains/stellar/CustomSorobanServer.ts +0 -93
  203. package/src/xchains/stellar/StellarWalletsKitXConnector.ts +0 -53
  204. package/src/xchains/stellar/StellarXService.ts +0 -93
  205. package/src/xchains/stellar/actions.ts +0 -24
  206. package/src/xchains/stellar/index.tsx +0 -2
  207. package/src/xchains/stellar/useStellarXConnectors.ts +0 -21
  208. package/src/xchains/stellar/utils.ts +0 -49
  209. package/src/xchains/sui/SuiXConnector.ts +0 -28
  210. package/src/xchains/sui/SuiXService.ts +0 -66
  211. package/src/xchains/sui/index.ts +0 -2
@@ -1,85 +0,0 @@
1
- import type { ChainType, XToken } from '@sodax/types';
2
- import type { XConnector } from './XConnector';
3
-
4
- /**
5
- * Abstract base class for blockchain service implementations.
6
- *
7
- * The XService class serves as a foundation for implementing blockchain-specific services
8
- * in a multi-chain environment. It provides a standardized interface for:
9
- * 1. Managing wallet connectors for different blockchain types
10
- * 2. Querying token balances across different chains
11
- *
12
- * Each blockchain implementation (e.g., Solana, EVM chains) extends this class
13
- * to provide chain-specific functionality while maintaining a consistent interface.
14
- *
15
- * @abstract
16
- * @class XService
17
- * @property {ChainType} xChainType - The blockchain type this service handles (e.g., 'SOLANA', 'EVM')
18
- * @property {XConnector[]} xConnectors - Available wallet connectors for this chain
19
- *
20
- */
21
- export abstract class XService {
22
- /** The blockchain type this service handles */
23
- public readonly xChainType: ChainType;
24
-
25
- /** Available wallet connectors for this chain */
26
- private xConnectors: XConnector[] = [];
27
-
28
- constructor(xChainType: ChainType) {
29
- this.xChainType = xChainType;
30
- }
31
-
32
- /**
33
- * Gets the balance of a specific token for an address
34
- * @param address The wallet address to check
35
- * @param xToken The token to get the balance for
36
- * @returns Promise resolving to the token balance as a bigint
37
- */
38
- public async getBalance(address: string | undefined, xToken: XToken): Promise<bigint> {
39
- return 0n;
40
- }
41
-
42
- /**
43
- * Gets balances for multiple tokens for an address
44
- * @param address The wallet address to check
45
- * @param xTokens Array of tokens to get balances for
46
- * @returns Promise resolving to object mapping token addresses to balances
47
- */
48
- public async getBalances(address: string | undefined, xTokens: readonly XToken[]): Promise<Record<string, bigint>> {
49
- if (!address) return {};
50
-
51
- const balancePromises = xTokens.map(async xToken => {
52
- const balance = await this.getBalance(address, xToken);
53
- return { address: xToken.address, balance };
54
- });
55
-
56
- const balances = await Promise.all(balancePromises);
57
- return balances.reduce<Record<string, bigint>>((acc, { address, balance }) => {
58
- acc[address] = balance;
59
- return acc;
60
- }, {});
61
- }
62
-
63
- /**
64
- * Gets all available connectors for this chain
65
- */
66
- public getXConnectors(): XConnector[] {
67
- return this.xConnectors;
68
- }
69
-
70
- /**
71
- * Sets the available connectors for this chain
72
- */
73
- public setXConnectors(xConnectors: XConnector[]): void {
74
- this.xConnectors = xConnectors;
75
- }
76
-
77
- /**
78
- * Gets a specific connector by its ID
79
- * @param xConnectorId The connector ID to look up
80
- * @returns The matching connector or undefined if not found
81
- */
82
- public getXConnectorById(xConnectorId: string): XConnector | undefined {
83
- return this.getXConnectors().find(xConnector => xConnector.id === xConnectorId);
84
- }
85
- }
package/src/core/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export { XService } from './XService';
2
- export { XConnector } from './XConnector';
@@ -1,11 +0,0 @@
1
- export { useXAccount } from './useXAccount';
2
- export { useXAccounts } from './useXAccounts';
3
- export { useXConnect } from './useXConnect';
4
- export { useXConnection } from './useXConnection';
5
- export { useXConnectors } from './useXConnectors';
6
- export { useXDisconnect } from './useXDisconnect';
7
- export { useXService } from './useXService';
8
- export { useXBalances } from './useXBalances';
9
- export { useEvmSwitchChain } from './useEvmSwitchChain';
10
- export { useWalletProvider } from './useWalletProvider';
11
- export { useXSignMessage } from './useXSignMessage';
@@ -1,44 +0,0 @@
1
- import type { InjectiveXService } from '@/xchains/injective';
2
- import { Wallet } from '@injectivelabs/wallet-base';
3
- import React from 'react';
4
- import { useEffect } from 'react';
5
- import { useXService } from './useXService';
6
- import type { EvmWalletStrategy } from '@injectivelabs/wallet-evm';
7
-
8
- /**
9
- * React hook that returns the current Ethereum chain ID when using MetaMask wallet for Injective.
10
- * Listens for chain changes and updates the state accordingly.
11
- *
12
- * @remarks
13
- * This hook only works with MetaMask wallet and requires the window.ethereum provider to be available.
14
- * For other wallets or when MetaMask is not available, it returns null.
15
- *
16
- * @returns The current Ethereum chain ID as a number, or null if not available/connected
17
- */
18
- export default function useEthereumChainId(): number | null {
19
- const injectiveXService = useXService('INJECTIVE') as unknown as InjectiveXService;
20
- const [ethereumChainId, setEthereumChainId] = React.useState<number | null>(null);
21
- useEffect(() => {
22
- if (!injectiveXService?.walletStrategy?.getWallet()) return;
23
- const walletStrategy = injectiveXService.walletStrategy;
24
- if (walletStrategy.getWallet() !== Wallet.Metamask) return;
25
-
26
- const getEthereumChainId = async () => {
27
- try {
28
- const chainId = await walletStrategy.getEthereumChainId();
29
- setEthereumChainId(Number.parseInt(chainId));
30
- } catch (e) {
31
- console.warn('Failed to get Ethereum chain ID:', e);
32
- }
33
- };
34
- getEthereumChainId();
35
-
36
- try {
37
- (walletStrategy.getStrategy() as EvmWalletStrategy).onChainIdChanged(getEthereumChainId);
38
- } catch (e) {
39
- console.warn('Failed to subscribe to chain ID changes:', e);
40
- }
41
- }, [injectiveXService?.walletStrategy]);
42
-
43
- return ethereumChainId;
44
- }
@@ -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
- }
@@ -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
- }