Skip to content

Commit

Permalink
Add chain ID query
Browse files Browse the repository at this point in the history
  • Loading branch information
peachbits committed Aug 9, 2024
1 parent 668c9a2 commit d586300
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 26 additions & 1 deletion src/cosmos/CosmosEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import { EdgeTokenId, MakeTxParams } from '../common/types'
import { cleanTxLogs } from '../common/utils'
import { CosmosTools } from './CosmosTools'
import {
asChainIdUpdate,
asCosmosPrivateKeys,
asCosmosTxOtherParams,
asCosmosWalletOtherData,
Expand Down Expand Up @@ -110,6 +111,7 @@ export class CosmosEngine extends CurrencyEngine<
feeCache: Map<string, CosmosFee>
stakedBalanceCache: string
stakingSupported: boolean
chainId: string

constructor(
env: PluginEnvironment<CosmosNetworkInfo>,
Expand All @@ -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) {
Expand Down Expand Up @@ -492,6 +495,25 @@ export class CosmosEngine extends CurrencyEngine<
}
}

async queryChainId(): Promise<void> {
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<void> {
let progress = 0
const allCurrencyCodes = [
Expand Down Expand Up @@ -874,6 +896,9 @@ export class CosmosEngine extends CurrencyEngine<
async startEngine(): Promise<void> {
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(
() => {}
Expand Down Expand Up @@ -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(
Expand Down
5 changes: 3 additions & 2 deletions src/cosmos/CosmosTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
12 changes: 11 additions & 1 deletion src/cosmos/cosmosTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -275,3 +277,11 @@ export const asCosmosInfoPayload = asObject({
archiveNode: asOptional(asHttpEndpoint)
})
export type CosmosInfoPayload = ReturnType<typeof asCosmosInfoPayload>

export const asChainIdUpdate = asObject({
result: asObject({
node_info: asObject({
network: asString // 'thorchain-mainnet-v1',
})
})
})
3 changes: 2 additions & 1 deletion src/cosmos/info/axelarInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
3 changes: 2 additions & 1 deletion src/cosmos/info/coreumInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
3 changes: 2 additions & 1 deletion src/cosmos/info/cosmoshubInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
3 changes: 2 additions & 1 deletion src/cosmos/info/osmosisInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
4 changes: 3 additions & 1 deletion src/cosmos/info/thorchainruneInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}}' }
Expand Down

0 comments on commit d586300

Please sign in to comment.