Skip to content

Commit

Permalink
Fix bug return amount is not enough (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChauNguyen-K authored Nov 15, 2023
1 parent f138b85 commit d4929ef
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
10 changes: 6 additions & 4 deletions pkg/source/smardex/pool_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func (p *PoolSimulator) CalcAmountOut(tokenAmountIn poolpkg.TokenAmount, tokenOu
amount0, amount1 := tokenAmountIn.Amount, result.amountOut
feeToAmount0, feeToAmount1 := p.FeeToAmount.Fees0, p.FeeToAmount.Fees1
newPriceAverageIn, newPriceAverageOut := priceAverageIn, priceAverageOut
newFictiveReserveIn, newFictiveReserveOut := result.newFictiveReserveIn, result.newFictiveReserveOut
if zeroForOne {
feeToAmount0 = feeToAmount0.Add(
feeToAmount0,
Expand All @@ -122,6 +123,7 @@ func (p *PoolSimulator) CalcAmountOut(tokenAmountIn poolpkg.TokenAmount, tokenOu
feeToAmount1,
new(big.Int).Div(new(big.Int).Mul(amount1, p.PairFee.FeesPool), p.PairFee.FeesBase))
newPriceAverageIn, newPriceAverageOut = priceAverageOut, priceAverageIn
newFictiveReserveIn, newFictiveReserveOut = result.newFictiveReserveOut, result.newFictiveReserveIn
}

if zeroForOne {
Expand All @@ -138,8 +140,8 @@ func (p *PoolSimulator) CalcAmountOut(tokenAmountIn poolpkg.TokenAmount, tokenOu
SwapInfo: SwapInfo{
NewReserveIn: new(big.Int).Sub(result.newReserveIn, feeToAmount0),
NewReserveOut: new(big.Int).Sub(result.newReserveOut, feeToAmount1),
NewFictiveReserveIn: result.newFictiveReserveIn,
NewFictiveReserveOut: result.newFictiveReserveOut,
NewFictiveReserveIn: newFictiveReserveIn,
NewFictiveReserveOut: newFictiveReserveOut,
NewPriceAverageIn: newPriceAverageIn,
NewPriceAverageOut: newPriceAverageOut,
PriceAverageLastTimestamp: big.NewInt(userTradeTimestamp),
Expand All @@ -162,8 +164,8 @@ func (p *PoolSimulator) CalcAmountOut(tokenAmountIn poolpkg.TokenAmount, tokenOu
SwapInfo: SwapInfo{
NewReserveIn: new(big.Int).Sub(result.newReserveIn, feeToAmount0),
NewReserveOut: new(big.Int).Sub(result.newReserveOut, feeToAmount1),
NewFictiveReserveIn: result.newFictiveReserveIn,
NewFictiveReserveOut: result.newFictiveReserveOut,
NewFictiveReserveIn: newFictiveReserveIn,
NewFictiveReserveOut: newFictiveReserveOut,
NewPriceAverageIn: newPriceAverageIn,
NewPriceAverageOut: newPriceAverageOut,
PriceAverageLastTimestamp: big.NewInt(userTradeTimestamp),
Expand Down
19 changes: 19 additions & 0 deletions pkg/source/smardex/pool_simulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,25 @@ func TestGetAmountOut(t *testing.T) {
expectedResFictive0: parseString("85593526029"),
expectedResFictive1: parseString("7289358689105108450240064"),
},
{
name: "Test case 3",
amountParams: GetAmountParameters{
amount: big.NewInt(2000000000000000000),
reserveIn: parseString("3278796445628485066"),
reserveOut: parseString("6213633437"),
fictiveReserveIn: parseString("1602466039436492633"),
fictiveReserveOut: parseString("3179127537"),
priceAverageIn: parseString("1602466039436492633"),
priceAverageOut: parseString("3179127537"),
feesLP: FEES_LP_DEFAULT_ETHEREUM,
feesPool: FEES_POOL_DEFAULT_ETHEREUM,
feesBase: FEES_BASE_ETHEREUM},
expectedAmountOutT0: parseString("1719846589"),
expectedReserve0: parseString("5278396445628485066"),
expectedReserve1: parseString("4493786848"),
expectedResFictive0: parseString("3530568934927624489"),
expectedResFictive1: parseString("1317438058"),
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit d4929ef

Please sign in to comment.