Skip to content

Commit

Permalink
Changing input of params_builder to map
Browse files Browse the repository at this point in the history
  • Loading branch information
Luiz Gustavo Abou Hatem De Liz committed Jul 7, 2023
1 parent ae8cfb5 commit b103ccb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
26 changes: 11 additions & 15 deletions balancer-js/src/modules/pools/queries/params_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,32 @@ export class ParamsBuilder implements PoolQueries.ParamsBuilder {

/**
* Encodes the query to get expected amount of BPT when joining a Pool with exact token inputs
* @param maxAmountsIn - the amounts each of token to deposit in the pool as liquidity, doesn't need to be for all tokens, order needs to match tokensIn
* @param tokensIn - The token address for each token that will be deposited in the pool, order needs to match maxAmountsIn
* @param maxAmountsInByToken - The amounts each of token, mapped by token address, to deposit in the pool as liquidity,
* doesn't need to have all tokens, only the ones that will be deposited
* @param minimumBPT - the minimum acceptable BPT to receive in return for deposited tokens (optional)
* @param fromInternalBalance
*/
buildQueryJoinExactIn({
maxAmountsIn,
tokensIn,
maxAmountsInByToken,
minimumBPT = Zero,
}: PoolQueries.JoinExactInParams): PoolQueries.queryJoinParams {
const bptIndex = this.pool.tokensList.findIndex((token) =>
this.pool.id.includes(token)
);
// Sort amounts in by token address
const tokenMaxAmountsInByAddress: Map<string, BigNumber> = tokensIn.reduce(
(acc, tokenAddress, index) => {
return acc.set(tokenAddress, maxAmountsIn[index]);
},
new Map<string, BigNumber>()
);

const assets = [...this.pool.tokensList];

let maxInWithoutBpt = this.pool.tokensList.map(
const maxAmountsIn = this.pool.tokensList.map(
(tokenAddress) =>
tokenMaxAmountsInByAddress.get(tokenAddress) ?? BigNumber.from('0')
maxAmountsInByToken.get(tokenAddress) ?? BigNumber.from('0')
);

let maxInWithoutBpt;

// Remove BPT token from amounts for user data
if (bptIndex > -1) {
maxInWithoutBpt = removeItem(maxInWithoutBpt, bptIndex);
maxInWithoutBpt = removeItem(maxAmountsIn, bptIndex);
} else {
maxInWithoutBpt = maxAmountsIn;
}

const userData = this.encoder.joinExactTokensInForBPTOut(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { expect } from 'chai';
import { BalancerSDK, Network, PoolType } from '@/.';
import { bn } from '@/lib/utils';
import { ParamsBuilder } from '.';
import { zipObject } from "lodash";
import { BigNumber } from "@ethersproject/bignumber";

dotenv.config();

Expand Down Expand Up @@ -63,18 +65,18 @@ const { balancerHelpers } = contracts;
describe('join and exit queries', () => {
// for each poolType test outputs
pools.forEach((pool) => {
context(`${pool.poolType} pool`, () => {
context(`${ pool.poolType } pool`, () => {
before(async () => {
queryParams = new ParamsBuilder(pool);
});

it('should joinExactIn', async () => {
const maxAmountsIn = [bn(1)];
const tokensIn = [pool.tokensList[1]];
const maxAmountsInByToken = new Map<string, BigNumber>([
[pool.tokensList[1], bn(1)],
]);

const params = queryParams.buildQueryJoinExactIn({
maxAmountsIn,
tokensIn,
maxAmountsInByToken,
});
const join = await balancerHelpers.callStatic.queryJoin(...params);
expect(Number(join.bptOut)).to.be.gt(0);
Expand Down
3 changes: 1 addition & 2 deletions balancer-js/src/modules/pools/queries/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ export interface Pool {
}

export interface JoinExactInParams {
maxAmountsIn: BigNumber[];
tokensIn: string[];
maxAmountsInByToken: Map<string, BigNumber>;
minimumBPT?: BigNumber;
}

Expand Down

0 comments on commit b103ccb

Please sign in to comment.