UNPKG

@wormhole-foundation/sdk-sui

Version:

SDK for Sui chains, used in conjunction with @wormhole-foundation/sdk

122 lines 4.84 kB
import { PlatformContext, Wormhole, chainToPlatform, isNative, nativeChainIds, decimals as nativeDecimals, networkPlatformConfigs, } from "@wormhole-foundation/sdk-connect"; import { SuiClient } from "@mysten/sui/client"; import { SuiAddress } from "./address.js"; import { SuiChain } from "./chain.js"; import { SUI_COIN } from "./constants.js"; import { _platform } from "./types.js"; import { getObjectFields } from "./utils.js"; /** * @category Sui */ export class SuiPlatform extends PlatformContext { static _platform = _platform; constructor(network, _config) { super(network, _config ?? networkPlatformConfigs(network, SuiPlatform._platform)); } getRpc(chain) { if (chain in this.config) return new SuiClient({ url: this.config[chain].rpc }); throw new Error("No configuration available for chain: " + chain); } getChain(chain) { if (chain in this.config) return new SuiChain(chain, this); throw new Error("No configuration available for chain: " + chain); } static nativeTokenId(network, chain) { if (!SuiPlatform.isSupportedChain(chain)) throw new Error(`invalid chain for ${_platform}: ${chain}`); return Wormhole.tokenId(chain, SUI_COIN); } static isNativeTokenId(network, chain, tokenId) { if (!SuiPlatform.isSupportedChain(chain)) return false; if (tokenId.chain !== chain) return false; const native = this.nativeTokenId(network, chain); return native === tokenId; } static isSupportedChain(chain) { const platform = chainToPlatform(chain); return platform === SuiPlatform._platform; } static async getDecimals(network, chain, rpc, token) { if (isNative(token)) return nativeDecimals.nativeDecimals(SuiPlatform._platform); const parsedAddress = new SuiAddress(token); try { const fields = await getObjectFields(rpc, parsedAddress.toString()); if (fields && "decimals" in fields) return fields["decimals"]; } catch { } const metadata = await rpc.getCoinMetadata({ coinType: parsedAddress.toString() }); if (metadata === null) throw new Error(`Can't fetch decimals for token ${parsedAddress.toString()}`); return metadata.decimals; } static async getCoins(connection, account, coinType) { let coins = []; let cursor = null; const owner = new SuiAddress(account).toString(); do { const result = await connection.getCoins({ owner, coinType, cursor, }); coins = [...coins, ...result.data]; cursor = result.hasNextPage ? result.nextCursor : null; } while (cursor); return coins; } static async getBalance(_network, _chain, rpc, walletAddr, token) { if (isNative(token)) { const { totalBalance } = await rpc.getBalance({ owner: walletAddr, }); return BigInt(totalBalance); } const { totalBalance } = await rpc.getBalance({ owner: walletAddr, coinType: token.toString(), }); return BigInt(totalBalance); } static async getBalances(_network, _chain, rpc, walletAddr) { const result = await rpc.getAllBalances({ owner: walletAddr }); const balances = {}; for (const { coinType, totalBalance } of result) { const address = coinType === SUI_COIN ? "native" : coinType; balances[address] = BigInt(totalBalance); } return balances; } static async sendWait(chain, rpc, stxns) { const txhashes = []; for (const stxn of stxns) { const pendingTx = await rpc.executeTransactionBlock(stxn); await rpc.waitForTransaction({ digest: pendingTx.digest }); txhashes.push(pendingTx.digest); } return txhashes; } static async getLatestBlock(rpc) { return Number(await rpc.getLatestCheckpointSequenceNumber()); } static async getLatestFinalizedBlock(rpc) { return this.getLatestBlock(rpc); } static chainFromChainId(chainId) { const networkChainPair = nativeChainIds.platformNativeChainIdToNetworkChain(SuiPlatform._platform, chainId); if (networkChainPair === undefined) throw new Error(`Unknown native chain id ${chainId}`); const [network, chain] = networkChainPair; return [network, chain]; } static async chainFromRpc(rpc) { const result = await rpc.call("sui_getChainIdentifier", []); return this.chainFromChainId(result); } } //# sourceMappingURL=platform.js.map