2024-09-23 07:19:00 +00:00
|
|
|
package transfer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
ethTypes "github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/status-im/status-go/errors"
|
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
walletCommon "github.com/status-im/status-go/services/wallet/common"
|
|
|
|
"github.com/status-im/status-go/services/wallet/responses"
|
|
|
|
"github.com/status-im/status-go/services/wallet/router/pathprocessor"
|
|
|
|
"github.com/status-im/status-go/services/wallet/router/routes"
|
|
|
|
"github.com/status-im/status-go/transactions"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BuildRouteExtraParams struct {
|
|
|
|
AddressFrom common.Address
|
|
|
|
AddressTo common.Address
|
|
|
|
Username string
|
|
|
|
PublicKey string
|
|
|
|
PackID *big.Int
|
|
|
|
SlippagePercentage float32
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tm *TransactionManager) ClearLocalRouterTransactionsData() {
|
|
|
|
tm.routerTransactions = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tm *TransactionManager) ApprovalRequiredForPath(pathProcessorName string) bool {
|
|
|
|
for _, desc := range tm.routerTransactions {
|
2024-10-16 05:09:56 +00:00
|
|
|
if desc.RouterPath.ProcessorName == pathProcessorName &&
|
|
|
|
desc.RouterPath.ApprovalRequired {
|
2024-09-23 07:19:00 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tm *TransactionManager) ApprovalPlacedForPath(pathProcessorName string) bool {
|
|
|
|
for _, desc := range tm.routerTransactions {
|
2024-10-16 05:09:56 +00:00
|
|
|
if desc.RouterPath.ProcessorName == pathProcessorName && desc.IsApprovalPlaced() {
|
2024-09-23 07:19:00 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tm *TransactionManager) TxPlacedForPath(pathProcessorName string) bool {
|
|
|
|
for _, desc := range tm.routerTransactions {
|
2024-10-16 05:09:56 +00:00
|
|
|
if desc.RouterPath.ProcessorName == pathProcessorName && desc.IsTxPlaced() {
|
2024-09-23 07:19:00 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-11-07 17:15:18 +00:00
|
|
|
func (tm *TransactionManager) getOrInitDetailsForPath(path *routes.Path) *RouterTransactionDetails {
|
|
|
|
for _, desc := range tm.routerTransactions {
|
|
|
|
if desc.RouterPath.ID() == path.ID() {
|
|
|
|
return desc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newDetails := &RouterTransactionDetails{
|
|
|
|
RouterPath: path,
|
|
|
|
}
|
|
|
|
tm.routerTransactions = append(tm.routerTransactions, newDetails)
|
|
|
|
|
|
|
|
return newDetails
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildApprovalTxForPath(transactor transactions.TransactorIface, path *routes.Path, addressFrom common.Address,
|
|
|
|
usedNonces map[uint64]int64, signer ethTypes.Signer) (*TransactionData, error) {
|
2024-09-23 07:19:00 +00:00
|
|
|
lastUsedNonce := int64(-1)
|
|
|
|
if nonce, ok := usedNonces[path.FromChain.ChainID]; ok {
|
|
|
|
lastUsedNonce = nonce
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := walletCommon.PackApprovalInputData(path.AmountIn.ToInt(), path.ApprovalContractAddress)
|
|
|
|
if err != nil {
|
2024-11-07 17:15:18 +00:00
|
|
|
return nil, err
|
2024-09-23 07:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addrTo := types.Address(path.FromToken.Address)
|
|
|
|
approavalSendArgs := &transactions.SendTxArgs{
|
|
|
|
Version: transactions.SendTxArgsVersion1,
|
|
|
|
|
|
|
|
// tx fields
|
|
|
|
From: types.Address(addressFrom),
|
|
|
|
To: &addrTo,
|
|
|
|
Value: (*hexutil.Big)(big.NewInt(0)),
|
|
|
|
Data: data,
|
|
|
|
Gas: (*hexutil.Uint64)(&path.ApprovalGasAmount),
|
|
|
|
MaxFeePerGas: path.MaxFeesPerGas,
|
|
|
|
MaxPriorityFeePerGas: path.ApprovalPriorityFee,
|
2024-10-28 12:27:39 +00:00
|
|
|
ValueOut: (*hexutil.Big)(big.NewInt(0)),
|
2024-09-23 07:19:00 +00:00
|
|
|
|
|
|
|
// additional fields version 1
|
|
|
|
FromChainID: path.FromChain.ChainID,
|
|
|
|
}
|
|
|
|
if path.FromToken != nil {
|
|
|
|
approavalSendArgs.FromTokenID = path.FromToken.Symbol
|
|
|
|
}
|
|
|
|
|
2024-11-07 17:15:18 +00:00
|
|
|
builtApprovalTx, usedNonce, err := transactor.ValidateAndBuildTransaction(approavalSendArgs.FromChainID, *approavalSendArgs, lastUsedNonce)
|
2024-09-23 07:19:00 +00:00
|
|
|
if err != nil {
|
2024-11-07 17:15:18 +00:00
|
|
|
return nil, err
|
2024-09-23 07:19:00 +00:00
|
|
|
}
|
|
|
|
approvalTxHash := signer.Hash(builtApprovalTx)
|
|
|
|
usedNonces[path.FromChain.ChainID] = int64(usedNonce)
|
|
|
|
|
2024-11-07 17:15:18 +00:00
|
|
|
return &TransactionData{
|
|
|
|
TxArgs: approavalSendArgs,
|
|
|
|
Tx: builtApprovalTx,
|
|
|
|
HashToSign: types.Hash(approvalTxHash),
|
|
|
|
}, nil
|
2024-09-23 07:19:00 +00:00
|
|
|
}
|
|
|
|
|
2024-11-07 17:15:18 +00:00
|
|
|
func buildTxForPath(transactor transactions.TransactorIface, path *routes.Path, pathProcessors map[string]pathprocessor.PathProcessor,
|
|
|
|
usedNonces map[uint64]int64, signer ethTypes.Signer, params BuildRouteExtraParams) (*TransactionData, error) {
|
2024-09-23 07:19:00 +00:00
|
|
|
lastUsedNonce := int64(-1)
|
|
|
|
if nonce, ok := usedNonces[path.FromChain.ChainID]; ok {
|
|
|
|
lastUsedNonce = nonce
|
|
|
|
}
|
|
|
|
|
|
|
|
processorInputParams := pathprocessor.ProcessorInputParams{
|
|
|
|
FromAddr: params.AddressFrom,
|
|
|
|
ToAddr: params.AddressTo,
|
|
|
|
FromChain: path.FromChain,
|
|
|
|
ToChain: path.ToChain,
|
|
|
|
FromToken: path.FromToken,
|
|
|
|
ToToken: path.ToToken,
|
|
|
|
AmountIn: path.AmountIn.ToInt(),
|
|
|
|
AmountOut: path.AmountOut.ToInt(),
|
|
|
|
|
|
|
|
Username: params.Username,
|
|
|
|
PublicKey: params.PublicKey,
|
|
|
|
PackID: params.PackID,
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := pathProcessors[path.ProcessorName].PackTxInputData(processorInputParams)
|
|
|
|
if err != nil {
|
2024-11-07 17:15:18 +00:00
|
|
|
return nil, err
|
2024-09-23 07:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addrTo := types.Address(params.AddressTo)
|
|
|
|
sendArgs := &transactions.SendTxArgs{
|
|
|
|
Version: transactions.SendTxArgsVersion1,
|
|
|
|
|
|
|
|
// tx fields
|
|
|
|
From: types.Address(params.AddressFrom),
|
|
|
|
To: &addrTo,
|
|
|
|
Value: path.AmountIn,
|
|
|
|
Data: data,
|
|
|
|
Gas: (*hexutil.Uint64)(&path.TxGasAmount),
|
|
|
|
MaxFeePerGas: path.MaxFeesPerGas,
|
|
|
|
MaxPriorityFeePerGas: path.TxPriorityFee,
|
|
|
|
|
|
|
|
// additional fields version 1
|
2024-11-07 13:16:00 +00:00
|
|
|
ValueIn: path.AmountIn,
|
2024-09-23 07:19:00 +00:00
|
|
|
ValueOut: path.AmountOut,
|
|
|
|
FromChainID: path.FromChain.ChainID,
|
|
|
|
ToChainID: path.ToChain.ChainID,
|
|
|
|
SlippagePercentage: params.SlippagePercentage,
|
|
|
|
}
|
|
|
|
if path.FromToken != nil {
|
|
|
|
sendArgs.FromTokenID = path.FromToken.Symbol
|
|
|
|
sendArgs.ToContractAddress = types.Address(path.FromToken.Address)
|
|
|
|
|
|
|
|
// special handling for transfer tx if selected token is not ETH
|
|
|
|
// TODO: we should fix that in the trasactor, but till then, the best place to handle it is here
|
|
|
|
if !path.FromToken.IsNative() {
|
|
|
|
sendArgs.Value = (*hexutil.Big)(big.NewInt(0))
|
|
|
|
|
|
|
|
if path.ProcessorName == pathprocessor.ProcessorTransferName ||
|
|
|
|
path.ProcessorName == pathprocessor.ProcessorStickersBuyName ||
|
|
|
|
path.ProcessorName == pathprocessor.ProcessorENSRegisterName ||
|
|
|
|
path.ProcessorName == pathprocessor.ProcessorENSReleaseName ||
|
2024-10-30 22:43:46 +00:00
|
|
|
path.ProcessorName == pathprocessor.ProcessorENSPublicKeyName ||
|
|
|
|
path.ProcessorName == pathprocessor.ProcessorERC721Name ||
|
|
|
|
path.ProcessorName == pathprocessor.ProcessorERC1155Name {
|
2024-09-23 07:19:00 +00:00
|
|
|
// 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)
|
|
|
|
sendArgs.To = &toContractAddr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if path.ToToken != nil {
|
|
|
|
sendArgs.ToTokenID = path.ToToken.Symbol
|
|
|
|
}
|
|
|
|
|
|
|
|
builtTx, usedNonce, err := pathProcessors[path.ProcessorName].BuildTransactionV2(sendArgs, lastUsedNonce)
|
|
|
|
if err != nil {
|
2024-11-07 17:15:18 +00:00
|
|
|
return nil, err
|
2024-09-23 07:19:00 +00:00
|
|
|
}
|
|
|
|
txHash := signer.Hash(builtTx)
|
|
|
|
usedNonces[path.FromChain.ChainID] = int64(usedNonce)
|
|
|
|
|
2024-11-07 17:15:18 +00:00
|
|
|
return &TransactionData{
|
|
|
|
TxArgs: sendArgs,
|
|
|
|
Tx: builtTx,
|
|
|
|
HashToSign: types.Hash(txHash),
|
|
|
|
}, nil
|
2024-09-23 07:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tm *TransactionManager) BuildTransactionsFromRoute(route routes.Route, pathProcessors map[string]pathprocessor.PathProcessor,
|
|
|
|
params BuildRouteExtraParams) (*responses.SigningDetails, error) {
|
|
|
|
if len(route) == 0 {
|
|
|
|
return nil, ErrNoRoute
|
|
|
|
}
|
|
|
|
|
|
|
|
accFrom, err := tm.accountsDB.GetAccountByAddress(types.Address(params.AddressFrom))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
keypair, err := tm.accountsDB.GetKeypairByKeyUID(accFrom.KeyUID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response := &responses.SigningDetails{
|
|
|
|
Address: accFrom.Address,
|
|
|
|
AddressPath: accFrom.Path,
|
|
|
|
KeyUid: accFrom.KeyUID,
|
|
|
|
SignOnKeycard: keypair.MigratedToKeycard(),
|
|
|
|
}
|
|
|
|
|
|
|
|
usedNonces := make(map[uint64]int64)
|
|
|
|
for _, path := range route {
|
|
|
|
signer := ethTypes.NewLondonSigner(big.NewInt(int64(path.FromChain.ChainID)))
|
|
|
|
|
2024-11-07 17:15:18 +00:00
|
|
|
txDetails := tm.getOrInitDetailsForPath(path)
|
|
|
|
|
2024-09-23 07:19:00 +00:00
|
|
|
// always check for approval tx first for the path and build it if needed
|
|
|
|
if path.ApprovalRequired && !tm.ApprovalPlacedForPath(path.ProcessorName) {
|
2024-11-07 17:15:18 +00:00
|
|
|
txDetails.ApprovalTxData, err = buildApprovalTxForPath(tm.transactor, path, params.AddressFrom, usedNonces, signer)
|
2024-09-23 07:19:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-11-07 17:15:18 +00:00
|
|
|
response.Hashes = append(response.Hashes, txDetails.ApprovalTxData.HashToSign)
|
2024-09-23 07:19:00 +00:00
|
|
|
|
|
|
|
// if approval is needed for swap, we cannot build the swap tx before the approval tx is mined
|
|
|
|
if path.ProcessorName == pathprocessor.ProcessorSwapParaswapName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// build tx for the path
|
2024-11-07 17:15:18 +00:00
|
|
|
txDetails.TxData, err = buildTxForPath(tm.transactor, path, pathProcessors, usedNonces, signer, params)
|
2024-09-23 07:19:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-11-07 17:15:18 +00:00
|
|
|
response.Hashes = append(response.Hashes, txDetails.TxData.HashToSign)
|
2024-09-23 07:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSignatureForTxHash(txHash string, signatures map[string]SignatureDetails) ([]byte, error) {
|
|
|
|
sigDetails, ok := signatures[txHash]
|
|
|
|
if !ok {
|
|
|
|
err := &errors.ErrorResponse{
|
|
|
|
Code: ErrMissingSignatureForTx.Code,
|
|
|
|
Details: fmt.Sprintf(ErrMissingSignatureForTx.Details, txHash),
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err := sigDetails.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rBytes, _ := hex.DecodeString(sigDetails.R)
|
|
|
|
sBytes, _ := hex.DecodeString(sigDetails.S)
|
|
|
|
vByte := byte(0)
|
|
|
|
if sigDetails.V == "01" {
|
|
|
|
vByte = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
signature := make([]byte, crypto.SignatureLength)
|
|
|
|
copy(signature[32-len(rBytes):32], rBytes)
|
|
|
|
copy(signature[64-len(rBytes):64], sBytes)
|
|
|
|
signature[64] = vByte
|
|
|
|
|
|
|
|
return signature, nil
|
|
|
|
}
|
|
|
|
|
2024-11-07 17:15:18 +00:00
|
|
|
func validateAndAddSignature(txData *TransactionData, signatures map[string]SignatureDetails) error {
|
|
|
|
if txData != nil && !txData.IsTxPlaced() {
|
|
|
|
var err error
|
|
|
|
txData.Signature, err = getSignatureForTxHash(txData.HashToSign.String(), signatures)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-23 07:19:00 +00:00
|
|
|
func (tm *TransactionManager) ValidateAndAddSignaturesToRouterTransactions(signatures map[string]SignatureDetails) error {
|
|
|
|
if len(tm.routerTransactions) == 0 {
|
|
|
|
return ErrNoTrsansactionsBeingBuilt
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if all transactions have been signed
|
2024-11-07 17:15:18 +00:00
|
|
|
var err error
|
2024-09-23 07:19:00 +00:00
|
|
|
for _, desc := range tm.routerTransactions {
|
2024-11-07 17:15:18 +00:00
|
|
|
err = validateAndAddSignature(desc.ApprovalTxData, signatures)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-09-23 07:19:00 +00:00
|
|
|
}
|
|
|
|
|
2024-11-07 17:15:18 +00:00
|
|
|
err = validateAndAddSignature(desc.TxData, signatures)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-09-23 07:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-11-07 17:15:18 +00:00
|
|
|
func addSignatureAndSendTransaction(
|
|
|
|
transactor transactions.TransactorIface,
|
|
|
|
txData *TransactionData,
|
|
|
|
multiTransactionID walletCommon.MultiTransactionIDType,
|
|
|
|
isApproval bool) (*responses.RouterSentTransaction, error) {
|
|
|
|
var txWithSignature *ethTypes.Transaction
|
|
|
|
var err error
|
|
|
|
|
|
|
|
txWithSignature, err = transactor.AddSignatureToTransaction(txData.TxArgs.FromChainID, txData.Tx, txData.Signature)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
txData.Tx = txWithSignature
|
|
|
|
|
|
|
|
txData.SentHash, err = transactor.SendTransactionWithSignature(common.Address(txData.TxArgs.From), txData.TxArgs.FromTokenID, multiTransactionID, txWithSignature)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return responses.NewRouterSentTransaction(txData.TxArgs, txData.SentHash, isApproval), nil
|
|
|
|
}
|
|
|
|
|
2024-09-23 07:19:00 +00:00
|
|
|
func (tm *TransactionManager) SendRouterTransactions(ctx context.Context, multiTx *MultiTransaction) (transactions []*responses.RouterSentTransaction, err error) {
|
|
|
|
transactions = make([]*responses.RouterSentTransaction, 0)
|
|
|
|
|
|
|
|
// send transactions
|
|
|
|
for _, desc := range tm.routerTransactions {
|
2024-11-07 17:15:18 +00:00
|
|
|
if desc.ApprovalTxData != nil && !desc.IsApprovalPlaced() {
|
|
|
|
var response *responses.RouterSentTransaction
|
|
|
|
response, err = addSignatureAndSendTransaction(tm.transactor, desc.ApprovalTxData, multiTx.ID, true)
|
2024-09-23 07:19:00 +00:00
|
|
|
if err != nil {
|
2024-10-16 05:09:56 +00:00
|
|
|
return
|
2024-09-23 07:19:00 +00:00
|
|
|
}
|
|
|
|
|
2024-11-07 17:15:18 +00:00
|
|
|
transactions = append(transactions, response)
|
2024-09-23 07:19:00 +00:00
|
|
|
|
|
|
|
// if approval is needed for swap, then we need to wait for the approval tx to be mined before sending the swap tx
|
2024-10-16 05:09:56 +00:00
|
|
|
if desc.RouterPath.ProcessorName == pathprocessor.ProcessorSwapParaswapName {
|
2024-09-23 07:19:00 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-07 17:15:18 +00:00
|
|
|
if desc.TxData != nil && !desc.IsTxPlaced() {
|
|
|
|
var response *responses.RouterSentTransaction
|
|
|
|
response, err = addSignatureAndSendTransaction(tm.transactor, desc.TxData, multiTx.ID, false)
|
2024-09-23 07:19:00 +00:00
|
|
|
if err != nil {
|
2024-10-16 05:09:56 +00:00
|
|
|
return
|
2024-09-23 07:19:00 +00:00
|
|
|
}
|
|
|
|
|
2024-11-07 17:15:18 +00:00
|
|
|
transactions = append(transactions, response)
|
2024-09-23 07:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2024-10-16 05:09:56 +00:00
|
|
|
|
|
|
|
func (tm *TransactionManager) GetRouterTransactions() []*RouterTransactionDetails {
|
|
|
|
return tm.routerTransactions
|
|
|
|
}
|