Skip to content

Commit

Permalink
Add test case same as CalcAmountOut
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisngyn committed Dec 17, 2024
1 parent 77b58f5 commit bb5421e
Show file tree
Hide file tree
Showing 2 changed files with 874 additions and 4 deletions.
11 changes: 7 additions & 4 deletions pkg/source/limitorder/pool_simulator_calc_amount_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,19 @@ func (p *PoolSimulator) calcAmountInWithSwapInfo(swapSide SwapSide, tokenAmountO
rate := new(big.Float).Quo(new(big.Float).SetInt(order.MakingAmount), new(big.Float).SetInt(order.TakingAmount))
amountInWei := new(big.Float).Quo(new(big.Float).SetInt(totalAmountOutBeforeFee), rate)
filledMakingAmountWei := totalAmountOutBeforeFee
filledTakingAmountWei, _ := amountInWei.Int(nil)
filledTakingAmountWei, acc := amountInWei.Int(nil)
if acc == big.Below {
filledTakingAmountWei.Add(filledTakingAmountWei, big.NewInt(1))
}

// order too small
if filledTakingAmountWei.Cmp(constant.ZeroBI) == 0 {
continue
}

actualAmountIn, feeAmountWeiByOrder := p.calcTakerAssetFeeAmountExactOut(order, filledTakingAmountWei)
totalFeeAmountWei.Add(totalFeeAmountWei, feeAmountWeiByOrder)
totalAmountInWei.Add(totalAmountInWei, actualAmountIn)
totalFeeAmountWei = new(big.Int).Add(totalFeeAmountWei, feeAmountWeiByOrder)
totalAmountInWei = new(big.Int).Add(totalAmountInWei, actualAmountIn)
filledOrderInfo := newFilledOrderInfo(order, filledTakingAmountWei.String(), filledMakingAmountWei.String(), feeAmountWeiByOrder.String())
swapInfo.FilledOrders = append(swapInfo.FilledOrders, filledOrderInfo)
isFulfillAmountOut = true
Expand Down Expand Up @@ -123,7 +126,7 @@ func (p *PoolSimulator) calcAmountInWithSwapInfo(swapSide SwapSide, tokenAmountO
totalAmountOut.Sub(totalAmountOut, remainingMakingAmountWei)
_, takerAssetFee := p.calcTakerAssetFeeAmountExactOut(order, remainingTakingAmountWei)
actualAmountIn := new(big.Int).Add(remainingTakingAmountWei, takerAssetFee)
totalAmountInWei.Add(totalAmountInWei, actualAmountIn)
totalAmountInWei = new(big.Int).Add(totalAmountInWei, actualAmountIn)
totalFeeAmountWei = new(big.Int).Add(totalFeeAmountWei, takerAssetFee)
filledOrderInfo := newFilledOrderInfo(order, remainingTakingAmountWei.String(), remainingMakingAmountWei.String(), takerAssetFee.String())
swapInfo.FilledOrders = append(swapInfo.FilledOrders, filledOrderInfo)
Expand Down
Loading

0 comments on commit bb5421e

Please sign in to comment.