From 89662aa3007a045321aab5a53f19272a484d025e Mon Sep 17 00:00:00 2001 From: saledjenic <86303051+saledjenic@users.noreply.github.com> Date: Mon, 19 Aug 2024 11:30:20 +0200 Subject: [PATCH] chore_: cherry-pick: missing statusgo changes for release (#5737) * fix_: when preparing a balance map skip chains where cannot get the balance, instead of returning the error * fix_: crash sending collectibles fixed When mapping `MultipathProcessorTxArgs` to `ProcessorInputParams` setting `FromChain` was missed. * fix_: crash when selected token cannot be found is fixed --- .../router/pathprocessor/processor_erc721.go | 4 ++ services/wallet/router/router_v2.go | 48 +++++++++++++------ 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/services/wallet/router/pathprocessor/processor_erc721.go b/services/wallet/router/pathprocessor/processor_erc721.go index d346d1a02..0f98d93da 100644 --- a/services/wallet/router/pathprocessor/processor_erc721.go +++ b/services/wallet/router/pathprocessor/processor_erc721.go @@ -16,6 +16,7 @@ import ( "github.com/status-im/status-go/contracts/community-tokens/collectibles" "github.com/status-im/status-go/contracts/erc721" "github.com/status-im/status-go/eth-node/types" + "github.com/status-im/status-go/params" "github.com/status-im/status-go/rpc" "github.com/status-im/status-go/services/wallet/token" "github.com/status-im/status-go/transactions" @@ -150,6 +151,9 @@ func (s *ERC721Processor) sendOrBuild(sendArgs *MultipathProcessorTxArgs, signer useSafeTransferFrom := true inputParams := ProcessorInputParams{ + FromChain: ¶ms.Network{ + ChainID: sendArgs.ChainID, + }, FromAddr: from, ToAddr: sendArgs.ERC721TransferTx.Recipient, FromToken: &token.Token{ diff --git a/services/wallet/router/router_v2.go b/services/wallet/router/router_v2.go index 20febdc25..c7f3a43bd 100644 --- a/services/wallet/router/router_v2.go +++ b/services/wallet/router/router_v2.go @@ -499,7 +499,8 @@ func (r *Router) SuggestedRoutesV2(ctx context.Context, input *RouteInputParams) } balanceMap, err := r.getBalanceMapForTokenOnChains(ctx, input, selectedFromChains) - if err != nil { + // return only if there are no balances, otherwise try to resolve the candidates for chains we know the balances for + if len(balanceMap) == 0 && err != nil { return nil, errors.CreateErrorResponseFromError(err) } @@ -541,38 +542,57 @@ func (r *Router) getBalanceMapForTokenOnChains(ctx context.Context, input *Route balanceMap = make(map[string]*big.Int) + chainError := func(chainId uint64, token string, intErr error) { + if err == nil { + err = fmt.Errorf("chain %d, token %s: %w", chainId, token, intErr) + } else { + err = fmt.Errorf("%s; chain %d, token %s: %w", err.Error(), chainId, token, intErr) + } + } + for _, chain := range selectedFromChains { + // check token existence token := input.SendType.FindToken(r.tokenManager, r.collectiblesService, input.AddrFrom, chain, input.TokenID) if token == nil { + chainError(chain.ChainID, input.TokenID, ErrTokenNotFound) + continue + } + // check native token existence + nativeToken := r.tokenManager.FindToken(chain, chain.NativeCurrencySymbol) + if nativeToken == nil { + chainError(chain.ChainID, chain.NativeCurrencySymbol, ErrNativeTokenNotFound) continue } // add token balance for the chain - tokenBalance := big.NewInt(1) - if input.SendType == ERC1155Transfer { + var tokenBalance *big.Int + if input.SendType == ERC721Transfer { + tokenBalance = big.NewInt(1) + } else if input.SendType == ERC1155Transfer { tokenBalance, err = r.getERC1155Balance(ctx, chain, token, input.AddrFrom) if err != nil { - return nil, errors.CreateErrorResponseFromError(err) + chainError(chain.ChainID, token.Symbol, errors.CreateErrorResponseFromError(err)) } - } else if input.SendType != ERC721Transfer { + } else { tokenBalance, err = r.getBalance(ctx, chain.ChainID, token, input.AddrFrom) if err != nil { - return nil, errors.CreateErrorResponseFromError(err) + chainError(chain.ChainID, token.Symbol, errors.CreateErrorResponseFromError(err)) } } - balanceMap[makeBalanceKey(chain.ChainID, token.Symbol)] = tokenBalance + // add only if balance is not nil + if tokenBalance != nil { + balanceMap[makeBalanceKey(chain.ChainID, token.Symbol)] = tokenBalance + } // add native token balance for the chain - nativeToken := r.tokenManager.FindToken(chain, chain.NativeCurrencySymbol) - if nativeToken == nil { - return nil, ErrNativeTokenNotFound - } - nativeBalance, err := r.getBalance(ctx, chain.ChainID, nativeToken, input.AddrFrom) if err != nil { - return nil, errors.CreateErrorResponseFromError(err) + chainError(chain.ChainID, token.Symbol, errors.CreateErrorResponseFromError(err)) + } + // add only if balance is not nil + if nativeBalance != nil { + balanceMap[makeBalanceKey(chain.ChainID, nativeToken.Symbol)] = nativeBalance } - balanceMap[makeBalanceKey(chain.ChainID, nativeToken.Symbol)] = nativeBalance } return