From 90de6a5d310bce447c32b63e52fb0a204249a526 Mon Sep 17 00:00:00 2001 From: it4rb Date: Thu, 12 Dec 2024 09:22:29 +0700 Subject: [PATCH] fix LO swapInfo.AmountIn in case take fee in takerAsset (#644) * fix LO swapInfo.AmountIn in case take fee in takerAsset * naming --- pkg/source/limitorder/pool_simulator.go | 4 ++++ pkg/source/limitorder/pool_simulator_test.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/pkg/source/limitorder/pool_simulator.go b/pkg/source/limitorder/pool_simulator.go index c82162adb..15a5df178 100644 --- a/pkg/source/limitorder/pool_simulator.go +++ b/pkg/source/limitorder/pool_simulator.go @@ -215,6 +215,7 @@ func (p *PoolSimulator) calcAmountWithSwapInfo(swapSide SwapSide, tokenAmountIn SwapSide: swapSide, AmountIn: tokenAmountIn.Amount.String(), } + totalFilledTakingAmountWei := big.NewInt(0) isFulfillAmountIn := false totalFeeAmountWei := new(big.Int) @@ -276,6 +277,7 @@ func (p *PoolSimulator) calcAmountWithSwapInfo(swapSide SwapSide, tokenAmountIn swapInfo.FilledOrders = append(swapInfo.FilledOrders, filledOrderInfo) isFulfillAmountIn = true addFilledMakingAmount(filledMakingAmountByMaker, order.Maker, filledMakingAmountWei) + totalFilledTakingAmountWei.Add(totalFilledTakingAmountWei, filledTakingAmountWei) // Currently, when Aggregator finds route and returns some orders and sends them to the smart contract to execute. // We will often meet edge cases that these orders can be fulfilled by a trading bot or taker on Aggregator. @@ -318,10 +320,12 @@ func (p *PoolSimulator) calcAmountWithSwapInfo(swapSide SwapSide, tokenAmountIn filledOrderInfo := newFilledOrderInfo(order, remainingTakingAmountWei.String(), remainingMakingAmountWei.String(), feeAmountWeiByOrder.String()) swapInfo.FilledOrders = append(swapInfo.FilledOrders, filledOrderInfo) addFilledMakingAmount(filledMakingAmountByMaker, order.Maker, remainingMakingAmountWei) + totalFilledTakingAmountWei.Add(totalFilledTakingAmountWei, remainingTakingAmountWei) } if !isFulfillAmountIn { return nil, SwapInfo{}, nil, ErrCannotFulfillAmountIn } + swapInfo.AmountIn = totalFilledTakingAmountWei.String() return totalAmountOutWei, swapInfo, totalFeeAmountWei, nil } diff --git a/pkg/source/limitorder/pool_simulator_test.go b/pkg/source/limitorder/pool_simulator_test.go index a99b88a60..976ba6a24 100644 --- a/pkg/source/limitorder/pool_simulator_test.go +++ b/pkg/source/limitorder/pool_simulator_test.go @@ -971,6 +971,7 @@ func TestPool_CalcAmountOut_v2(t *testing.T) { assert.Equal(t, tc.expAmountOut, res.TokenAmountOut.Amount.String()) si := res.SwapInfo.(SwapInfo) + assert.Equal(t, bignumber.NewBig10(tc.amountIn), bignumber.NewBig10(si.AmountIn)) oid := make([]int64, 0, len(si.FilledOrders)) oinfo := "" for _, o := range si.FilledOrders { @@ -1079,6 +1080,7 @@ func TestPool_UpdateBalance(t *testing.T) { assert.Equal(t, swap.expAmountOut, res.TokenAmountOut.Amount.String()) si := res.SwapInfo.(SwapInfo) + assert.Equal(t, bignumber.NewBig10(swap.amountIn), bignumber.NewBig10(si.AmountIn)) oid := make([]int64, 0, len(si.FilledOrders)) oinfo := "" for _, o := range si.FilledOrders { @@ -1229,6 +1231,7 @@ func TestPool_Inventory(t *testing.T) { assert.Equal(t, swap.expAmountOut, res.TokenAmountOut.Amount.String()) si := res.SwapInfo.(SwapInfo) + assert.Equal(t, bignumber.NewBig10(swap.amountIn), bignumber.NewBig10(si.AmountIn)) oid := make([]int64, 0, len(si.FilledOrders)) oinfo := "" for _, o := range si.FilledOrders { @@ -1363,6 +1366,7 @@ func TestPool_CalcAmountOut_TakerAssetFee(t *testing.T) { } assert.Equal(t, tc.expOrderIds, oid, oinfo) fmt.Println(oinfo) + fmt.Println("--", si.AmountIn) }) } }