Skip to content

Commit 8f6dc5b

Browse files
calculate best taking amount fusion order (#177)
* calculate best taking amount fusion order * fix wrong amount
1 parent d7788d9 commit 8f6dc5b

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

pkg/oneinch/auctioncalculator/amount_calculator.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ func (c AmountCalculator) GetRequiredTakingAmount(
6363
return c.getAuctionBumpedAmount(withFee, ts, blockBaseFee)
6464
}
6565

66+
func (c AmountCalculator) GetBestRequiredTakingAmount(
67+
takingAmount *big.Int,
68+
ts *big.Int,
69+
blockBaseFee *big.Int,
70+
) *big.Int {
71+
withFee := c.feeCalculator.GetTakingAmountWhitelist(takingAmount)
72+
return c.getAuctionBumpedAmount(withFee, ts, blockBaseFee)
73+
}
74+
6675
func (c AmountCalculator) GetRequiredMakingAmount(
6776
taker common.Address,
6877
makingAmount *big.Int,

pkg/oneinch/fusionorder/fusion_order.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ func (o FusionOrder) CalcTakingAmount(
3737
return o.amountCalculator.GetRequiredTakingAmount(taker, takingAmount, blockTime, baseFee)
3838
}
3939

40+
func (o FusionOrder) CalcBestTakingAmount(
41+
makingAmount *big.Int,
42+
blockTime,
43+
baseFee *big.Int,
44+
) *big.Int {
45+
takingAmount := util.CalcTakingAmount(makingAmount, o.LimitOrder.MakingAmount, o.LimitOrder.TakingAmount)
46+
return o.amountCalculator.GetBestRequiredTakingAmount(takingAmount, blockTime, baseFee)
47+
}
48+
4049
func (o FusionOrder) CalcMakingAmount(
4150
taker common.Address,
4251
takingAmount *big.Int,

pkg/oneinch/limitorder/fee_calculator.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,20 @@ func (c FeeCalculator) getFee(taker common.Address) *big.Int {
112112
return new(big.Int).SetInt64(Base10000 + resolverFee + integratorFee)
113113
}
114114

115+
func (c FeeCalculator) getWhitelistFee() *big.Int {
116+
resolverFee, integratorFee := c.GetWhitelistFees()
117+
return new(big.Int).SetInt64(Base10000 + resolverFee + integratorFee)
118+
}
119+
115120
// GetTakingAmount https://github.com/1inch/limit-order-sdk/blob/1793d32bd36c6cfea909caafbc15e8023a033249/src/limit-order/extensions/fee-taker/fee-calculator.ts#L13
116121
func (c FeeCalculator) GetTakingAmount(taker common.Address, orderTakingAmount *big.Int) *big.Int {
117122
return util.MulDiv(orderTakingAmount, c.getFee(taker), big.NewInt(Base10000), util.Ceil)
118123
}
119124

125+
func (c FeeCalculator) GetTakingAmountWhitelist(orderTakingAmount *big.Int) *big.Int {
126+
return util.MulDiv(orderTakingAmount, c.getWhitelistFee(), big.NewInt(Base10000), util.Ceil)
127+
}
128+
120129
// GetMakingAmount https://github.com/1inch/limit-order-sdk/blob/1793d32bd36c6cfea909caafbc15e8023a033249/src/limit-order/extensions/fee-taker/fee-calculator.ts#L23
121130
func (c FeeCalculator) GetMakingAmount(taker common.Address, orderMakingAmount *big.Int) *big.Int {
122131
return util.MulDiv(orderMakingAmount, big.NewInt(Base10000), c.getFee(taker), util.Floor)
@@ -134,6 +143,13 @@ func (c FeeCalculator) GetFeesForTaker(taker common.Address) (int64, int64) {
134143
return resolverFee, int64(c.fees.Integrator.Fee)
135144
}
136145

146+
func (c FeeCalculator) GetWhitelistFees() (int64, int64) {
147+
discountNumerator := Base10000 - c.fees.Resolver.WhitelistDiscount
148+
resolverFee := int64(discountNumerator) * int64(c.fees.Resolver.Fee) / Base10000
149+
150+
return resolverFee, int64(c.fees.Integrator.Fee)
151+
}
152+
137153
// GetResolverFee which resolver pays to resolver fee receiver
138154
func (c FeeCalculator) GetResolverFee(taker common.Address, orderTakingAmount *big.Int) *big.Int {
139155
takingAmount := c.GetTakingAmount(taker, orderTakingAmount)

0 commit comments

Comments
 (0)