diff --git a/.changeset/good-queens-trade.md b/.changeset/good-queens-trade.md new file mode 100644 index 00000000..ad53fe03 --- /dev/null +++ b/.changeset/good-queens-trade.md @@ -0,0 +1,5 @@ +--- +'@galacticcouncil/sdk': minor +--- + +Update stableswap spot price calculation (math) diff --git a/packages/sdk/package.json b/packages/sdk/package.json index e862a2f4..f006957c 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -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", diff --git a/packages/sdk/src/pool/stable/StableMath.ts b/packages/sdk/src/pool/stable/StableMath.ts index 7fa604a4..9f761615 100644 --- a/packages/sdk/src/pool/stable/StableMath.ts +++ b/packages/sdk/src/pool/stable/StableMath.ts @@ -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'; @@ -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( @@ -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( @@ -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( @@ -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( @@ -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( @@ -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); } } diff --git a/packages/sdk/src/pool/stable/StableSwap.ts b/packages/sdk/src/pool/stable/StableSwap.ts index c323213f..b35260ae 100644 --- a/packages/sdk/src/pool/stable/StableSwap.ts +++ b/packages/sdk/src/pool/stable/StableSwap.ts @@ -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( @@ -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 {