Skip to content

Commit 1b2036d

Browse files
authored
GenKthBestPaths: Compare amountOut when amountOutUsd is the same (#214)
1 parent f25f533 commit 1b2036d

File tree

4 files changed

+10
-11
lines changed

4 files changed

+10
-11
lines changed

internal/pkg/usecase/findroute/common/gen_path.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ func getKthPathAtTokenOut(
229229

230230
func betterAmountOut(nodeA, nodeB *nodeInfo, gasFeeInclude bool) bool {
231231
// If we consider gas fee, prioritize node with more AmountUsd
232-
if gasFeeInclude {
232+
// If amountUsd is the same, compare amountOut regardless of gasFeeInclude
233+
if gasFeeInclude && !utils.Float64AlmostEqual(nodeA.tokenAmount.AmountUsd, nodeA.tokenAmount.AmountUsd) {
233234
return nodeA.tokenAmount.AmountUsd > nodeB.tokenAmount.AmountUsd
234235
}
235236
// Otherwise, prioritize node with more token Amount

internal/pkg/usecase/getroute/cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func (c *cache) setRouteToCache(ctx context.Context, routeSummary *valueobject.R
163163

164164
func (c *cache) getCachePointTTL(amount float64) (time.Duration, bool) {
165165
for _, cachePoint := range c.config.TTLByAmount {
166-
if float64AlmostEqual(cachePoint.Amount, amount) {
166+
if utils.Float64AlmostEqual(cachePoint.Amount, amount) {
167167
return cachePoint.TTL, true
168168
}
169169
}

internal/pkg/usecase/getroute/util.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

internal/pkg/utils/math.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
package utils
22

33
import (
4+
"math"
45
"math/big"
56

67
"github.com/KyberNetwork/router-service/internal/pkg/constant"
78
)
89

10+
const float64EqualityThreshold = 1e-9
11+
12+
func Float64AlmostEqual(a, b float64) bool {
13+
return math.Abs(a-b) <= float64EqualityThreshold
14+
}
15+
916
func NewBig10(s string) (res *big.Int) {
1017
res, _ = new(big.Int).SetString(s, 10)
1118
return res

0 commit comments

Comments
 (0)