UNPKG

@wormhole-foundation/sdk-cosmwasm

Version:

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

127 lines 5.61 kB
import { CosmWasmClient } from "@cosmjs/cosmwasm-stargate"; import { QueryClient, setupBankExtension, setupIbcExtension } from "@cosmjs/stargate"; import { PlatformContext, Wormhole, decimals, isNative, nativeChainIds, networkPlatformConfigs, } from "@wormhole-foundation/sdk-connect"; import { CosmwasmChain } from "./chain.js"; import { chainToNativeDenoms, networkChainToChannels } from "./constants.js"; import { _platform } from "./types.js"; import { chainToPlatform } from "@wormhole-foundation/sdk-connect"; import { CosmwasmAddress } from "./address.js"; import { IBC_TRANSFER_PORT } from "./constants.js"; import { Gateway } from "./gateway.js"; /** * @category Cosmwasm */ export class CosmwasmPlatform extends PlatformContext { static _platform = _platform; constructor(network, _config) { super(network, _config ?? networkPlatformConfigs(network, CosmwasmPlatform._platform)); } async getRpc(chain) { if (chain in this.config && this.config[chain].rpc) return await CosmWasmClient.connect(this.config[chain].rpc); throw new Error("No configuration available for chain: " + chain); } getChain(chain, rpc) { if (!(chain in this.config)) throw new Error("No configuration available for chain: " + chain); return new CosmwasmChain(chain, this, rpc); } static getQueryClient = (rpc) => { return QueryClient.withExtensions(rpc["cometClient"], setupBankExtension, setupIbcExtension); }; // cached channels from config if available static getIbcChannels(network, chain) { return networkChainToChannels.has(network, chain) ? networkChainToChannels.get(network, chain) : null; } static nativeTokenId(network, chain) { if (!this.isSupportedChain(chain)) throw new Error(`invalid chain for CosmWasm: ${chain}`); return Wormhole.chainAddress(chain, this.getNativeDenom(network, chain)); } static isSupportedChain(chain) { const platform = chainToPlatform(chain); return platform === CosmwasmPlatform._platform; } static isNativeTokenId(network, chain, tokenId) { if (!this.isSupportedChain(chain)) return false; if (tokenId.chain !== chain) return false; const native = this.nativeTokenId(network, chain); return native == tokenId; } static async getDecimals(_network, _chain, rpc, token) { if (isNative(token)) return decimals.nativeDecimals(CosmwasmPlatform._platform); let addrStr = new CosmwasmAddress(token).toString(); if (addrStr.startsWith("factory")) { addrStr = Gateway.factoryToCw20(new CosmwasmAddress(addrStr)).toString(); } const { decimals: numDecimals } = await rpc.queryContractSmart(addrStr, { token_info: {}, }); return numDecimals; } static async getBalance(_network, chain, rpc, walletAddress, token) { if (isNative(token)) { const [network, _] = await CosmwasmPlatform.chainFromRpc(rpc); const { amount } = await rpc.getBalance(walletAddress, this.getNativeDenom(network, chain)); return BigInt(amount); } const addrStr = new CosmwasmAddress(token).toString(); const { amount } = await rpc.getBalance(walletAddress, addrStr); return BigInt(amount); } static async getBalances(_network, chain, rpc, walletAddress) { const client = CosmwasmPlatform.getQueryClient(rpc); const allBalances = await client.bank.allBalances(walletAddress); const [network, _] = await CosmwasmPlatform.chainFromRpc(rpc); const balances = {}; for (const balance of allBalances) { const denom = balance.denom; const address = isNative(denom) ? this.getNativeDenom(network, chain) : new CosmwasmAddress(denom).toString(); balances[address] = BigInt(balance.amount); } return balances; } static getNativeDenom(network, chain) { return chainToNativeDenoms.get(network, chain); } static async sendWait(chain, rpc, stxns) { const txhashes = []; for (const stxn of stxns) { const result = await rpc.broadcastTx(stxn); if (result.code !== 0) throw new Error(`Error sending transaction (${result.transactionHash}): ${result.rawLog}`); txhashes.push(result.transactionHash); } return txhashes; } static async getLatestBlock(rpc) { return await rpc.getHeight(); } static async getLatestFinalizedBlock(rpc) { throw new Error("not implemented"); } static chainFromChainId(chainMoniker) { const networkChainPair = nativeChainIds.platformNativeChainIdToNetworkChain(CosmwasmPlatform._platform, chainMoniker); if (networkChainPair === undefined) throw new Error(`Unknown Cosmwasm chainId ${chainMoniker}`); const [network, chain] = networkChainPair; return [network, chain]; } static async chainFromRpc(rpc) { const chainId = await rpc.getChainId(); return this.chainFromChainId(chainId); } static async getCounterpartyChannel(sourceChannel, rpc) { const queryClient = CosmwasmPlatform.getQueryClient(rpc); const conn = await queryClient.ibc.channel.channel(IBC_TRANSFER_PORT, sourceChannel); return conn.channel?.counterparty?.channelId ?? null; } } //# sourceMappingURL=platform.js.map