Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sharp-paws-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'backend': patch
---

SOR - Rename increase/decrease as add/sub
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ export class ComposableStablePool implements BasePool {
if (amountOutWithRate.amount < 0n) throw new Error('Swap output negative');

if (mutateBalances) {
tIn.increase(swapAmount.amount);
tOut.decrease(amountOutWithRate.amount);
tIn.add(swapAmount);
tOut.sub(amountOutWithRate);

if (tIn.index === this.bptIndex) {
this.totalShares = this.totalShares - swapAmount.amount;
Expand Down Expand Up @@ -252,8 +252,8 @@ export class ComposableStablePool implements BasePool {
if (amountIn.amount < 0n) throw new Error('Swap output negative');

if (mutateBalances) {
tIn.increase(amountIn.amount);
tIn.decrease(swapAmount.amount);
tIn.add(amountIn);
tIn.sub(swapAmount);

if (tIn.index === this.bptIndex) {
this.totalShares = this.totalShares - amountIn.amount;
Expand Down
8 changes: 4 additions & 4 deletions modules/sor/lib/poolsV2/fx/fxPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ export class FxPool implements BasePool {
const amountOut = TokenAmount.fromRawAmount(fxAmountOut.token, fxAmountOut.amount);

if (mutateBalances) {
poolPairData.tIn.increase(swapAmount.amount);
poolPairData.tOut.decrease(amountOut.amount);
poolPairData.tIn.add(swapAmount);
poolPairData.tOut.sub(amountOut);
}

return amountOut;
Expand All @@ -172,8 +172,8 @@ export class FxPool implements BasePool {
const amountIn = TokenAmount.fromRawAmount(fxAmountIn.token, fxAmountIn.amount);

if (mutateBalances) {
poolPairData.tIn.decrease(amountIn.amount);
poolPairData.tOut.increase(swapAmount.amount);
poolPairData.tIn.sub(amountIn);
poolPairData.tOut.add(swapAmount);
}

return amountIn;
Expand Down
8 changes: 4 additions & 4 deletions modules/sor/lib/poolsV2/fx/fxPoolToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export class FxPoolToken extends BasePoolToken {
this.numeraire = truncatedNumeraire * this.scalar36;
}

public increase(amount: bigint): TokenAmount {
this.amount = this.amount + amount;
public add(amount: TokenAmount): TokenAmount {
this.amount = this.amount + amount.amount;
this.scale18 = this.amount * this.scalar;
const truncatedNumeraire = MathFx.mulDownFixed(
this.amount,
Expand All @@ -40,8 +40,8 @@ export class FxPoolToken extends BasePoolToken {
return this;
}

public decrease(amount: bigint): TokenAmount {
this.amount = this.amount - amount;
public sub(amount: TokenAmount): TokenAmount {
this.amount = this.amount - amount.amount;
this.scale18 = this.amount * this.scalar;
const truncatedNumeraire = MathFx.mulDownFixed(
this.amount,
Expand Down
8 changes: 4 additions & 4 deletions modules/sor/lib/poolsV2/gyro2/gyro2Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ export class Gyro2Pool implements BasePool {
const outAmount = TokenAmount.fromScale18Amount(tokenOut, outAmountScale18).divDownFixed(tOut.rate);

if (mutateBalances) {
tIn.increase(swapAmount.amount);
tOut.decrease(outAmount.amount);
tIn.add(swapAmount);
tOut.sub(outAmount);
}

return outAmount;
Expand Down Expand Up @@ -190,8 +190,8 @@ export class Gyro2Pool implements BasePool {
const inAmount = this.addSwapFeeAmount(TokenAmount.fromScale18Amount(tokenIn, inAmountLessSwapFeeRateUndone));

if (mutateBalances) {
tIn.decrease(inAmount.amount);
tOut.increase(swapAmount.amount);
tIn.sub(inAmount);
tOut.add(swapAmount);
}

return inAmount;
Expand Down
8 changes: 4 additions & 4 deletions modules/sor/lib/poolsV2/gyro3/gyro3Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ export class Gyro3Pool implements BasePool {
const outAmount = TokenAmount.fromScale18Amount(tokenOut, outAmountScale18);

if (mutateBalances) {
tIn.increase(swapAmount.amount);
tOut.decrease(outAmount.amount);
tIn.add(swapAmount);
tOut.sub(outAmount);
}

return outAmount;
Expand All @@ -141,8 +141,8 @@ export class Gyro3Pool implements BasePool {
const inAmount = this.addSwapFeeAmount(TokenAmount.fromScale18Amount(tokenIn, inAmountLessFee, true));

if (mutateBalances) {
tIn.decrease(inAmount.amount);
tOut.increase(swapAmount.amount);
tIn.sub(inAmount);
tOut.add(swapAmount);
}

return inAmount;
Expand Down
8 changes: 4 additions & 4 deletions modules/sor/lib/poolsV2/gyroE/gyroEPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ export class GyroEPool implements BasePool {
);

if (mutateBalances) {
tIn.increase(inAmountLive.amount);
tOut.decrease(outAmount.amount);
tIn.add(inAmountLive);
tOut.sub(outAmount);
}

return outAmount;
Expand Down Expand Up @@ -208,8 +208,8 @@ export class GyroEPool implements BasePool {
);

if (mutateBalances) {
tIn.decrease(inAmount.amount);
tOut.increase(outAmountLive.amount);
tIn.sub(inAmount);
tOut.add(outAmountLive);
}

return inAmount;
Expand Down
8 changes: 4 additions & 4 deletions modules/sor/lib/poolsV2/metastable/metastablePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ export class MetaStablePool implements BasePool {
if (amountOutWithRate.amount < 0n) throw new Error('Swap output negative');

if (mutateBalances) {
tIn.increase(swapAmount.amount);
tOut.decrease(amountOutWithRate.amount);
tIn.add(swapAmount);
tOut.sub(amountOutWithRate);
}

return amountOutWithRate;
Expand Down Expand Up @@ -166,8 +166,8 @@ export class MetaStablePool implements BasePool {
if (amountInWithRate.amount < 0n) throw new Error('Swap output negative');

if (mutateBalances) {
tIn.increase(amountInWithRate.amount);
tOut.decrease(swapAmount.amount);
tIn.add(amountInWithRate);
tOut.sub(swapAmount);
}

return amountInWithRate;
Expand Down
8 changes: 4 additions & 4 deletions modules/sor/lib/poolsV2/stable/stablePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ export class StablePool implements BasePool {
if (amountOut.amount < 0n) throw new Error('Swap output negative');

if (mutateBalances) {
tIn.increase(swapAmount.amount);
tOut.decrease(amountOut.amount);
tIn.add(swapAmount);
tOut.sub(amountOut);
}

return amountOut;
Expand Down Expand Up @@ -156,8 +156,8 @@ export class StablePool implements BasePool {
if (amountInWithFee.amount < 0n) throw new Error('Swap output negative');

if (mutateBalances) {
tIn.increase(amountInWithFee.amount);
tOut.decrease(swapAmount.amount);
tIn.add(amountInWithFee);
tOut.sub(swapAmount);
}

return amountInWithFee;
Expand Down
30 changes: 16 additions & 14 deletions modules/sor/lib/poolsV3/basePoolV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class BasePoolV3 {
): TokenAmount {
const { tIn, tOut } = this.getPoolTokens(tokenIn, tokenOut);

let calculatedAmount: bigint;
let amountOut: TokenAmount;

if (tIn.token.isSameAddress(this.id)) {
// if liquidityManagement.disableUnbalancedLiquidity is true return 0
Expand All @@ -118,7 +118,7 @@ export class BasePoolV3 {
this.poolState,
this.hookState,
);
calculatedAmount = amountsOutRaw[tOut.index];
amountOut = TokenAmount.fromRawAmount(tOut.token, amountsOutRaw[tOut.index]);
} else if (tOut.token.isSameAddress(this.id)) {
// if liquidityManagement.disableUnbalancedLiquidity is true return 0
// as the pool does not allow unbalanced operations. 0 return marks the
Expand All @@ -138,10 +138,10 @@ export class BasePoolV3 {
this.poolState,
this.hookState,
);
calculatedAmount = bptAmountOutRaw;
amountOut = TokenAmount.fromRawAmount(tOut.token, bptAmountOutRaw);
} else {
// swap
calculatedAmount = this.vault.swap(
const calculatedAmount = this.vault.swap(
{
amountRaw: swapAmount.amount,
tokenIn: tIn.token.address,
Expand All @@ -151,15 +151,16 @@ export class BasePoolV3 {
this.poolState,
this.hookState,
);
amountOut = TokenAmount.fromRawAmount(tOut.token, calculatedAmount);
}

if (mutateBalances) {
tIn.increase(swapAmount.amount);
tOut.decrease(calculatedAmount);
tIn.add(swapAmount);
tOut.sub(amountOut);
this.poolState.balancesLiveScaled18 = this.tokens.map((t) => t.scale18);
}

return TokenAmount.fromRawAmount(tOut.token, calculatedAmount);
return amountOut;
}

public swapGivenOut(
Expand All @@ -170,7 +171,7 @@ export class BasePoolV3 {
): TokenAmount {
const { tIn, tOut } = this.getPoolTokens(tokenIn, tokenOut);

let calculatedAmount: bigint;
let amountIn: TokenAmount;

if (tIn.token.isSameAddress(this.id)) {
// if liquidityManagement.disableUnbalancedLiquidity is true return 0
Expand All @@ -191,7 +192,7 @@ export class BasePoolV3 {
this.poolState,
this.hookState,
);
calculatedAmount = bptAmountInRaw;
amountIn = TokenAmount.fromRawAmount(tIn.token, bptAmountInRaw);
} else if (tOut.token.isSameAddress(this.id)) {
// if liquidityManagement.disableUnbalancedLiquidity is true return 0
// as the pool does not allow unbalanced operations. 0 return marks the
Expand All @@ -211,10 +212,10 @@ export class BasePoolV3 {
this.poolState,
this.hookState,
);
calculatedAmount = amountsInRaw[tIn.index];
amountIn = TokenAmount.fromRawAmount(tIn.token, amountsInRaw[tIn.index]);
} else {
// swap
calculatedAmount = this.vault.swap(
const calculatedAmount = this.vault.swap(
{
amountRaw: swapAmount.amount,
tokenIn: tIn.token.address,
Expand All @@ -224,15 +225,16 @@ export class BasePoolV3 {
this.poolState,
this.hookState,
);
amountIn = TokenAmount.fromRawAmount(tIn.token, calculatedAmount);
}

if (mutateBalances) {
tIn.increase(calculatedAmount);
tOut.decrease(swapAmount.amount);
tIn.add(amountIn);
tOut.sub(swapAmount);
this.poolState.balancesLiveScaled18 = this.tokens.map((t) => t.scale18);
}

return TokenAmount.fromRawAmount(tIn.token, calculatedAmount);
return amountIn;
}

public getNormalizedLiquidity(tokenIn: Token, tokenOut: Token): bigint {
Expand Down
8 changes: 4 additions & 4 deletions modules/sor/lib/utils/basePoolToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ export class BasePoolToken extends TokenAmount {
this.index = index;
}

public increase(amount: bigint): TokenAmount {
this.amount = this.amount + amount;
public add(amount: TokenAmount): TokenAmount {
this.amount = this.amount + amount.amount;
this.scale18 = this.amount * this.scalar;
return this;
}

public decrease(amount: bigint): TokenAmount {
this.amount = this.amount - amount;
public sub(amount: TokenAmount): TokenAmount {
this.amount = this.amount - amount.amount;
this.scale18 = this.amount * this.scalar;
return this;
}
Expand Down
8 changes: 4 additions & 4 deletions modules/sor/lib/utils/poolTokenWithRate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ export class PoolTokenWithRate extends BasePoolToken {
this.scale18 = (this.amount * this.scalar * this.rate) / WAD;
}

public increase(amount: bigint): TokenAmount {
this.amount = this.amount + amount;
public add(amount: TokenAmount): TokenAmount {
this.amount = this.amount + amount.amount;
this.scale18 = (this.amount * this.scalar * this.rate) / WAD;
return this;
}

public decrease(amount: bigint): TokenAmount {
this.amount = this.amount - amount;
public sub(amount: TokenAmount): TokenAmount {
this.amount = this.amount - amount.amount;
this.scale18 = (this.amount * this.scalar * this.rate) / WAD;
return this;
}
Expand Down