Skip to content

Commit

Permalink
Merge pull request #28 from balancer/27-max-addremove-amounts
Browse files Browse the repository at this point in the history
feat: Add max add/remove methods.
  • Loading branch information
johngrantuk authored Jul 26, 2024
2 parents 1d11f65 + 10fc897 commit 4dc7f0e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"publishConfig": {
"access": "public"
},
"version": "0.0.6",
"version": "0.0.7",
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
Expand Down
2 changes: 2 additions & 0 deletions typescript/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const MAX_UINT256 =
115792089237316195423570985008687907853269984665640564039457584007913129639935n;
22 changes: 22 additions & 0 deletions typescript/src/stable/stablePool.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { MAX_UINT256 } from '../constants';
import { MathSol } from '../utils/math';
import {
MaxSwapParams,
Expand Down Expand Up @@ -45,6 +46,27 @@ export class Stable implements PoolBase {
);
}

getMaxSingleTokenAddAmount(): bigint {
return MAX_UINT256;
}

getMaxSingleTokenRemoveAmount(
isExactIn: boolean,
totalSupply: bigint,
tokenOutBalance: bigint,
tokenOutScalingFactor: bigint,
tokenOutRate: bigint,
): bigint {
return this.getMaxSwapAmount({
swapKind: isExactIn ? SwapKind.GivenIn : SwapKind.GivenOut,
balancesLiveScaled18: [totalSupply, tokenOutBalance],
tokenRates: [1000000000000000000n, tokenOutRate],
scalingFactors: [1000000000000000000n, tokenOutScalingFactor],
indexIn: 0,
indexOut: 1,
});
}

onSwap(swapParams: SwapParams): bigint {
const {
swapKind,
Expand Down
8 changes: 8 additions & 0 deletions typescript/src/vault/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ export enum SwapKind {

export interface PoolBase {
getMaxSwapAmount(maxSwapParams: MaxSwapParams): bigint;
getMaxSingleTokenRemoveAmount(
isExactIn: boolean,
totalSupply: bigint,
tokenOutBalance: bigint,
tokenOutScalingFactor: bigint,
tokenOutRate: bigint,
): bigint;
getMaxSingleTokenAddAmount(): bigint;
onSwap(swapParams: SwapParams): bigint;
computeInvariant(balancesLiveScaled18: bigint[]): bigint;
computeBalance(
Expand Down
22 changes: 22 additions & 0 deletions typescript/src/weighted/weightedPool.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { MAX_UINT256 } from '../constants';
import { MathSol } from '../utils/math';
import {
MaxSwapParams,
Expand Down Expand Up @@ -39,6 +40,27 @@ export class Weighted implements PoolBase {
);
}

getMaxSingleTokenAddAmount(): bigint {
return MAX_UINT256;
}

getMaxSingleTokenRemoveAmount(
isExactIn: boolean,
totalSupply: bigint,
tokenOutBalance: bigint,
tokenOutScalingFactor: bigint,
tokenOutRate: bigint,
): bigint {
return this.getMaxSwapAmount({
swapKind: isExactIn ? SwapKind.GivenIn : SwapKind.GivenOut,
balancesLiveScaled18: [totalSupply, tokenOutBalance],
tokenRates: [1000000000000000000n, tokenOutRate],
scalingFactors: [1000000000000000000n, tokenOutScalingFactor],
indexIn: 0,
indexOut: 1,
});
}

onSwap(swapParams: SwapParams): bigint {
const {
swapKind,
Expand Down

0 comments on commit 4dc7f0e

Please sign in to comment.