Skip to content

Commit

Permalink
fix big array of tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
kolima committed Jun 14, 2024
1 parent b0067cc commit 12bd99f
Showing 1 changed file with 36 additions and 12 deletions.
48 changes: 36 additions & 12 deletions projects/myso-v2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ const getContracts = async (chainId) => {
return data.contracts;
};

const CoveredCallStrategiesAbi = "function strategies(uint256) view returns (address underlying, uint128 maxDeposits, uint128 minDeposits, uint128 startTime, uint128 tenor, uint128 minStrike, uint128 subscribeEndTime, uint256 totalDeposits, uint128 tokenRewardsPerDeposit)"
const CoveredCallStrategiesAbi =
"function strategies(uint256) view returns (address underlying, uint128 maxDeposits, uint128 minDeposits, uint128 startTime, uint128 tenor, uint128 minStrike, uint128 subscribeEndTime, uint256 totalDeposits, uint128 tokenRewardsPerDeposit)";

const whaleMatchTotalSubscriptionsAbi = "function totalSubscriptions(address) view returns (uint256)";
const whaleMatchTotalSubscriptionsAbi =
"function totalSubscriptions(address) view returns (uint256)";

const getBlitzMatchBalances = async (api, contracts, fromBlock) => {
const vaultFactory = contracts?.find(
Expand All @@ -63,6 +65,16 @@ const getBlitzMatchBalances = async (api, contracts, fromBlock) => {
fromBlock,
});

const CHUNK_SIZE = 40;

function chunkArray(array, chunkSize) {
const chunks = [];
for (let i = 0; i < array.length; i += chunkSize) {
chunks.push(array.slice(i, i + chunkSize));
}
return chunks;
}

let ownerTokens = logs.map((i) => {
return [
contracts
Expand All @@ -72,10 +84,19 @@ const getBlitzMatchBalances = async (api, contracts, fromBlock) => {
];
});

return sumTokens2({ api, ownerTokens, });
let ownerTokenChunks = chunkArray(ownerTokens, CHUNK_SIZE);

return processChunks(api, ownerTokenChunks);
};

const getCoveredCallOfTheWeekBalances = async (api, contracts,) => {
async function processChunks(api, ownerTokenChunks) {
const promises = ownerTokenChunks.map((chunk) =>
sumTokens2({ api, ownerTokens: chunk })
);
await Promise.all(promises);
}

const getCoveredCallOfTheWeekBalances = async (api, contracts) => {
const coveredCallOfTheWeeks = contracts?.filter(
(contract) => contract.type === "p2p"
);
Expand All @@ -90,8 +111,10 @@ const getCoveredCallOfTheWeekBalances = async (api, contracts,) => {
abi: CoveredCallStrategiesAbi,
});

coveredCallOfTheWeekStrategies.forEach((strategy) => api.add(strategy.underlying, strategy.totalDeposits))
}
coveredCallOfTheWeekStrategies.forEach((strategy) =>
api.add(strategy.underlying, strategy.totalDeposits)
);
};

const getWhaleMatchBalances = async (api, contracts, fromBlock) => {
const fundingPoolFactory = contracts.find(
Expand All @@ -101,8 +124,7 @@ const getWhaleMatchBalances = async (api, contracts, fromBlock) => {
const fundingPools =
contracts.filter((contract) => contract.type === "funding_pool") || [];

if (!(fundingPoolFactory && fundingPools.length))
return;
if (!(fundingPoolFactory && fundingPools.length)) return;

const logs = await getLogs({
api,
Expand All @@ -121,16 +143,18 @@ const getWhaleMatchBalances = async (api, contracts, fromBlock) => {
})),
abi: whaleMatchTotalSubscriptionsAbi,
});
const tokens = await api.multiCall({ abi: 'address:depositToken', calls: logs.map(log => log.fundingPool) })
api.add(tokens, loanProposalBalances)
const tokens = await api.multiCall({
abi: "address:depositToken",
calls: logs.map((log) => log.fundingPool),
});
api.add(tokens, loanProposalBalances);
}


let tokensAndOwners = fundingPools.map((fundingPool) => {
return [fundingPool.loanCcyToken, fundingPool.contractAddr];
});

return sumTokens2({ api, tokensAndOwners, });
return sumTokens2({ api, tokensAndOwners });
};

async function tvl(api) {
Expand Down

0 comments on commit 12bd99f

Please sign in to comment.