Skip to content

Commit

Permalink
Return a copy of big.Int
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisngyn committed Dec 17, 2024
1 parent d164a33 commit 4f06af1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
8 changes: 4 additions & 4 deletions pkg/source/limitorder/pool_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)))
Expand Down
11 changes: 5 additions & 6 deletions pkg/source/limitorder/pool_simulator_calc_amount_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}

0 comments on commit 4f06af1

Please sign in to comment.