From eb4634d06c5929827583f1c188bea8ca22cd900d Mon Sep 17 00:00:00 2001 From: Duc Liem Nguyen Date: Tue, 20 Feb 2024 14:52:20 +0700 Subject: [PATCH] return remaining amount (#18) --- entities/pool.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/entities/pool.go b/entities/pool.go index 8473375..b560f8b 100644 --- a/entities/pool.go +++ b/entities/pool.go @@ -46,12 +46,14 @@ type SwapResult struct { amountCalculated *big.Int sqrtRatioX96 *big.Int liquidity *big.Int + remainingAmountIn *big.Int currentTick int crossInitTickLoops int } type GetAmountResult struct { ReturnedAmount *entities.CurrencyAmount + RemainingAmountIn *entities.CurrencyAmount NewPoolState *Pool CrossInitTickLoops int } @@ -191,6 +193,7 @@ func (p *Pool) GetOutputAmount(inputAmount *entities.CurrencyAmount, sqrtPriceLi } return &GetAmountResult{ ReturnedAmount: entities.FromRawAmount(outputToken, new(big.Int).Mul(swapResult.amountCalculated, constants.NegativeOne)), + RemainingAmountIn: entities.FromRawAmount(inputAmount.Currency, swapResult.remainingAmountIn), NewPoolState: pool, CrossInitTickLoops: swapResult.crossInitTickLoops, }, nil @@ -375,11 +378,11 @@ func (p *Pool) swap(zeroForOne bool, amountSpecified, sqrtPriceLimitX96 *big.Int } } return &SwapResult{ - amountCalculated: state.amountCalculated, - sqrtRatioX96: state.sqrtPriceX96, - liquidity: state.liquidity, - currentTick: state.tick, - + amountCalculated: state.amountCalculated, + sqrtRatioX96: state.sqrtPriceX96, + liquidity: state.liquidity, + currentTick: state.tick, + remainingAmountIn: state.amountSpecifiedRemaining, crossInitTickLoops: crossInitTickLoops, }, nil }