From 18aaff54406116d1b96b7c256901e31831e33e90 Mon Sep 17 00:00:00 2001 From: Maxence Raballand Date: Tue, 14 May 2024 16:40:13 +0200 Subject: [PATCH] fix: fix loop for balances --- .changeset/ten-hats-yawn.md | 5 +++++ src/actions/balances.ts | 43 +++++++++++++++++++++---------------- 2 files changed, 29 insertions(+), 19 deletions(-) create mode 100644 .changeset/ten-hats-yawn.md diff --git a/.changeset/ten-hats-yawn.md b/.changeset/ten-hats-yawn.md new file mode 100644 index 0000000..929d51c --- /dev/null +++ b/.changeset/ten-hats-yawn.md @@ -0,0 +1,5 @@ +--- +"@mangrovedao/mgv": patch +--- + +Fix loop for balances diff --git a/src/actions/balances.ts b/src/actions/balances.ts index 3a94dd8..e738b7c 100644 --- a/src/actions/balances.ts +++ b/src/actions/balances.ts @@ -58,35 +58,34 @@ export async function getBalances( return acc }, [] as Token[]) - const tokenBalanceCalls = tokens.map( - (token) => - ({ - address: token.address, - abi: erc20Abi, - functionName: 'balanceOf', - args: [args.user], - }) as const, - ) + const tokenBalanceCalls = tokens.map((token) => { + return { + address: token.address, + abi: erc20Abi, + functionName: 'balanceOf', + args: [args.user], + } as const + }) const overlyingCalls = tokens.flatMap((token) => - logics.map((logic) => - logic.logicOverlying.getOverlyingContractParams({ + logics.map((logic) => { + return logic.logicOverlying.getOverlyingContractParams({ token: token, logic: logic, name: logic.name, - }), - ), + }) + }), ) as ContractFunctionParameters[] const logicBalancesCalls = tokens.flatMap((token) => - logics.map((logic) => - logic.logicBalance.getRoutingLogicBalanceParams({ + logics.map((logic) => { + return logic.logicBalance.getRoutingLogicBalanceParams({ token: token.address, logic: logic.logic, name: logic.name, user: args.user, - }), - ), + }) + }), ) as ContractFunctionParameters[] const result = await getAction( @@ -107,7 +106,7 @@ export async function getBalances( const overlying = tokens.flatMap((token, i) => logics.map((logic, j) => { - const res = result[tokens.length * (i + 1) + j] + const res = result[tokenBalanceCalls.length + i * logics.length + j] const overlying: OverlyingResponse = res.status === 'success' ? logic.logicOverlying.parseOverlyingContractResponse( @@ -129,7 +128,13 @@ export async function getBalances( const logicBalances = tokens.flatMap((token, i) => logics.map((logic, j) => { - const res = result[overlying.length + tokens.length * (i + 1) + j] + const res = + result[ + tokenBalanceCalls.length + + overlyingCalls.length + + logics.length * i + + j + ] const balance = res.status === 'success' ? (res.result as bigint) : 0n return { token, logic, balance } }),