2023-02-15 12:28:19 +00:00
|
|
|
package transfer
|
2021-09-09 14:28:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-02 23:37:51 +00:00
|
|
|
"encoding/json"
|
2022-07-04 07:48:30 +00:00
|
|
|
"fmt"
|
2024-06-02 23:37:51 +00:00
|
|
|
"time"
|
2021-09-09 14:28:54 +00:00
|
|
|
|
2024-06-02 23:37:51 +00:00
|
|
|
"github.com/pkg/errors"
|
2024-10-28 20:54:17 +00:00
|
|
|
"go.uber.org/zap"
|
2024-06-02 23:37:51 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2022-07-15 08:53:56 +00:00
|
|
|
"github.com/status-im/status-go/account"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2024-10-28 20:54:17 +00:00
|
|
|
"github.com/status-im/status-go/logutils"
|
2023-05-11 07:50:07 +00:00
|
|
|
wallet_common "github.com/status-im/status-go/services/wallet/common"
|
2024-11-05 21:20:27 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/requests"
|
2024-06-06 20:08:25 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/router/pathprocessor"
|
2024-06-02 23:37:51 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/walletevent"
|
2023-09-29 17:56:27 +00:00
|
|
|
"github.com/status-im/status-go/signal"
|
2024-06-02 23:37:51 +00:00
|
|
|
"github.com/status-im/status-go/transactions"
|
2021-09-09 14:28:54 +00:00
|
|
|
)
|
|
|
|
|
2024-05-02 15:36:42 +00:00
|
|
|
const multiTransactionColumns = "id, from_network_id, from_tx_hash, from_address, from_asset, from_amount, to_network_id, to_tx_hash, to_address, to_asset, to_amount, type, cross_tx_id, timestamp"
|
|
|
|
const selectMultiTransactionColumns = "id, COALESCE(from_network_id, 0), from_tx_hash, from_address, from_asset, from_amount, COALESCE(to_network_id, 0), to_tx_hash, to_address, to_asset, to_amount, type, cross_tx_id, timestamp"
|
2023-06-13 14:20:48 +00:00
|
|
|
|
2024-06-02 23:37:51 +00:00
|
|
|
var pendingTxTimeout time.Duration = 10 * time.Minute
|
|
|
|
var ErrWatchPendingTxTimeout = errors.New("timeout watching for pending transaction")
|
|
|
|
var ErrPendingTxNotExists = errors.New("pending transaction does not exist")
|
|
|
|
|
2024-03-12 09:15:30 +00:00
|
|
|
func (tm *TransactionManager) InsertMultiTransaction(multiTransaction *MultiTransaction) (wallet_common.MultiTransactionIDType, error) {
|
2024-05-23 16:22:57 +00:00
|
|
|
return multiTransaction.ID, tm.storage.CreateMultiTransaction(multiTransaction)
|
2023-06-13 14:20:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tm *TransactionManager) UpdateMultiTransaction(multiTransaction *MultiTransaction) error {
|
2024-05-23 16:22:57 +00:00
|
|
|
return tm.storage.UpdateMultiTransaction(multiTransaction)
|
2023-06-13 14:20:48 +00:00
|
|
|
}
|
|
|
|
|
2024-05-26 08:31:13 +00:00
|
|
|
func (tm *TransactionManager) CreateMultiTransactionFromCommand(command *MultiTransactionCommand,
|
2024-06-06 20:08:25 +00:00
|
|
|
data []*pathprocessor.MultipathProcessorTxArgs) (*MultiTransaction, error) {
|
2023-06-21 14:09:55 +00:00
|
|
|
|
|
|
|
multiTransaction := multiTransactionFromCommand(command)
|
|
|
|
|
2024-08-26 07:53:21 +00:00
|
|
|
// Extract network from args
|
2024-07-17 08:09:49 +00:00
|
|
|
switch multiTransaction.Type {
|
|
|
|
case MultiTransactionSend, MultiTransactionApprove, MultiTransactionSwap:
|
|
|
|
if multiTransaction.FromNetworkID == wallet_common.UnknownChainID && len(data) == 1 {
|
|
|
|
multiTransaction.FromNetworkID = data[0].ChainID
|
|
|
|
}
|
2024-07-31 04:45:11 +00:00
|
|
|
case MultiTransactionBridge:
|
2024-08-26 07:53:21 +00:00
|
|
|
if len(data) == 1 && data[0].HopTx != nil {
|
|
|
|
if multiTransaction.FromNetworkID == wallet_common.UnknownChainID {
|
|
|
|
multiTransaction.FromNetworkID = data[0].HopTx.ChainID
|
|
|
|
}
|
|
|
|
if multiTransaction.ToNetworkID == wallet_common.UnknownChainID {
|
|
|
|
multiTransaction.ToNetworkID = data[0].HopTx.ChainIDTo
|
|
|
|
}
|
|
|
|
}
|
2024-07-31 04:45:11 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported multi transaction type: %v", multiTransaction.Type)
|
2023-10-30 09:03:29 +00:00
|
|
|
}
|
2024-05-02 15:36:42 +00:00
|
|
|
|
|
|
|
return multiTransaction, nil
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (tm *TransactionManager) SendTransactionForSigningToKeycard(ctx context.Context, multiTransaction *MultiTransaction, data []*pathprocessor.MultipathProcessorTxArgs, pathProcessors map[string]pathprocessor.PathProcessor) error {
|
2024-05-02 15:36:42 +00:00
|
|
|
acc, err := tm.accountsDB.GetAccountByAddress(types.Address(multiTransaction.FromAddress))
|
2023-06-21 14:09:55 +00:00
|
|
|
if err != nil {
|
2024-05-02 15:36:42 +00:00
|
|
|
return err
|
2023-06-21 14:09:55 +00:00
|
|
|
}
|
|
|
|
|
2024-05-02 15:36:42 +00:00
|
|
|
kp, err := tm.accountsDB.GetKeypairByKeyUID(acc.KeyUID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-29 17:56:27 +00:00
|
|
|
|
2024-05-02 15:36:42 +00:00
|
|
|
if !kp.MigratedToKeycard() {
|
|
|
|
return fmt.Errorf("account being used is not migrated to a keycard, password is required")
|
|
|
|
}
|
2023-09-29 17:56:27 +00:00
|
|
|
|
2024-05-02 15:36:42 +00:00
|
|
|
tm.multiTransactionForKeycardSigning = multiTransaction
|
2024-06-06 20:08:25 +00:00
|
|
|
tm.multipathTransactionsData = data
|
|
|
|
hashes, err := tm.buildTransactions(pathProcessors)
|
2024-05-02 15:36:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-29 17:56:27 +00:00
|
|
|
|
2024-06-20 08:50:59 +00:00
|
|
|
signal.SendWalletEvent(signal.SignTransactions, hashes)
|
2023-09-29 17:56:27 +00:00
|
|
|
|
2024-05-02 15:36:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-09-29 17:56:27 +00:00
|
|
|
|
2024-06-06 20:08:25 +00:00
|
|
|
func (tm *TransactionManager) SendTransactions(ctx context.Context, multiTransaction *MultiTransaction, data []*pathprocessor.MultipathProcessorTxArgs, pathProcessors map[string]pathprocessor.PathProcessor, account *account.SelectedExtKey) (*MultiTransactionCommandResult, error) {
|
2024-05-23 16:22:57 +00:00
|
|
|
updateDataFromMultiTx(data, multiTransaction)
|
2024-06-06 20:08:25 +00:00
|
|
|
hashes, err := sendTransactions(data, pathProcessors, account)
|
2023-06-21 14:09:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &MultiTransactionCommandResult{
|
2024-05-02 15:36:42 +00:00
|
|
|
ID: int64(multiTransaction.ID),
|
2023-06-21 14:09:55 +00:00
|
|
|
Hashes: hashes,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-11-05 21:20:27 +00:00
|
|
|
func (tm *TransactionManager) ProceedWithTransactionsSignatures(ctx context.Context, signatures map[string]requests.SignatureDetails) (*MultiTransactionCommandResult, error) {
|
2024-05-23 16:22:57 +00:00
|
|
|
if err := addSignaturesToTransactions(tm.transactionsForKeycardSigning, signatures); err != nil {
|
|
|
|
return nil, err
|
2023-09-29 17:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// send transactions
|
|
|
|
hashes := make(map[uint64][]types.Hash)
|
2024-05-23 16:22:57 +00:00
|
|
|
for _, desc := range tm.transactionsForKeycardSigning {
|
|
|
|
txWithSignature, err := tm.transactor.AddSignatureToTransaction(desc.chainID, desc.builtTx, desc.signature)
|
2023-09-29 17:56:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-05-23 16:22:57 +00:00
|
|
|
|
|
|
|
hash, err := tm.transactor.SendTransactionWithSignature(desc.from, tm.multiTransactionForKeycardSigning.FromAsset, tm.multiTransactionForKeycardSigning.ID, txWithSignature)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err // TODO: One of transfers within transaction could have been sent. Need to notify user about it
|
|
|
|
}
|
2023-09-29 17:56:27 +00:00
|
|
|
hashes[desc.chainID] = append(hashes[desc.chainID], hash)
|
|
|
|
}
|
|
|
|
|
2024-05-02 15:36:42 +00:00
|
|
|
_, err := tm.InsertMultiTransaction(tm.multiTransactionForKeycardSigning)
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("failed to insert multi transaction", zap.Error(err))
|
2024-05-02 15:36:42 +00:00
|
|
|
}
|
|
|
|
|
2023-09-29 17:56:27 +00:00
|
|
|
return &MultiTransactionCommandResult{
|
|
|
|
ID: int64(tm.multiTransactionForKeycardSigning.ID),
|
|
|
|
Hashes: hashes,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-03-12 09:15:30 +00:00
|
|
|
func (tm *TransactionManager) GetMultiTransactions(ctx context.Context, ids []wallet_common.MultiTransactionIDType) ([]*MultiTransaction, error) {
|
2024-05-26 08:31:13 +00:00
|
|
|
return tm.storage.ReadMultiTransactions(&MultiTxDetails{IDs: ids})
|
2023-06-13 14:20:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tm *TransactionManager) GetBridgeOriginMultiTransaction(ctx context.Context, toChainID uint64, crossTxID string) (*MultiTransaction, error) {
|
2024-05-23 16:22:57 +00:00
|
|
|
details := NewMultiTxDetails()
|
|
|
|
details.ToChainID = toChainID
|
|
|
|
details.CrossTxID = crossTxID
|
|
|
|
|
2024-05-26 08:31:13 +00:00
|
|
|
multiTxs, err := tm.storage.ReadMultiTransactions(details)
|
2023-06-13 14:20:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, multiTx := range multiTxs {
|
|
|
|
// Origin MultiTxs will have a missing "ToTxHash"
|
|
|
|
if multiTx.ToTxHash == emptyHash {
|
|
|
|
return multiTx, nil
|
2023-06-02 20:08:45 +00:00
|
|
|
}
|
2023-06-13 14:20:48 +00:00
|
|
|
}
|
2023-06-02 20:08:45 +00:00
|
|
|
|
2023-06-13 14:20:48 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tm *TransactionManager) GetBridgeDestinationMultiTransaction(ctx context.Context, toChainID uint64, crossTxID string) (*MultiTransaction, error) {
|
2024-05-23 16:22:57 +00:00
|
|
|
details := NewMultiTxDetails()
|
|
|
|
details.ToChainID = toChainID
|
|
|
|
details.CrossTxID = crossTxID
|
|
|
|
|
2024-05-26 08:31:13 +00:00
|
|
|
multiTxs, err := tm.storage.ReadMultiTransactions(details)
|
2023-06-13 14:20:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-03-01 19:35:19 +00:00
|
|
|
}
|
|
|
|
|
2023-06-13 14:20:48 +00:00
|
|
|
for _, multiTx := range multiTxs {
|
|
|
|
// Destination MultiTxs will have a missing "FromTxHash"
|
|
|
|
if multiTx.FromTxHash == emptyHash {
|
|
|
|
return multiTx, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
2023-03-01 19:35:19 +00:00
|
|
|
}
|
2024-06-02 23:37:51 +00:00
|
|
|
|
|
|
|
func (tm *TransactionManager) WatchTransaction(ctx context.Context, chainID uint64, transactionHash common.Hash) error {
|
|
|
|
// Workaround to keep the blocking call until the clients use the PendingTxTracker APIs
|
|
|
|
eventChan := make(chan walletevent.Event, 2)
|
|
|
|
sub := tm.eventFeed.Subscribe(eventChan)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
status, err := tm.pendingTracker.Watch(ctx, wallet_common.ChainID(chainID), transactionHash)
|
|
|
|
if err == nil && *status != transactions.Pending {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("transaction is not pending", zap.String("status", *status))
|
2024-06-02 23:37:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case we := <-eventChan:
|
|
|
|
if transactions.EventPendingTransactionStatusChanged == we.Type {
|
|
|
|
var p transactions.StatusChangedPayload
|
|
|
|
err = json.Unmarshal([]byte(we.Message), &p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if p.ChainID == wallet_common.ChainID(chainID) && p.Hash == transactionHash {
|
2024-09-23 07:19:00 +00:00
|
|
|
signal.SendWalletEvent(signal.TransactionStatusChanged, p)
|
2024-06-02 23:37:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-time.After(pendingTxTimeout):
|
|
|
|
return ErrWatchPendingTxTimeout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|