Skip to content

Commit

Permalink
Merge pull request #41 from mangrovedao/fix/loopBalances
Browse files Browse the repository at this point in the history
fix: fix loop for balances
  • Loading branch information
maxencerb authored May 14, 2024
2 parents c0ec2ed + 18aaff5 commit 419e985
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-hats-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mangrovedao/mgv": patch
---

Fix loop for balances
43 changes: 24 additions & 19 deletions src/actions/balances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,35 +58,34 @@ export async function getBalances<TLogics extends Logic[] = Logic[]>(
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(
Expand All @@ -107,7 +106,7 @@ export async function getBalances<TLogics extends Logic[] = Logic[]>(

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(
Expand All @@ -129,7 +128,13 @@ export async function getBalances<TLogics extends Logic[] = Logic[]>(

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 }
}),
Expand Down

0 comments on commit 419e985

Please sign in to comment.