From cf3917968ce7267b3b3371dbace845a9773f6855 Mon Sep 17 00:00:00 2001 From: Bruno Eidam Guerios Date: Mon, 11 Nov 2024 16:18:06 -0300 Subject: [PATCH 1/2] Fix getPoolStateWithBalancesV3 with less 18 decimals tokens --- .../utils/getPoolStateWithBalancesV3.ts | 11 +- ...oolStateWithBalancesV3.integration.test.ts | 101 ++++++++++++++++++ 2 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 test/v3/utils/getPoolStateWithBalancesV3.integration.test.ts diff --git a/src/entities/utils/getPoolStateWithBalancesV3.ts b/src/entities/utils/getPoolStateWithBalancesV3.ts index 10403119..a6fe51be 100644 --- a/src/entities/utils/getPoolStateWithBalancesV3.ts +++ b/src/entities/utils/getPoolStateWithBalancesV3.ts @@ -6,6 +6,7 @@ import { CHAINS, VAULT_V3 } from '@/utils'; import { getSortedTokens } from './getSortedTokens'; import { PoolState, PoolStateWithBalances } from '../types'; import { vaultExtensionAbi_V3 } from '@/abi'; +import { TokenAmount } from '../tokenAmount'; export const getPoolStateWithBalancesV3 = async ( poolState: PoolState, @@ -40,9 +41,12 @@ export const getPoolStateWithBalancesV3 = async ( } const totalShares = outputs[0].result as bigint; - const balances = outputs[1].result as bigint[]; + const balancesScale18 = outputs[1].result as bigint[]; const sortedTokens = getSortedTokens(poolState.tokens, chainId); + const balances = sortedTokens.map((token, i) => + TokenAmount.fromScale18Amount(token, balancesScale18[i]), + ); const poolStateWithBalances: PoolStateWithBalances = { ...poolState, @@ -50,7 +54,10 @@ export const getPoolStateWithBalancesV3 = async ( address: token.address, decimals: token.decimals, index: i, - balance: formatUnits(balances[i], token.decimals) as HumanAmount, + balance: formatUnits( + balances[i].amount, + token.decimals, + ) as HumanAmount, })), totalShares: formatEther(totalShares) as HumanAmount, }; diff --git a/test/v3/utils/getPoolStateWithBalancesV3.integration.test.ts b/test/v3/utils/getPoolStateWithBalancesV3.integration.test.ts new file mode 100644 index 00000000..e97bdde9 --- /dev/null +++ b/test/v3/utils/getPoolStateWithBalancesV3.integration.test.ts @@ -0,0 +1,101 @@ +// pnpm test -- v3/utils/getPoolStateWithBalancesV3.integration.test.ts + +import { config } from 'dotenv'; +config(); + +import { + Hex, + PoolState, + ChainId, + PoolType, + getPoolStateWithBalancesV3, + PoolStateWithBalances, +} from '@/index'; +import { POOLS, TOKENS } from '../../lib/utils'; +import { ANVIL_NETWORKS, startFork } from '../../anvil/anvil-global-setup'; + +const protocolVersion = 3; + +const chainId = ChainId.SEPOLIA; + +const poolId = POOLS[chainId].MOCK_USDC_DAI_POOL.id; +const USDC = TOKENS[chainId].USDC; +const DAI = TOKENS[chainId].DAI; + +describe('add liquidity test', () => { + let poolState: PoolState; + let rpcUrl: string; + + beforeAll(async () => { + // setup mock api + const api = new MockApi(); + + // get pool state from api + poolState = await api.getPool(poolId); + + ({ rpcUrl } = await startFork( + ANVIL_NETWORKS[ChainId[chainId]], + undefined, + 7057106n, + )); + }); + + describe('getPoolStateWithBalancesV3', () => { + test('<18 decimals tokens', async () => { + const poolStateWithBalances = await getPoolStateWithBalancesV3( + poolState, + chainId, + rpcUrl, + ); + + const mockData: PoolStateWithBalances = { + ...poolState, + tokens: [ + { + address: USDC.address, + decimals: USDC.decimals, + index: 0, + balance: '9585.21526', + }, + { + address: DAI.address, + decimals: DAI.decimals, + index: 1, + balance: '10256.288668913000293429', + }, + ], + totalShares: '9912.817276660069114899', + }; + + expect(poolStateWithBalances).to.deep.eq(mockData); + }); + }); +}); + +/*********************** Mock To Represent API Requirements **********************/ +class MockApi { + public async getPool(id: Hex): Promise { + const tokens = [ + { + address: USDC.address, + decimals: USDC.decimals, + index: 0, + }, + { + address: DAI.address, + decimals: DAI.decimals, + index: 1, + }, + ]; + + return { + id, + address: id, + type: PoolType.Weighted, + tokens, + protocolVersion, + }; + } +} + +/******************************************************************************/ From 2712ee015d8d2d5f3ad90453707a4d501f0fea5c Mon Sep 17 00:00:00 2001 From: Bruno Eidam Guerios Date: Mon, 11 Nov 2024 16:18:25 -0300 Subject: [PATCH 2/2] Add changeset --- .changeset/pink-phones-suffer.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/pink-phones-suffer.md diff --git a/.changeset/pink-phones-suffer.md b/.changeset/pink-phones-suffer.md new file mode 100644 index 00000000..87ca46ac --- /dev/null +++ b/.changeset/pink-phones-suffer.md @@ -0,0 +1,5 @@ +--- +"@balancer/sdk": patch +--- + +Fix getPoolStateWithBalancesV3 with less 18 decimals tokens