From 71c6a66aedf7af904dd78ba88faa89d51a100a62 Mon Sep 17 00:00:00 2001 From: Sirapop Date: Wed, 25 Sep 2024 11:18:10 +0700 Subject: [PATCH] chore: not save ibc channels when cannot get saved ibc channels data (#114) --- methods/tvl/getTVL.js | 4 ++- methods/tvl/saveIBCChannels.js | 34 ++++++++++--------- package.json | 2 +- .../devnet-amplifier/variables.tf.example | 2 +- .../devnet-verifiers/variables.tf.example | 2 +- terraform/mainnet/variables.tf.example | 2 +- terraform/stagenet/variables.tf.example | 2 +- terraform/testnet/variables.tf.example | 2 +- 8 files changed, 27 insertions(+), 23 deletions(-) diff --git a/methods/tvl/getTVL.js b/methods/tvl/getTVL.js index ba1546d0..bb9174f2 100644 --- a/methods/tvl/getTVL.js +++ b/methods/tvl/getTVL.js @@ -9,6 +9,7 @@ const { getBalance, getTokenSupply } = require('../../utils/chain/evm'); const { getCosmosBalance, getIBCSupply } = require('../../utils/chain/cosmos'); const { IBC_CHANNEL_COLLECTION, TVL_COLLECTION, getChainsList, getChainData, getAxelarConfig, getAssetsList, getAssetData, getITSAssetsList, getITSAssetData, getContracts, getTVLConfig, getCustomTVLConfig } = require('../../utils/config'); const { toHash, getAddress, split, toArray } = require('../../utils/parser'); +const { sleep } = require('../../utils/operator'); const { isString, lastString } = require('../../utils/string'); const { isNumber, toNumber } = require('../../utils/number'); const { timeDiff } = require('../../utils/time'); @@ -189,7 +190,8 @@ module.exports = async params => { source_escrow_addresses = toArray(toArray(ibc_channels).map(d => d.counterparty?.escrow_address)); break; } - else await saveIBCChannels(); + else if (data) await saveIBCChannels(); + await sleep(3000); } } diff --git a/methods/tvl/saveIBCChannels.js b/methods/tvl/saveIBCChannels.js index b6b84cb7..0ac3c5bb 100644 --- a/methods/tvl/saveIBCChannels.js +++ b/methods/tvl/saveIBCChannels.js @@ -18,24 +18,26 @@ module.exports = async () => { } const { data } = { ...await read(IBC_CHANNEL_COLLECTION, { match_all: {} }, { size: 1000 }) }; - allChannels = toArray(allChannels).map(d => { return { ...toArray(data).find(_d => _d.channel_id === d.channel_id), ...d }; }); + allChannels = toArray(allChannels).map(d => ({ ...toArray(data).find(_d => _d.channel_id === d.channel_id), ...d })); - await Promise.all(allChannels.map(channel => new Promise(async resolve => { - const { channel_id, port_id, version, counterparty, updated_at } = { ...channel }; - let { chain_id, escrow_address } = { ...channel }; + if (data) { + await Promise.all(allChannels.map(channel => new Promise(async resolve => { + const { channel_id, port_id, version, counterparty, updated_at } = { ...channel }; + let { chain_id, escrow_address } = { ...channel }; - if (!chain_id || !escrow_address || (counterparty && !counterparty.escrow_address) || timeDiff(updated_at * 1000, 'minutes') > 240) { - const response = await request(createInstance(getLCD(), { gzip: true }), { path: `/ibc/core/channel/v1/channels/${channel_id}/ports/${port_id}/client_state` }); - const { client_state } = { ...response?.identified_client_state }; - chain_id = client_state?.chain_id || chain_id; + if (!chain_id || !escrow_address || (counterparty && !counterparty.escrow_address) || timeDiff(updated_at * 1000, 'minutes') > 240) { + const response = await request(createInstance(getLCD(), { gzip: true }), { path: `/ibc/core/channel/v1/channels/${channel_id}/ports/${port_id}/client_state` }); + const { client_state } = { ...response?.identified_client_state }; + chain_id = client_state?.chain_id || chain_id; - if (chain_id) { - escrow_address = getAddress(`${version}\x00${port_id}/${channel_id}`) || escrow_address; - const { prefix_address } = { ...getChainData(chain_id, 'cosmos') }; - if (counterparty && prefix_address) counterparty.escrow_address = getAddress(`${version}\x00${counterparty.port_id}/${counterparty.channel_id}`, prefix_address); - await write(IBC_CHANNEL_COLLECTION, channel_id, { ...channel, chain_id, counterparty, escrow_address, latest_height: isNumber(client_state?.latest_height?.revision_height) ? toNumber(client_state.latest_height.revision_height) : undefined, updated_at: moment().unix() }, false, false); + if (chain_id) { + escrow_address = getAddress(`${version}\x00${port_id}/${channel_id}`) || escrow_address; + const { prefix_address } = { ...getChainData(chain_id, 'cosmos') }; + if (counterparty && prefix_address) counterparty.escrow_address = getAddress(`${version}\x00${counterparty.port_id}/${counterparty.channel_id}`, prefix_address); + await write(IBC_CHANNEL_COLLECTION, channel_id, { ...channel, chain_id, counterparty, escrow_address, latest_height: isNumber(client_state?.latest_height?.revision_height) ? toNumber(client_state.latest_height.revision_height) : undefined, updated_at: moment().unix() }, false, false); + } } - } - resolve(); - }))); + resolve(); + }))); + } }; \ No newline at end of file diff --git a/package.json b/package.json index 5f18a9d5..cd9e9662 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "axelarscan-api", - "version": "0.0.85", + "version": "0.0.86", "description": "Axelarscan API", "main": "index.js", "scripts": { diff --git a/terraform/devnet-amplifier/variables.tf.example b/terraform/devnet-amplifier/variables.tf.example index abf9357c..2222516a 100644 --- a/terraform/devnet-amplifier/variables.tf.example +++ b/terraform/devnet-amplifier/variables.tf.example @@ -45,7 +45,7 @@ variable "log_level" { variable "app_version" { description = "App version, same as docker image version" - default = "0.0.85" + default = "0.0.86" validation { error_message = "Must be valid semantic version. $Major.$Minor.$Patch" condition = can(regex("^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$", var.app_version)) diff --git a/terraform/devnet-verifiers/variables.tf.example b/terraform/devnet-verifiers/variables.tf.example index c34b9e65..724bf119 100644 --- a/terraform/devnet-verifiers/variables.tf.example +++ b/terraform/devnet-verifiers/variables.tf.example @@ -45,7 +45,7 @@ variable "log_level" { variable "app_version" { description = "App version, same as docker image version" - default = "0.0.85" + default = "0.0.86" validation { error_message = "Must be valid semantic version. $Major.$Minor.$Patch" condition = can(regex("^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$", var.app_version)) diff --git a/terraform/mainnet/variables.tf.example b/terraform/mainnet/variables.tf.example index 572db7f0..0fd8ca7a 100644 --- a/terraform/mainnet/variables.tf.example +++ b/terraform/mainnet/variables.tf.example @@ -45,7 +45,7 @@ variable "log_level" { variable "app_version" { description = "App version, same as docker image version" - default = "0.0.85" + default = "0.0.86" validation { error_message = "Must be valid semantic version. $Major.$Minor.$Patch" condition = can(regex("^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$", var.app_version)) diff --git a/terraform/stagenet/variables.tf.example b/terraform/stagenet/variables.tf.example index 927ee0d0..c19bbb3e 100644 --- a/terraform/stagenet/variables.tf.example +++ b/terraform/stagenet/variables.tf.example @@ -45,7 +45,7 @@ variable "log_level" { variable "app_version" { description = "App version, same as docker image version" - default = "0.0.85" + default = "0.0.86" validation { error_message = "Must be valid semantic version. $Major.$Minor.$Patch" condition = can(regex("^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$", var.app_version)) diff --git a/terraform/testnet/variables.tf.example b/terraform/testnet/variables.tf.example index bea8970f..b44175d3 100644 --- a/terraform/testnet/variables.tf.example +++ b/terraform/testnet/variables.tf.example @@ -45,7 +45,7 @@ variable "log_level" { variable "app_version" { description = "App version, same as docker image version" - default = "0.0.85" + default = "0.0.86" validation { error_message = "Must be valid semantic version. $Major.$Minor.$Patch" condition = can(regex("^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$", var.app_version))