2024-05-16 08:55:46 +00:00
|
|
|
package router
|
2022-06-09 13:09:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-11-23 17:49:23 +00:00
|
|
|
"errors"
|
2022-06-09 13:09:56 +00:00
|
|
|
"math"
|
|
|
|
"math/big"
|
2022-11-23 17:49:23 +00:00
|
|
|
"sort"
|
2022-12-19 12:37:37 +00:00
|
|
|
"strings"
|
2022-06-09 13:09:56 +00:00
|
|
|
"sync"
|
|
|
|
|
2022-12-19 12:37:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum"
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
2022-06-09 13:09:56 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2022-09-13 07:10:59 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2022-12-19 12:37:37 +00:00
|
|
|
"github.com/status-im/status-go/contracts"
|
2024-03-25 12:40:00 +00:00
|
|
|
gaspriceoracle "github.com/status-im/status-go/contracts/gas-price-oracle"
|
2022-12-19 12:37:37 +00:00
|
|
|
"github.com/status-im/status-go/contracts/ierc20"
|
2022-06-09 13:09:56 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2024-06-12 20:13:16 +00:00
|
|
|
protocolCommon "github.com/status-im/status-go/protocol/common"
|
2022-12-19 12:37:37 +00:00
|
|
|
"github.com/status-im/status-go/rpc"
|
2024-05-16 08:55:46 +00:00
|
|
|
"github.com/status-im/status-go/services/ens"
|
|
|
|
"github.com/status-im/status-go/services/stickers"
|
2022-06-09 13:09:56 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/async"
|
2022-09-13 07:10:59 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/bigint"
|
2024-05-16 08:55:46 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/collectibles"
|
2023-08-24 08:45:14 +00:00
|
|
|
walletCommon "github.com/status-im/status-go/services/wallet/common"
|
2024-05-16 08:55:46 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/market"
|
2024-06-06 20:08:25 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/router/pathprocessor"
|
2022-09-13 07:10:59 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/token"
|
2024-04-01 13:39:17 +00:00
|
|
|
walletToken "github.com/status-im/status-go/services/wallet/token"
|
2024-05-16 08:55:46 +00:00
|
|
|
"github.com/status-im/status-go/transactions"
|
2022-06-09 13:09:56 +00:00
|
|
|
)
|
|
|
|
|
2024-05-14 19:11:16 +00:00
|
|
|
// //////////////////////////////////////////////////////////////////////////////
|
|
|
|
// TODO: once new router is in place, remove this `router.go` file,
|
|
|
|
// rename and make `router_v2.go` file the main and only file
|
|
|
|
// //////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2024-06-05 07:56:02 +00:00
|
|
|
// TODO: remove the following two consts once we fully move to routerV2
|
2023-08-24 08:45:14 +00:00
|
|
|
const EstimateUsername = "RandomUsername"
|
|
|
|
const EstimatePubKey = "0x04bb2024ce5d72e45d4a4f8589ae657ef9745855006996115a23a1af88d536cf02c0524a585fce7bfa79d6a9669af735eda6205d6c7e5b3cdc2b8ff7b2fa1f0b56"
|
2022-09-13 07:10:59 +00:00
|
|
|
|
|
|
|
type Path struct {
|
2022-12-19 12:37:37 +00:00
|
|
|
BridgeName string
|
|
|
|
From *params.Network
|
|
|
|
To *params.Network
|
|
|
|
MaxAmountIn *hexutil.Big
|
|
|
|
AmountIn *hexutil.Big
|
|
|
|
AmountInLocked bool
|
|
|
|
AmountOut *hexutil.Big
|
|
|
|
GasAmount uint64
|
|
|
|
GasFees *SuggestedFees
|
|
|
|
BonderFees *hexutil.Big
|
|
|
|
TokenFees *big.Float
|
|
|
|
Cost *big.Float
|
|
|
|
EstimatedTime TransactionEstimation
|
|
|
|
ApprovalRequired bool
|
|
|
|
ApprovalGasFees *big.Float
|
|
|
|
ApprovalAmountRequired *hexutil.Big
|
|
|
|
ApprovalContractAddress *common.Address
|
2022-11-23 17:49:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Path) Equal(o *Path) bool {
|
|
|
|
return p.From.ChainID == o.From.ChainID && p.To.ChainID == o.To.ChainID
|
2022-09-13 07:10:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Graph = []*Node
|
|
|
|
|
|
|
|
type Node struct {
|
|
|
|
Path *Path
|
|
|
|
Children Graph
|
|
|
|
}
|
|
|
|
|
|
|
|
func newNode(path *Path) *Node {
|
|
|
|
return &Node{Path: path, Children: make(Graph, 0)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildGraph(AmountIn *big.Int, routes []*Path, level int, sourceChainIDs []uint64) Graph {
|
|
|
|
graph := make(Graph, 0)
|
|
|
|
for _, route := range routes {
|
|
|
|
found := false
|
|
|
|
for _, chainID := range sourceChainIDs {
|
|
|
|
if chainID == route.From.ChainID {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if found {
|
|
|
|
continue
|
2022-06-09 13:09:56 +00:00
|
|
|
}
|
2022-09-13 07:10:59 +00:00
|
|
|
node := newNode(route)
|
2022-06-09 13:09:56 +00:00
|
|
|
|
2022-09-13 07:10:59 +00:00
|
|
|
newRoutes := make([]*Path, 0)
|
|
|
|
for _, r := range routes {
|
2022-11-23 17:49:23 +00:00
|
|
|
if route.Equal(r) {
|
2022-09-13 07:10:59 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
newRoutes = append(newRoutes, r)
|
2022-06-09 13:09:56 +00:00
|
|
|
}
|
2022-09-13 07:10:59 +00:00
|
|
|
|
|
|
|
newAmountIn := new(big.Int).Sub(AmountIn, route.MaxAmountIn.ToInt())
|
|
|
|
if newAmountIn.Sign() > 0 {
|
|
|
|
newSourceChainIDs := make([]uint64, len(sourceChainIDs))
|
|
|
|
copy(newSourceChainIDs, sourceChainIDs)
|
|
|
|
newSourceChainIDs = append(newSourceChainIDs, route.From.ChainID)
|
|
|
|
node.Children = buildGraph(newAmountIn, newRoutes, level+1, newSourceChainIDs)
|
|
|
|
|
|
|
|
if len(node.Children) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
graph = append(graph, node)
|
2022-06-09 13:09:56 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 07:10:59 +00:00
|
|
|
return graph
|
2022-06-09 13:09:56 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 17:49:23 +00:00
|
|
|
func (n Node) buildAllRoutes() [][]*Path {
|
|
|
|
res := make([][]*Path, 0)
|
|
|
|
|
|
|
|
if len(n.Children) == 0 && n.Path != nil {
|
|
|
|
res = append(res, []*Path{n.Path})
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, node := range n.Children {
|
|
|
|
for _, route := range node.buildAllRoutes() {
|
|
|
|
extendedRoute := route
|
|
|
|
if n.Path != nil {
|
|
|
|
extendedRoute = append([]*Path{n.Path}, route...)
|
|
|
|
}
|
|
|
|
res = append(res, extendedRoute)
|
2022-09-13 07:10:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 17:49:23 +00:00
|
|
|
return res
|
|
|
|
}
|
2022-09-13 07:10:59 +00:00
|
|
|
|
2022-11-23 17:49:23 +00:00
|
|
|
func filterRoutes(routes [][]*Path, amountIn *big.Int, fromLockedAmount map[uint64]*hexutil.Big) [][]*Path {
|
|
|
|
if len(fromLockedAmount) == 0 {
|
|
|
|
return routes
|
|
|
|
}
|
|
|
|
|
|
|
|
filteredRoutesLevel1 := make([][]*Path, 0)
|
|
|
|
for _, route := range routes {
|
|
|
|
routeOk := true
|
|
|
|
fromIncluded := make(map[uint64]bool)
|
|
|
|
fromExcluded := make(map[uint64]bool)
|
|
|
|
for chainID, amount := range fromLockedAmount {
|
2024-06-06 20:08:25 +00:00
|
|
|
if amount.ToInt().Cmp(pathprocessor.ZeroBigIntValue) == 0 {
|
2022-11-23 17:49:23 +00:00
|
|
|
fromExcluded[chainID] = false
|
|
|
|
} else {
|
|
|
|
fromIncluded[chainID] = false
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
for _, path := range route {
|
|
|
|
if _, ok := fromExcluded[path.From.ChainID]; ok {
|
|
|
|
routeOk = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if _, ok := fromIncluded[path.From.ChainID]; ok {
|
|
|
|
fromIncluded[path.From.ChainID] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, value := range fromIncluded {
|
|
|
|
if !value {
|
|
|
|
routeOk = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if routeOk {
|
|
|
|
filteredRoutesLevel1 = append(filteredRoutesLevel1, route)
|
2022-09-13 07:10:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 17:49:23 +00:00
|
|
|
filteredRoutesLevel2 := make([][]*Path, 0)
|
|
|
|
for _, route := range filteredRoutesLevel1 {
|
|
|
|
routeOk := true
|
|
|
|
for _, path := range route {
|
|
|
|
if amount, ok := fromLockedAmount[path.From.ChainID]; ok {
|
|
|
|
requiredAmountIn := new(big.Int).Sub(amountIn, amount.ToInt())
|
|
|
|
restAmountIn := big.NewInt(0)
|
|
|
|
|
|
|
|
for _, otherPath := range route {
|
|
|
|
if path.Equal(otherPath) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
restAmountIn = new(big.Int).Add(otherPath.MaxAmountIn.ToInt(), restAmountIn)
|
|
|
|
}
|
|
|
|
if restAmountIn.Cmp(requiredAmountIn) >= 0 {
|
|
|
|
path.AmountIn = amount
|
|
|
|
path.AmountInLocked = true
|
|
|
|
} else {
|
|
|
|
routeOk = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if routeOk {
|
|
|
|
filteredRoutesLevel2 = append(filteredRoutesLevel2, route)
|
|
|
|
}
|
2022-09-13 07:10:59 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 17:49:23 +00:00
|
|
|
return filteredRoutesLevel2
|
|
|
|
}
|
|
|
|
|
|
|
|
func findBest(routes [][]*Path) []*Path {
|
|
|
|
var best []*Path
|
|
|
|
bestCost := big.NewFloat(math.Inf(1))
|
|
|
|
for _, route := range routes {
|
|
|
|
currentCost := big.NewFloat(0)
|
|
|
|
for _, path := range route {
|
|
|
|
currentCost = new(big.Float).Add(currentCost, path.Cost)
|
|
|
|
}
|
|
|
|
|
|
|
|
if currentCost.Cmp(bestCost) == -1 {
|
|
|
|
best = route
|
|
|
|
bestCost = currentCost
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return best
|
2022-09-13 07:10:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SuggestedRoutes struct {
|
|
|
|
Best []*Path
|
|
|
|
Candidates []*Path
|
|
|
|
TokenPrice float64
|
|
|
|
NativeChainTokenPrice float64
|
|
|
|
}
|
|
|
|
|
2022-11-23 17:49:23 +00:00
|
|
|
func newSuggestedRoutes(
|
|
|
|
amountIn *big.Int,
|
|
|
|
candidates []*Path,
|
|
|
|
fromLockedAmount map[uint64]*hexutil.Big,
|
|
|
|
) *SuggestedRoutes {
|
2022-09-13 07:10:59 +00:00
|
|
|
if len(candidates) == 0 {
|
|
|
|
return &SuggestedRoutes{
|
|
|
|
Candidates: candidates,
|
|
|
|
Best: candidates,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
node := &Node{
|
|
|
|
Path: nil,
|
|
|
|
Children: buildGraph(amountIn, candidates, 0, []uint64{}),
|
|
|
|
}
|
2022-11-23 17:49:23 +00:00
|
|
|
routes := node.buildAllRoutes()
|
|
|
|
routes = filterRoutes(routes, amountIn, fromLockedAmount)
|
|
|
|
best := findBest(routes)
|
2022-09-13 07:10:59 +00:00
|
|
|
|
|
|
|
if len(best) > 0 {
|
2022-11-23 17:49:23 +00:00
|
|
|
sort.Slice(best, func(i, j int) bool {
|
|
|
|
return best[i].AmountInLocked
|
|
|
|
})
|
2022-09-13 07:10:59 +00:00
|
|
|
rest := new(big.Int).Set(amountIn)
|
|
|
|
for _, path := range best {
|
|
|
|
diff := new(big.Int).Sub(rest, path.MaxAmountIn.ToInt())
|
2024-06-06 20:08:25 +00:00
|
|
|
if diff.Cmp(pathprocessor.ZeroBigIntValue) >= 0 {
|
2022-11-23 17:49:23 +00:00
|
|
|
path.AmountIn = (*hexutil.Big)(path.MaxAmountIn.ToInt())
|
2022-09-13 07:10:59 +00:00
|
|
|
} else {
|
|
|
|
path.AmountIn = (*hexutil.Big)(new(big.Int).Set(rest))
|
|
|
|
}
|
|
|
|
rest.Sub(rest, path.AmountIn.ToInt())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &SuggestedRoutes{
|
|
|
|
Candidates: candidates,
|
|
|
|
Best: best,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-16 08:55:46 +00:00
|
|
|
func NewRouter(rpcClient *rpc.Client, transactor *transactions.Transactor, tokenManager *token.Manager, marketManager *market.Manager,
|
2024-06-12 20:13:16 +00:00
|
|
|
collectibles *collectibles.Service, collectiblesManager *collectibles.Manager, ensService *ens.Service, stickersService *stickers.Service,
|
|
|
|
featureFlags *protocolCommon.FeatureFlags) *Router {
|
2024-06-06 20:08:25 +00:00
|
|
|
processors := make(map[string]pathprocessor.PathProcessor)
|
|
|
|
transfer := pathprocessor.NewTransferProcessor(rpcClient, transactor)
|
|
|
|
erc721Transfer := pathprocessor.NewERC721Processor(rpcClient, transactor)
|
|
|
|
erc1155Transfer := pathprocessor.NewERC1155Processor(rpcClient, transactor)
|
|
|
|
hop := pathprocessor.NewHopBridgeProcessor(rpcClient, transactor, tokenManager)
|
|
|
|
paraswap := pathprocessor.NewSwapParaswapProcessor(rpcClient, transactor, tokenManager)
|
|
|
|
ensRegister := pathprocessor.NewENSRegisterProcessor(rpcClient, transactor, ensService)
|
|
|
|
ensRelease := pathprocessor.NewENSReleaseProcessor(rpcClient, transactor, ensService)
|
|
|
|
ensPublicKey := pathprocessor.NewENSPublicKeyProcessor(rpcClient, transactor, ensService)
|
2024-06-11 08:00:17 +00:00
|
|
|
buyStickers := pathprocessor.NewStickersBuyProcessor(rpcClient, transactor, stickersService)
|
2024-06-06 20:08:25 +00:00
|
|
|
|
|
|
|
processors[transfer.Name()] = transfer
|
|
|
|
processors[erc721Transfer.Name()] = erc721Transfer
|
|
|
|
processors[hop.Name()] = hop
|
|
|
|
processors[erc1155Transfer.Name()] = erc1155Transfer
|
|
|
|
processors[paraswap.Name()] = paraswap
|
|
|
|
processors[ensRegister.Name()] = ensRegister
|
|
|
|
processors[ensRelease.Name()] = ensRelease
|
|
|
|
processors[ensPublicKey.Name()] = ensPublicKey
|
2024-06-11 08:00:17 +00:00
|
|
|
processors[buyStickers.Name()] = buyStickers
|
2022-09-13 07:10:59 +00:00
|
|
|
|
2024-06-12 20:13:16 +00:00
|
|
|
if featureFlags.EnableCelerBridge {
|
|
|
|
// TODO: Celar Bridge is out of scope for 2.30, check it thoroughly once we decide to include it again
|
|
|
|
cbridge := pathprocessor.NewCelerBridgeProcessor(rpcClient, transactor, tokenManager)
|
|
|
|
processors[cbridge.Name()] = cbridge
|
|
|
|
}
|
|
|
|
|
2024-05-16 08:55:46 +00:00
|
|
|
return &Router{
|
|
|
|
rpcClient: rpcClient,
|
|
|
|
tokenManager: tokenManager,
|
|
|
|
marketManager: marketManager,
|
|
|
|
collectiblesService: collectibles,
|
|
|
|
collectiblesManager: collectiblesManager,
|
|
|
|
ensService: ensService,
|
|
|
|
stickersService: stickersService,
|
|
|
|
feesManager: &FeeManager{rpcClient},
|
2024-06-06 20:08:25 +00:00
|
|
|
pathProcessors: processors,
|
2024-06-12 20:13:16 +00:00
|
|
|
featureFlags: featureFlags,
|
2024-05-16 08:55:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Router) GetFeesManager() *FeeManager {
|
|
|
|
return r.feesManager
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (r *Router) GetPathProcessors() map[string]pathprocessor.PathProcessor {
|
|
|
|
return r.pathProcessors
|
2022-09-13 07:10:59 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 14:25:08 +00:00
|
|
|
func containsNetworkChainID(network *params.Network, chainIDs []uint64) bool {
|
2022-09-13 07:10:59 +00:00
|
|
|
for _, chainID := range chainIDs {
|
|
|
|
if chainID == network.ChainID {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
type Router struct {
|
2024-05-16 08:55:46 +00:00
|
|
|
rpcClient *rpc.Client
|
|
|
|
tokenManager *token.Manager
|
|
|
|
marketManager *market.Manager
|
|
|
|
collectiblesService *collectibles.Service
|
|
|
|
collectiblesManager *collectibles.Manager
|
|
|
|
ensService *ens.Service
|
|
|
|
stickersService *stickers.Service
|
|
|
|
feesManager *FeeManager
|
2024-06-06 20:08:25 +00:00
|
|
|
pathProcessors map[string]pathprocessor.PathProcessor
|
2024-06-12 20:13:16 +00:00
|
|
|
featureFlags *protocolCommon.FeatureFlags
|
2022-12-19 12:37:37 +00:00
|
|
|
}
|
|
|
|
|
2024-04-01 13:39:17 +00:00
|
|
|
func (r *Router) requireApproval(ctx context.Context, sendType SendType, approvalContractAddress *common.Address, account common.Address, network *params.Network, token *token.Token, amountIn *big.Int) (
|
|
|
|
bool, *big.Int, uint64, uint64, error) {
|
2024-06-11 08:00:17 +00:00
|
|
|
if sendType.IsCollectiblesTransfer() || sendType.IsEnsTransfer() || sendType.IsStickersTransfer() {
|
2024-04-01 13:39:17 +00:00
|
|
|
return false, nil, 0, 0, nil
|
2023-08-24 08:45:14 +00:00
|
|
|
}
|
|
|
|
|
2022-12-19 12:37:37 +00:00
|
|
|
if token.IsNative() {
|
2024-04-01 13:39:17 +00:00
|
|
|
return false, nil, 0, 0, nil
|
2022-12-19 12:37:37 +00:00
|
|
|
}
|
2023-08-29 12:59:37 +00:00
|
|
|
contractMaker, err := contracts.NewContractMaker(r.rpcClient)
|
|
|
|
if err != nil {
|
2024-04-01 13:39:17 +00:00
|
|
|
return false, nil, 0, 0, err
|
2022-12-19 12:37:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
contract, err := contractMaker.NewERC20(network.ChainID, token.Address)
|
|
|
|
if err != nil {
|
2024-04-01 13:39:17 +00:00
|
|
|
return false, nil, 0, 0, err
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
if approvalContractAddress == nil || *approvalContractAddress == pathprocessor.ZeroAddress {
|
2024-04-01 13:39:17 +00:00
|
|
|
return false, nil, 0, 0, nil
|
2022-12-19 12:37:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
allowance, err := contract.Allowance(&bind.CallOpts{
|
|
|
|
Context: ctx,
|
2024-04-01 13:39:17 +00:00
|
|
|
}, account, *approvalContractAddress)
|
2022-12-19 12:37:37 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2024-04-01 13:39:17 +00:00
|
|
|
return false, nil, 0, 0, err
|
2022-12-19 12:37:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if allowance.Cmp(amountIn) >= 0 {
|
2024-04-01 13:39:17 +00:00
|
|
|
return false, nil, 0, 0, nil
|
2022-12-19 12:37:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ethClient, err := r.rpcClient.EthClient(network.ChainID)
|
|
|
|
if err != nil {
|
2024-04-01 13:39:17 +00:00
|
|
|
return false, nil, 0, 0, err
|
2022-12-19 12:37:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
erc20ABI, err := abi.JSON(strings.NewReader(ierc20.IERC20ABI))
|
|
|
|
if err != nil {
|
2024-04-01 13:39:17 +00:00
|
|
|
return false, nil, 0, 0, err
|
2022-12-19 12:37:37 +00:00
|
|
|
}
|
|
|
|
|
2024-04-01 13:39:17 +00:00
|
|
|
data, err := erc20ABI.Pack("approve", approvalContractAddress, amountIn)
|
2022-12-19 12:37:37 +00:00
|
|
|
if err != nil {
|
2024-04-01 13:39:17 +00:00
|
|
|
return false, nil, 0, 0, err
|
2022-12-19 12:37:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
estimate, err := ethClient.EstimateGas(context.Background(), ethereum.CallMsg{
|
|
|
|
From: account,
|
|
|
|
To: &token.Address,
|
2024-06-06 20:08:25 +00:00
|
|
|
Value: pathprocessor.ZeroBigIntValue,
|
2022-12-19 12:37:37 +00:00
|
|
|
Data: data,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-04-01 13:39:17 +00:00
|
|
|
return false, nil, 0, 0, err
|
2022-12-19 12:37:37 +00:00
|
|
|
}
|
|
|
|
|
2024-03-25 12:40:00 +00:00
|
|
|
// fetching l1 fee
|
2024-04-01 13:39:17 +00:00
|
|
|
var l1Fee uint64
|
2024-03-25 12:40:00 +00:00
|
|
|
oracleContractAddress, err := gaspriceoracle.ContractAddress(network.ChainID)
|
2024-04-01 13:39:17 +00:00
|
|
|
if err == nil {
|
|
|
|
oracleContract, err := gaspriceoracle.NewGaspriceoracleCaller(oracleContractAddress, ethClient)
|
|
|
|
if err != nil {
|
|
|
|
return false, nil, 0, 0, err
|
|
|
|
}
|
2024-03-25 12:40:00 +00:00
|
|
|
|
2024-04-01 13:39:17 +00:00
|
|
|
callOpt := &bind.CallOpts{}
|
2022-12-19 12:37:37 +00:00
|
|
|
|
2024-04-01 13:39:17 +00:00
|
|
|
l1FeeResult, _ := oracleContract.GetL1Fee(callOpt, data)
|
|
|
|
l1Fee = l1FeeResult.Uint64()
|
|
|
|
}
|
2024-03-25 12:40:00 +00:00
|
|
|
|
2024-04-01 13:39:17 +00:00
|
|
|
return true, amountIn, estimate, l1Fee, nil
|
2022-09-13 07:10:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Router) getBalance(ctx context.Context, network *params.Network, token *token.Token, account common.Address) (*big.Int, error) {
|
2024-05-16 08:55:46 +00:00
|
|
|
client, err := r.rpcClient.EthClient(network.ChainID)
|
2022-09-13 07:10:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-16 08:55:46 +00:00
|
|
|
return r.tokenManager.GetBalance(ctx, client, account, token.Address)
|
2022-09-13 07:10:59 +00:00
|
|
|
}
|
|
|
|
|
2024-02-06 11:36:25 +00:00
|
|
|
func (r *Router) getERC1155Balance(ctx context.Context, network *params.Network, token *token.Token, account common.Address) (*big.Int, error) {
|
|
|
|
tokenID, success := new(big.Int).SetString(token.Symbol, 10)
|
|
|
|
if !success {
|
|
|
|
return nil, errors.New("failed to convert token symbol to big.Int")
|
|
|
|
}
|
|
|
|
|
2024-05-16 08:55:46 +00:00
|
|
|
balances, err := r.collectiblesManager.FetchERC1155Balances(
|
2024-03-05 18:57:02 +00:00
|
|
|
ctx,
|
|
|
|
account,
|
|
|
|
walletCommon.ChainID(network.ChainID),
|
|
|
|
token.Address,
|
|
|
|
[]*bigint.BigInt{&bigint.BigInt{Int: tokenID}},
|
|
|
|
)
|
2024-02-06 11:36:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-05 18:57:02 +00:00
|
|
|
if len(balances) != 1 || balances[0] == nil {
|
|
|
|
return nil, errors.New("invalid ERC1155 balance fetch response")
|
|
|
|
}
|
|
|
|
|
|
|
|
return balances[0].Int, nil
|
2024-02-06 11:36:25 +00:00
|
|
|
}
|
|
|
|
|
2024-05-16 08:55:46 +00:00
|
|
|
func (r *Router) SuggestedRoutes(
|
2022-11-23 17:49:23 +00:00
|
|
|
ctx context.Context,
|
|
|
|
sendType SendType,
|
2023-11-02 11:35:28 +00:00
|
|
|
addrFrom common.Address,
|
|
|
|
addrTo common.Address,
|
2022-11-23 17:49:23 +00:00
|
|
|
amountIn *big.Int,
|
2023-08-24 08:45:14 +00:00
|
|
|
tokenID string,
|
2024-04-01 13:39:17 +00:00
|
|
|
toTokenID string,
|
2022-11-23 17:49:23 +00:00
|
|
|
disabledFromChainIDs,
|
|
|
|
disabledToChaindIDs,
|
|
|
|
preferedChainIDs []uint64,
|
|
|
|
gasFeeMode GasFeeMode,
|
|
|
|
fromLockedAmount map[uint64]*hexutil.Big,
|
2024-05-16 08:55:46 +00:00
|
|
|
testnetMode bool,
|
2022-11-23 17:49:23 +00:00
|
|
|
) (*SuggestedRoutes, error) {
|
2022-06-09 13:09:56 +00:00
|
|
|
|
2024-05-16 08:55:46 +00:00
|
|
|
networks, err := r.rpcClient.NetworkManager.Get(false)
|
2022-06-09 13:09:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-16 08:55:46 +00:00
|
|
|
prices, err := sendType.FetchPrices(r.marketManager, tokenID)
|
2022-09-13 07:10:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-06-09 13:09:56 +00:00
|
|
|
var (
|
|
|
|
group = async.NewAtomicGroup(ctx)
|
|
|
|
mu sync.Mutex
|
2022-09-13 07:10:59 +00:00
|
|
|
candidates = make([]*Path, 0)
|
2022-06-09 13:09:56 +00:00
|
|
|
)
|
2024-04-01 13:39:17 +00:00
|
|
|
|
2022-06-09 13:09:56 +00:00
|
|
|
for networkIdx := range networks {
|
|
|
|
network := networks[networkIdx]
|
2024-05-16 08:55:46 +00:00
|
|
|
if network.IsTest != testnetMode {
|
2022-06-09 13:09:56 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-09-12 13:54:32 +00:00
|
|
|
|
2022-10-25 14:25:08 +00:00
|
|
|
if containsNetworkChainID(network, disabledFromChainIDs) {
|
2022-09-13 07:10:59 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !sendType.isAvailableFor(network) {
|
|
|
|
continue
|
2022-07-14 14:22:42 +00:00
|
|
|
}
|
2023-08-24 08:45:14 +00:00
|
|
|
|
2024-05-16 08:55:46 +00:00
|
|
|
token := sendType.FindToken(r.tokenManager, r.collectiblesService, addrFrom, network, tokenID)
|
2022-09-13 07:10:59 +00:00
|
|
|
if token == nil {
|
|
|
|
continue
|
|
|
|
}
|
2023-09-12 13:54:32 +00:00
|
|
|
|
2024-04-01 13:39:17 +00:00
|
|
|
var toToken *walletToken.Token
|
|
|
|
if sendType == Swap {
|
2024-05-16 08:55:46 +00:00
|
|
|
toToken = sendType.FindToken(r.tokenManager, r.collectiblesService, common.Address{}, network, toTokenID)
|
2024-04-01 13:39:17 +00:00
|
|
|
}
|
|
|
|
|
2024-05-16 08:55:46 +00:00
|
|
|
nativeToken := r.tokenManager.FindToken(network, network.NativeCurrencySymbol)
|
2022-09-13 07:10:59 +00:00
|
|
|
if nativeToken == nil {
|
2022-07-14 14:22:42 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-09-12 13:54:32 +00:00
|
|
|
|
2022-06-09 13:09:56 +00:00
|
|
|
group.Add(func(c context.Context) error {
|
2024-05-16 08:55:46 +00:00
|
|
|
gasFees, err := r.feesManager.SuggestedFees(ctx, network.ChainID)
|
2022-09-13 07:10:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-06-09 13:09:56 +00:00
|
|
|
}
|
|
|
|
|
2023-08-24 08:45:14 +00:00
|
|
|
// Default value is 1 as in case of erc721 as we built the token we are sure the account owns it
|
|
|
|
balance := big.NewInt(1)
|
2024-02-06 11:36:25 +00:00
|
|
|
if sendType == ERC1155Transfer {
|
|
|
|
balance, err = r.getERC1155Balance(ctx, network, token, addrFrom)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if sendType != ERC721Transfer {
|
2023-11-02 11:35:28 +00:00
|
|
|
balance, err = r.getBalance(ctx, network, token, addrFrom)
|
2023-08-24 08:45:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-13 07:10:59 +00:00
|
|
|
}
|
2022-06-09 13:09:56 +00:00
|
|
|
|
2022-11-23 17:49:23 +00:00
|
|
|
maxAmountIn := (*hexutil.Big)(balance)
|
|
|
|
if amount, ok := fromLockedAmount[network.ChainID]; ok {
|
|
|
|
if amount.ToInt().Cmp(balance) == 1 {
|
|
|
|
return errors.New("locked amount cannot be bigger than balance")
|
|
|
|
}
|
|
|
|
maxAmountIn = amount
|
|
|
|
}
|
|
|
|
|
2023-11-02 11:35:28 +00:00
|
|
|
nativeBalance, err := r.getBalance(ctx, network, nativeToken, addrFrom)
|
2022-09-13 07:10:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-06-09 13:09:56 +00:00
|
|
|
}
|
2022-09-13 07:10:59 +00:00
|
|
|
maxFees := gasFees.feeFor(gasFeeMode)
|
|
|
|
|
2024-05-16 08:55:46 +00:00
|
|
|
estimatedTime := r.feesManager.TransactionEstimatedTime(ctx, network.ChainID, maxFees)
|
2024-06-06 20:08:25 +00:00
|
|
|
for _, pProcessor := range r.pathProcessors {
|
|
|
|
// Skip processors that are added because of the Router V2, to not break the current functionality
|
|
|
|
if pProcessor.Name() == pathprocessor.ProcessorENSRegisterName ||
|
|
|
|
pProcessor.Name() == pathprocessor.ProcessorENSReleaseName ||
|
2024-06-11 08:00:17 +00:00
|
|
|
pProcessor.Name() == pathprocessor.ProcessorENSPublicKeyName ||
|
|
|
|
pProcessor.Name() == pathprocessor.ProcessorStickersBuyName {
|
2024-06-05 07:56:02 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
if !sendType.canUseProcessor(pProcessor) {
|
2023-08-24 08:45:14 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-09-13 07:10:59 +00:00
|
|
|
for _, dest := range networks {
|
2024-05-16 08:55:46 +00:00
|
|
|
if dest.IsTest != testnetMode {
|
2022-09-13 07:10:59 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !sendType.isAvailableFor(network) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-05-16 10:22:32 +00:00
|
|
|
if !sendType.isAvailableBetween(network, dest) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-12-01 12:41:24 +00:00
|
|
|
if len(preferedChainIDs) > 0 && !containsNetworkChainID(dest, preferedChainIDs) {
|
2022-09-13 07:10:59 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-10-25 14:25:08 +00:00
|
|
|
if containsNetworkChainID(dest, disabledToChaindIDs) {
|
2022-09-13 07:10:59 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-06-12 20:14:30 +00:00
|
|
|
processorInputParams := pathprocessor.ProcessorInputParams{
|
2024-06-05 07:56:02 +00:00
|
|
|
FromChain: network,
|
|
|
|
ToChain: dest,
|
|
|
|
FromToken: token,
|
|
|
|
ToToken: toToken,
|
|
|
|
ToAddr: addrTo,
|
|
|
|
FromAddr: addrFrom,
|
|
|
|
AmountIn: amountIn,
|
|
|
|
}
|
|
|
|
|
2024-06-12 20:14:30 +00:00
|
|
|
can, err := pProcessor.AvailableFor(processorInputParams)
|
2022-09-13 07:10:59 +00:00
|
|
|
if err != nil || !can {
|
|
|
|
continue
|
|
|
|
}
|
2024-06-06 20:08:25 +00:00
|
|
|
if maxAmountIn.ToInt().Cmp(pathprocessor.ZeroBigIntValue) == 0 {
|
2024-05-14 19:11:16 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-06-12 20:14:30 +00:00
|
|
|
bonderFees, tokenFees, err := pProcessor.CalculateFees(processorInputParams)
|
2022-09-13 07:10:59 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2024-06-06 20:08:25 +00:00
|
|
|
if bonderFees.Cmp(pathprocessor.ZeroBigIntValue) != 0 {
|
2023-10-26 07:47:16 +00:00
|
|
|
if maxAmountIn.ToInt().Cmp(amountIn) >= 0 {
|
|
|
|
if bonderFees.Cmp(amountIn) >= 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if bonderFees.Cmp(maxAmountIn.ToInt()) >= 0 {
|
|
|
|
continue
|
|
|
|
}
|
2023-07-04 10:01:09 +00:00
|
|
|
}
|
2023-01-20 09:25:49 +00:00
|
|
|
}
|
2022-09-13 07:10:59 +00:00
|
|
|
gasLimit := uint64(0)
|
2024-05-21 14:33:36 +00:00
|
|
|
if sendType.isTransfer(false) {
|
2024-06-12 20:14:30 +00:00
|
|
|
gasLimit, err = pProcessor.EstimateGas(processorInputParams)
|
2022-09-13 07:10:59 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else {
|
2024-05-16 08:55:46 +00:00
|
|
|
gasLimit = sendType.EstimateGas(r.ensService, r.stickersService, network, addrFrom, tokenID)
|
2022-09-13 07:10:59 +00:00
|
|
|
}
|
2023-11-16 10:12:01 +00:00
|
|
|
|
2024-06-12 20:14:30 +00:00
|
|
|
approvalContractAddress, err := pProcessor.GetContractAddress(processorInputParams)
|
2024-05-23 12:38:39 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
approvalRequired, approvalAmountRequired, approvalGasLimit, l1ApprovalFee, err := r.requireApproval(ctx, sendType, &approvalContractAddress, addrFrom, network, token, amountIn)
|
2024-03-25 12:40:00 +00:00
|
|
|
if err != nil {
|
2022-09-13 07:10:59 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-03-25 12:40:00 +00:00
|
|
|
|
2024-04-01 13:39:17 +00:00
|
|
|
var l1GasFeeWei uint64
|
2024-04-04 09:32:22 +00:00
|
|
|
if sendType.needL1Fee() {
|
2024-06-12 20:14:30 +00:00
|
|
|
txInputData, err := pProcessor.PackTxInputData(processorInputParams)
|
2024-04-04 09:32:22 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2024-03-25 12:40:00 +00:00
|
|
|
|
2024-05-21 14:33:36 +00:00
|
|
|
l1GasFeeWei, _ = r.feesManager.GetL1Fee(ctx, network.ChainID, txInputData)
|
2024-04-04 09:32:22 +00:00
|
|
|
l1GasFeeWei += l1ApprovalFee
|
|
|
|
}
|
2024-04-01 13:39:17 +00:00
|
|
|
|
2024-03-25 12:40:00 +00:00
|
|
|
gasFees.L1GasFee = weiToGwei(big.NewInt(int64(l1GasFeeWei)))
|
|
|
|
|
|
|
|
requiredNativeBalance := new(big.Int).Mul(gweiToWei(maxFees), big.NewInt(int64(gasLimit)))
|
|
|
|
requiredNativeBalance.Add(requiredNativeBalance, new(big.Int).Mul(gweiToWei(maxFees), big.NewInt(int64(approvalGasLimit))))
|
|
|
|
requiredNativeBalance.Add(requiredNativeBalance, big.NewInt(int64(l1GasFeeWei))) // add l1Fee to requiredNativeBalance, in case of L1 chain l1Fee is 0
|
|
|
|
|
|
|
|
if nativeBalance.Cmp(requiredNativeBalance) <= 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removed the required fees from maxAMount in case of native token tx
|
|
|
|
if token.IsNative() {
|
|
|
|
maxAmountIn = (*hexutil.Big)(new(big.Int).Sub(maxAmountIn.ToInt(), requiredNativeBalance))
|
|
|
|
}
|
|
|
|
|
|
|
|
ethPrice := big.NewFloat(prices["ETH"])
|
|
|
|
|
2022-12-19 12:37:37 +00:00
|
|
|
approvalGasFees := new(big.Float).Mul(gweiToEth(maxFees), big.NewFloat((float64(approvalGasLimit))))
|
|
|
|
|
|
|
|
approvalGasCost := new(big.Float)
|
2024-03-25 12:40:00 +00:00
|
|
|
approvalGasCost.Mul(approvalGasFees, ethPrice)
|
|
|
|
|
|
|
|
l1GasCost := new(big.Float)
|
|
|
|
l1GasCost.Mul(gasFees.L1GasFee, ethPrice)
|
2022-12-19 12:37:37 +00:00
|
|
|
|
2022-09-13 07:10:59 +00:00
|
|
|
gasCost := new(big.Float)
|
2024-03-25 12:40:00 +00:00
|
|
|
gasCost.Mul(new(big.Float).Mul(gweiToEth(maxFees), big.NewFloat(float64(gasLimit))), ethPrice)
|
2022-12-19 12:37:37 +00:00
|
|
|
|
2022-09-13 07:10:59 +00:00
|
|
|
tokenFeesAsFloat := new(big.Float).Quo(
|
|
|
|
new(big.Float).SetInt(tokenFees),
|
|
|
|
big.NewFloat(math.Pow(10, float64(token.Decimals))),
|
|
|
|
)
|
|
|
|
tokenCost := new(big.Float)
|
2023-08-24 08:45:14 +00:00
|
|
|
tokenCost.Mul(tokenFeesAsFloat, big.NewFloat(prices[tokenID]))
|
2022-12-19 12:37:37 +00:00
|
|
|
|
2022-09-13 07:10:59 +00:00
|
|
|
cost := new(big.Float)
|
|
|
|
cost.Add(tokenCost, gasCost)
|
2022-12-19 12:37:37 +00:00
|
|
|
cost.Add(cost, approvalGasCost)
|
2024-03-25 12:40:00 +00:00
|
|
|
cost.Add(cost, l1GasCost)
|
2022-06-09 13:09:56 +00:00
|
|
|
mu.Lock()
|
2022-09-13 07:10:59 +00:00
|
|
|
candidates = append(candidates, &Path{
|
2024-06-06 20:08:25 +00:00
|
|
|
BridgeName: pProcessor.Name(),
|
2022-12-19 12:37:37 +00:00
|
|
|
From: network,
|
|
|
|
To: dest,
|
|
|
|
MaxAmountIn: maxAmountIn,
|
2024-06-06 20:08:25 +00:00
|
|
|
AmountIn: (*hexutil.Big)(pathprocessor.ZeroBigIntValue),
|
|
|
|
AmountOut: (*hexutil.Big)(pathprocessor.ZeroBigIntValue),
|
2022-12-19 12:37:37 +00:00
|
|
|
GasAmount: gasLimit,
|
|
|
|
GasFees: gasFees,
|
|
|
|
BonderFees: (*hexutil.Big)(bonderFees),
|
|
|
|
TokenFees: tokenFeesAsFloat,
|
|
|
|
Cost: cost,
|
|
|
|
EstimatedTime: estimatedTime,
|
|
|
|
ApprovalRequired: approvalRequired,
|
|
|
|
ApprovalGasFees: approvalGasFees,
|
|
|
|
ApprovalAmountRequired: (*hexutil.Big)(approvalAmountRequired),
|
2024-05-23 12:38:39 +00:00
|
|
|
ApprovalContractAddress: &approvalContractAddress,
|
2022-09-13 07:10:59 +00:00
|
|
|
})
|
2022-06-09 13:09:56 +00:00
|
|
|
mu.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
group.Wait()
|
2022-09-13 07:10:59 +00:00
|
|
|
|
2022-11-23 17:49:23 +00:00
|
|
|
suggestedRoutes := newSuggestedRoutes(amountIn, candidates, fromLockedAmount)
|
2023-08-24 08:45:14 +00:00
|
|
|
suggestedRoutes.TokenPrice = prices[tokenID]
|
2022-09-13 07:10:59 +00:00
|
|
|
suggestedRoutes.NativeChainTokenPrice = prices["ETH"]
|
|
|
|
for _, path := range suggestedRoutes.Best {
|
2024-06-12 20:14:30 +00:00
|
|
|
processorInputParams := pathprocessor.ProcessorInputParams{
|
2024-06-05 07:56:02 +00:00
|
|
|
FromChain: path.From,
|
|
|
|
ToChain: path.To,
|
|
|
|
AmountIn: path.AmountIn.ToInt(),
|
|
|
|
FromToken: &token.Token{
|
|
|
|
Symbol: tokenID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-06-12 20:14:30 +00:00
|
|
|
amountOut, err := r.pathProcessors[path.BridgeName].CalculateAmountOut(processorInputParams)
|
2022-09-13 07:10:59 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
path.AmountOut = (*hexutil.Big)(amountOut)
|
|
|
|
}
|
2023-08-24 08:45:14 +00:00
|
|
|
|
2022-09-13 07:10:59 +00:00
|
|
|
return suggestedRoutes, nil
|
2022-06-09 13:09:56 +00:00
|
|
|
}
|