diff --git a/config/chains.yml b/config/chains.yml index d419d371..6eaefbaa 100644 --- a/config/chains.yml +++ b/config/chains.yml @@ -1673,6 +1673,7 @@ mainnet: ojo: chain_id: "agamotto" chain_name: "ojo" + unstable: true endpoints: lcd: - "https://api.agamotto-val-stage-0.ojo.network" diff --git a/methods/tvl/getTVL.js b/methods/tvl/getTVL.js index ce8c3b32..93ebdbce 100644 --- a/methods/tvl/getTVL.js +++ b/methods/tvl/getTVL.js @@ -40,10 +40,10 @@ module.exports = async params => { const itsAssetsData = toArray(await getITSAssetsList()); const { gateway_contracts } = { ...await getContracts() }; - const { asset, chain, force_update } = { ...params }; + const { asset, chain, force_update, is_interval, custom_assets_only } = { ...params }; let { assets, chains } = { ...params }; assets = toArray(assets || asset); - assets = assets.length === 0 ? _.concat(assetsData, itsAssetsData).map(d => d.id) : await Promise.all(assets.map(d => new Promise(async resolve => resolve((await getAssetData(d, assetsData))?.denom || (await getITSAssetData(d, itsAssetsData))?.id)))); + assets = assets.length === 0 ? _.concat(assetsData, itsAssetsData).filter(d => !custom_assets_only || d.is_custom).map(d => d.id) : await Promise.all(assets.map(d => new Promise(async resolve => resolve((await getAssetData(d, assetsData))?.denom || (await getITSAssetData(d, itsAssetsData))?.id)))); chains = toArray(chains || chain); chains = chains.length === 0 ? getChainsList().filter(d => (d.chain_type === 'cosmos' || gateway_contracts?.[d.id]?.address) && !d.no_tvl).map(d => d.id) : _.uniq(_.concat('axelarnet', toArray(chains.map(d => getChainData(d)?.id)))); @@ -56,6 +56,8 @@ module.exports = async params => { // set cacheId on querying single asset on every chains const cacheId = assets.length === 1 && hasAllChains && normalizeCacheId(_.head(assets)); let cache; + let cachesForIntervalUpdate; + if (!force_update) { // query cache if (cacheId) { @@ -86,6 +88,15 @@ module.exports = async params => { } } } + else if (is_interval) { + const response = await read(TVL_COLLECTION, { + bool: { + should: assets.map(id => ({ match: { _id: normalizeCacheId(id) } })), + minimum_should_match: 1, + }, + }, { size: assets.length }); + cachesForIntervalUpdate = response?.data; + } const axelarConfig = await getAxelarConfig(); const axelarnet = getChainData('axelarnet'); @@ -93,6 +104,12 @@ module.exports = async params => { const data = []; for (const asset of assets) { + const cacheData = toArray(cachesForIntervalUpdate).find(d => d.asset === asset); + if (cacheData?.updated_at && timeDiff(cacheData.updated_at * 1000) < CACHE_AGE_SECONDS) { + data.push(cacheData); + continue; + } + let assetData = await getAssetData(asset, assetsData); let assetType = 'gateway'; @@ -234,7 +251,7 @@ module.exports = async params => { ]) ), supply_urls: toArray(!isNativeOnCosmos && toArray(escrow_addresses).length > 0 && [ibc_denom && `${LCDUrl}/cosmos/bank/v1beta1/supply/${encodeURIComponent(ibc_denom)}`, `${LCDUrl}/cosmos/bank/v1beta1/supply`]), - success: isNumber(isNotNativeOnAxelarnet ? total : supply) || !ibc_denom, + success: isNumber(isNotNativeOnAxelarnet ? total : supply) || !ibc_denom || d.unstable, }; } } catch (error) {} @@ -278,10 +295,17 @@ module.exports = async params => { let result = { data, updated_at: moment().unix() }; let not_updated_on_chains; if (data.length === 0 && cache) result = cache; - else if (cacheId) { + else if (cacheId || custom_assets_only) { const unsuccessData = data.filter(d => !d.success); // caching - if (unsuccessData.length === 0) await write(TVL_COLLECTION, cacheId, result); + if (unsuccessData.length === 0) { + if (cacheId) await write(TVL_COLLECTION, cacheId, result); + else if (data.length > 0) { + for (const d of data) { + await write(TVL_COLLECTION, normalizeCacheId(d.asset), { data: [d], updated_at: moment().unix() }); + } + } + } else not_updated_on_chains = unsuccessData.flatMap(d => Object.entries(d.tvl).filter(([k, v]) => !v?.success).map(([k, v]) => k)); } return { ...result, not_updated_on_chains }; diff --git a/package.json b/package.json index 863198ce..371f4cc3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "axelarscan-api", - "version": "0.0.101", + "version": "0.0.102", "description": "Axelarscan API", "main": "index.js", "scripts": { diff --git a/services/interval-update/updateTVL.js b/services/interval-update/updateTVL.js index eb65f169..46cabe82 100644 --- a/services/interval-update/updateTVL.js +++ b/services/interval-update/updateTVL.js @@ -10,8 +10,9 @@ module.exports = async params => { // run every 15 minutes if (minute % 15 !== 0) return; const data = {}; + await getTVL({ force_update: true, is_interval: true, custom_assets_only: true }); for (const d of toArray(_.concat(await getAssetsList(), await getITSAssetsList())).filter(d => !params?.id || d.id === params.id).filter(d => params?.id || (minute % 30 === 0 ? d.id.startsWith('0x') : !d.id.startsWith('0x')))) { - data[d.id] = await getTVL({ asset: d.id, force_update: true }); + data[d.id] = await getTVL({ asset: d.id, force_update: true, is_interval: true }); } return data; }; \ No newline at end of file diff --git a/terraform/devnet-amplifier/variables.tf.example b/terraform/devnet-amplifier/variables.tf.example index d96fc99d..67fb1d7c 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.101" + default = "0.0.102" 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 eef1aefd..53ad5b8e 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.101" + default = "0.0.102" 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 f7ce46e8..331c08b5 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.101" + default = "0.0.102" 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 f60c30d5..d7759459 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.101" + default = "0.0.102" 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 e82dd94b..8cf61211 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.101" + default = "0.0.102" 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))