Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router clean up #5778

Merged
merged 6 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions services/communitytokens/estimations.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import (
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/services/wallet/bigint"
"github.com/status-im/status-go/services/wallet/router"
"github.com/status-im/status-go/services/wallet/router/fees"
"github.com/status-im/status-go/transactions"
)

type CommunityTokenFees struct {
GasUnits uint64 `json:"gasUnits"`
SuggestedFees *router.SuggestedFeesGwei `json:"suggestedFees"`
GasUnits uint64 `json:"gasUnits"`
SuggestedFees *fees.SuggestedFeesGwei `json:"suggestedFees"`
}

func weiToGwei(val *big.Int) *big.Float {
Expand Down Expand Up @@ -373,7 +373,7 @@ func (s *Service) prepareCommunityTokenFees(ctx context.Context, from common.Add
}, nil
}

func (s *Service) suggestedFeesToSendTxArgs(from common.Address, to *common.Address, gas uint64, suggestedFees *router.SuggestedFeesGwei) transactions.SendTxArgs {
func (s *Service) suggestedFeesToSendTxArgs(from common.Address, to *common.Address, gas uint64, suggestedFees *fees.SuggestedFeesGwei) transactions.SendTxArgs {
sendArgs := transactions.SendTxArgs{}
sendArgs.From = types.Address(from)
sendArgs.To = (*types.Address)(to)
Expand Down
6 changes: 3 additions & 3 deletions services/communitytokens/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"github.com/status-im/status-go/services/utils"
"github.com/status-im/status-go/services/wallet/bigint"
wcommon "github.com/status-im/status-go/services/wallet/common"
"github.com/status-im/status-go/services/wallet/router"
"github.com/status-im/status-go/services/wallet/router/fees"
"github.com/status-im/status-go/services/wallet/walletevent"
"github.com/status-im/status-go/signal"
"github.com/status-im/status-go/transactions"
Expand All @@ -49,7 +49,7 @@ type Service struct {
walletFeed *event.Feed
walletWatcher *walletevent.Watcher
transactor *transactions.Transactor
feeManager *router.FeeManager
feeManager *fees.FeeManager
}

// Returns a new Collectibles Service.
Expand All @@ -63,7 +63,7 @@ func NewService(rpcClient *rpc.Client, accountsManager *account.GethManager, pen
db: communitytokensdatabase.NewCommunityTokensDatabase(appDb),
walletFeed: walletFeed,
transactor: transactor,
feeManager: &router.FeeManager{RPCClient: rpcClient},
feeManager: &fees.FeeManager{RPCClient: rpcClient},
}
}

Expand Down
46 changes: 14 additions & 32 deletions services/wallet/api.go
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why is this needed.

  • When we're release WakuV2, we don't forget WakuV1 and rename WakuV2 to WakuV1.
  • What if we release a newer version of router? Will it be RouterV2 or RouterV3 then?

I believe this breaking change is very unnecessary.
Same about renaming files. Why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Router V2 was just a replacement for the old router, to not mess up with the code, while we're improving it. Also I don't like V2 in endpoint names, if we really want to have versioning then it should be done another way and passed as a property of input params.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we really want to have versioning then it should be done another way and passed as a property of input params.

Or even have a separate service for this 🤔


Not a big deal. If the desktop/mobile will be adapted then it's 90% ok IMO.
Just in general, it's important to have a mindset of not introducing any breaking changes, I guess that's my point

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/status-im/status-go/services/wallet/onramp"
"github.com/status-im/status-go/services/wallet/requests"
"github.com/status-im/status-go/services/wallet/router"
"github.com/status-im/status-go/services/wallet/router/fees"
"github.com/status-im/status-go/services/wallet/router/pathprocessor"
"github.com/status-im/status-go/services/wallet/thirdparty"
"github.com/status-im/status-go/services/wallet/token"
Expand Down Expand Up @@ -468,7 +469,7 @@ func (api *API) FetchTokenDetails(ctx context.Context, symbols []string) (map[st
return api.s.marketManager.FetchTokenDetails(symbols)
}

func (api *API) GetSuggestedFees(ctx context.Context, chainID uint64) (*router.SuggestedFeesGwei, error) {
func (api *API) GetSuggestedFees(ctx context.Context, chainID uint64) (*fees.SuggestedFeesGwei, error) {
log.Debug("call to GetSuggestedFees")
return api.router.GetFeesManager().SuggestedFeesGwei(ctx, chainID)
}
Expand All @@ -478,7 +479,7 @@ func (api *API) GetEstimatedLatestBlockNumber(ctx context.Context, chainID uint6
return api.s.blockChainState.GetEstimatedLatestBlockNumber(ctx, chainID)
}

func (api *API) GetTransactionEstimatedTime(ctx context.Context, chainID uint64, maxFeePerGas *big.Float) (router.TransactionEstimation, error) {
func (api *API) GetTransactionEstimatedTime(ctx context.Context, chainID uint64, maxFeePerGas *big.Float) (fees.TransactionEstimation, error) {
log.Debug("call to getTransactionEstimatedTime")
return api.router.GetFeesManager().TransactionEstimatedTime(ctx, chainID, gweiToWei(maxFeePerGas)), nil
}
Expand All @@ -488,47 +489,28 @@ func gweiToWei(val *big.Float) *big.Int {
return res
}

func (api *API) GetSuggestedRoutes(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just mark it as deprecated and return an error for now. And remove the function when the clients are updated.
No need for such destructive actions 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I wanted to clean the api, not to keep things around, that's why we added the same names with V2 suffixes, to not make breaking changes. Also I plan to sync with @alwx (actually we already chat in the wallet channel) on this and don't plan to merge this before mobile is ready for this change, then we will sync and merge changes at the same time.

ctx context.Context,
sendType router.SendType,
addrFrom common.Address,
addrTo common.Address,
amountIn *hexutil.Big,
tokenID string,
toTokenID string,
disabledFromChainIDs,
disabledToChainIDs,
preferedChainIDs []uint64,
gasFeeMode router.GasFeeMode,
fromLockedAmount map[uint64]*hexutil.Big,
) (*router.SuggestedRoutes, error) {
func (api *API) GetSuggestedRoutes(ctx context.Context, input *requests.RouteInputParams) (*router.SuggestedRoutes, error) {
log.Debug("call to GetSuggestedRoutes")

testnetMode, err := api.s.rpcClient.NetworkManager.GetTestNetworksEnabled()
if err != nil {
return nil, err
}

return api.router.SuggestedRoutes(ctx, sendType, addrFrom, addrTo, amountIn.ToInt(), tokenID, toTokenID, disabledFromChainIDs,
disabledToChainIDs, preferedChainIDs, gasFeeMode, fromLockedAmount, testnetMode)
return api.router.SuggestedRoutes(ctx, input)
}

func (api *API) GetSuggestedRoutesV2(ctx context.Context, input *router.RouteInputParams) (*router.SuggestedRoutesV2, error) {
log.Debug("call to GetSuggestedRoutesV2")
func (api *API) GetSuggestedRoutesAsync(ctx context.Context, input *requests.RouteInputParams) {
log.Debug("call to GetSuggestedRoutesAsync")

return api.router.SuggestedRoutesV2(ctx, input)
api.router.SuggestedRoutesAsync(input)
}

func (api *API) GetSuggestedRoutesV2Async(ctx context.Context, input *router.RouteInputParams) {
log.Debug("call to GetSuggestedRoutesV2Async")
func (api *API) StopSuggestedRoutesAsyncCalculation(ctx context.Context) {
log.Debug("call to StopSuggestedRoutesAsyncCalculation")

api.router.SuggestedRoutesV2Async(input)
api.router.StopSuggestedRoutesAsyncCalculation()
}

func (api *API) StopSuggestedRoutesV2AsyncCalcualtion(ctx context.Context) {
log.Debug("call to StopSuggestedRoutesV2AsyncCalcualtion")
func (api *API) StopSuggestedRoutesCalculation(ctx context.Context) {
log.Debug("call to StopSuggestedRoutesCalculation")

api.router.StopSuggestedRoutesV2AsyncCalcualtion()
api.router.StopSuggestedRoutesCalculation()
}

// Generates addresses for the provided paths, response doesn't include `HasActivity` value (if you need it check `GetAddressDetails` function)
Expand Down
13 changes: 13 additions & 0 deletions services/wallet/common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type MultiTransactionIDType int64

const (
NoMultiTransactionID = MultiTransactionIDType(0)
HexAddressLength = 42
)

type ChainID uint64
Expand All @@ -32,6 +33,18 @@ const (

var (
ZeroAddress = ethCommon.HexToAddress("0x0000000000000000000000000000000000000000")

SupportedNetworks = map[uint64]bool{
EthereumMainnet: true,
OptimismMainnet: true,
ArbitrumMainnet: true,
}

SupportedTestNetworks = map[uint64]bool{
EthereumSepolia: true,
OptimismSepolia: true,
ArbitrumSepolia: true,
}
)

type ContractType byte
Expand Down
51 changes: 51 additions & 0 deletions services/wallet/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package common

import (
"context"
"math/big"
"reflect"

gethParams "github.com/ethereum/go-ethereum/params"
"github.com/status-im/status-go/params"
)

Expand All @@ -24,3 +27,51 @@ func NetworksToChainIDs(networks []*params.Network) []uint64 {

return chainIDs
}

func ArrayContainsElement[T comparable](el T, arr []T) bool {
for _, e := range arr {
if e == el {
return true
}
}
return false
}

func IsSingleChainOperation(fromChains []*params.Network, toChains []*params.Network) bool {
return len(fromChains) == 1 &&
len(toChains) == 1 &&
fromChains[0].ChainID == toChains[0].ChainID
}

// CopyMapGeneric creates a copy of any map, if the deepCopyValue function is provided, it will be used to copy values.
func CopyMapGeneric(original interface{}, deepCopyValueFn func(interface{}) interface{}) interface{} {
originalVal := reflect.ValueOf(original)
if originalVal.Kind() != reflect.Map {
return nil
}

newMap := reflect.MakeMap(originalVal.Type())
for iter := originalVal.MapRange(); iter.Next(); {
if deepCopyValueFn != nil {
newMap.SetMapIndex(iter.Key(), reflect.ValueOf(deepCopyValueFn(iter.Value().Interface())))
} else {
newMap.SetMapIndex(iter.Key(), iter.Value())
}
}

return newMap.Interface()
}

func GweiToEth(val *big.Float) *big.Float {
return new(big.Float).Quo(val, big.NewFloat(1000000000))
}

func WeiToGwei(val *big.Int) *big.Float {
result := new(big.Float)
result.SetInt(val)

unit := new(big.Int)
unit.SetInt64(gethParams.GWei)

return result.Quo(result, new(big.Float).SetInt(unit))
}
Loading