Skip to content

Commit

Permalink
feat: Add maxSwapAmount.
Browse files Browse the repository at this point in the history
  • Loading branch information
johngrantuk committed Jul 12, 2024
1 parent 4763e9f commit 56044ce
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 4 deletions.
23 changes: 22 additions & 1 deletion typescript/src/stable/stablePool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { MathSol } from '../utils/math';
import { type PoolBase, SwapKind, type SwapParams } from '../vault/types';
import {
MaxSwapParams,
type PoolBase,
SwapKind,
type SwapParams,
} from '../vault/types';
import {
_computeOutGivenExactIn,
_computeInGivenExactOut,
Expand All @@ -16,6 +21,22 @@ export class Stable implements PoolBase {
this.amp = poolState.amp;
}

getMaxSwapAmount(maxSwapParams: MaxSwapParams): bigint {
const {
swapKind,
balancesLiveScaled18,
indexIn,
indexOut,
tokenRates,
} = maxSwapParams;
if (swapKind === SwapKind.GivenIn)
return MathSol.mulDownFixed(
balancesLiveScaled18[indexOut],
MathSol.divDownFixed(tokenRates[indexOut], tokenRates[indexIn]),
);
return balancesLiveScaled18[indexOut];
}

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

export interface PoolBase {
getMaxSwapAmount(maxSwapParams: MaxSwapParams): bigint;
onSwap(swapParams: SwapParams): bigint;
computeInvariant(balancesLiveScaled18: bigint[]): bigint;
computeBalance(
Expand All @@ -13,6 +14,14 @@ export interface PoolBase {
): bigint;
}

export type MaxSwapParams = {
swapKind: SwapKind;
balancesLiveScaled18: bigint[];
tokenRates: bigint[];
indexIn: number;
indexOut: number;
};

export type SwapParams = {
swapKind: SwapKind;
amountGivenScaled18: bigint;
Expand Down
23 changes: 22 additions & 1 deletion typescript/src/weighted/weightedPool.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { type PoolBase, SwapKind, type SwapParams } from '../vault/types';
import { MathSol } from '../utils/math';
import {
MaxSwapParams,
type PoolBase,
SwapKind,
type SwapParams,
} from '../vault/types';
import {
_computeOutGivenExactIn,
_computeInGivenExactOut,
Expand All @@ -8,13 +14,28 @@ import {

export class Weighted implements PoolBase {
public normalizedWeights: bigint[];
// Swap limits: amounts swapped may not be larger than this percentage of the total balance.
public _MAX_IN_RATIO = 300000000000000000n;
public _MAX_OUT_RATIO = 300000000000000000n;

constructor(poolState: {
weights: bigint[];
}) {
this.normalizedWeights = poolState.weights;
}

getMaxSwapAmount(swapParams: MaxSwapParams): bigint {
if (swapParams.swapKind === SwapKind.GivenIn)
return MathSol.mulDownFixed(
swapParams.balancesLiveScaled18[swapParams.indexIn],
this._MAX_IN_RATIO,
);
return MathSol.mulDownFixed(
swapParams.balancesLiveScaled18[swapParams.indexOut],
this._MAX_OUT_RATIO,
);
}

onSwap(swapParams: SwapParams): bigint {
const {
swapKind,
Expand Down
75 changes: 75 additions & 0 deletions typescript/test/stablePool.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { describe, expect, test } from 'vitest';
import { SwapKind } from '../src/index';
import { Stable } from '../src/stable';

describe('stable pool', () => {
const pool = new Stable({
amp: 60000000000000000000n,
});
describe('getMaxSwapAmount', () => {
describe('no rate', () => {
test('exact in', () => {
const swapParams = {
swapKind: SwapKind.GivenIn,
amountGivenScaled18: 0n,
balancesLiveScaled18: [
60000000000000000000n,
40000000000000000000n,
],
tokenRates: [1000000000000000000n, 1000000000000000000n],
indexIn: 0,
indexOut: 1,
};
const maxSwapAmount = pool.getMaxSwapAmount(swapParams);
expect(maxSwapAmount).to.eq(40000000000000000000n);
});
test('exact out', () => {
const swapParams = {
swapKind: SwapKind.GivenOut,
amountGivenScaled18: 0n,
balancesLiveScaled18: [
60000000000000000000n,
40000000000000000000n,
],
tokenRates: [1000000000000000000n, 1000000000000000000n],
indexIn: 0,
indexOut: 1,
};
const maxSwapAmount = pool.getMaxSwapAmount(swapParams);
expect(maxSwapAmount).to.eq(40000000000000000000n);
});
});
describe('with rate', () => {
test('exact in', () => {
const swapParams = {
swapKind: SwapKind.GivenIn,
amountGivenScaled18: 0n,
balancesLiveScaled18: [
60000000000000000000n,
40000000000000000000n,
],
tokenRates: [2000000000000000000n, 4000000000000000000n],
indexIn: 0,
indexOut: 1,
};
const maxSwapAmount = pool.getMaxSwapAmount(swapParams);
expect(maxSwapAmount).to.eq(80000000000000000000n);
});
test('exact out', () => {
const swapParams = {
swapKind: SwapKind.GivenOut,
amountGivenScaled18: 0n,
balancesLiveScaled18: [
60000000000000000000n,
40000000000000000000n,
],
tokenRates: [2000000000000000000n, 4000000000000000000n],
indexIn: 0,
indexOut: 1,
};
const maxSwapAmount = pool.getMaxSwapAmount(swapParams);
expect(maxSwapAmount).to.eq(40000000000000000000n);
});
});
});
});
8 changes: 6 additions & 2 deletions typescript/test/utils/readTestData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ function mapPool(
...pool,
scalingFactors: pool.scalingFactors.map((sf) => BigInt(sf)),
swapFee: BigInt(pool.swapFee),
balancesLiveScaled18: pool.balancesLiveScaled18.map((b) => BigInt(b)),
balancesLiveScaled18: pool.balancesLiveScaled18.map((b) =>
BigInt(b),
),
tokenRates: pool.tokenRates.map((r) => BigInt(r)),
totalSupply: BigInt(pool.totalSupply),
weights: (
Expand All @@ -156,7 +158,9 @@ function mapPool(
...pool,
scalingFactors: pool.scalingFactors.map((sf) => BigInt(sf)),
swapFee: BigInt(pool.swapFee),
balancesLiveScaled18: pool.balancesLiveScaled18.map((b) => BigInt(b)),
balancesLiveScaled18: pool.balancesLiveScaled18.map((b) =>
BigInt(b),
),
tokenRates: pool.tokenRates.map((r) => BigInt(r)),
totalSupply: BigInt(pool.totalSupply),
amp: BigInt((pool as TransformBigintToString<StablePool>).amp),
Expand Down
41 changes: 41 additions & 0 deletions typescript/test/weightedPool.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { describe, expect, test } from 'vitest';
import { SwapKind } from '../src/index';
import { Weighted } from '../src/weighted';

describe('weighted pool', () => {
const pool = new Weighted({
weights: [60000000000000000000n, 40000000000000000000n],
});
describe('getMaxSwapAmount', () => {
test('exact in', () => {
const swapParams = {
swapKind: SwapKind.GivenIn,
amountGivenScaled18: 0n,
balancesLiveScaled18: [
60000000000000000000n,
40000000000000000000n,
],
tokenRates: [1000000000000000000n, 1000000000000000000n],
indexIn: 0,
indexOut: 1,
};
const maxSwapAmount = pool.getMaxSwapAmount(swapParams);
expect(maxSwapAmount).to.eq(18000000000000000000n);
});
test('exact out', () => {
const swapParams = {
swapKind: SwapKind.GivenOut,
amountGivenScaled18: 0n,
balancesLiveScaled18: [
60000000000000000000n,
40000000000000000000n,
],
tokenRates: [1000000000000000000n, 1000000000000000000n],
indexIn: 0,
indexOut: 1,
};
const maxSwapAmount = pool.getMaxSwapAmount(swapParams);
expect(maxSwapAmount).to.eq(12000000000000000000n);
});
});
});

0 comments on commit 56044ce

Please sign in to comment.