Skip to content

Commit

Permalink
chore_: router response moved to wallet responses location
Browse files Browse the repository at this point in the history
Following the approach we did for keeping requests at the same location, these changes introduce
new location for responses. `SuggestedRoutesResponse` is moved there and renamed to
`RouterSuggestedRoutes` and code is updated accordingly.

New type `Route` defined (since a single route is composed of zero or more paths).

Types `Route`, `Path`, `Graph` and `Node` belong to a new `routs` package now.
  • Loading branch information
saledjenic committed Aug 28, 2024
1 parent a759fe6 commit e57bf22
Show file tree
Hide file tree
Showing 10 changed files with 337 additions and 316 deletions.
15 changes: 15 additions & 0 deletions services/wallet/responses/router_suggested_routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package responses

import (
"github.com/status-im/status-go/errors"
routs "github.com/status-im/status-go/services/wallet/router/routes"
)

type RouterSuggestedRoutes struct {
Uuid string `json:"Uuid"`
Best routs.Route `json:"Best,omitempty"`
Candidates routs.Route `json:"Candidates,omitempty"`
TokenPrice *float64 `json:"TokenPrice,omitempty"`
NativeChainTokenPrice *float64 `json:"NativeChainTokenPrice,omitempty"`
ErrorResponse *errors.ErrorResponse `json:"ErrorResponse,omitempty"`
}
5 changes: 3 additions & 2 deletions services/wallet/router/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package router

import (
"github.com/status-im/status-go/services/wallet/common"
routs "github.com/status-im/status-go/services/wallet/router/routes"
)

func removeBestRouteFromAllRouters(allRoutes [][]*Path, best []*Path) [][]*Path {
func removeBestRouteFromAllRouters(allRoutes []routs.Route, best routs.Route) []routs.Route {
for i := len(allRoutes) - 1; i >= 0; i-- {
route := allRoutes[i]
routeFound := true
Expand Down Expand Up @@ -45,7 +46,7 @@ func getChainPriority(chainID uint64) int {
}
}

func getRoutePriority(route []*Path) int {
func getRoutePriority(route routs.Route) int {
priority := 0
for _, path := range route {
priority += getChainPriority(path.FromChain.ChainID)
Expand Down
17 changes: 9 additions & 8 deletions services/wallet/router/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/status-im/status-go/services/wallet/common"
"github.com/status-im/status-go/services/wallet/router/pathprocessor"
routs "github.com/status-im/status-go/services/wallet/router/routes"

"go.uber.org/zap"
)
Expand All @@ -20,7 +21,7 @@ func init() {
}
}

func filterRoutes(routes [][]*Path, amountIn *big.Int, fromLockedAmount map[uint64]*hexutil.Big) [][]*Path {
func filterRoutes(routes []routs.Route, amountIn *big.Int, fromLockedAmount map[uint64]*hexutil.Big) []routs.Route {
for i := len(routes) - 1; i >= 0; i-- {
routeAmount := big.NewInt(0)
for _, p := range routes[i] {
Expand All @@ -43,8 +44,8 @@ func filterRoutes(routes [][]*Path, amountIn *big.Int, fromLockedAmount map[uint
}

// filterNetworkCompliance performs the first level of filtering based on network inclusion/exclusion criteria.
func filterNetworkCompliance(routes [][]*Path, fromLockedAmount map[uint64]*hexutil.Big) [][]*Path {
filteredRoutes := make([][]*Path, 0)
func filterNetworkCompliance(routes []routs.Route, fromLockedAmount map[uint64]*hexutil.Big) []routs.Route {
filteredRoutes := make([]routs.Route, 0)
if routes == nil || fromLockedAmount == nil {
return filteredRoutes
}
Expand All @@ -65,7 +66,7 @@ func filterNetworkCompliance(routes [][]*Path, fromLockedAmount map[uint64]*hexu
}

// isValidForNetworkCompliance checks if a route complies with network inclusion/exclusion criteria.
func isValidForNetworkCompliance(route []*Path, fromIncluded, fromExcluded map[uint64]bool) bool {
func isValidForNetworkCompliance(route routs.Route, fromIncluded, fromExcluded map[uint64]bool) bool {
logger.Debug("Initial inclusion/exclusion maps",
zap.Any("fromIncluded", fromIncluded),
zap.Any("fromExcluded", fromExcluded),
Expand Down Expand Up @@ -117,8 +118,8 @@ func setupRouteValidationMaps(fromLockedAmount map[uint64]*hexutil.Big) (map[uin
}

// filterCapacityValidation performs the second level of filtering based on amount and capacity validation.
func filterCapacityValidation(routes [][]*Path, amountIn *big.Int, fromLockedAmount map[uint64]*hexutil.Big) [][]*Path {
filteredRoutes := make([][]*Path, 0)
func filterCapacityValidation(routes []routs.Route, amountIn *big.Int, fromLockedAmount map[uint64]*hexutil.Big) []routs.Route {
filteredRoutes := make([]routs.Route, 0)

for _, route := range routes {
if hasSufficientCapacity(route, amountIn, fromLockedAmount) {
Expand All @@ -129,7 +130,7 @@ func filterCapacityValidation(routes [][]*Path, amountIn *big.Int, fromLockedAmo
}

// hasSufficientCapacity checks if a route has sufficient capacity to handle the required amount.
func hasSufficientCapacity(route []*Path, amountIn *big.Int, fromLockedAmount map[uint64]*hexutil.Big) bool {
func hasSufficientCapacity(route routs.Route, amountIn *big.Int, fromLockedAmount map[uint64]*hexutil.Big) bool {
for _, path := range route {
if amount, ok := fromLockedAmount[path.FromChain.ChainID]; ok {
if path.AmountIn.ToInt().Cmp(amount.ToInt()) != 0 {
Expand All @@ -153,7 +154,7 @@ func hasSufficientCapacity(route []*Path, amountIn *big.Int, fromLockedAmount ma
}

// calculateRestAmountIn calculates the remaining amount in for the route excluding the specified path
func calculateRestAmountIn(route []*Path, excludePath *Path) *big.Int {
func calculateRestAmountIn(route routs.Route, excludePath *routs.Path) *big.Int {
restAmountIn := big.NewInt(0)
for _, path := range route {
if path != excludePath {
Expand Down
Loading

0 comments on commit e57bf22

Please sign in to comment.