feat!: Return prices array along with SuggestedRoutes

This commit is contained in:
Khushboo Mehta 2024-09-06 13:00:22 +02:00 committed by Anthony Laibe
parent 0235889e12
commit 4816769506
3 changed files with 28 additions and 29 deletions

View File

@ -6,10 +6,9 @@ import (
) )
type RouterSuggestedRoutes struct { type RouterSuggestedRoutes struct {
Uuid string `json:"Uuid"` Uuid string `json:"Uuid"`
Best routes.Route `json:"Best,omitempty"` Best routes.Route `json:"Best,omitempty"`
Candidates routes.Route `json:"Candidates,omitempty"` Candidates routes.Route `json:"Candidates,omitempty"`
TokenPrice *float64 `json:"TokenPrice,omitempty"` UpdatedPrices map[string]float64 `json:"UpdatedPrices,omitempty"`
NativeChainTokenPrice *float64 `json:"NativeChainTokenPrice,omitempty"` ErrorResponse *errors.ErrorResponse `json:"ErrorResponse,omitempty"`
ErrorResponse *errors.ErrorResponse `json:"ErrorResponse,omitempty"`
} }

View File

@ -55,11 +55,10 @@ type ProcessorError struct {
} }
type SuggestedRoutes struct { type SuggestedRoutes struct {
Uuid string Uuid string
Best routes.Route Best routes.Route
Candidates routes.Route Candidates routes.Route
TokenPrice float64 UpdatedPrices map[string]float64
NativeChainTokenPrice float64
} }
type Router struct { type Router struct {
@ -128,18 +127,14 @@ func (r *Router) SetTestBalanceMap(balanceMap map[string]*big.Int) {
} }
func newSuggestedRoutes( func newSuggestedRoutes(
uuid string, input *requests.RouteInputParams,
amountIn *big.Int,
candidates routes.Route, candidates routes.Route,
fromLockedAmount map[uint64]*hexutil.Big, updatedPrices map[string]float64,
tokenPrice float64,
nativeChainTokenPrice float64,
) (*SuggestedRoutes, []routes.Route) { ) (*SuggestedRoutes, []routes.Route) {
suggestedRoutes := &SuggestedRoutes{ suggestedRoutes := &SuggestedRoutes{
Uuid: uuid, Uuid: input.Uuid,
Candidates: candidates, Candidates: candidates,
TokenPrice: tokenPrice, UpdatedPrices: updatedPrices,
NativeChainTokenPrice: nativeChainTokenPrice,
} }
if len(candidates) == 0 { if len(candidates) == 0 {
return suggestedRoutes, nil return suggestedRoutes, nil
@ -147,10 +142,10 @@ func newSuggestedRoutes(
node := &routes.Node{ node := &routes.Node{
Path: nil, Path: nil,
Children: routes.BuildGraph(amountIn, candidates, 0, []uint64{}), Children: routes.BuildGraph(input.AmountIn.ToInt(), candidates, 0, []uint64{}),
} }
allRoutes := node.BuildAllRoutes() allRoutes := node.BuildAllRoutes()
allRoutes = filterRoutes(allRoutes, amountIn, fromLockedAmount) allRoutes = filterRoutes(allRoutes, input.AmountIn.ToInt(), input.FromLockedAmount)
if len(allRoutes) > 0 { if len(allRoutes) > 0 {
sort.Slice(allRoutes, func(i, j int) bool { sort.Slice(allRoutes, func(i, j int) bool {
@ -176,8 +171,7 @@ func sendRouterResult(uuid string, result interface{}, err error) {
if suggestedRoutes, ok := result.(*SuggestedRoutes); ok && suggestedRoutes != nil { if suggestedRoutes, ok := result.(*SuggestedRoutes); ok && suggestedRoutes != nil {
routesResponse.Best = suggestedRoutes.Best routesResponse.Best = suggestedRoutes.Best
routesResponse.Candidates = suggestedRoutes.Candidates routesResponse.Candidates = suggestedRoutes.Candidates
routesResponse.TokenPrice = &suggestedRoutes.TokenPrice routesResponse.UpdatedPrices = suggestedRoutes.UpdatedPrices
routesResponse.NativeChainTokenPrice = &suggestedRoutes.NativeChainTokenPrice
} }
signal.SendWalletEvent(signal.SuggestedRoutes, routesResponse) signal.SendWalletEvent(signal.SuggestedRoutes, routesResponse)
@ -851,7 +845,7 @@ func (r *Router) resolveRoutes(ctx context.Context, input *requests.RouteInputPa
if input.TestsMode { if input.TestsMode {
prices = input.TestParams.TokenPrices prices = input.TestParams.TokenPrices
} else { } else {
prices, err = input.SendType.FetchPrices(r.marketManager, input.TokenID) prices, err = input.SendType.FetchPrices(r.marketManager, []string{input.TokenID, input.ToTokenID})
if err != nil { if err != nil {
return nil, errors.CreateErrorResponseFromError(err) return nil, errors.CreateErrorResponseFromError(err)
} }
@ -861,7 +855,7 @@ func (r *Router) resolveRoutes(ctx context.Context, input *requests.RouteInputPa
nativeTokenPrice := prices[pathprocessor.EthSymbol] nativeTokenPrice := prices[pathprocessor.EthSymbol]
var allRoutes []routes.Route var allRoutes []routes.Route
suggestedRoutes, allRoutes = newSuggestedRoutes(input.Uuid, input.AmountIn.ToInt(), candidates, input.FromLockedAmount, tokenPrice, nativeTokenPrice) suggestedRoutes, allRoutes = newSuggestedRoutes(input, candidates, prices)
defer func() { defer func() {
if suggestedRoutes.Best != nil && len(suggestedRoutes.Best) > 0 { if suggestedRoutes.Best != nil && len(suggestedRoutes.Best) > 0 {

View File

@ -2,6 +2,7 @@ package sendtype
import ( import (
"math/big" "math/big"
"slices"
"strings" "strings"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -39,8 +40,11 @@ func (s SendType) IsStickersTransfer() bool {
return s == StickersBuy return s == StickersBuy
} }
func (s SendType) FetchPrices(marketManager *market.Manager, tokenID string) (map[string]float64, error) { func (s SendType) FetchPrices(marketManager *market.Manager, tokenIDs []string) (map[string]float64, error) {
symbols := []string{tokenID, "ETH"} nonUniqueSymbols := append(tokenIDs, "ETH")
// remove duplicate enteries
slices.Sort(nonUniqueSymbols)
symbols := slices.Compact(nonUniqueSymbols)
if s.IsCollectiblesTransfer() { if s.IsCollectiblesTransfer() {
symbols = []string{"ETH"} symbols = []string{"ETH"}
} }
@ -54,7 +58,9 @@ func (s SendType) FetchPrices(marketManager *market.Manager, tokenID string) (ma
prices[symbol] = pricePerCurrency["USD"] prices[symbol] = pricePerCurrency["USD"]
} }
if s.IsCollectiblesTransfer() { if s.IsCollectiblesTransfer() {
prices[tokenID] = 0 for _, tokenID := range tokenIDs {
prices[tokenID] = 0
}
} }
return prices, nil return prices, nil
} }