Skip to content

Commit

Permalink
Fix Danogo TVL (#7323)
Browse files Browse the repository at this point in the history
* [CNIO-535] Multiply asset value with asset quantity

* Fix overidden asset quantity (#2)

* minor fix

---------

Co-authored-by: g1nt0ki <[email protected]>
  • Loading branch information
anhtv-teko and g1nt0ki authored Aug 30, 2023
1 parent a11164b commit 65beb48
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions projects/danogo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,30 @@ const fetchSmartContractUTXOs = async (address) => {
return kupoResponse.data;
}

const fetchAssetValue = async (assetIds) => {
const fetchAssetValue = async (assetsInfo) => {
const assetIds = Object.keys(assetsInfo)
let totalAssetsValue = 0;
const gatewayResponse = await postURL(`${DANOGO_GATEWAY_ENDPOINT}/cardano-asset-value`, { assetIds: assetIds});
return gatewayResponse.data.data.assetValues
.map((asset) => Number(BigInt(asset.adaValue) * BigInt(100) / BigInt(ADA_TO_LOVELACE)) / 100)
.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
gatewayResponse.data.data.assetValues.forEach((asset) => {
const assetValue = Number(BigInt(asset.adaValue) * BigInt(100) / BigInt(ADA_TO_LOVELACE)) / 100;
totalAssetsValue += assetValue * assetsInfo[asset.assetId];
});
return totalAssetsValue;
}

function mergeObjectsWithSum(target, ...sources) {
for (const source of sources) {
for (const key in source) {
if (source.hasOwnProperty(key)) {
if (key in target) {
target[key] += source[key];
} else {
target[key] = source[key];
}
}
}
}
return target;
}

const fetch = async () => {
Expand All @@ -36,24 +55,23 @@ const fetch = async () => {
return fetchSmartContractUTXOs(address)
}));

let assetIds = [];
let assetInfos = {};
let totalValueLocked = 0;
smartContractsUtxos.forEach(async (smUtxos) => {
smUtxos.forEach((utxo) => {
totalValueLocked += utxo.value.coins / ADA_TO_LOVELACE;
Object.keys(utxo.value.assets).forEach((assetId) => {
assetIds.push(assetId);
})
assetInfos = mergeObjectsWithSum(assetInfos, utxo.value.assets);
})
});

const totalAssetsValues = await fetchAssetValue(assetIds);
const totalAssetsValues = await fetchAssetValue(assetInfos);
totalValueLocked += totalAssetsValues;

return { cardano: totalValueLocked };
}

module.exports = {
misrepresentedTokens: true,
timetravel: false,
cardano: {
tvl: fetch
Expand Down

0 comments on commit 65beb48

Please sign in to comment.