@sodax/wallet-sdk-react 0.0.1-rc.7 → 1.0.0-beta

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.
@@ -33,7 +33,7 @@ export const SodaxWalletProvider = ({ children, rpcConfig }: { children: React.R
33
33
  const wallets = useMemo(() => [new UnsafeBurnerWalletAdapter()], []);
34
34
 
35
35
  return (
36
- <WagmiProvider config={wagmiConfig} reconnectOnMount={true}>
36
+ <WagmiProvider config={wagmiConfig}>
37
37
  <SuiClientProvider networks={{ mainnet: { url: getFullnodeUrl('mainnet') } }} defaultNetwork="mainnet">
38
38
  <SuiWalletProvider autoConnect={true}>
39
39
  <SolanaConnectionProvider endpoint={rpcConfig['solana'] ?? ''}>
@@ -45,7 +45,7 @@ export abstract class XService {
45
45
  * @param xTokens Array of tokens to get balances for
46
46
  * @returns Promise resolving to object mapping token addresses to balances
47
47
  */
48
- public async getBalances(address: string | undefined, xTokens: XToken[]): Promise<Record<string, bigint>> {
48
+ public async getBalances(address: string | undefined, xTokens: readonly XToken[]): Promise<Record<string, bigint>> {
49
49
  if (!address) return {};
50
50
 
51
51
  const balancePromises = xTokens.map(async xToken => {
@@ -44,7 +44,7 @@ export function useXBalances({
44
44
  xChainId,
45
45
  xTokens,
46
46
  address,
47
- }: { xChainId: ChainId; xTokens: XToken[]; address: string | undefined }): UseQueryResult<{
47
+ }: { xChainId: ChainId; xTokens: readonly XToken[]; address: string | undefined }): UseQueryResult<{
48
48
  [key: string]: bigint;
49
49
  }> {
50
50
  const xService = useXService(getXChainType(xChainId));
@@ -1,22 +1,24 @@
1
1
  import { XService } from '@/core/XService';
2
- import type { RpcConfig, XToken } from '@sodax/types';
2
+ import {
3
+ ARBITRUM_MAINNET_CHAIN_ID,
4
+ AVALANCHE_MAINNET_CHAIN_ID,
5
+ BASE_MAINNET_CHAIN_ID,
6
+ BSC_MAINNET_CHAIN_ID,
7
+ ETHEREUM_MAINNET_CHAIN_ID,
8
+ HYPEREVM_MAINNET_CHAIN_ID,
9
+ LIGHTLINK_MAINNET_CHAIN_ID,
10
+ OPTIMISM_MAINNET_CHAIN_ID,
11
+ POLYGON_MAINNET_CHAIN_ID,
12
+ SONIC_MAINNET_CHAIN_ID,
13
+ type RpcConfig,
14
+ type XToken,
15
+ } from '@sodax/types';
3
16
  import { getWagmiChainId, isNativeToken } from '@/utils';
4
17
 
5
18
  import { type Address, defineChain, erc20Abi } from 'viem';
6
19
  import { getPublicClient } from 'wagmi/actions';
7
20
  import { type Config, createConfig, http } from 'wagmi';
8
- import {
9
- mainnet,
10
- avalanche,
11
- base,
12
- optimism,
13
- polygon,
14
- arbitrum,
15
- bsc,
16
- sonic,
17
- nibiru,
18
- lightlinkPhoenix,
19
- } from 'wagmi/chains';
21
+ import { mainnet, avalanche, base, optimism, polygon, arbitrum, bsc, sonic, lightlinkPhoenix } from 'wagmi/chains';
20
22
 
21
23
  // HyperEVM chain is not supported by viem, so we need to define it manually
22
24
  export const hyper = /*#__PURE__*/ defineChain({
@@ -46,19 +48,19 @@ export const hyper = /*#__PURE__*/ defineChain({
46
48
 
47
49
  export const createWagmiConfig = (config: RpcConfig) => {
48
50
  return createConfig({
49
- chains: [mainnet, avalanche, arbitrum, base, bsc, sonic, optimism, polygon, nibiru, hyper, lightlinkPhoenix],
51
+ chains: [mainnet, avalanche, arbitrum, base, bsc, sonic, optimism, polygon, hyper, lightlinkPhoenix],
52
+ ssr: true,
50
53
  transports: {
51
- [mainnet.id]: http(config['mainnet']),
52
- [avalanche.id]: http(config['avalanche']),
53
- [arbitrum.id]: http(config['arbitrum']),
54
- [base.id]: http(config['base']),
55
- [bsc.id]: http(config['bsc']),
56
- [sonic.id]: http(config['sonic']),
57
- [optimism.id]: http(config['optimism']),
58
- [polygon.id]: http(config['polygon']),
59
- [nibiru.id]: http(config['nibiru']),
60
- [hyper.id]: http(config['hyper']),
61
- [lightlinkPhoenix.id]: http(config['lightlinkPhoenix']),
54
+ [mainnet.id]: http(config[ETHEREUM_MAINNET_CHAIN_ID]),
55
+ [avalanche.id]: http(config[AVALANCHE_MAINNET_CHAIN_ID]),
56
+ [arbitrum.id]: http(config[ARBITRUM_MAINNET_CHAIN_ID]),
57
+ [base.id]: http(config[BASE_MAINNET_CHAIN_ID]),
58
+ [bsc.id]: http(config[BSC_MAINNET_CHAIN_ID]),
59
+ [sonic.id]: http(config[SONIC_MAINNET_CHAIN_ID]),
60
+ [optimism.id]: http(config[OPTIMISM_MAINNET_CHAIN_ID]),
61
+ [polygon.id]: http(config[POLYGON_MAINNET_CHAIN_ID]),
62
+ [hyper.id]: http(config[HYPEREVM_MAINNET_CHAIN_ID]),
63
+ [lightlinkPhoenix.id]: http(config[LIGHTLINK_MAINNET_CHAIN_ID]),
62
64
  },
63
65
  });
64
66
  };
@@ -22,14 +22,10 @@ export class SuiXService extends XService {
22
22
 
23
23
  // getBalance is not used because getBalances uses getAllBalances which returns all balances
24
24
 
25
- async getBalances(address: string | undefined, xTokens: XToken[]) {
25
+ async getBalances(address: string | undefined, xTokens: readonly XToken[]): Promise<Record<string, bigint>> {
26
26
  if (!address) return {};
27
-
28
27
  try {
29
- const allBalances = await this.suiClient.getAllBalances({
30
- owner: address,
31
- });
32
- const tokenMap = xTokens.reduce((map, xToken) => {
28
+ const balancePromises = xTokens.map(async xToken => {
33
29
  let coinType = isNativeToken(xToken) ? '0x2::sui::SUI' : xToken.address;
34
30
 
35
31
  // TODO: hard coded for getting legacy bnUSD balance
@@ -41,11 +37,25 @@ export class SuiXService extends XService {
41
37
  '0x3917a812fe4a6d6bc779c5ab53f8a80ba741f8af04121193fc44e0f662e2ceb::balanced_dollar::BALANCED_DOLLAR';
42
38
  }
43
39
 
44
- const balance = allBalances.find(b => b.coinType === coinType);
40
+ const balance = await this.suiClient.getBalance({
41
+ owner: address,
42
+ coinType: coinType,
43
+ });
44
+
45
+ return {
46
+ address: xToken.address,
47
+ balance: balance ? BigInt(balance.totalBalance) : undefined,
48
+ };
49
+ });
45
50
 
46
- if (balance) map[xToken.address] = balance.totalBalance;
47
- return map;
48
- }, {});
51
+ const results = await Promise.all(balancePromises);
52
+
53
+ const tokenMap: Record<string, bigint> = {};
54
+ results.forEach(result => {
55
+ if (result.balance !== undefined) {
56
+ tokenMap[result.address] = result.balance;
57
+ }
58
+ });
49
59
 
50
60
  return tokenMap;
51
61
  } catch (e) {