Skip to content

Commit

Permalink
✨ feat: add panta chain support, improve ethereum get balance logic (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rustin01 authored Dec 27, 2024
1 parent 71cbe73 commit 02b4103
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 11 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@delandlabs/hibit-id-sdk",
"private": true,
"scripts": {
"build:coin-base": "turbo run @delandlabs/coin-base#build",
"dev:sdk": "turbo run dev --filter=@delandlabs/hibit-id-wallet-sdk",
"build:sdk": "turbo run @delandlabs/hibit-id-wallet-sdk#build",
"test:coin-kaspa": "turbo run @delandlabs/coin-kaspa#test",
Expand Down
1 change: 1 addition & 0 deletions packages/coin-base/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export class ChainNetwork {
static EvmBitlayerTestNet = new ChainNetwork(new BigNumber(200810));
static EvmSwanNet = new ChainNetwork(new BigNumber(254));
static EvmSwanTestNet = new ChainNetwork(new BigNumber(20241133));
static EvmPantaNet = new ChainNetwork(new BigNumber(331));

static SolanaMainNet = new ChainNetwork(new BigNumber(0x3));
static SolanaTestNet = new ChainNetwork(new BigNumber(0x2));
Expand Down
15 changes: 15 additions & 0 deletions packages/coin-ethereum/src/chain-wallet/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,18 @@ export const EthereumSwanTestnet: ChainInfo = {
explorer: 'https://proxima-explorer.swanchain.io',
rpcUrls: ['https://rpc-proxima.swanchain.io']
};

export const EthereumPanta: ChainInfo = {
chainId: new ChainId(Chain.Ethereum, ChainNetwork.EvmPantaNet),
name: 'Panta',
fullName: 'Panta Mainnet',
icon: '/chain-icons/Panta.svg',
nativeAssetSymbol: 'PANTA',
nativeAssetDecimals: 18,
supportedSignaturesSchemas: [WalletSignatureSchema.EvmEcdsa],
explorer: 'http://pantascan.xyz/',
rpcUrls: ['https://node2.panta.network'],
isMainnet: false,
isNativeGas: true,
ecosystem: Ecosystem.EVM,
};
36 changes: 25 additions & 11 deletions packages/coin-ethereum/src/chain-wallet/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,39 @@ export class EthereumChainWallet extends BaseChainWallet {
if (!assetInfo.chain.equals(CHAIN)) {
throw new Error(`${CHAIN_NAME}: invalid asset chain`);
}

const chainInfo = getChain(new ChainId(assetInfo.chain, assetInfo.chainNetwork));
if (!chainInfo) {
throw new Error(
`${CHAIN_NAME}: unsupported asset chain ${assetInfo.chain.toString()}_${assetInfo.chainNetwork.toString()}`
);
}
const provider = new JsonRpcProvider(chainInfo.rpcUrls[0], chainInfo.chainId.network.value.toNumber());

// native
if (assetInfo.chainAssetType.equals(NATIVE_ASSET)) {
const balance = await this.provider.getBalance(address);
return new BigNumber(balance.toString()).shiftedBy(-assetInfo.decimalPlaces.value);
try {
const balance = await provider.getBalance(address);
return new BigNumber(balance.toString()).shiftedBy(-assetInfo.decimalPlaces.value);
} catch (e) {
console.error(e);
provider.destroy()
throw new Error(`${CHAIN_NAME}: failed to get native balance`);
}
}
// erc20
if (assetInfo.chainAssetType.equals(FT_ASSET)) {
const chainInfo = getChain(new ChainId(assetInfo.chain, assetInfo.chainNetwork));
if (!chainInfo) {
throw new Error(
`${CHAIN_NAME}: unsupported asset chain ${assetInfo.chain.toString()}_${assetInfo.chainNetwork.toString()}`
);
}
const provider = new JsonRpcProvider(chainInfo.rpcUrls[0], chainInfo.chainId.network.value.toNumber());
const contract = new Contract(assetInfo.contractAddress, erc20Abi, provider);
const getDecimals = contract.decimals();
const getBalance = contract.balanceOf(address);
const [decimals, balance] = await Promise.all([getDecimals, getBalance]);
return new BigNumber(balance.toString()).shiftedBy(-Number(decimals));
try {
const [decimals, balance] = await Promise.all([getDecimals, getBalance]);
return new BigNumber(balance.toString()).shiftedBy(-Number(decimals));
} catch (e) {
console.error(e);
provider.destroy()
throw new Error(`${CHAIN_NAME}: failed to get balance`);
}
}

throw new Error(`${CHAIN_NAME}: unsupported chain asset type ${assetInfo.chainAssetType.toString()}`);
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/lib/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export enum HibitIdChainId {
EthereumScrollSepolia = '60_534351',
EthereumBitlayer = '60_200901',
EthereumBitlayerTestnet = '60_200810',
EthereumPanta = '60_331',

SolanaMainnet = '501_3',
SolanaTestnet = '501_2',
Expand Down

0 comments on commit 02b4103

Please sign in to comment.