From 89104b3027549c150237b345e9c8fbade9bb9604 Mon Sep 17 00:00:00 2001 From: Dario Gabriel Lipicar Date: Wed, 30 Oct 2024 19:43:46 -0300 Subject: [PATCH] fix(wallet)_: properly use amount in erc1155 transfers --- .../router/pathprocessor/processor_erc1155.go | 51 +------------ .../router/pathprocessor/processor_erc721.go | 73 +------------------ .../transfer/transaction_manager_route.go | 4 +- 3 files changed, 5 insertions(+), 123 deletions(-) diff --git a/services/wallet/router/pathprocessor/processor_erc1155.go b/services/wallet/router/pathprocessor/processor_erc1155.go index 9d3e06326..68531c93f 100644 --- a/services/wallet/router/pathprocessor/processor_erc1155.go +++ b/services/wallet/router/pathprocessor/processor_erc1155.go @@ -151,54 +151,6 @@ func (s *ERC1155Processor) sendOrBuild(sendArgs *MultipathProcessorTxArgs, signe return tx, nil } -func (s *ERC1155Processor) sendOrBuildV2(sendArgs *transactions.SendTxArgs, signerFn bind.SignerFn, lastUsedNonce int64) (tx *ethTypes.Transaction, err error) { - ethClient, err := s.rpcClient.EthClient(sendArgs.FromChainID) - if err != nil { - return tx, createERC1155ErrorResponse(err) - } - - contract, err := ierc1155.NewIerc1155(common.Address(sendArgs.ToContractAddress), ethClient) - if err != nil { - return tx, createERC1155ErrorResponse(err) - } - - id, err := walletCommon.GetTokenIdFromSymbol(sendArgs.FromTokenID) - if err != nil { - return tx, createERC1155ErrorResponse(err) - } - - var nonce uint64 - if lastUsedNonce < 0 { - nonce, err = s.transactor.NextNonce(s.rpcClient, sendArgs.FromChainID, sendArgs.From) - if err != nil { - return tx, createERC1155ErrorResponse(err) - } - } else { - nonce = uint64(lastUsedNonce) + 1 - } - - argNonce := hexutil.Uint64(nonce) - sendArgs.Nonce = &argNonce - txOpts := sendArgs.ToTransactOpts(signerFn) - from := common.Address(sendArgs.From) - tx, err = contract.SafeTransferFrom( - txOpts, - from, - common.Address(*sendArgs.To), - id, - sendArgs.Value.ToInt(), - []byte{}, - ) - if err != nil { - return tx, createERC1155ErrorResponse(err) - } - err = s.transactor.StoreAndTrackPendingTx(from, sendArgs.FromTokenID, sendArgs.FromChainID, sendArgs.MultiTransactionID, tx) - if err != nil { - return tx, createERC1155ErrorResponse(err) - } - return tx, nil -} - func (s *ERC1155Processor) Send(sendArgs *MultipathProcessorTxArgs, lastUsedNonce int64, verifiedAccount *account.SelectedExtKey) (hash types.Hash, usedNonce uint64, err error) { tx, err := s.sendOrBuild(sendArgs, getSigner(sendArgs.ChainID, sendArgs.ERC1155TransferTx.From, verifiedAccount), lastUsedNonce) if err != nil { @@ -213,8 +165,7 @@ func (s *ERC1155Processor) BuildTransaction(sendArgs *MultipathProcessorTxArgs, } func (s *ERC1155Processor) BuildTransactionV2(sendArgs *transactions.SendTxArgs, lastUsedNonce int64) (*ethTypes.Transaction, uint64, error) { - tx, err := s.sendOrBuildV2(sendArgs, nil, lastUsedNonce) - return tx, tx.Nonce(), err + return s.transactor.ValidateAndBuildTransaction(sendArgs.FromChainID, *sendArgs, lastUsedNonce) } func (s *ERC1155Processor) CalculateAmountOut(params ProcessorInputParams) (*big.Int, error) { diff --git a/services/wallet/router/pathprocessor/processor_erc721.go b/services/wallet/router/pathprocessor/processor_erc721.go index d4124fc28..ff46911c3 100644 --- a/services/wallet/router/pathprocessor/processor_erc721.go +++ b/services/wallet/router/pathprocessor/processor_erc721.go @@ -209,76 +209,6 @@ func (s *ERC721Processor) sendOrBuild(sendArgs *MultipathProcessorTxArgs, signer return tx, nil } -func (s *ERC721Processor) sendOrBuildV2(sendArgs *transactions.SendTxArgs, signerFn bind.SignerFn, lastUsedNonce int64) (tx *ethTypes.Transaction, err error) { - from := common.Address(sendArgs.From) - to := common.Address(*sendArgs.To) - - useSafeTransferFrom := true - inputParams := ProcessorInputParams{ - FromChain: ¶ms.Network{ - ChainID: sendArgs.FromChainID, - }, - FromAddr: from, - ToAddr: to, - FromToken: &token.Token{ - Symbol: sendArgs.FromTokenID, - Address: common.Address(sendArgs.ToContractAddress), - }, - } - err = s.checkIfFunctionExists(inputParams, functionNameSafeTransferFrom) - if err != nil { - useSafeTransferFrom = false - } - - ethClient, err := s.rpcClient.EthClient(sendArgs.FromChainID) - if err != nil { - return tx, createERC721ErrorResponse(err) - } - - contract, err := collectibles.NewCollectibles(common.Address(sendArgs.ToContractAddress), ethClient) - if err != nil { - return tx, createERC721ErrorResponse(err) - } - - id, err := walletCommon.GetTokenIdFromSymbol(sendArgs.FromTokenID) - if err != nil { - return tx, createERC1155ErrorResponse(err) - } - - var nonce uint64 - if lastUsedNonce < 0 { - nonce, err = s.transactor.NextNonce(s.rpcClient, sendArgs.FromChainID, sendArgs.From) - if err != nil { - return tx, createERC721ErrorResponse(err) - } - } else { - nonce = uint64(lastUsedNonce) + 1 - } - - argNonce := hexutil.Uint64(nonce) - sendArgs.Nonce = &argNonce - txOpts := sendArgs.ToTransactOpts(signerFn) - if useSafeTransferFrom { - tx, err = contract.SafeTransferFrom(txOpts, - from, - to, - id) - } else { - tx, err = contract.TransferFrom(txOpts, - from, - to, - id) - } - if err != nil { - return tx, createERC721ErrorResponse(err) - } - err = s.transactor.StoreAndTrackPendingTx(from, sendArgs.FromTokenID, sendArgs.FromChainID, sendArgs.MultiTransactionID, tx) - if err != nil { - return tx, createERC721ErrorResponse(err) - } - return tx, nil -} - func (s *ERC721Processor) Send(sendArgs *MultipathProcessorTxArgs, lastUsedNonce int64, verifiedAccount *account.SelectedExtKey) (hash types.Hash, usedNonce uint64, err error) { tx, err := s.sendOrBuild(sendArgs, getSigner(sendArgs.ChainID, sendArgs.ERC721TransferTx.From, verifiedAccount), lastUsedNonce) if err != nil { @@ -293,8 +223,7 @@ func (s *ERC721Processor) BuildTransaction(sendArgs *MultipathProcessorTxArgs, l } func (s *ERC721Processor) BuildTransactionV2(sendArgs *transactions.SendTxArgs, lastUsedNonce int64) (*ethTypes.Transaction, uint64, error) { - tx, err := s.sendOrBuildV2(sendArgs, nil, lastUsedNonce) - return tx, tx.Nonce(), err + return s.transactor.ValidateAndBuildTransaction(sendArgs.FromChainID, *sendArgs, lastUsedNonce) } func (s *ERC721Processor) CalculateAmountOut(params ProcessorInputParams) (*big.Int, error) { diff --git a/services/wallet/transfer/transaction_manager_route.go b/services/wallet/transfer/transaction_manager_route.go index 44b99ba90..d0136ac22 100644 --- a/services/wallet/transfer/transaction_manager_route.go +++ b/services/wallet/transfer/transaction_manager_route.go @@ -171,7 +171,9 @@ func (tm *TransactionManager) buildTxForPath(path *routes.Path, pathProcessors m path.ProcessorName == pathprocessor.ProcessorStickersBuyName || path.ProcessorName == pathprocessor.ProcessorENSRegisterName || path.ProcessorName == pathprocessor.ProcessorENSReleaseName || - path.ProcessorName == pathprocessor.ProcessorENSPublicKeyName { + path.ProcessorName == pathprocessor.ProcessorENSPublicKeyName || + path.ProcessorName == pathprocessor.ProcessorERC721Name || + path.ProcessorName == pathprocessor.ProcessorERC1155Name { // TODO: update functions from `TransactorIface` to use `ToContractAddress` (as an address of the contract a transaction should be sent to) // and `To` (as the destination address, recipient) of `SendTxArgs` struct appropriately toContractAddr := types.Address(path.FromToken.Address)