UNPKG

@wormhole-foundation/sdk-sui-tokenbridge

Version:

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

57 lines 2.64 kB
import { coinTypeKeyName, dummyFieldName, getDynamicFieldValue, getPackageIdFromType, isValidSuiType, trimSuiType, } from "@wormhole-foundation/sdk-sui"; export const getTokenFromTokenRegistry = async (provider, tokenBridgeStateObjectId, tokenType) => { if (!isValidSuiType(tokenType)) { throw new Error(`Invalid Sui type: ${tokenType}`); } const { object: state } = await provider.getObject({ objectId: tokenBridgeStateObjectId, include: { json: true }, }); if (!state || !state.json) { throw new Error(`Unable to fetch object fields from token bridge state. Object ID: ${tokenBridgeStateObjectId}`); } // The token registry shares the token bridge's package id (derived from the state type). const tokenRegistryPackageId = getPackageIdFromType(state.type); const tokenRegistryObjectId = state.json["token_registry"]?.["id"]; if (!tokenRegistryObjectId) { throw new Error("Unable to fetch token registry object ID"); } try { const name = dummyFieldName(`${tokenRegistryPackageId}::token_registry::Key<${tokenType}>`); const { dynamicField } = await provider.getDynamicField({ parentId: tokenRegistryObjectId, name, }); const { object } = await provider.getObject({ objectId: dynamicField.fieldId, include: { json: true }, }); if (!object || !object.json) return null; return { type: object.type, json: object.json }; } catch { // dynamic field not found → token not registered return null; } }; export const getTokenCoinType = async (provider, tokenBridgeStateObjectId, tokenAddress, tokenChain) => { const { object: state } = await provider.getObject({ objectId: tokenBridgeStateObjectId, include: { json: true }, }); if (!state || !state.json) throw new Error("Unable to fetch object fields from token bridge state"); const tokenRegistryPackageId = getPackageIdFromType(state.type); const coinTypesObjectId = state.json["token_registry"]?.["coin_types"]?.["id"]; if (!coinTypesObjectId) { throw new Error("Unable to fetch coin types"); } const name = coinTypeKeyName(`${tokenRegistryPackageId}::token_registry::CoinTypeKey`, tokenChain, tokenAddress); const value = await getDynamicFieldValue(provider, coinTypesObjectId, name); if (value === null || value === undefined) return null; // The value is the coin type as an ascii::String (rendered directly). return trimSuiType(value); }; //# sourceMappingURL=utils.js.map