@sodax/wallet-sdk-react 0.0.1-rc.5 → 0.0.1-rc.6
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 +21 -35
- package/dist/index.cjs +260 -397
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +37 -15540
- package/dist/index.d.ts +37 -15540
- package/dist/index.mjs +255 -373
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/Hydrate.ts +62 -0
- package/src/SodaxWalletProvider.tsx +18 -20
- package/src/actions/getXChainType.ts +2 -4
- package/src/core/XService.ts +4 -10
- package/src/hooks/index.ts +1 -1
- package/src/hooks/useEvmSwitchChain.ts +2 -3
- package/src/hooks/useWalletProvider.ts +23 -18
- package/src/hooks/useXBalances.ts +1 -1
- package/src/hooks/useXConnect.ts +1 -2
- package/src/hooks/useXSignMessage.ts +82 -0
- package/src/index.ts +0 -1
- package/src/types/index.ts +0 -23
- package/src/useXWagmiStore.ts +91 -0
- package/src/utils/index.ts +2 -16
- package/src/xchains/evm/EvmXConnector.ts +1 -1
- package/src/xchains/evm/EvmXService.ts +32 -75
- package/src/xchains/icon/IconXService.ts +2 -2
- package/src/xchains/icon/actions.ts +28 -0
- package/src/xchains/injective/InjectiveKelprXConnector.ts +1 -1
- package/src/xchains/injective/InjectiveMetamaskXConnector.ts +1 -1
- package/src/xchains/injective/actions.ts +25 -0
- package/src/xchains/stellar/StellarXService.ts +1 -2
- package/src/xchains/stellar/actions.ts +24 -0
- package/src/constants/index.ts +0 -1
- package/src/constants/xChains.ts +0 -164
- package/src/hooks/useWalletProviderOptions.ts +0 -51
- package/src/useXWagmiStore.tsx +0 -164
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sodax/wallet-sdk-react",
|
|
3
3
|
"license": "MIT",
|
|
4
|
-
"version": "0.0.1-rc.
|
|
4
|
+
"version": "0.0.1-rc.6",
|
|
5
5
|
"description": "Wallet SDK of Sodax",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/index.cjs",
|
|
@@ -45,8 +45,8 @@
|
|
|
45
45
|
"viem": "2.29.2",
|
|
46
46
|
"wagmi": "^2.14.12",
|
|
47
47
|
"zustand": "4.5.2",
|
|
48
|
-
"@sodax/wallet-sdk-core": "0.0.1-rc.
|
|
49
|
-
"@sodax/types": "0.0.1-rc.
|
|
48
|
+
"@sodax/wallet-sdk-core": "0.0.1-rc.7",
|
|
49
|
+
"@sodax/types": "0.0.1-rc.18"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@types/react": "^19.0.8",
|
package/src/Hydrate.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useCurrentAccount, useCurrentWallet, useSuiClient } from '@mysten/dapp-kit';
|
|
4
|
+
import { useEffect } from 'react';
|
|
5
|
+
import { EvmXService } from './xchains/evm';
|
|
6
|
+
import { SolanaXService } from './xchains/solana/SolanaXService';
|
|
7
|
+
import { SuiXService } from './xchains/sui';
|
|
8
|
+
import { useAnchorProvider } from './xchains/solana/hooks/useAnchorProvider';
|
|
9
|
+
import { useConnection, useWallet } from '@solana/wallet-adapter-react';
|
|
10
|
+
import { useConfig } from 'wagmi';
|
|
11
|
+
|
|
12
|
+
export const Hydrate = () => {
|
|
13
|
+
// sui
|
|
14
|
+
const suiClient = useSuiClient();
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
if (suiClient) {
|
|
17
|
+
SuiXService.getInstance().suiClient = suiClient;
|
|
18
|
+
}
|
|
19
|
+
}, [suiClient]);
|
|
20
|
+
const { currentWallet: suiWallet } = useCurrentWallet();
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
if (suiWallet) {
|
|
23
|
+
SuiXService.getInstance().suiWallet = suiWallet;
|
|
24
|
+
}
|
|
25
|
+
}, [suiWallet]);
|
|
26
|
+
const suiAccount = useCurrentAccount();
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
if (suiAccount) {
|
|
29
|
+
SuiXService.getInstance().suiAccount = suiAccount;
|
|
30
|
+
}
|
|
31
|
+
}, [suiAccount]);
|
|
32
|
+
|
|
33
|
+
// solana
|
|
34
|
+
const { connection: solanaConnection } = useConnection();
|
|
35
|
+
const solanaWallet = useWallet();
|
|
36
|
+
const solanaProvider = useAnchorProvider();
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
if (solanaConnection) {
|
|
39
|
+
SolanaXService.getInstance().connection = solanaConnection;
|
|
40
|
+
}
|
|
41
|
+
}, [solanaConnection]);
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
if (solanaWallet) {
|
|
44
|
+
SolanaXService.getInstance().wallet = solanaWallet;
|
|
45
|
+
}
|
|
46
|
+
}, [solanaWallet]);
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
if (solanaProvider) {
|
|
49
|
+
SolanaXService.getInstance().provider = solanaProvider;
|
|
50
|
+
}
|
|
51
|
+
}, [solanaProvider]);
|
|
52
|
+
|
|
53
|
+
// evm
|
|
54
|
+
const wagmiConfig = useConfig();
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
if (wagmiConfig) {
|
|
57
|
+
EvmXService.getInstance().wagmiConfig = wagmiConfig;
|
|
58
|
+
}
|
|
59
|
+
}, [wagmiConfig]);
|
|
60
|
+
|
|
61
|
+
return null;
|
|
62
|
+
};
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
3
|
// biome-ignore lint/style/useImportType: <explanation>
|
|
4
|
-
import React from 'react';
|
|
5
|
-
import { useEffect, useMemo } from 'react';
|
|
4
|
+
import React, { useMemo } from 'react';
|
|
6
5
|
|
|
7
6
|
// sui
|
|
8
7
|
import { SuiClientProvider, WalletProvider as SuiWalletProvider } from '@mysten/dapp-kit';
|
|
@@ -17,34 +16,29 @@ import {
|
|
|
17
16
|
WalletProvider as SolanaWalletProvider,
|
|
18
17
|
} from '@solana/wallet-adapter-react';
|
|
19
18
|
import { UnsafeBurnerWalletAdapter } from '@solana/wallet-adapter-wallets';
|
|
20
|
-
import type { XConfig } from './types';
|
|
21
|
-
import { initXWagmiStore, InitXWagmiStore } from './useXWagmiStore';
|
|
22
19
|
|
|
23
|
-
import {
|
|
20
|
+
import type { RpcConfig } from '@sodax/types';
|
|
24
21
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
22
|
+
import { Hydrate } from './Hydrate';
|
|
23
|
+
import { createWagmiConfig } from './xchains/evm/EvmXService';
|
|
24
|
+
import { reconnectIcon } from './xchains/icon/actions';
|
|
25
|
+
// import { reconnectInjective } from './xchains/injective/actions';
|
|
26
|
+
import { reconnectStellar } from './xchains/stellar/actions';
|
|
29
27
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
28
|
+
export const SodaxWalletProvider = ({ children, rpcConfig }: { children: React.ReactNode; rpcConfig: RpcConfig }) => {
|
|
29
|
+
const wagmiConfig = useMemo(() => {
|
|
30
|
+
return createWagmiConfig(rpcConfig);
|
|
31
|
+
}, [rpcConfig]);
|
|
34
32
|
|
|
35
33
|
const wallets = useMemo(() => [new UnsafeBurnerWalletAdapter()], []);
|
|
36
34
|
|
|
37
|
-
const wagmiConfig = useMemo(() => {
|
|
38
|
-
return getWagmiConfig(chains);
|
|
39
|
-
}, [chains]);
|
|
40
|
-
|
|
41
35
|
return (
|
|
42
|
-
<WagmiProvider config={wagmiConfig}>
|
|
36
|
+
<WagmiProvider config={wagmiConfig} reconnectOnMount={true}>
|
|
43
37
|
<SuiClientProvider networks={{ mainnet: { url: getFullnodeUrl('mainnet') } }} defaultNetwork="mainnet">
|
|
44
38
|
<SuiWalletProvider autoConnect={true}>
|
|
45
|
-
<SolanaConnectionProvider endpoint={
|
|
39
|
+
<SolanaConnectionProvider endpoint={rpcConfig['solana'] ?? ''}>
|
|
46
40
|
<SolanaWalletProvider wallets={wallets} autoConnect>
|
|
47
|
-
<
|
|
41
|
+
<Hydrate />
|
|
48
42
|
{children}
|
|
49
43
|
</SolanaWalletProvider>
|
|
50
44
|
</SolanaConnectionProvider>
|
|
@@ -53,3 +47,7 @@ export const SodaxWalletProvider = ({ children, config }: { children: React.Reac
|
|
|
53
47
|
</WagmiProvider>
|
|
54
48
|
);
|
|
55
49
|
};
|
|
50
|
+
|
|
51
|
+
reconnectIcon();
|
|
52
|
+
// reconnectInjective();
|
|
53
|
+
reconnectStellar();
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import type
|
|
2
|
-
|
|
3
|
-
import { xChainMap } from '@/constants/xChains';
|
|
1
|
+
import { baseChainInfo, type ChainId, type ChainType } from '@sodax/types';
|
|
4
2
|
|
|
5
3
|
export function getXChainType(xChainId: ChainId | undefined): ChainType | undefined {
|
|
6
4
|
if (!xChainId) {
|
|
7
5
|
return undefined;
|
|
8
6
|
}
|
|
9
|
-
return
|
|
7
|
+
return baseChainInfo[xChainId].type;
|
|
10
8
|
}
|
package/src/core/XService.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ChainType, XToken } from '@sodax/types';
|
|
2
2
|
import type { XConnector } from './XConnector';
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -33,10 +33,9 @@ export abstract class XService {
|
|
|
33
33
|
* Gets the balance of a specific token for an address
|
|
34
34
|
* @param address The wallet address to check
|
|
35
35
|
* @param xToken The token to get the balance for
|
|
36
|
-
* @param xChainId The chain ID to query
|
|
37
36
|
* @returns Promise resolving to the token balance as a bigint
|
|
38
37
|
*/
|
|
39
|
-
public async getBalance(address: string | undefined, xToken: XToken
|
|
38
|
+
public async getBalance(address: string | undefined, xToken: XToken): Promise<bigint> {
|
|
40
39
|
return 0n;
|
|
41
40
|
}
|
|
42
41
|
|
|
@@ -44,18 +43,13 @@ export abstract class XService {
|
|
|
44
43
|
* Gets balances for multiple tokens for an address
|
|
45
44
|
* @param address The wallet address to check
|
|
46
45
|
* @param xTokens Array of tokens to get balances for
|
|
47
|
-
* @param xChainId The chain ID to query
|
|
48
46
|
* @returns Promise resolving to object mapping token addresses to balances
|
|
49
47
|
*/
|
|
50
|
-
public async getBalances(
|
|
51
|
-
address: string | undefined,
|
|
52
|
-
xTokens: XToken[],
|
|
53
|
-
xChainId: ChainId,
|
|
54
|
-
): Promise<Record<string, bigint>> {
|
|
48
|
+
public async getBalances(address: string | undefined, xTokens: XToken[]): Promise<Record<string, bigint>> {
|
|
55
49
|
if (!address) return {};
|
|
56
50
|
|
|
57
51
|
const balancePromises = xTokens.map(async xToken => {
|
|
58
|
-
const balance = await this.getBalance(address, xToken
|
|
52
|
+
const balance = await this.getBalance(address, xToken);
|
|
59
53
|
return { address: xToken.address, balance };
|
|
60
54
|
});
|
|
61
55
|
|
package/src/hooks/index.ts
CHANGED
|
@@ -6,6 +6,6 @@ export { useXConnectors } from './useXConnectors';
|
|
|
6
6
|
export { useXDisconnect } from './useXDisconnect';
|
|
7
7
|
export { useXService } from './useXService';
|
|
8
8
|
export { useXBalances } from './useXBalances';
|
|
9
|
-
export { useWalletProviderOptions } from './useWalletProviderOptions';
|
|
10
9
|
export { useEvmSwitchChain } from './useEvmSwitchChain';
|
|
11
10
|
export { useWalletProvider } from './useWalletProvider';
|
|
11
|
+
export { useXSignMessage } from './useXSignMessage';
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { xChainMap } from '@/constants/xChains';
|
|
2
1
|
import { useCallback, useMemo } from 'react';
|
|
3
2
|
import { useAccount, useSwitchChain } from 'wagmi';
|
|
4
|
-
import type
|
|
3
|
+
import { baseChainInfo, type ChainId } from '@sodax/types';
|
|
5
4
|
import { getXChainType } from '@/actions';
|
|
6
5
|
import type { InjectiveXService } from '@/xchains/injective';
|
|
7
6
|
import { useXService } from '@/hooks/useXService';
|
|
@@ -56,7 +55,7 @@ export const switchEthereumChain = async () => {
|
|
|
56
55
|
|
|
57
56
|
export const useEvmSwitchChain = (expectedXChainId: ChainId): UseEvmSwitchChainReturn => {
|
|
58
57
|
const xChainType = getXChainType(expectedXChainId);
|
|
59
|
-
const expectedChainId =
|
|
58
|
+
const expectedChainId = baseChainInfo[expectedXChainId].chainId as number;
|
|
60
59
|
|
|
61
60
|
const injectiveXService = useXService('INJECTIVE') as unknown as InjectiveXService;
|
|
62
61
|
const ethereumChainId = useEthereumChainId();
|
|
@@ -1,4 +1,12 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
ChainId,
|
|
3
|
+
IEvmWalletProvider,
|
|
4
|
+
IIconWalletProvider,
|
|
5
|
+
IInjectiveWalletProvider,
|
|
6
|
+
ISolanaWalletProvider,
|
|
7
|
+
IStellarWalletProvider,
|
|
8
|
+
ISuiWalletProvider,
|
|
9
|
+
} from '@sodax/types';
|
|
2
10
|
import { useMemo } from 'react';
|
|
3
11
|
import {
|
|
4
12
|
EvmWalletProvider,
|
|
@@ -9,9 +17,7 @@ import {
|
|
|
9
17
|
SolanaWalletProvider,
|
|
10
18
|
} from '@sodax/wallet-sdk-core';
|
|
11
19
|
import { getXChainType } from '../actions';
|
|
12
|
-
import type { InjectiveEoaAddress } from '@sodax/types';
|
|
13
20
|
import { usePublicClient, useWalletClient } from 'wagmi';
|
|
14
|
-
import { getWagmiChainId } from '../utils';
|
|
15
21
|
import { type SolanaXService, type StellarXService, useXAccount, useXService } from '..';
|
|
16
22
|
import type { SuiXService } from '../xchains/sui/SuiXService';
|
|
17
23
|
import { CHAIN_INFO, SupportedChainId } from '../xchains/icon/IconXService';
|
|
@@ -37,22 +43,18 @@ import type { InjectiveXService } from '../xchains/injective/InjectiveXService';
|
|
|
37
43
|
export function useWalletProvider(
|
|
38
44
|
spokeChainId: ChainId | undefined,
|
|
39
45
|
):
|
|
40
|
-
|
|
|
41
|
-
|
|
|
42
|
-
|
|
|
43
|
-
|
|
|
44
|
-
|
|
|
45
|
-
|
|
|
46
|
+
| IEvmWalletProvider
|
|
47
|
+
| ISuiWalletProvider
|
|
48
|
+
| IIconWalletProvider
|
|
49
|
+
| IInjectiveWalletProvider
|
|
50
|
+
| IStellarWalletProvider
|
|
51
|
+
| ISolanaWalletProvider
|
|
46
52
|
| undefined {
|
|
47
53
|
const xChainType = getXChainType(spokeChainId);
|
|
48
|
-
|
|
49
54
|
// EVM-specific hooks
|
|
50
|
-
const evmPublicClient = usePublicClient(
|
|
51
|
-
|
|
52
|
-
});
|
|
53
|
-
const { data: evmWalletClient } = useWalletClient({
|
|
54
|
-
chainId: spokeChainId ? getWagmiChainId(spokeChainId) : undefined,
|
|
55
|
-
});
|
|
55
|
+
const evmPublicClient = usePublicClient();
|
|
56
|
+
|
|
57
|
+
const { data: evmWalletClient } = useWalletClient();
|
|
56
58
|
|
|
57
59
|
// Cross-chain hooks
|
|
58
60
|
const xService = useXService(getXChainType(spokeChainId));
|
|
@@ -111,6 +113,9 @@ export function useWalletProvider(
|
|
|
111
113
|
|
|
112
114
|
case 'STELLAR': {
|
|
113
115
|
const stellarXService = xService as StellarXService;
|
|
116
|
+
if (!stellarXService.walletsKit) {
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
114
119
|
|
|
115
120
|
return new StellarWalletProvider({
|
|
116
121
|
type: 'BROWSER_EXTENSION',
|
|
@@ -123,11 +128,11 @@ export function useWalletProvider(
|
|
|
123
128
|
const solanaXService = xService as SolanaXService;
|
|
124
129
|
|
|
125
130
|
if (!solanaXService.wallet) {
|
|
126
|
-
|
|
131
|
+
return undefined;
|
|
127
132
|
}
|
|
128
133
|
|
|
129
134
|
if (!solanaXService.connection) {
|
|
130
|
-
|
|
135
|
+
return undefined;
|
|
131
136
|
}
|
|
132
137
|
|
|
133
138
|
return new SolanaWalletProvider({
|
package/src/hooks/useXConnect.ts
CHANGED
|
@@ -41,8 +41,7 @@ export function useXConnect(): UseMutationResult<XAccount | undefined, Error, XC
|
|
|
41
41
|
const { connectAsync: evmConnectAsync } = useConnect();
|
|
42
42
|
const { mutateAsync: suiConnectAsync } = useConnectWallet();
|
|
43
43
|
|
|
44
|
-
|
|
45
|
-
const { select, connect, connected } = useWallet();
|
|
44
|
+
const { select, connect } = useWallet();
|
|
46
45
|
|
|
47
46
|
return useMutation({
|
|
48
47
|
mutationFn: async (xConnector: XConnector) => {
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { useWallet } from '@solana/wallet-adapter-react';
|
|
2
|
+
import { useMutation, type UseMutationResult } from '@tanstack/react-query';
|
|
3
|
+
import type { ChainType } from '@sodax/types';
|
|
4
|
+
import { useSignMessage } from 'wagmi';
|
|
5
|
+
import { useSignPersonalMessage } from '@mysten/dapp-kit';
|
|
6
|
+
import { StellarXService } from '@/xchains/stellar';
|
|
7
|
+
import { InjectiveXService } from '@/xchains/injective';
|
|
8
|
+
import { useXAccount } from './useXAccount';
|
|
9
|
+
import { getEthereumAddress } from '@injectivelabs/sdk-ts';
|
|
10
|
+
import { Wallet } from '@injectivelabs/wallet-base';
|
|
11
|
+
|
|
12
|
+
type SignMessageReturnType = `0x${string}` | Uint8Array | string | undefined;
|
|
13
|
+
|
|
14
|
+
export function useXSignMessage(): UseMutationResult<
|
|
15
|
+
SignMessageReturnType,
|
|
16
|
+
Error,
|
|
17
|
+
{ xChainType: ChainType; message: string },
|
|
18
|
+
unknown
|
|
19
|
+
> {
|
|
20
|
+
const { signMessage } = useWallet();
|
|
21
|
+
const { signMessageAsync: evmSignMessage } = useSignMessage();
|
|
22
|
+
|
|
23
|
+
const { mutateAsync: signPersonalMessage } = useSignPersonalMessage();
|
|
24
|
+
|
|
25
|
+
const { address: injectiveAddress } = useXAccount('INJECTIVE');
|
|
26
|
+
|
|
27
|
+
return useMutation({
|
|
28
|
+
mutationFn: async ({ xChainType, message }: { xChainType: ChainType; message: string }) => {
|
|
29
|
+
let signature: SignMessageReturnType;
|
|
30
|
+
|
|
31
|
+
switch (xChainType) {
|
|
32
|
+
case 'EVM': {
|
|
33
|
+
signature = await evmSignMessage({ message });
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
case 'SUI': {
|
|
37
|
+
const res = await signPersonalMessage({ message: new Uint8Array(new TextEncoder().encode(message)) });
|
|
38
|
+
signature = res.signature;
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
case 'SOLANA': {
|
|
42
|
+
if (!signMessage) {
|
|
43
|
+
throw new Error('Solana wallet not connected');
|
|
44
|
+
}
|
|
45
|
+
signature = await signMessage(new TextEncoder().encode(message));
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
case 'STELLAR': {
|
|
50
|
+
const res = await StellarXService.getInstance().walletsKit.signMessage(message);
|
|
51
|
+
signature = res.signedMessage;
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
case 'INJECTIVE': {
|
|
56
|
+
if (!injectiveAddress) {
|
|
57
|
+
throw new Error('Injective address not found');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const ethereumAddress = getEthereumAddress(injectiveAddress);
|
|
61
|
+
const walletStrategy = InjectiveXService.getInstance().walletStrategy;
|
|
62
|
+
const res = await walletStrategy.signArbitrary(
|
|
63
|
+
walletStrategy.getWallet() === Wallet.Metamask ? ethereumAddress : injectiveAddress,
|
|
64
|
+
message,
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
if (!res) {
|
|
68
|
+
throw new Error('Injective signature not found');
|
|
69
|
+
}
|
|
70
|
+
signature = res;
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
default:
|
|
75
|
+
console.warn('Unsupported chain type');
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return signature;
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
}
|
package/src/index.ts
CHANGED
package/src/types/index.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { ChainType } from '@sodax/types';
|
|
2
|
-
import type { EvmChainId } from '@/xchains/evm/EvmXService';
|
|
3
2
|
|
|
4
3
|
export type XAccount = {
|
|
5
4
|
address: string | undefined;
|
|
@@ -20,25 +19,3 @@ export enum WalletId {
|
|
|
20
19
|
SUI = 'sui',
|
|
21
20
|
KEPLR = 'keplr',
|
|
22
21
|
}
|
|
23
|
-
|
|
24
|
-
export type EVMConfig = {
|
|
25
|
-
chains: EvmChainId[];
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
export type SuiConfig = {
|
|
29
|
-
isMainnet: boolean;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export type SolanaConfig = {
|
|
33
|
-
endpoint: string;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
export type XConfig = {
|
|
37
|
-
[key in ChainType]: key extends 'EVM'
|
|
38
|
-
? EVMConfig
|
|
39
|
-
: key extends 'SUI'
|
|
40
|
-
? SuiConfig
|
|
41
|
-
: key extends 'SOLANA'
|
|
42
|
-
? SolanaConfig
|
|
43
|
-
: unknown;
|
|
44
|
-
};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import type { ChainType } from '@sodax/types';
|
|
4
|
+
import { create } from 'zustand';
|
|
5
|
+
import { createJSONStorage, persist, devtools } from 'zustand/middleware';
|
|
6
|
+
import { immer } from 'zustand/middleware/immer';
|
|
7
|
+
import type { XService } from './core';
|
|
8
|
+
import type { XConnection } from './types';
|
|
9
|
+
import { EvmXService } from './xchains/evm';
|
|
10
|
+
import { InjectiveKelprXConnector, InjectiveMetamaskXConnector, InjectiveXService } from './xchains/injective';
|
|
11
|
+
import { SolanaXService } from './xchains/solana/SolanaXService';
|
|
12
|
+
import { StellarXService } from './xchains/stellar';
|
|
13
|
+
import { SuiXService } from './xchains/sui';
|
|
14
|
+
import { IconXService } from './xchains/icon';
|
|
15
|
+
import { IconHanaXConnector } from './xchains/icon/IconHanaXConnector';
|
|
16
|
+
|
|
17
|
+
type XWagmiStore = {
|
|
18
|
+
xServices: Partial<Record<ChainType, XService>>;
|
|
19
|
+
xConnections: Partial<Record<ChainType, XConnection>>;
|
|
20
|
+
|
|
21
|
+
setXConnection: (xChainType: ChainType, xConnection: XConnection) => void;
|
|
22
|
+
unsetXConnection: (xChainType: ChainType) => void;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const initXServices = () => {
|
|
26
|
+
const xServices = {};
|
|
27
|
+
['EVM', 'INJECTIVE', 'STELLAR', 'SUI', 'SOLANA', 'ICON'].forEach(key => {
|
|
28
|
+
const xChainType = key as ChainType;
|
|
29
|
+
|
|
30
|
+
switch (xChainType) {
|
|
31
|
+
// EVM, SUI, Solana wallet connectors are supported by their own sdks. wagmi, @mysten/dapp-kit, @solana/wallet-adapter-react.
|
|
32
|
+
case 'EVM':
|
|
33
|
+
xServices[xChainType] = EvmXService.getInstance();
|
|
34
|
+
xServices[xChainType].setXConnectors([]);
|
|
35
|
+
break;
|
|
36
|
+
case 'SUI':
|
|
37
|
+
xServices[xChainType] = SuiXService.getInstance();
|
|
38
|
+
xServices[xChainType].setXConnectors([]);
|
|
39
|
+
break;
|
|
40
|
+
case 'SOLANA':
|
|
41
|
+
xServices[xChainType] = SolanaXService.getInstance();
|
|
42
|
+
xServices[xChainType].setXConnectors([]);
|
|
43
|
+
break;
|
|
44
|
+
|
|
45
|
+
// Injective, Stellar, Icon wallet connectors are supported by sodax wallet-sdk-react sdk.
|
|
46
|
+
case 'INJECTIVE':
|
|
47
|
+
xServices[xChainType] = InjectiveXService.getInstance();
|
|
48
|
+
xServices[xChainType].setXConnectors([new InjectiveMetamaskXConnector(), new InjectiveKelprXConnector()]);
|
|
49
|
+
break;
|
|
50
|
+
case 'STELLAR':
|
|
51
|
+
xServices[xChainType] = StellarXService.getInstance();
|
|
52
|
+
xServices[xChainType].setXConnectors([]);
|
|
53
|
+
break;
|
|
54
|
+
case 'ICON':
|
|
55
|
+
xServices[xChainType] = IconXService.getInstance();
|
|
56
|
+
xServices[xChainType].setXConnectors([new IconHanaXConnector()]);
|
|
57
|
+
break;
|
|
58
|
+
default:
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
return xServices;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export const useXWagmiStore = create<XWagmiStore>()(
|
|
67
|
+
devtools(
|
|
68
|
+
persist(
|
|
69
|
+
immer((set, get) => ({
|
|
70
|
+
xServices: initXServices(),
|
|
71
|
+
xConnections: {},
|
|
72
|
+
setXConnection: (xChainType: ChainType, xConnection: XConnection) => {
|
|
73
|
+
set(state => {
|
|
74
|
+
state.xConnections[xChainType] = xConnection;
|
|
75
|
+
});
|
|
76
|
+
},
|
|
77
|
+
unsetXConnection: (xChainType: ChainType) => {
|
|
78
|
+
set(state => {
|
|
79
|
+
delete state.xConnections[xChainType];
|
|
80
|
+
});
|
|
81
|
+
},
|
|
82
|
+
})),
|
|
83
|
+
{
|
|
84
|
+
name: 'xwagmi-store',
|
|
85
|
+
storage: createJSONStorage(() => localStorage),
|
|
86
|
+
partialize: state => ({ xConnections: state.xConnections }),
|
|
87
|
+
},
|
|
88
|
+
),
|
|
89
|
+
{ name: 'xwagmi-store' },
|
|
90
|
+
),
|
|
91
|
+
);
|
package/src/utils/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { baseChainInfo, type ChainId, type XToken } from '@sodax/types';
|
|
2
2
|
|
|
3
3
|
export const isNativeToken = (xToken: XToken) => {
|
|
4
4
|
const nativeAddresses = [
|
|
@@ -14,20 +14,6 @@ export const isNativeToken = (xToken: XToken) => {
|
|
|
14
14
|
return nativeAddresses.includes(xToken.address);
|
|
15
15
|
};
|
|
16
16
|
|
|
17
|
-
// TODO: remove this? move to dapp-kit?
|
|
18
17
|
export const getWagmiChainId = (xChainId: ChainId): number => {
|
|
19
|
-
|
|
20
|
-
'0xa869.fuji': 43113,
|
|
21
|
-
'sonic-blaze': 57054,
|
|
22
|
-
sonic: 146,
|
|
23
|
-
'0xa86a.avax': 43114,
|
|
24
|
-
'0x38.bsc': 56,
|
|
25
|
-
'0xa4b1.arbitrum': 42161,
|
|
26
|
-
'0x2105.base': 8453,
|
|
27
|
-
'0xa.optimism': 10,
|
|
28
|
-
'0x89.polygon': 137,
|
|
29
|
-
hyper: 999,
|
|
30
|
-
lightlink: 1890,
|
|
31
|
-
};
|
|
32
|
-
return xChainMap[xChainId] ?? 0;
|
|
18
|
+
return baseChainInfo[xChainId].chainId as number;
|
|
33
19
|
};
|