From 4f06af14a31255775e85758eefe69ea1bc5757f0 Mon Sep 17 00:00:00 2001 From: Chien Nguyen Date: Tue, 17 Dec 2024 17:19:22 +0700 Subject: [PATCH] Return a copy of big.Int --- pkg/source/limitorder/pool_simulator.go | 8 ++++---- .../limitorder/pool_simulator_calc_amount_in.go | 11 +++++------ 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/pkg/source/limitorder/pool_simulator.go b/pkg/source/limitorder/pool_simulator.go index d095d5535..a42f0c32a 100644 --- a/pkg/source/limitorder/pool_simulator.go +++ b/pkg/source/limitorder/pool_simulator.go @@ -328,12 +328,12 @@ func (p *PoolSimulator) calcMakerAssetFeeAmount(order *order, filledMakingAmount // given total takingAmount, calculate fee and takingAmountAfterFee func (p *PoolSimulator) calcTakerAssetFeeAmountExactIn(order *order, takingAmount *big.Int) (takingAmountAfterFee *big.Int, fee *big.Int) { if !order.IsTakerAssetFee { - return takingAmount, big.NewInt(0) + return new(big.Int).Set(takingAmount), big.NewInt(0) } feePct := order.MakerTokenFeePercent // reuse same field if feePct == 0 { - return takingAmount, big.NewInt(0) + return new(big.Int).Set(takingAmount), big.NewInt(0) } // fee = ceiling(takingAmountAfterFee * feePct / BasisPoint) @@ -354,12 +354,12 @@ func (p *PoolSimulator) calcTakerAssetFeeAmountExactIn(order *order, takingAmoun // given filled takingAmountAfterFee, calculate fee and total takingAmount func (p *PoolSimulator) calcTakerAssetFeeAmountExactOut(order *order, takingAmountAfterFee *big.Int) (takingAmount *big.Int, fee *big.Int) { if !order.IsTakerAssetFee { - return takingAmountAfterFee, big.NewInt(0) + return new(big.Int).Set(takingAmountAfterFee), big.NewInt(0) } feePct := order.MakerTokenFeePercent // reuse same field if feePct == 0 { - return takingAmountAfterFee, big.NewInt(0) + return new(big.Int).Set(takingAmountAfterFee), big.NewInt(0) } amount := new(big.Int).Mul(takingAmountAfterFee, big.NewInt(int64(feePct))) diff --git a/pkg/source/limitorder/pool_simulator_calc_amount_in.go b/pkg/source/limitorder/pool_simulator_calc_amount_in.go index e80a5f730..1364cf65e 100644 --- a/pkg/source/limitorder/pool_simulator_calc_amount_in.go +++ b/pkg/source/limitorder/pool_simulator_calc_amount_in.go @@ -145,12 +145,12 @@ func (p *PoolSimulator) calcAmountInWithSwapInfo(swapSide SwapSide, tokenAmountO // input is the received amount after fee. func (p *PoolSimulator) calcMakerAssetAmountBeforeFee(order *order, makingAmount *big.Int) (makingAmountBeforeFee *big.Int, fee *big.Int) { if order.IsTakerAssetFee { - return makingAmount, big.NewInt(0) + return new(big.Int).Set(makingAmount), big.NewInt(0) } feePct := order.MakerTokenFeePercent if feePct == 0 { - return makingAmount, big.NewInt(0) + return new(big.Int).Set(makingAmount), big.NewInt(0) } // makingAmountBeforeFee = makingAmount * BasisPoint / (BasisPoint - feePct) @@ -167,8 +167,7 @@ func (p *PoolSimulator) calcMakerAssetAmountBeforeFee(order *order, makingAmount func divCeil(a, b *big.Int) *big.Int { // (a + b - 1) / b - return new(big.Int).Div( - new(big.Int).Sub(new(big.Int).Add(a, b), big.NewInt(1)), - b, - ) + a = new(big.Int).Add(a, b) + a = new(big.Int).Sub(a, big.NewInt(1)) + return a.Div(a, b) }