Skip to content

Commit

Permalink
shoud only update Reserve when UpdateBalance's called (#566)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunspirit99 authored Oct 31, 2024
1 parent d639fde commit cad9ccb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
49 changes: 45 additions & 4 deletions pkg/liquidity-source/fluid/dex-t1/pool_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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
Expand All @@ -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
}

Expand All @@ -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{} {
Expand Down
6 changes: 6 additions & 0 deletions pkg/liquidity-source/fluid/dex-t1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"-"`
}

0 comments on commit cad9ccb

Please sign in to comment.