From d586300e85e7cc7830e2c79e13f62df0e2667211 Mon Sep 17 00:00:00 2001 From: Matthew Date: Wed, 31 Jul 2024 14:30:13 -0700 Subject: [PATCH] Add chain ID query --- CHANGELOG.md | 1 + src/cosmos/CosmosEngine.ts | 27 ++++++++++++++++++++++++++- src/cosmos/CosmosTools.ts | 5 +++-- src/cosmos/cosmosTypes.ts | 12 +++++++++++- src/cosmos/info/axelarInfo.ts | 3 ++- src/cosmos/info/coreumInfo.ts | 3 ++- src/cosmos/info/cosmoshubInfo.ts | 3 ++- src/cosmos/info/osmosisInfo.ts | 3 ++- src/cosmos/info/thorchainruneInfo.ts | 4 +++- 9 files changed, 52 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8df6f2a4b..b3b3acbfb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - added: (zkSync) Add USDC +- added: (Cosmos) Add chain ID updater - fixed: (zkSync) Fix USDC.e currency code ## 4.17.1 (2024-07-30) diff --git a/src/cosmos/CosmosEngine.ts b/src/cosmos/CosmosEngine.ts index bf0637ade..f9d924df3 100644 --- a/src/cosmos/CosmosEngine.ts +++ b/src/cosmos/CosmosEngine.ts @@ -56,6 +56,7 @@ import { EdgeTokenId, MakeTxParams } from '../common/types' import { cleanTxLogs } from '../common/utils' import { CosmosTools } from './CosmosTools' import { + asChainIdUpdate, asCosmosPrivateKeys, asCosmosTxOtherParams, asCosmosWalletOtherData, @@ -110,6 +111,7 @@ export class CosmosEngine extends CurrencyEngine< feeCache: Map stakedBalanceCache: string stakingSupported: boolean + chainId: string constructor( env: PluginEnvironment, @@ -129,6 +131,7 @@ export class CosmosEngine extends CurrencyEngine< this.feeCache = new Map() this.stakedBalanceCache = '0' this.stakingSupported = true + this.chainId = this.networkInfo.defaultChainId this.otherMethods = { getMaxTx: async (params: MakeTxParams) => { switch (params.type) { @@ -492,6 +495,25 @@ export class CosmosEngine extends CurrencyEngine< } } + async queryChainId(): Promise { + if (this.networkInfo.chainIdUpdateUrl != null) { + try { + const res = await this.fetchCors(this.networkInfo.chainIdUpdateUrl) + if (!res.ok) { + const message = await res.text() + throw new Error(message) + } + const raw = await res.json() + const clean = asChainIdUpdate(raw) + this.chainId = clean.result.node_info.network + } catch (e: any) { + this.error(`queryChainId Error `, e) + return + } + } + clearTimeout(this.timers.queryChainId) + } + async queryTransactions(): Promise { let progress = 0 const allCurrencyCodes = [ @@ -874,6 +896,9 @@ export class CosmosEngine extends CurrencyEngine< async startEngine(): Promise { this.engineOn = true await this.tools.connectClient() + this.addToLoop('queryChainId', TRANSACTION_POLL_MILLISECONDS).catch( + () => {} + ) this.addToLoop('queryBalance', ACCOUNT_POLL_MILLISECONDS).catch(() => {}) this.addToLoop('queryBlockheight', ACCOUNT_POLL_MILLISECONDS).catch( () => {} @@ -1166,7 +1191,7 @@ export class CosmosEngine extends CurrencyEngine< accountNumber: longify(this.accountNumber), authInfoBytes, bodyBytes, - chainId: this.tools.chainData.chain_id + chainId: this.chainId }) const signer = await this.tools.createSigner(keys.mnemonic) const signResponse = await signer.signDirect( diff --git a/src/cosmos/CosmosTools.ts b/src/cosmos/CosmosTools.ts index cc9227d54..e4421d8e7 100644 --- a/src/cosmos/CosmosTools.ts +++ b/src/cosmos/CosmosTools.ts @@ -60,9 +60,10 @@ export class CosmosTools implements EdgeCurrencyTools { ) this.methods = methods this.registry = registry - const { chainId, url } = this.networkInfo.chainInfo + const { chainName, url } = this.networkInfo.chainInfo const chainData = chains.find( - chain => chain.chain_id === chainId && chain.network_type === 'mainnet' + chain => + chain.chain_name === chainName && chain.network_type === 'mainnet' ) if (chainData == null) { throw new Error('Unknown chain') diff --git a/src/cosmos/cosmosTypes.ts b/src/cosmos/cosmosTypes.ts index 853357353..f563abb69 100644 --- a/src/cosmos/cosmosTypes.ts +++ b/src/cosmos/cosmosTypes.ts @@ -70,9 +70,11 @@ export interface CosmosNetworkInfo { bech32AddressPrefix: string bip39Path: string chainInfo: { - chainId: string + chainName: string url: string } + defaultChainId: string + chainIdUpdateUrl?: string defaultTransactionFeeUrl?: HttpEndpoint nativeDenom: string pluginMnemonicKeyName: string @@ -275,3 +277,11 @@ export const asCosmosInfoPayload = asObject({ archiveNode: asOptional(asHttpEndpoint) }) export type CosmosInfoPayload = ReturnType + +export const asChainIdUpdate = asObject({ + result: asObject({ + node_info: asObject({ + network: asString // 'thorchain-mainnet-v1', + }) + }) +}) diff --git a/src/cosmos/info/axelarInfo.ts b/src/cosmos/info/axelarInfo.ts index c1f9b4d5e..493da4bfc 100644 --- a/src/cosmos/info/axelarInfo.ts +++ b/src/cosmos/info/axelarInfo.ts @@ -12,9 +12,10 @@ const networkInfo: CosmosNetworkInfo = { bech32AddressPrefix: 'axelar', bip39Path: `m/44'/118'/0'/0/0`, chainInfo: { - chainId: 'axelar-dojo-1', + chainName: 'axelar', url: 'https://raw.githubusercontent.com/cosmos/chain-registry/master/axelar/chain.json' }, + defaultChainId: 'axelar-dojo-1', nativeDenom: 'uaxl', pluginMnemonicKeyName: 'axelarMnemonic', rpcNode: { diff --git a/src/cosmos/info/coreumInfo.ts b/src/cosmos/info/coreumInfo.ts index b1fb15a06..c81e5a316 100644 --- a/src/cosmos/info/coreumInfo.ts +++ b/src/cosmos/info/coreumInfo.ts @@ -133,9 +133,10 @@ const networkInfo: CosmosNetworkInfo = { bech32AddressPrefix: 'core', bip39Path: `m/44'/990'/0'/0/0`, chainInfo: { - chainId: 'coreum-mainnet-1', + chainName: 'coreum', url: 'https://raw.githubusercontent.com/cosmos/chain-registry/master/coreum/chain.json' }, + defaultChainId: 'coreum-mainnet-1', nativeDenom: 'ucore', pluginMnemonicKeyName: 'coreumMnemonic', rpcNode: { diff --git a/src/cosmos/info/cosmoshubInfo.ts b/src/cosmos/info/cosmoshubInfo.ts index 92ea5ba6b..301bf064f 100644 --- a/src/cosmos/info/cosmoshubInfo.ts +++ b/src/cosmos/info/cosmoshubInfo.ts @@ -12,9 +12,10 @@ const networkInfo: CosmosNetworkInfo = { bech32AddressPrefix: 'cosmos', bip39Path: `m/44'/118'/0'/0/0`, chainInfo: { - chainId: 'cosmoshub-4', + chainName: 'cosmoshub', url: 'https://raw.githubusercontent.com/cosmos/chain-registry/master/cosmoshub/chain.json' }, + defaultChainId: 'cosmoshub-4', nativeDenom: 'uatom', pluginMnemonicKeyName: 'cosmoshubMnemonic', rpcNode: { diff --git a/src/cosmos/info/osmosisInfo.ts b/src/cosmos/info/osmosisInfo.ts index 47cbc6f55..fc68c292d 100644 --- a/src/cosmos/info/osmosisInfo.ts +++ b/src/cosmos/info/osmosisInfo.ts @@ -25,9 +25,10 @@ const networkInfo: CosmosNetworkInfo = { bech32AddressPrefix: 'osmo', bip39Path: `m/44'/118'/0'/0/0`, chainInfo: { - chainId: 'osmosis-1', + chainName: 'osmosis', url: 'https://raw.githubusercontent.com/cosmos/chain-registry/master/osmosis/chain.json' }, + defaultChainId: 'osmosis-1', nativeDenom: 'uosmo', pluginMnemonicKeyName: 'osmosisMnemonic', rpcNode: { diff --git a/src/cosmos/info/thorchainruneInfo.ts b/src/cosmos/info/thorchainruneInfo.ts index c01e158bb..c175f1241 100644 --- a/src/cosmos/info/thorchainruneInfo.ts +++ b/src/cosmos/info/thorchainruneInfo.ts @@ -13,9 +13,11 @@ const networkInfo: CosmosNetworkInfo = { bech32AddressPrefix: 'thor', bip39Path: `m/44'/931'/0'/0/0`, chainInfo: { - chainId: 'thorchain-mainnet-v1', + chainName: 'thorchain', url: 'https://raw.githubusercontent.com/cosmos/chain-registry/master/thorchain/chain.json' }, + defaultChainId: 'thorchain-mainnet-v1', + chainIdUpdateUrl: 'https://rpc.ninerealms.com/status', defaultTransactionFeeUrl: { url: 'https://thornode.ninerealms.com/thorchain/network', headers: { 'x-client-id': '{{ninerealmsClientId}}' }