Skip to content

Commit

Permalink
feat: update tvl for custom assets (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
nrsirapop authored Oct 25, 2024
1 parent 4f8a925 commit a6391b8
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 12 deletions.
1 change: 1 addition & 0 deletions config/chains.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1673,6 +1673,7 @@ mainnet:
ojo:
chain_id: "agamotto"
chain_name: "ojo"
unstable: true
endpoints:
lcd:
- "https://api.agamotto-val-stage-0.ojo.network"
Expand Down
34 changes: 29 additions & 5 deletions methods/tvl/getTVL.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))));

Expand All @@ -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) {
Expand Down Expand Up @@ -86,13 +88,28 @@ 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');
const axelarnetLCDUrl = _.head(axelarnet.endpoints?.lcd);

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';

Expand Down Expand Up @@ -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) {}
Expand Down Expand Up @@ -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 };
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "axelarscan-api",
"version": "0.0.101",
"version": "0.0.102",
"description": "Axelarscan API",
"main": "index.js",
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion services/interval-update/updateTVL.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
2 changes: 1 addition & 1 deletion terraform/devnet-amplifier/variables.tf.example
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion terraform/devnet-verifiers/variables.tf.example
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion terraform/mainnet/variables.tf.example
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion terraform/stagenet/variables.tf.example
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion terraform/testnet/variables.tf.example
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit a6391b8

Please sign in to comment.