Skip to content

Commit

Permalink
Merge pull request #117 from galacticcouncil/fix/stableswap-spot
Browse files Browse the repository at this point in the history
Update stableswap spot price calc (math)
  • Loading branch information
nohaapav authored Feb 5, 2025
2 parents c72b8bb + a21b95a commit cc487d3
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/good-queens-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@galacticcouncil/sdk': minor
---

Update stableswap spot price calculation (math)
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@galacticcouncil/math-lbp": "^1.0.0",
"@galacticcouncil/math-liquidity-mining": "^1.0.0",
"@galacticcouncil/math-omnipool": "^1.0.0",
"@galacticcouncil/math-stableswap": "^1.0.0",
"@galacticcouncil/math-stableswap": "^1.1.0",
"@galacticcouncil/math-xyk": "^1.0.0",
"@thi.ng/cache": "^2.1.35",
"@thi.ng/memoize": "^4.0.2",
Expand Down
96 changes: 87 additions & 9 deletions packages/sdk/src/pool/stable/StableMath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
calculate_pool_trade_fee,
calculate_shares,
calculate_shares_for_amount,
calculate_spot_price_with_fee,
pool_account_name,
} from '@galacticcouncil/math-stableswap';

Expand All @@ -22,7 +23,13 @@ export class StableMath {
finalBlock: string,
currentBlock: string
) {
return calculate_amplification(initialAmp, finalAmp, initialBlock, finalBlock, currentBlock);
return calculate_amplification(
initialAmp,
finalAmp,
initialBlock,
finalBlock,
currentBlock
);
}

static calculateInGivenOut(
Expand All @@ -33,7 +40,14 @@ export class StableMath {
amplification: string,
fee: string
): string {
return calculate_in_given_out(reserves, assetIn, assetOut, amountOut, amplification, fee);
return calculate_in_given_out(
reserves,
assetIn,
assetOut,
amountOut,
amplification,
fee
);
}

static calculateAddOneAsset(
Expand All @@ -44,7 +58,14 @@ export class StableMath {
shareIssuance: string,
fee: string
) {
return calculate_add_one_asset(reserves, shares, assetIn, amplification, shareIssuance, fee);
return calculate_add_one_asset(
reserves,
shares,
assetIn,
amplification,
shareIssuance,
fee
);
}

static calculateSharesForAmount(
Expand All @@ -55,7 +76,14 @@ export class StableMath {
shareIssuance: string,
fee: string
) {
return calculate_shares_for_amount(reserves, assetIn, amount, amplification, shareIssuance, fee);
return calculate_shares_for_amount(
reserves,
assetIn,
amount,
amplification,
shareIssuance,
fee
);
}

static calculateOutGivenIn(
Expand All @@ -66,7 +94,14 @@ export class StableMath {
amplification: string,
fee: string
): string {
return calculate_out_given_in(reserves, assetIn, assetOut, amountIn, amplification, fee);
return calculate_out_given_in(
reserves,
assetIn,
assetOut,
amountIn,
amplification,
fee
);
}

static calculateLiquidityOutOneAsset(
Expand All @@ -77,14 +112,57 @@ export class StableMath {
shareIssuance: string,
withdrawFee: string
) {
return calculate_liquidity_out_one_asset(reserves, shares, assetOut, amplification, shareIssuance, withdrawFee);
return calculate_liquidity_out_one_asset(
reserves,
shares,
assetOut,
amplification,
shareIssuance,
withdrawFee
);
}

static calculateShares(reserves: string, assets: string, amplification: string, shareIssuance: string, fee: string) {
return calculate_shares(reserves, assets, amplification, shareIssuance, fee);
static calculateShares(
reserves: string,
assets: string,
amplification: string,
shareIssuance: string,
fee: string
) {
return calculate_shares(
reserves,
assets,
amplification,
shareIssuance,
fee
);
}

static calculateSpotPriceWithFee(
poolId: string,
reserves: string,
amplification: string,
assetIn: string,
assetOut: string,
shareIssuance: string,
fee: string
): string {
return calculate_spot_price_with_fee(
poolId,
reserves,
amplification,
assetIn,
assetOut,
shareIssuance,
fee
);
}

static calculatePoolTradeFee(amount: string, feeNumerator: number, feeDenominator: number): string {
static calculatePoolTradeFee(
amount: string,
feeNumerator: number,
feeDenominator: number
): string {
return calculate_pool_trade_fee(amount, feeNumerator, feeDenominator);
}
}
36 changes: 28 additions & 8 deletions packages/sdk/src/pool/stable/StableSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,17 +250,27 @@ export class StableSwap implements Pool {
}

spotPriceInGivenOut(poolPair: PoolPair): BigNumber {
const one = scale(ONE, poolPair.decimalsOut);
const spot = StableMath.calculateSpotPriceWithFee(
this.id,
this.getReserves(),
this.amplification,
poolPair.assetIn,
poolPair.assetOut,
this.totalIssuance,
'0'
);

if (poolPair.assetOut == this.id) {
return this.calculateAddOneAsset(poolPair, one);
return bnum(spot);
}

if (poolPair.assetIn == this.id) {
return this.calculateSharesForAmount(poolPair, one);
const base = scale(ONE, poolPair.decimalsIn - poolPair.decimalsOut);
return bnum(spot).div(base);
}

return this.calculateIn(poolPair, one);
const base = scale(ONE, 18 - poolPair.decimalsIn);
return bnum(spot).div(base);
}

private calculateOut(
Expand Down Expand Up @@ -330,17 +340,27 @@ export class StableSwap implements Pool {
}

spotPriceOutGivenIn(poolPair: PoolPair): BigNumber {
const one = scale(ONE, poolPair.decimalsIn);
const spot = StableMath.calculateSpotPriceWithFee(
this.id,
this.getReserves(),
this.amplification,
poolPair.assetOut,
poolPair.assetIn,
this.totalIssuance,
'0'
);

if (poolPair.assetIn == this.id) {
return this.calculateWithdrawOneAsset(poolPair, one);
return bnum(spot);
}

if (poolPair.assetOut == this.id) {
return this.calculateShares(poolPair, one);
const base = scale(ONE, poolPair.decimalsOut - poolPair.decimalsIn);
return bnum(spot).div(base);
}

return this.calculateOut(poolPair, one);
const base = scale(ONE, 18 - poolPair.decimalsOut);
return bnum(spot).div(base);
}

calculateTradeFee(amount: BigNumber, fees: StableSwapFees): BigNumber {
Expand Down

0 comments on commit cc487d3

Please sign in to comment.