From cad9ccb6b4d56df0f4ca10fd9edf8194dbcfe232 Mon Sep 17 00:00:00 2001 From: SunSpirit <48086732+sunspirit99@users.noreply.github.com> Date: Thu, 31 Oct 2024 15:11:41 +0700 Subject: [PATCH] shoud only update Reserve when UpdateBalance's called (#566) --- .../fluid/dex-t1/pool_simulator.go | 49 +++++++++++++++++-- pkg/liquidity-source/fluid/dex-t1/types.go | 6 +++ 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/pkg/liquidity-source/fluid/dex-t1/pool_simulator.go b/pkg/liquidity-source/fluid/dex-t1/pool_simulator.go index 7c35da224..a63dab8ca 100644 --- a/pkg/liquidity-source/fluid/dex-t1/pool_simulator.go +++ b/pkg/liquidity-source/fluid/dex-t1/pool_simulator.go @@ -91,7 +91,21 @@ func (s *PoolSimulator) CalcAmountOut(param poolpkg.CalcAmountOutParams) (*poolp amountInAfterFee := new(big.Int).Sub(param.TokenAmountIn.Amount, fee) - _, tokenAmountOut, err := swapIn(swap0To1, amountInAfterFee, s.CollateralReserves, s.DebtReserves, + collateralReserves := CollateralReserves{ + Token0RealReserves: new(big.Int).Set(s.CollateralReserves.Token0RealReserves), + Token1RealReserves: new(big.Int).Set(s.CollateralReserves.Token1RealReserves), + Token0ImaginaryReserves: new(big.Int).Set(s.CollateralReserves.Token0ImaginaryReserves), + Token1ImaginaryReserves: new(big.Int).Set(s.CollateralReserves.Token1ImaginaryReserves), + } + + debtReserves := DebtReserves{ + Token0RealReserves: new(big.Int).Set(s.DebtReserves.Token0RealReserves), + Token1RealReserves: new(big.Int).Set(s.DebtReserves.Token1RealReserves), + Token0ImaginaryReserves: new(big.Int).Set(s.DebtReserves.Token0ImaginaryReserves), + Token1ImaginaryReserves: new(big.Int).Set(s.DebtReserves.Token1ImaginaryReserves), + } + + _, tokenAmountOut, err := swapIn(swap0To1, amountInAfterFee, collateralReserves, debtReserves, int64(tokenInDecimals), int64(tokenOutDecimals)) if err != nil { return nil, err @@ -101,7 +115,11 @@ func (s *PoolSimulator) CalcAmountOut(param poolpkg.CalcAmountOutParams) (*poolp TokenAmountOut: &poolpkg.TokenAmount{Token: param.TokenOut, Amount: tokenAmountOut}, Fee: &poolpkg.TokenAmount{Token: param.TokenAmountIn.Token, Amount: fee}, Gas: defaultGas.Swap, - SwapInfo: s.StaticExtra, + SwapInfo: SwapInfo{ + HasNative: s.HasNative, + NewCollateralReserves: collateralReserves, + NewDebtReserves: debtReserves, + }, }, nil } @@ -121,7 +139,21 @@ func (s *PoolSimulator) CalcAmountIn(param poolpkg.CalcAmountInParams) (*poolpkg tokenInDecimals = s.Token1Decimals } - tokenAmountIn, _, err := swapOut(swap0To1, param.TokenAmountOut.Amount, s.CollateralReserves, s.DebtReserves, + collateralReserves := CollateralReserves{ + Token0RealReserves: new(big.Int).Set(s.CollateralReserves.Token0RealReserves), + Token1RealReserves: new(big.Int).Set(s.CollateralReserves.Token1RealReserves), + Token0ImaginaryReserves: new(big.Int).Set(s.CollateralReserves.Token0ImaginaryReserves), + Token1ImaginaryReserves: new(big.Int).Set(s.CollateralReserves.Token1ImaginaryReserves), + } + + debtReserves := DebtReserves{ + Token0RealReserves: new(big.Int).Set(s.DebtReserves.Token0RealReserves), + Token1RealReserves: new(big.Int).Set(s.DebtReserves.Token1RealReserves), + Token0ImaginaryReserves: new(big.Int).Set(s.DebtReserves.Token0ImaginaryReserves), + Token1ImaginaryReserves: new(big.Int).Set(s.DebtReserves.Token1ImaginaryReserves), + } + + tokenAmountIn, _, err := swapOut(swap0To1, param.TokenAmountOut.Amount, collateralReserves, debtReserves, int64(tokenInDecimals), int64(tokenOutDecimals)) if err != nil { return nil, err @@ -137,7 +169,11 @@ func (s *PoolSimulator) CalcAmountIn(param poolpkg.CalcAmountInParams) (*poolpkg TokenAmountIn: &poolpkg.TokenAmount{Token: param.TokenIn, Amount: amountInAfterFee}, Fee: &poolpkg.TokenAmount{Token: param.TokenIn, Amount: fee}, Gas: defaultGas.Swap, - SwapInfo: s.StaticExtra, + SwapInfo: SwapInfo{ + HasNative: s.HasNative, + NewCollateralReserves: collateralReserves, + NewDebtReserves: debtReserves, + }, }, nil } @@ -154,6 +190,11 @@ func (t *PoolSimulator) UpdateBalance(params poolpkg.UpdateBalanceParams) { t.Info.Reserves[i] = new(big.Int).Sub(t.Info.Reserves[i], outputAmount) } } + + if swapInfo, ok := params.SwapInfo.(PoolExtra); ok { + t.CollateralReserves = swapInfo.CollateralReserves + t.DebtReserves = swapInfo.DebtReserves + } } func (s *PoolSimulator) GetMetaInfo(_ string, _ string) interface{} { diff --git a/pkg/liquidity-source/fluid/dex-t1/types.go b/pkg/liquidity-source/fluid/dex-t1/types.go index d50f55b08..eb6f8925f 100644 --- a/pkg/liquidity-source/fluid/dex-t1/types.go +++ b/pkg/liquidity-source/fluid/dex-t1/types.go @@ -48,3 +48,9 @@ type StaticExtra struct { DexReservesResolver string `json:"dexReservesResolver"` HasNative bool `json:"hasNative"` } + +type SwapInfo struct { + HasNative bool `json:"hasNative"` + NewCollateralReserves CollateralReserves `json:"-"` + NewDebtReserves DebtReserves `json:"-"` +}