Skip to content

Commit

Permalink
fix: Use proper scaling for maxSwaps.
Browse files Browse the repository at this point in the history
  • Loading branch information
johngrantuk committed Jul 12, 2024
1 parent 56044ce commit 9c80ee1
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
7 changes: 4 additions & 3 deletions typescript/src/stable/stablePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ export class Stable implements PoolBase {
indexIn,
indexOut,
tokenRates,
scalingFactors
} = maxSwapParams;
if (swapKind === SwapKind.GivenIn)
return MathSol.mulDownFixed(
return MathSol.divDownFixed(MathSol.mulDownFixed(
balancesLiveScaled18[indexOut],
MathSol.divDownFixed(tokenRates[indexOut], tokenRates[indexIn]),
);
return balancesLiveScaled18[indexOut];
), scalingFactors[indexIn]);
return MathSol.divDownFixed(balancesLiveScaled18[indexOut], scalingFactors[indexOut]);
}

onSwap(swapParams: SwapParams): bigint {
Expand Down
1 change: 1 addition & 0 deletions typescript/src/vault/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type MaxSwapParams = {
swapKind: SwapKind;
balancesLiveScaled18: bigint[];
tokenRates: bigint[];
scalingFactors: bigint[];
indexIn: number;
indexOut: number;
};
Expand Down
8 changes: 4 additions & 4 deletions typescript/src/weighted/weightedPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ export class Weighted implements PoolBase {

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

onSwap(swapParams: SwapParams): bigint {
Expand Down
8 changes: 6 additions & 2 deletions typescript/test/stablePool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ describe('stable pool', () => {
40000000000000000000n,
],
tokenRates: [1000000000000000000n, 1000000000000000000n],
scalingFactors: [1000000000000000000000000000000n, 1000000000000000000n],
indexIn: 0,
indexOut: 1,
};
const maxSwapAmount = pool.getMaxSwapAmount(swapParams);
expect(maxSwapAmount).to.eq(40000000000000000000n);
expect(maxSwapAmount).to.eq(40000000n);
});
test('exact out', () => {
const swapParams = {
Expand All @@ -32,6 +33,7 @@ describe('stable pool', () => {
40000000000000000000n,
],
tokenRates: [1000000000000000000n, 1000000000000000000n],
scalingFactors: [1000000000000000000000000000000n, 1000000000000000000n],
indexIn: 0,
indexOut: 1,
};
Expand All @@ -49,6 +51,7 @@ describe('stable pool', () => {
40000000000000000000n,
],
tokenRates: [2000000000000000000n, 4000000000000000000n],
scalingFactors: [1000000000000000000n, 1000000000000000000000000000000n],
indexIn: 0,
indexOut: 1,
};
Expand All @@ -64,11 +67,12 @@ describe('stable pool', () => {
40000000000000000000n,
],
tokenRates: [2000000000000000000n, 4000000000000000000n],
scalingFactors: [1000000000000000000n, 1000000000000000000000000000000n],
indexIn: 0,
indexOut: 1,
};
const maxSwapAmount = pool.getMaxSwapAmount(swapParams);
expect(maxSwapAmount).to.eq(40000000000000000000n);
expect(maxSwapAmount).to.eq(40000000n);
});
});
});
Expand Down
4 changes: 3 additions & 1 deletion typescript/test/weightedPool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('weighted pool', () => {
40000000000000000000n,
],
tokenRates: [1000000000000000000n, 1000000000000000000n],
scalingFactors: [1000000000000000000n, 1000000000000000000000000000000n],
indexIn: 0,
indexOut: 1,
};
Expand All @@ -31,11 +32,12 @@ describe('weighted pool', () => {
40000000000000000000n,
],
tokenRates: [1000000000000000000n, 1000000000000000000n],
scalingFactors: [1000000000000000000n, 1000000000000000000000000000000n],
indexIn: 0,
indexOut: 1,
};
const maxSwapAmount = pool.getMaxSwapAmount(swapParams);
expect(maxSwapAmount).to.eq(12000000000000000000n);
expect(maxSwapAmount).to.eq(12000000n);
});
});
});

0 comments on commit 9c80ee1

Please sign in to comment.