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"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2021-09-09 14:28:54 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2022-07-15 08:53:56 +00:00
|
|
|
"github.com/status-im/status-go/account"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2022-09-13 07:10:59 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/bridge"
|
2023-05-11 07:50:07 +00:00
|
|
|
wallet_common "github.com/status-im/status-go/services/wallet/common"
|
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-05-02 15:36:42 +00:00
|
|
|
data []*bridge.TransactionBridge) (*MultiTransaction, error) {
|
2023-06-21 14:09:55 +00:00
|
|
|
|
|
|
|
multiTransaction := multiTransactionFromCommand(command)
|
|
|
|
|
2024-03-25 19:05:06 +00:00
|
|
|
if multiTransaction.Type == MultiTransactionSend && multiTransaction.FromNetworkID == 0 && len(data) == 1 {
|
|
|
|
multiTransaction.FromNetworkID = data[0].ChainID
|
2023-10-30 09:03:29 +00:00
|
|
|
}
|
2024-05-02 15:36:42 +00:00
|
|
|
|
|
|
|
return multiTransaction, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tm *TransactionManager) SendTransactionForSigningToKeycard(ctx context.Context, multiTransaction *MultiTransaction, data []*bridge.TransactionBridge, bridges map[string]bridge.Bridge) error {
|
|
|
|
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
|
|
|
|
tm.transactionsBridgeData = data
|
|
|
|
hashes, err := tm.buildTransactions(bridges)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-29 17:56:27 +00:00
|
|
|
|
2024-05-02 15:36:42 +00:00
|
|
|
signal.SendTransactionsForSigningEvent(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-05-02 15:36:42 +00:00
|
|
|
func (tm *TransactionManager) SendTransactions(ctx context.Context, multiTransaction *MultiTransaction, data []*bridge.TransactionBridge, bridges map[string]bridge.Bridge, account *account.SelectedExtKey) (*MultiTransactionCommandResult, error) {
|
2024-05-23 16:22:57 +00:00
|
|
|
updateDataFromMultiTx(data, multiTransaction)
|
|
|
|
hashes, err := sendTransactions(data, bridges, 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
|
|
|
|
}
|
|
|
|
|
2023-09-29 17:56:27 +00:00
|
|
|
func (tm *TransactionManager) ProceedWithTransactionsSignatures(ctx context.Context, signatures map[string]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 {
|
|
|
|
log.Error("failed to insert multi transaction", "err", err)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
log.Error("transaction is not pending", "status", status)
|
|
|
|
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 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-time.After(pendingTxTimeout):
|
|
|
|
return ErrWatchPendingTxTimeout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|