From 517c9f2b7aca4f047ef3bb31414b2b451fbf7ff9 Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 2 Jan 2025 16:40:03 +0300 Subject: [PATCH 1/2] bump some contracts support for spark finance --- projects/spark/index.js | 55 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/projects/spark/index.js b/projects/spark/index.js index b39e27907731..73be1bf5a5fa 100644 --- a/projects/spark/index.js +++ b/projects/spark/index.js @@ -1,11 +1,54 @@ -const { sumTokens } = require("../helper/chain/fuel") +const { sumTokens } = require("../helper/chain/fuel"); async function tvl(api) { - const contractId = '0xfe2c524ad8e088f33d232a45dbea43e792861640b71aa1814b30506bf8430ee5' - return sumTokens({ api, owner: contractId, }) + const contractIds = [ + '0xfe2c524ad8e088f33d232a45dbea43e792861640b71aa1814b30506bf8430ee5', + '0xdafe498b31f24ea5577055e86bf77e96bcba2c39a7ae47abaa819c303a45a352', + '0x81e83f73530c262b0dbf5414649a875c48a48144de3c08ff68cb9d54b36f2eaa', + ]; + + try { + const balancesList = await Promise.all( + contractIds.map(async (contractId) => { + try { + const balances = await sumTokens({ api, owner: contractId }); + console.log(`Balances from contract ${contractId}:`, balances); + return balances; + } catch (err) { + console.error(`Error fetching balance for contract ${contractId}:`, err.message); + return {}; + } + }) + ); + + const combinedBalances = balancesList.reduce((acc, balances) => { + Object.entries(balances).forEach(([key, value]) => { + const numericValue = BigInt(value || 0); + acc[key] = acc[key] ? BigInt(Math.max(Number(acc[key]), Number(numericValue))) : numericValue; + }); + return acc; + }, {}); + + console.log("Final combined balances:", combinedBalances); + + const totalTvl = Object.values(combinedBalances).reduce((sum, value) => { + return sum + Number(value); + }, 0); + + const formattedBalances = Object.fromEntries( + Object.entries(combinedBalances).map(([key, value]) => [key, value.toString()]) + ); + + console.log("Formatted Balances for TVL:", formattedBalances); + + return formattedBalances; + } catch (err) { + console.error("Unexpected error during TVL calculation:", err.message); + return {}; + } } module.exports = { - fuel: { tvl }, - timetravel: false, -} \ No newline at end of file + fuel: { tvl }, + timetravel: false, +}; \ No newline at end of file From 00973395eb0074d79ceb2e222c8edc4b0bdde807 Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 2 Jan 2025 17:33:03 +0300 Subject: [PATCH 2/2] remove try catch --- projects/spark/index.js | 68 ++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 39 deletions(-) diff --git a/projects/spark/index.js b/projects/spark/index.js index 73be1bf5a5fa..0bc5d2ce8e64 100644 --- a/projects/spark/index.js +++ b/projects/spark/index.js @@ -7,45 +7,35 @@ async function tvl(api) { '0x81e83f73530c262b0dbf5414649a875c48a48144de3c08ff68cb9d54b36f2eaa', ]; - try { - const balancesList = await Promise.all( - contractIds.map(async (contractId) => { - try { - const balances = await sumTokens({ api, owner: contractId }); - console.log(`Balances from contract ${contractId}:`, balances); - return balances; - } catch (err) { - console.error(`Error fetching balance for contract ${contractId}:`, err.message); - return {}; - } - }) - ); - - const combinedBalances = balancesList.reduce((acc, balances) => { - Object.entries(balances).forEach(([key, value]) => { - const numericValue = BigInt(value || 0); - acc[key] = acc[key] ? BigInt(Math.max(Number(acc[key]), Number(numericValue))) : numericValue; - }); - return acc; - }, {}); - - console.log("Final combined balances:", combinedBalances); - - const totalTvl = Object.values(combinedBalances).reduce((sum, value) => { - return sum + Number(value); - }, 0); - - const formattedBalances = Object.fromEntries( - Object.entries(combinedBalances).map(([key, value]) => [key, value.toString()]) - ); - - console.log("Formatted Balances for TVL:", formattedBalances); - - return formattedBalances; - } catch (err) { - console.error("Unexpected error during TVL calculation:", err.message); - return {}; - } + const balancesList = await Promise.all( + contractIds.map(async (contractId) => { + const balances = await sumTokens({ api, owner: contractId }); + console.log(`Balances from contract ${contractId}:`, balances); + return balances; + }) + ); + + const combinedBalances = balancesList.reduce((acc, balances) => { + Object.entries(balances).forEach(([key, value]) => { + const numericValue = BigInt(value || 0); + acc[key] = acc[key] ? BigInt(Math.max(Number(acc[key]), Number(numericValue))) : numericValue; + }); + return acc; + }, {}); + + console.log("Final combined balances:", combinedBalances); + + const totalTvl = Object.values(combinedBalances).reduce((sum, value) => { + return sum + Number(value); + }, 0); + + const formattedBalances = Object.fromEntries( + Object.entries(combinedBalances).map(([key, value]) => [key, value.toString()]) + ); + + console.log("Formatted Balances for TVL:", formattedBalances); + + return formattedBalances; } module.exports = {