@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,117 +0,0 @@
|
|
|
1
|
-
import { XService } from '@/core/XService';
|
|
2
|
-
import type { IconService } from 'icon-sdk-js';
|
|
3
|
-
import * as IconSdkRaw from 'icon-sdk-js';
|
|
4
|
-
import type { XToken } from '@sodax/types';
|
|
5
|
-
import { isNativeToken } from '@/utils';
|
|
6
|
-
|
|
7
|
-
const IconSdk = ('default' in IconSdkRaw.default ? IconSdkRaw.default : IconSdkRaw) as typeof IconSdkRaw;
|
|
8
|
-
const { IconService: IconServiceConstructor, Builder: IconBuilder, Converter: IconConverter } = IconSdk;
|
|
9
|
-
export interface CallData {
|
|
10
|
-
target: string;
|
|
11
|
-
method: string;
|
|
12
|
-
params: string[];
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export enum SupportedChainId {
|
|
16
|
-
MAINNET = 1,
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
interface ChainInfo {
|
|
20
|
-
readonly name: string;
|
|
21
|
-
readonly node: string;
|
|
22
|
-
readonly APIEndpoint: string;
|
|
23
|
-
readonly debugAPIEndpoint: string;
|
|
24
|
-
readonly chainId: number;
|
|
25
|
-
readonly tracker: string;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export const CHAIN_INFO: { readonly [chainId: number]: ChainInfo } = {
|
|
29
|
-
[SupportedChainId.MAINNET]: {
|
|
30
|
-
name: 'ICON Mainnet',
|
|
31
|
-
node: 'https://ctz.solidwallet.io',
|
|
32
|
-
APIEndpoint: 'https://ctz.solidwallet.io/api/v3',
|
|
33
|
-
debugAPIEndpoint: 'https://api.icon.community/api/v3d',
|
|
34
|
-
chainId: 1,
|
|
35
|
-
tracker: 'https://tracker.icon.community',
|
|
36
|
-
},
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export class IconXService extends XService {
|
|
40
|
-
private static instance: IconXService;
|
|
41
|
-
|
|
42
|
-
public iconService: IconService;
|
|
43
|
-
|
|
44
|
-
private constructor() {
|
|
45
|
-
super('ICON');
|
|
46
|
-
this.iconService = new IconServiceConstructor(
|
|
47
|
-
new IconServiceConstructor.HttpProvider(CHAIN_INFO[SupportedChainId.MAINNET].APIEndpoint),
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
public static getInstance(): IconXService {
|
|
52
|
-
if (!IconXService.instance) {
|
|
53
|
-
IconXService.instance = new IconXService();
|
|
54
|
-
}
|
|
55
|
-
return IconXService.instance;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
private async getAggregateData(requireSuccess: boolean, calls: CallData[]) {
|
|
59
|
-
const rawTx = new IconBuilder.CallBuilder()
|
|
60
|
-
// muticall address on mainnet
|
|
61
|
-
.to('cxa4aa9185e23558cff990f494c1fd2845f6cbf741')
|
|
62
|
-
.method('tryAggregate')
|
|
63
|
-
.params({ requireSuccess: IconConverter.toHex(requireSuccess ? 1 : 0), calls })
|
|
64
|
-
.build();
|
|
65
|
-
|
|
66
|
-
try {
|
|
67
|
-
const result = await this.iconService.call(rawTx).execute();
|
|
68
|
-
const aggs = result['returnData'];
|
|
69
|
-
|
|
70
|
-
const data = aggs.map(agg => {
|
|
71
|
-
if (agg['success'] === '0x0') {
|
|
72
|
-
return null;
|
|
73
|
-
}
|
|
74
|
-
return agg['returnData'];
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
return data;
|
|
78
|
-
} catch (err) {
|
|
79
|
-
console.error(err);
|
|
80
|
-
return Array(calls.length).fill(null);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
async getBalances(address: string | undefined, xTokens: XToken[]) {
|
|
85
|
-
if (!address) return {};
|
|
86
|
-
|
|
87
|
-
const balances = {};
|
|
88
|
-
|
|
89
|
-
const nativeXToken = xTokens.find(xToken => isNativeToken(xToken));
|
|
90
|
-
const nonNativeXTokens = xTokens.filter(xToken => !isNativeToken(xToken));
|
|
91
|
-
|
|
92
|
-
if (nativeXToken) {
|
|
93
|
-
const balance = await this.iconService.getBalance(address).execute();
|
|
94
|
-
balances[nativeXToken.address] = BigInt(balance.toFixed());
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const cds: CallData[] = nonNativeXTokens.map(token => {
|
|
98
|
-
return {
|
|
99
|
-
target: token.address,
|
|
100
|
-
method: 'balanceOf',
|
|
101
|
-
params: [address],
|
|
102
|
-
};
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
const data: string[] = await this.getAggregateData(
|
|
106
|
-
false,
|
|
107
|
-
cds.filter(cd => cd.target.startsWith('cx')),
|
|
108
|
-
);
|
|
109
|
-
|
|
110
|
-
return nonNativeXTokens.reduce((agg, token, idx) => {
|
|
111
|
-
const balance = data[idx];
|
|
112
|
-
balances[token.address] = BigInt(balance);
|
|
113
|
-
|
|
114
|
-
return agg;
|
|
115
|
-
}, balances);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { useXWagmiStore } from '@/useXWagmiStore';
|
|
2
|
-
import { ICONexRequestEventType, ICONexResponseEventType, request } from './iconex';
|
|
3
|
-
|
|
4
|
-
export const reconnectIcon = async () => {
|
|
5
|
-
const iconConnection = useXWagmiStore.getState().xConnections.ICON;
|
|
6
|
-
if (!iconConnection) return;
|
|
7
|
-
|
|
8
|
-
const recentXConnectorId = iconConnection.xConnectorId;
|
|
9
|
-
|
|
10
|
-
const detail = await request({
|
|
11
|
-
type: ICONexRequestEventType.REQUEST_ADDRESS,
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
if (detail?.type === ICONexResponseEventType.RESPONSE_ADDRESS) {
|
|
15
|
-
useXWagmiStore.setState({
|
|
16
|
-
xConnections: {
|
|
17
|
-
...useXWagmiStore.getState().xConnections,
|
|
18
|
-
ICON: {
|
|
19
|
-
xAccount: {
|
|
20
|
-
address: detail?.payload,
|
|
21
|
-
xChainType: 'ICON',
|
|
22
|
-
},
|
|
23
|
-
xConnectorId: recentXConnectorId,
|
|
24
|
-
},
|
|
25
|
-
},
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
};
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
const ICONEX_RELAY_RESPONSE = 'ICONEX_RELAY_RESPONSE';
|
|
2
|
-
const ICONEX_RELAY_REQUEST = 'ICONEX_RELAY_REQUEST';
|
|
3
|
-
|
|
4
|
-
export enum ICONexRequestEventType {
|
|
5
|
-
REQUEST_HAS_ACCOUNT = 'REQUEST_HAS_ACCOUNT',
|
|
6
|
-
REQUEST_HAS_ADDRESS = 'REQUEST_HAS_ADDRESS',
|
|
7
|
-
REQUEST_ADDRESS = 'REQUEST_ADDRESS',
|
|
8
|
-
REQUEST_JSON = 'REQUEST_JSON',
|
|
9
|
-
REQUEST_SIGNING = 'REQUEST_SIGNING',
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export enum ICONexResponseEventType {
|
|
13
|
-
RESPONSE_HAS_ACCOUNT = 'RESPONSE_HAS_ACCOUNT',
|
|
14
|
-
RESPONSE_HAS_ADDRESS = 'RESPONSE_HAS_ADDRESS',
|
|
15
|
-
RESPONSE_ADDRESS = 'RESPONSE_ADDRESS',
|
|
16
|
-
RESPONSE_JSON = 'RESPONSE_JSON',
|
|
17
|
-
RESPONSE_SIGNING = 'RESPONSE_SIGNING',
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export interface ICONexRequestEvent {
|
|
21
|
-
type: ICONexRequestEventType;
|
|
22
|
-
payload?: any;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export interface ICONexResponseEvent {
|
|
26
|
-
type: ICONexResponseEventType;
|
|
27
|
-
payload?: any;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export type ICONexEvent = ICONexRequestEvent | ICONexResponseEvent;
|
|
31
|
-
|
|
32
|
-
export const request = (event: ICONexRequestEvent): Promise<ICONexResponseEvent> => {
|
|
33
|
-
return new Promise((resolve, reject) => {
|
|
34
|
-
const handler = evt => {
|
|
35
|
-
window.removeEventListener(ICONEX_RELAY_RESPONSE, handler);
|
|
36
|
-
resolve(evt.detail);
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
window.addEventListener(ICONEX_RELAY_RESPONSE, handler);
|
|
40
|
-
window.dispatchEvent(
|
|
41
|
-
new CustomEvent(ICONEX_RELAY_REQUEST, {
|
|
42
|
-
detail: event,
|
|
43
|
-
}),
|
|
44
|
-
);
|
|
45
|
-
});
|
|
46
|
-
};
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import type { XAccount } from '@/types';
|
|
2
|
-
import { XConnector } from '@/core';
|
|
3
|
-
import { getInjectiveAddress } from '@injectivelabs/sdk-ts';
|
|
4
|
-
import { type Wallet, isEvmBrowserWallet, isCosmosBrowserWallet } from '@injectivelabs/wallet-base';
|
|
5
|
-
import { isCosmosWalletInstalled } from '@injectivelabs/wallet-cosmos';
|
|
6
|
-
import { InjectiveXService } from './InjectiveXService';
|
|
7
|
-
|
|
8
|
-
const WALLET_ICONS: Partial<Record<Wallet, string>> = {
|
|
9
|
-
metamask: 'https://raw.githubusercontent.com/balancednetwork/icons/master/wallets/metamask.svg',
|
|
10
|
-
keplr: 'https://raw.githubusercontent.com/balancednetwork/icons/master/wallets/keplr.svg',
|
|
11
|
-
leap: 'https://assets.leapwallet.io/logos/leap-cosmos-logo.svg',
|
|
12
|
-
rabby: 'https://raw.githubusercontent.com/RabbyHub/logo/master/symbol.svg',
|
|
13
|
-
phantom: 'https://raw.githubusercontent.com/balancednetwork/icons/master/wallets/phantom.svg',
|
|
14
|
-
'okx-wallet': 'https://static.okx.com/cdn/assets/imgs/247/58E63FEA47A2B7D7.png',
|
|
15
|
-
'trust-wallet': 'https://trustwallet.com/assets/images/media/assets/twLogo.svg',
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export class InjectiveXConnector extends XConnector {
|
|
19
|
-
private wallet: Wallet;
|
|
20
|
-
|
|
21
|
-
constructor(name: string, wallet: Wallet) {
|
|
22
|
-
super('INJECTIVE', name, wallet);
|
|
23
|
-
this.wallet = wallet;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
getXService(): InjectiveXService {
|
|
27
|
-
return InjectiveXService.getInstance();
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
async connect(): Promise<XAccount | undefined> {
|
|
31
|
-
if (isCosmosBrowserWallet(this.wallet) && !isCosmosWalletInstalled(this.wallet)) {
|
|
32
|
-
return undefined;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const walletStrategy = this.getXService().walletStrategy;
|
|
36
|
-
await walletStrategy.setWallet(this.wallet);
|
|
37
|
-
const addresses = await walletStrategy.getAddresses();
|
|
38
|
-
|
|
39
|
-
if (!addresses?.length) return undefined;
|
|
40
|
-
|
|
41
|
-
const address = isEvmBrowserWallet(this.wallet) ? getInjectiveAddress(addresses[0]) : addresses[0];
|
|
42
|
-
|
|
43
|
-
return {
|
|
44
|
-
address,
|
|
45
|
-
xChainType: this.xChainType,
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
async disconnect(): Promise<void> {
|
|
50
|
-
if (isEvmBrowserWallet(this.wallet)) {
|
|
51
|
-
const walletStrategy = this.getXService().walletStrategy;
|
|
52
|
-
await walletStrategy.setWallet(this.wallet);
|
|
53
|
-
await walletStrategy.disconnect();
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
public get icon(): string | undefined {
|
|
58
|
-
return WALLET_ICONS[this.wallet];
|
|
59
|
-
}
|
|
60
|
-
}
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { XService } from '@/core/XService';
|
|
2
|
-
import { Network, getNetworkEndpoints } from '@injectivelabs/networks';
|
|
3
|
-
import { ChainGrpcWasmApi, IndexerGrpcAccountPortfolioApi } from '@injectivelabs/sdk-ts';
|
|
4
|
-
import { ChainId as InjectiveChainId } from '@injectivelabs/ts-types';
|
|
5
|
-
import { MsgBroadcaster } from '@injectivelabs/wallet-core';
|
|
6
|
-
import type { XToken } from '@sodax/types';
|
|
7
|
-
import { mainnet } from 'wagmi/chains';
|
|
8
|
-
import { WalletStrategy } from '@injectivelabs/wallet-strategy';
|
|
9
|
-
|
|
10
|
-
export class InjectiveXService extends XService {
|
|
11
|
-
private static instance: InjectiveXService;
|
|
12
|
-
|
|
13
|
-
public walletStrategy: WalletStrategy;
|
|
14
|
-
public indexerGrpcAccountPortfolioApi: IndexerGrpcAccountPortfolioApi;
|
|
15
|
-
public chainGrpcWasmApi: ChainGrpcWasmApi;
|
|
16
|
-
public msgBroadcaster: MsgBroadcaster;
|
|
17
|
-
|
|
18
|
-
private constructor() {
|
|
19
|
-
super('INJECTIVE');
|
|
20
|
-
|
|
21
|
-
const endpoints = getNetworkEndpoints(Network.Mainnet);
|
|
22
|
-
|
|
23
|
-
this.walletStrategy = new WalletStrategy({
|
|
24
|
-
chainId: InjectiveChainId.Mainnet,
|
|
25
|
-
strategies: {},
|
|
26
|
-
evmOptions: {
|
|
27
|
-
evmChainId: mainnet.id,
|
|
28
|
-
rpcUrl: mainnet.rpcUrls.default.http[0],
|
|
29
|
-
},
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
this.indexerGrpcAccountPortfolioApi = new IndexerGrpcAccountPortfolioApi(endpoints.indexer);
|
|
33
|
-
this.chainGrpcWasmApi = new ChainGrpcWasmApi(endpoints.grpc);
|
|
34
|
-
this.msgBroadcaster = new MsgBroadcaster({
|
|
35
|
-
walletStrategy: this.walletStrategy,
|
|
36
|
-
network: Network.Mainnet,
|
|
37
|
-
endpoints,
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
public static getInstance(): InjectiveXService {
|
|
42
|
-
if (!InjectiveXService.instance) {
|
|
43
|
-
InjectiveXService.instance = new InjectiveXService();
|
|
44
|
-
}
|
|
45
|
-
return InjectiveXService.instance;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
async getBalance(address: string | undefined, xToken: XToken) {
|
|
49
|
-
if (!address) return 0n;
|
|
50
|
-
|
|
51
|
-
const portfolio = await this.indexerGrpcAccountPortfolioApi.fetchAccountPortfolioBalances(address);
|
|
52
|
-
|
|
53
|
-
const xTokenAddress = xToken.address;
|
|
54
|
-
|
|
55
|
-
const balance = portfolio.bankBalancesList.find(_balance => _balance.denom === xTokenAddress);
|
|
56
|
-
if (balance) {
|
|
57
|
-
return BigInt(balance.amount);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
return 0n;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { useXWagmiStore } from '@/useXWagmiStore';
|
|
2
|
-
import { InjectiveXService } from './InjectiveXService';
|
|
3
|
-
import { isEvmBrowserWallet } from '@injectivelabs/wallet-base';
|
|
4
|
-
import { getInjectiveAddress } from '@injectivelabs/sdk-ts';
|
|
5
|
-
import type { Wallet } from '@injectivelabs/wallet-base';
|
|
6
|
-
|
|
7
|
-
export const reconnectInjective = async () => {
|
|
8
|
-
const injectiveConnection = useXWagmiStore.getState().xConnections.INJECTIVE;
|
|
9
|
-
if (!injectiveConnection) return;
|
|
10
|
-
|
|
11
|
-
const recentXConnectorId = injectiveConnection.xConnectorId;
|
|
12
|
-
const walletStrategy = InjectiveXService.getInstance().walletStrategy;
|
|
13
|
-
await walletStrategy.setWallet(recentXConnectorId as Wallet);
|
|
14
|
-
const addresses = await walletStrategy.getAddresses();
|
|
15
|
-
|
|
16
|
-
const address = isEvmBrowserWallet(recentXConnectorId as Wallet)
|
|
17
|
-
? getInjectiveAddress(addresses?.[0])
|
|
18
|
-
: addresses?.[0];
|
|
19
|
-
|
|
20
|
-
useXWagmiStore.setState({
|
|
21
|
-
xConnections: {
|
|
22
|
-
...useXWagmiStore.getState().xConnections,
|
|
23
|
-
INJECTIVE: {
|
|
24
|
-
xAccount: {
|
|
25
|
-
address,
|
|
26
|
-
xChainType: 'INJECTIVE',
|
|
27
|
-
},
|
|
28
|
-
xConnectorId: recentXConnectorId,
|
|
29
|
-
},
|
|
30
|
-
},
|
|
31
|
-
});
|
|
32
|
-
};
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import type { XAccount } from '@/types';
|
|
2
|
-
|
|
3
|
-
import { XConnector } from '@/core';
|
|
4
|
-
import type { NearWalletBase } from '@hot-labs/near-connect';
|
|
5
|
-
import { NearXService } from './NearXService';
|
|
6
|
-
|
|
7
|
-
export class NearXConnector extends XConnector {
|
|
8
|
-
_wallet: NearWalletBase;
|
|
9
|
-
|
|
10
|
-
constructor(wallet: NearWalletBase) {
|
|
11
|
-
super('NEAR', wallet.manifest.name, wallet.manifest.id);
|
|
12
|
-
this._wallet = wallet;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
getXService(): NearXService {
|
|
16
|
-
return NearXService.getInstance();
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
async connect(): Promise<XAccount | undefined> {
|
|
20
|
-
const walletSelector = this.getXService().walletSelector;
|
|
21
|
-
const wallet = await walletSelector.connect({ walletId: this._wallet.manifest.id });
|
|
22
|
-
const accounts = await wallet.getAccounts();
|
|
23
|
-
|
|
24
|
-
if (accounts.length === 0 || accounts[0] === undefined) {
|
|
25
|
-
return undefined;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return {
|
|
29
|
-
address: accounts[0].accountId,
|
|
30
|
-
xChainType: this.xChainType,
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
async disconnect(): Promise<void> {
|
|
35
|
-
const walletSelector = this.getXService().walletSelector;
|
|
36
|
-
await walletSelector.disconnect(this._wallet);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
public get icon() {
|
|
40
|
-
return this._wallet.manifest.icon;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { XService } from '@/core/XService';
|
|
2
|
-
import type { XToken } from '@sodax/types';
|
|
3
|
-
import { NearConnector } from '@hot-labs/near-connect';
|
|
4
|
-
import { JsonRpcProvider } from 'near-api-js';
|
|
5
|
-
|
|
6
|
-
export class NearXService extends XService {
|
|
7
|
-
private static instance: NearXService;
|
|
8
|
-
|
|
9
|
-
public walletSelector: NearConnector;
|
|
10
|
-
|
|
11
|
-
private constructor() {
|
|
12
|
-
super('NEAR');
|
|
13
|
-
|
|
14
|
-
this.walletSelector = new NearConnector({
|
|
15
|
-
network: 'mainnet',
|
|
16
|
-
logger: console,
|
|
17
|
-
autoConnect: true,
|
|
18
|
-
excludedWallets: ['okx-wallet'],
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
public static getInstance(): NearXService {
|
|
23
|
-
if (!NearXService.instance) {
|
|
24
|
-
NearXService.instance = new NearXService();
|
|
25
|
-
}
|
|
26
|
-
return NearXService.instance;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
async getBalance(address: string | undefined, xToken: XToken): Promise<bigint> {
|
|
30
|
-
const url = 'https://1rpc.io/near';
|
|
31
|
-
// reference: https://near.github.io/near-api-js/classes/_near-js_providers.json-rpc-provider.JsonRpcProvider.html
|
|
32
|
-
const provider = new JsonRpcProvider({ url });
|
|
33
|
-
|
|
34
|
-
// get native balance
|
|
35
|
-
if (xToken.symbol === 'NEAR') {
|
|
36
|
-
const account = await provider.viewAccount({ accountId: address ?? '' });
|
|
37
|
-
return BigInt(account.amount);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// Near Fungible Token Standard(https://github.com/near/NEPs/blob/master/neps/nep-0141.md)
|
|
41
|
-
// get balance of the token
|
|
42
|
-
|
|
43
|
-
const res = await provider.callFunction<number>({ contractId: xToken.address, method: 'ft_balance_of', args: { account_id: address } });
|
|
44
|
-
return BigInt(res ?? 0);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { useXService } from '@/hooks';
|
|
2
|
-
import { type UseQueryResult, useQuery } from '@tanstack/react-query';
|
|
3
|
-
|
|
4
|
-
import { NearXConnector } from './NearXConnector';
|
|
5
|
-
import type { NearXService } from './NearXService';
|
|
6
|
-
|
|
7
|
-
export const useNearXConnectors = (): UseQueryResult<NearXConnector[] | undefined, Error | null> => {
|
|
8
|
-
const xService = useXService('NEAR') as NearXService;
|
|
9
|
-
|
|
10
|
-
return useQuery({
|
|
11
|
-
queryKey: ['near-wallets'],
|
|
12
|
-
queryFn: async () => {
|
|
13
|
-
if (!xService) {
|
|
14
|
-
return [];
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
await xService.walletSelector.whenManifestLoaded;
|
|
18
|
-
const wallets = xService.walletSelector.availableWallets;
|
|
19
|
-
|
|
20
|
-
return wallets.map(wallet => new NearXConnector(wallet));
|
|
21
|
-
},
|
|
22
|
-
});
|
|
23
|
-
};
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import type { XAccount } from '@/types';
|
|
2
|
-
|
|
3
|
-
import { XConnector } from '@/core';
|
|
4
|
-
import { SolanaXService } from './SolanaXService';
|
|
5
|
-
|
|
6
|
-
export class SolanaXConnector extends XConnector {
|
|
7
|
-
wallet: any;
|
|
8
|
-
constructor(wallet: any) {
|
|
9
|
-
super('SOLANA', wallet?.adapter.name, wallet?.adapter.name);
|
|
10
|
-
this.wallet = wallet;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
getXService(): SolanaXService {
|
|
14
|
-
return SolanaXService.getInstance();
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
async connect(): Promise<XAccount | undefined> {
|
|
18
|
-
return;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
async disconnect(): Promise<void> {}
|
|
22
|
-
|
|
23
|
-
public get icon() {
|
|
24
|
-
return this.wallet?.adapter.icon;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { XService } from '@/core/XService';
|
|
2
|
-
import { isNativeToken } from '@/utils';
|
|
3
|
-
import type { XToken } from '@sodax/types';
|
|
4
|
-
import { type Connection, PublicKey } from '@solana/web3.js';
|
|
5
|
-
import { getAccount, getAssociatedTokenAddressSync } from '@solana/spl-token';
|
|
6
|
-
import type { WalletContextState } from '@solana/wallet-adapter-react';
|
|
7
|
-
|
|
8
|
-
export class SolanaXService extends XService {
|
|
9
|
-
private static instance: SolanaXService;
|
|
10
|
-
|
|
11
|
-
public connection: Connection | undefined;
|
|
12
|
-
public wallet: WalletContextState | undefined;
|
|
13
|
-
|
|
14
|
-
private constructor() {
|
|
15
|
-
super('SOLANA');
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
public static getInstance(): SolanaXService {
|
|
19
|
-
if (!SolanaXService.instance) {
|
|
20
|
-
SolanaXService.instance = new SolanaXService();
|
|
21
|
-
}
|
|
22
|
-
return SolanaXService.instance;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
async getBalance(address: string | undefined, xToken: XToken): Promise<bigint> {
|
|
26
|
-
if (!address) return BigInt(0);
|
|
27
|
-
|
|
28
|
-
const connection = this.connection;
|
|
29
|
-
if (!connection) {
|
|
30
|
-
return BigInt(0);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
try {
|
|
34
|
-
if (isNativeToken(xToken)) {
|
|
35
|
-
const newBalance = await connection.getBalance(new PublicKey(address));
|
|
36
|
-
return BigInt(newBalance);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const tokenAccountPubkey = getAssociatedTokenAddressSync(new PublicKey(xToken.address), new PublicKey(address));
|
|
40
|
-
const tokenAccount = await getAccount(connection, tokenAccountPubkey);
|
|
41
|
-
return BigInt(tokenAccount.amount);
|
|
42
|
-
} catch {
|
|
43
|
-
return BigInt(0);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import type { XAccount } from '@/types';
|
|
2
|
-
import { XConnector } from '@/core';
|
|
3
|
-
import type { StacksProvider } from '@stacks/connect';
|
|
4
|
-
import { request, disconnect } from '@stacks/connect';
|
|
5
|
-
|
|
6
|
-
export interface StacksProviderConfig {
|
|
7
|
-
/** The provider ID matching the window path, e.g. 'LeatherProvider' or 'XverseProviders.BitcoinProvider' */
|
|
8
|
-
id: string;
|
|
9
|
-
name: string;
|
|
10
|
-
icon: string;
|
|
11
|
-
installUrl?: string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/** Resolves a provider from `window` by dot-separated ID, matching @stacks/connect-ui's getProviderFromId */
|
|
15
|
-
function getProviderFromId(id: string): StacksProvider | undefined {
|
|
16
|
-
// biome-ignore lint/suspicious/noExplicitAny: window property traversal requires any
|
|
17
|
-
return id.split('.').reduce<any>((acc, part) => acc?.[part], window) as StacksProvider | undefined;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export class StacksXConnector extends XConnector {
|
|
21
|
-
private readonly config: StacksProviderConfig;
|
|
22
|
-
|
|
23
|
-
constructor(config: StacksProviderConfig) {
|
|
24
|
-
super('STACKS', config.name, config.id);
|
|
25
|
-
this.config = config;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
async connect(): Promise<XAccount | undefined> {
|
|
29
|
-
const provider = this.getProvider();
|
|
30
|
-
|
|
31
|
-
if (!provider) {
|
|
32
|
-
if (this.config.installUrl) {
|
|
33
|
-
window.open(this.config.installUrl, '_blank');
|
|
34
|
-
}
|
|
35
|
-
return undefined;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const response = await request({ provider }, 'stx_getAddresses');
|
|
39
|
-
// @ts-ignore
|
|
40
|
-
const stxAddress = response.addresses.find(a => a.purpose === 'stacks');
|
|
41
|
-
|
|
42
|
-
if (!stxAddress) {
|
|
43
|
-
return undefined;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return {
|
|
47
|
-
address: stxAddress.address,
|
|
48
|
-
xChainType: this.xChainType,
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
async disconnect(): Promise<void> {
|
|
53
|
-
disconnect();
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
public get icon(): string {
|
|
57
|
-
return this.config.icon;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
public getProvider(): StacksProvider | undefined {
|
|
61
|
-
return getProviderFromId(this.config.id);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { XService } from '@/core/XService';
|
|
2
|
-
import type { XToken } from '@sodax/types';
|
|
3
|
-
import { fetchCallReadOnlyFunction, Cl, type UIntCV, type ResponseOkCV } from '@stacks/transactions';
|
|
4
|
-
import { networkFrom, type StacksNetwork } from '@stacks/network';
|
|
5
|
-
|
|
6
|
-
export class StacksXService extends XService {
|
|
7
|
-
private static instance: StacksXService;
|
|
8
|
-
|
|
9
|
-
public network: StacksNetwork | undefined;
|
|
10
|
-
|
|
11
|
-
private constructor() {
|
|
12
|
-
super('STACKS');
|
|
13
|
-
this.network = networkFrom('mainnet');
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
public static getInstance(): StacksXService {
|
|
17
|
-
if (!StacksXService.instance) {
|
|
18
|
-
StacksXService.instance = new StacksXService();
|
|
19
|
-
}
|
|
20
|
-
return StacksXService.instance;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
async getBalance(address: string | undefined, xToken: XToken): Promise<bigint> {
|
|
24
|
-
if (!address) return 0n;
|
|
25
|
-
|
|
26
|
-
// native STX balance
|
|
27
|
-
if (xToken.symbol === 'STX') {
|
|
28
|
-
const url = `${this.network?.client.baseUrl}/extended/v1/address/${address}/balances`;
|
|
29
|
-
try {
|
|
30
|
-
const response = await fetch(url);
|
|
31
|
-
if (!response.ok) {
|
|
32
|
-
throw new Error(`Error fetching data: ${response.statusText}`);
|
|
33
|
-
}
|
|
34
|
-
const data = await response.json();
|
|
35
|
-
return BigInt(data.stx.balance);
|
|
36
|
-
} catch (error) {
|
|
37
|
-
console.error('Error fetching STX balance:', error);
|
|
38
|
-
return 0n;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// SIP-010 fungible token balance via read-only contract call
|
|
43
|
-
const [contractAddress, contractName] = xToken.address.split('.');
|
|
44
|
-
try {
|
|
45
|
-
const result = (await fetchCallReadOnlyFunction({
|
|
46
|
-
contractAddress,
|
|
47
|
-
contractName,
|
|
48
|
-
functionName: 'get-balance',
|
|
49
|
-
functionArgs: [Cl.principal(address)],
|
|
50
|
-
network: this.network,
|
|
51
|
-
senderAddress: address,
|
|
52
|
-
})) as ResponseOkCV<UIntCV>;
|
|
53
|
-
return result.value.value as bigint;
|
|
54
|
-
} catch (error) {
|
|
55
|
-
console.error('Error fetching token balance:', error);
|
|
56
|
-
return 0n;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|