Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TRD-783] Implement CalcAmountIn for kyber limit order #657

Merged
Merged
Prev Previous commit
Next Next commit
Reduce math Add or Sub operations
chrisngyn committed Dec 18, 2024
commit 5d75d47ecd6ac3980d4bbfcbb3f06cd047e75e0d
11 changes: 6 additions & 5 deletions pkg/source/limitorder/pool_simulator.go
Original file line number Diff line number Diff line change
@@ -273,9 +273,9 @@ func (p *PoolSimulator) calcAmountOutWithSwapInfo(swapSide SwapSide, tokenAmount
}

feeAmountWeiByOrder := p.calcMakerAssetFeeAmount(order, filledMakingAmountWei)
totalFeeAmountWei = new(big.Int).Add(totalFeeAmountWei, feeAmountWeiByOrder)
totalFeeAmountWei.Add(totalFeeAmountWei, feeAmountWeiByOrder)
actualAmountOut := new(big.Int).Sub(filledMakingAmountWei, feeAmountWeiByOrder)
totalAmountOutWei = new(big.Int).Add(totalAmountOutWei, actualAmountOut)
totalAmountOutWei.Add(totalAmountOutWei, actualAmountOut)
filledOrderInfo := newFilledOrderInfo(order, filledTakingAmountWei.String(), filledMakingAmountWei.String(), feeAmountWeiByOrder.String())
swapInfo.FilledOrders = append(swapInfo.FilledOrders, filledOrderInfo)
isFulfillAmountIn = true
@@ -306,11 +306,12 @@ func (p *PoolSimulator) calcAmountOutWithSwapInfo(swapSide SwapSide, tokenAmount
break
}
_, takerAssetFee := p.calcTakerAssetFeeAmountExactOut(order, remainingTakingAmountWei)
totalAmountIn = new(big.Int).Sub(new(big.Int).Sub(totalAmountIn, takerAssetFee), remainingTakingAmountWei)
totalAmountIn.Sub(totalAmountIn, takerAssetFee)
totalAmountIn.Sub(totalAmountIn, remainingTakingAmountWei)
feeAmountWeiByOrder := p.calcMakerAssetFeeAmount(order, remainingMakingAmountWei)
actualAmountOut := new(big.Int).Sub(remainingMakingAmountWei, feeAmountWeiByOrder)
totalAmountOutWei = new(big.Int).Add(totalAmountOutWei, actualAmountOut)
totalFeeAmountWei = new(big.Int).Add(totalFeeAmountWei, feeAmountWeiByOrder)
totalAmountOutWei.Add(totalAmountOutWei, actualAmountOut)
totalFeeAmountWei.Add(totalFeeAmountWei, feeAmountWeiByOrder)
filledOrderInfo := newFilledOrderInfo(order, remainingTakingAmountWei.String(), remainingMakingAmountWei.String(), feeAmountWeiByOrder.String())
swapInfo.FilledOrders = append(swapInfo.FilledOrders, filledOrderInfo)
addFilledMakingAmount(filledMakingAmountByMaker, order.Maker, remainingMakingAmountWei)
6 changes: 3 additions & 3 deletions pkg/source/limitorder/pool_simulator_calc_amount_in.go
Original file line number Diff line number Diff line change
@@ -92,8 +92,8 @@ func (p *PoolSimulator) calcAmountInWithSwapInfo(swapSide SwapSide, tokenAmountO
}

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