Skip to content

Commit

Permalink
Merge pull request #490 from balancer/487-pool-state-with-balances-le…
Browse files Browse the repository at this point in the history
…ss-18-decimals

Fix getPoolStateWithBalancesV3 with less 18 decimals tokens
  • Loading branch information
brunoguerios authored Nov 20, 2024
2 parents ec2042e + 2712ee0 commit df5526f
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-phones-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@balancer/sdk": patch
---

Fix getPoolStateWithBalancesV3 with less 18 decimals tokens
11 changes: 9 additions & 2 deletions src/entities/utils/getPoolStateWithBalancesV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -40,17 +41,23 @@ 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,
tokens: sortedTokens.map((token, i) => ({
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,
};
Expand Down
101 changes: 101 additions & 0 deletions test/v3/utils/getPoolStateWithBalancesV3.integration.test.ts
Original file line number Diff line number Diff line change
@@ -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<PoolState> {
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,
};
}
}

/******************************************************************************/

0 comments on commit df5526f

Please sign in to comment.