Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: fetch token balance for sui #27

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions example-next/src/components/Tokens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,27 @@ const tokens: TokenMetadata[] = [
symbol: 'USDt',
chainId: '397',
},
{
address: '0x2::sui::SUI',
decimals: 9,
name: 'Sui Token',
symbol: 'SUI',
chainId: 'sui',
},
{
address: '0xc060006111016b8a020ad5b33834984a437aaa7d3c74c18e09a95d48aceab08c::coin::COIN',
decimals: 6,
name: 'Tether USD',
symbol: 'USDT',
chainId: 'sui',
},
{
address: '0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC',
decimals: 6,
name: 'USDC',
symbol: 'USDC',
chainId: 'sui',
},
];

export default Tokens;
41 changes: 41 additions & 0 deletions packages/react/src/actions/getToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { getCosmosTokenBalanceAndAllowance, getCosmosTokenMetadata } from './cos
import { getEVMTokenBalanceAndAllowance, getEVMTokenMetadata } from './evm/getEVMToken.js';
import { viewMethodOnNear } from './near/readCalls.js';
import { getSolanaTokenBalanceAndAllowance } from './solana/getSolanaToken.js';
import { getSuiTokenInfo } from './sui/getSuiTokenInfo.js';
import { getTonTokenBalanceAndAllowance, getTonTokenMetadata } from './ton/getTonToken.js';

/**
Expand Down Expand Up @@ -133,6 +134,32 @@ export const getTokenMetadata = async ({ token, chain, config }: GetTokenMetadat
};
}

if (chain.type === 'sui') {
if (areTokensEqual(token, ETH_ADDRESS)) {
return { ...chain.nativeCurrency, address: ETH_ADDRESS, chainId: chain.id };
}

let res;
try {
res = await getSuiTokenInfo({
chain,
method: 'suix_getCoinMetadata',
params: [token],
});
} catch (error) {
console.error('Error fetching Sui token metadata:', error);
throw new Error('Failed to fetch Sui token metadata');
}

return {
name: res.name,
symbol: res.symbol,
decimals: res.decimals,
address: token,
chainId: chain.id,
};
}

throw new Error('Chain type not supported');
};

Expand Down Expand Up @@ -259,5 +286,19 @@ export const getTokenBalanceAndAllowance = (async (params) => {
return { balance, allowance };
}

if (chain.type === 'sui') {
let balance = 0n;

try {
const balanceResponse = await getSuiTokenInfo({ chain, method: 'suix_getBalance', params: [account, token] });
balance = BigInt(balanceResponse.totalBalance);
} catch (error) {
console.error('Error fetching Sui token balance:', error);
throw new Error('Failed to fetch Sui token balance');
}

return { balance, allowance: 0n };
}

throw new Error('Chain type not supported');
}) as GetTokenBalanceAndAllowanceFunction;
43 changes: 43 additions & 0 deletions packages/react/src/actions/sui/getSuiTokenInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { ChainData } from '../../types/index.js';

export const getSuiTokenInfo = async ({
chain,
method,
params,
}: {
chain: ChainData;
method: string;
params: Array<string>;
}) => {
const SUI_RPC_URL = chain.rpcUrls.default.http[0];
const payload = {
jsonrpc: '2.0',
id: 1,
method,
params,
};

try {
const response = await fetch(SUI_RPC_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
});

const data = await response.json();

if (data.error) {
throw new Error(`Failed to query Sui RPC with ${method} method:: ${data.error.message}`);
}

return data.result;
} catch (error) {
if (error instanceof Error) {
throw new Error(`Failed to query Sui RPC with ${method} method : ${error.message}`);
} else {
throw new Error(`Failed to query Sui RPC with ${method} method `);
}
}
};
Loading