2023-02-15 12:28:19 +00:00
|
|
|
package transfer
|
2021-09-09 14:28:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
2022-07-04 07:48:30 +00:00
|
|
|
"fmt"
|
2021-09-09 14:28:54 +00:00
|
|
|
"math/big"
|
2022-07-04 07:48:30 +00:00
|
|
|
"strings"
|
2021-09-09 14:28:54 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2022-07-15 08:53:56 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
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"
|
|
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
|
|
|
"github.com/status-im/status-go/params"
|
2023-02-20 09:32:45 +00:00
|
|
|
"github.com/status-im/status-go/rpc/chain"
|
2021-09-09 14:28:54 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/async"
|
|
|
|
"github.com/status-im/status-go/services/wallet/bigint"
|
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"
|
2022-07-15 08:53:56 +00:00
|
|
|
"github.com/status-im/status-go/transactions"
|
2021-09-09 14:28:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type TransactionManager struct {
|
2022-07-15 08:53:56 +00:00
|
|
|
db *sql.DB
|
|
|
|
gethManager *account.GethManager
|
|
|
|
transactor *transactions.Transactor
|
|
|
|
config *params.NodeConfig
|
|
|
|
accountsDB *accounts.Database
|
|
|
|
}
|
|
|
|
|
2023-02-15 12:28:19 +00:00
|
|
|
func NewTransactionManager(db *sql.DB, gethManager *account.GethManager, transactor *transactions.Transactor,
|
|
|
|
config *params.NodeConfig, accountsDB *accounts.Database) *TransactionManager {
|
|
|
|
return &TransactionManager{
|
|
|
|
db: db,
|
|
|
|
gethManager: gethManager,
|
|
|
|
transactor: transactor,
|
|
|
|
config: config,
|
|
|
|
accountsDB: accountsDB,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-15 08:53:56 +00:00
|
|
|
type MultiTransactionType uint8
|
|
|
|
|
2023-04-21 11:59:29 +00:00
|
|
|
// TODO: extend with know types
|
2022-07-15 08:53:56 +00:00
|
|
|
const (
|
|
|
|
MultiTransactionSend = iota
|
|
|
|
MultiTransactionSwap
|
|
|
|
MultiTransactionBridge
|
|
|
|
)
|
|
|
|
|
|
|
|
type MultiTransaction struct {
|
|
|
|
ID uint `json:"id"`
|
|
|
|
Timestamp uint64 `json:"timestamp"`
|
|
|
|
FromAddress common.Address `json:"fromAddress"`
|
|
|
|
ToAddress common.Address `json:"toAddress"`
|
|
|
|
FromAsset string `json:"fromAsset"`
|
|
|
|
ToAsset string `json:"toAsset"`
|
|
|
|
FromAmount *hexutil.Big `json:"fromAmount"`
|
2023-06-02 20:08:45 +00:00
|
|
|
ToAmount *hexutil.Big `json:"toAmount"`
|
2022-07-15 08:53:56 +00:00
|
|
|
Type MultiTransactionType `json:"type"`
|
|
|
|
}
|
|
|
|
|
2023-06-12 10:49:32 +00:00
|
|
|
type MultiTransactionCommand struct {
|
|
|
|
FromAddress common.Address `json:"fromAddress"`
|
|
|
|
ToAddress common.Address `json:"toAddress"`
|
|
|
|
FromAsset string `json:"fromAsset"`
|
|
|
|
ToAsset string `json:"toAsset"`
|
|
|
|
FromAmount *hexutil.Big `json:"fromAmount"`
|
|
|
|
Type MultiTransactionType `json:"type"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type MultiTransactionCommandResult struct {
|
2022-07-15 08:53:56 +00:00
|
|
|
ID int64 `json:"id"`
|
|
|
|
Hashes map[uint64][]types.Hash `json:"hashes"`
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type PendingTrxType string
|
|
|
|
|
|
|
|
const (
|
2023-05-05 11:03:06 +00:00
|
|
|
RegisterENS PendingTrxType = "RegisterENS"
|
|
|
|
ReleaseENS PendingTrxType = "ReleaseENS"
|
|
|
|
SetPubKey PendingTrxType = "SetPubKey"
|
|
|
|
BuyStickerPack PendingTrxType = "BuyStickerPack"
|
|
|
|
WalletTransfer PendingTrxType = "WalletTransfer"
|
|
|
|
CollectibleDeployment PendingTrxType = "CollectibleDeployment"
|
|
|
|
CollectibleAirdrop PendingTrxType = "CollectibleAirdrop"
|
|
|
|
CollectibleRemoteSelfDestruct PendingTrxType = "CollectibleRemoteSelfDestruct"
|
2023-06-02 08:07:00 +00:00
|
|
|
CollectibleBurn PendingTrxType = "CollectibleBurn"
|
2021-09-09 14:28:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type PendingTransaction struct {
|
2023-02-15 12:28:19 +00:00
|
|
|
Hash common.Hash `json:"hash"`
|
|
|
|
Timestamp uint64 `json:"timestamp"`
|
|
|
|
Value bigint.BigInt `json:"value"`
|
|
|
|
From common.Address `json:"from"`
|
|
|
|
To common.Address `json:"to"`
|
|
|
|
Data string `json:"data"`
|
|
|
|
Symbol string `json:"symbol"`
|
|
|
|
GasPrice bigint.BigInt `json:"gasPrice"`
|
|
|
|
GasLimit bigint.BigInt `json:"gasLimit"`
|
|
|
|
Type PendingTrxType `json:"type"`
|
|
|
|
AdditionalData string `json:"additionalData"`
|
|
|
|
ChainID uint64 `json:"network_id"`
|
|
|
|
MultiTransactionID MultiTransactionIDType `json:"multi_transaction_id"`
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|
|
|
|
|
2023-04-21 11:59:29 +00:00
|
|
|
type TransactionIdentity struct {
|
2023-05-11 07:50:07 +00:00
|
|
|
ChainID wallet_common.ChainID `json:"chainId"`
|
|
|
|
Hash common.Hash `json:"hash"`
|
|
|
|
Address common.Address `json:"address"`
|
2023-04-21 11:59:29 +00:00
|
|
|
}
|
|
|
|
|
2023-02-15 12:28:19 +00:00
|
|
|
const selectFromPending = `SELECT hash, timestamp, value, from_address, to_address, data,
|
|
|
|
symbol, gas_price, gas_limit, type, additional_data,
|
|
|
|
network_id, COALESCE(multi_transaction_id, 0)
|
|
|
|
FROM pending_transactions
|
|
|
|
`
|
2021-09-09 14:28:54 +00:00
|
|
|
|
2023-02-15 12:28:19 +00:00
|
|
|
func rowsToTransactions(rows *sql.Rows) (transactions []*PendingTransaction, err error) {
|
2021-09-09 14:28:54 +00:00
|
|
|
for rows.Next() {
|
|
|
|
transaction := &PendingTransaction{
|
|
|
|
Value: bigint.BigInt{Int: new(big.Int)},
|
|
|
|
GasPrice: bigint.BigInt{Int: new(big.Int)},
|
|
|
|
GasLimit: bigint.BigInt{Int: new(big.Int)},
|
|
|
|
}
|
|
|
|
err := rows.Scan(&transaction.Hash,
|
|
|
|
&transaction.Timestamp,
|
|
|
|
(*bigint.SQLBigIntBytes)(transaction.Value.Int),
|
|
|
|
&transaction.From,
|
|
|
|
&transaction.To,
|
|
|
|
&transaction.Data,
|
|
|
|
&transaction.Symbol,
|
|
|
|
(*bigint.SQLBigIntBytes)(transaction.GasPrice.Int),
|
|
|
|
(*bigint.SQLBigIntBytes)(transaction.GasLimit.Int),
|
|
|
|
&transaction.Type,
|
|
|
|
&transaction.AdditionalData,
|
|
|
|
&transaction.ChainID,
|
2023-02-15 12:28:19 +00:00
|
|
|
&transaction.MultiTransactionID,
|
2021-09-09 14:28:54 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
transactions = append(transactions, transaction)
|
|
|
|
}
|
|
|
|
return transactions, nil
|
|
|
|
}
|
|
|
|
|
2023-02-15 12:28:19 +00:00
|
|
|
func (tm *TransactionManager) GetAllPending(chainIDs []uint64) ([]*PendingTransaction, error) {
|
|
|
|
if len(chainIDs) == 0 {
|
|
|
|
return nil, errors.New("at least 1 chainID is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
inVector := strings.Repeat("?, ", len(chainIDs)-1) + "?"
|
|
|
|
var parameters []interface{}
|
|
|
|
for _, c := range chainIDs {
|
|
|
|
parameters = append(parameters, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := tm.db.Query(fmt.Sprintf(selectFromPending+"WHERE network_id in (%s)", inVector), parameters...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
return rowsToTransactions(rows)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tm *TransactionManager) GetPendingByAddress(chainIDs []uint64, address common.Address) ([]*PendingTransaction, error) {
|
2022-08-08 18:25:53 +00:00
|
|
|
if len(chainIDs) == 0 {
|
|
|
|
return nil, errors.New("at least 1 chainID is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
inVector := strings.Repeat("?, ", len(chainIDs)-1) + "?"
|
|
|
|
var parameters []interface{}
|
|
|
|
for _, c := range chainIDs {
|
|
|
|
parameters = append(parameters, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
parameters = append(parameters, address)
|
|
|
|
|
2023-02-15 12:28:19 +00:00
|
|
|
rows, err := tm.db.Query(fmt.Sprintf(selectFromPending+"WHERE network_id in (%s) AND from_address = ?", inVector), parameters...)
|
2021-09-09 14:28:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2023-02-15 12:28:19 +00:00
|
|
|
return rowsToTransactions(rows)
|
|
|
|
}
|
2021-09-09 14:28:54 +00:00
|
|
|
|
2023-02-15 12:28:19 +00:00
|
|
|
// GetPendingEntry returns sql.ErrNoRows if no pending transaction is found for the given identity
|
2023-04-21 11:59:29 +00:00
|
|
|
// TODO: consider using address also in case we expect to have also for the receiver
|
2023-02-15 12:28:19 +00:00
|
|
|
func (tm *TransactionManager) GetPendingEntry(chainID uint64, hash common.Hash) (*PendingTransaction, error) {
|
|
|
|
row := tm.db.QueryRow(`SELECT timestamp, value, from_address, to_address, data,
|
|
|
|
symbol, gas_price, gas_limit, type, additional_data,
|
|
|
|
network_id, COALESCE(multi_transaction_id, 0)
|
|
|
|
FROM pending_transactions
|
|
|
|
WHERE network_id = ? AND hash = ?`, chainID, hash)
|
|
|
|
transaction := &PendingTransaction{
|
|
|
|
Hash: hash,
|
|
|
|
Value: bigint.BigInt{Int: new(big.Int)},
|
|
|
|
GasPrice: bigint.BigInt{Int: new(big.Int)},
|
|
|
|
GasLimit: bigint.BigInt{Int: new(big.Int)},
|
|
|
|
ChainID: chainID,
|
|
|
|
}
|
|
|
|
err := row.Scan(
|
|
|
|
&transaction.Timestamp,
|
|
|
|
(*bigint.SQLBigIntBytes)(transaction.Value.Int),
|
|
|
|
&transaction.From,
|
|
|
|
&transaction.To,
|
|
|
|
&transaction.Data,
|
|
|
|
&transaction.Symbol,
|
|
|
|
(*bigint.SQLBigIntBytes)(transaction.GasPrice.Int),
|
|
|
|
(*bigint.SQLBigIntBytes)(transaction.GasLimit.Int),
|
|
|
|
&transaction.Type,
|
|
|
|
&transaction.AdditionalData,
|
|
|
|
&transaction.ChainID,
|
|
|
|
&transaction.MultiTransactionID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|
|
|
|
|
2023-02-15 12:28:19 +00:00
|
|
|
return transaction, nil
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|
|
|
|
|
2023-02-15 12:28:19 +00:00
|
|
|
func (tm *TransactionManager) AddPending(transaction PendingTransaction) error {
|
2021-09-09 14:28:54 +00:00
|
|
|
insert, err := tm.db.Prepare(`INSERT OR REPLACE INTO pending_transactions
|
|
|
|
(network_id, hash, timestamp, value, from_address, to_address,
|
2023-02-15 12:28:19 +00:00
|
|
|
data, symbol, gas_price, gas_limit, type, additional_data, multi_transaction_id)
|
2021-09-09 14:28:54 +00:00
|
|
|
VALUES
|
2023-02-15 12:28:19 +00:00
|
|
|
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
|
2021-09-09 14:28:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = insert.Exec(
|
|
|
|
transaction.ChainID,
|
|
|
|
transaction.Hash,
|
|
|
|
transaction.Timestamp,
|
|
|
|
(*bigint.SQLBigIntBytes)(transaction.Value.Int),
|
|
|
|
transaction.From,
|
|
|
|
transaction.To,
|
|
|
|
transaction.Data,
|
|
|
|
transaction.Symbol,
|
|
|
|
(*bigint.SQLBigIntBytes)(transaction.GasPrice.Int),
|
|
|
|
(*bigint.SQLBigIntBytes)(transaction.GasLimit.Int),
|
|
|
|
transaction.Type,
|
|
|
|
transaction.AdditionalData,
|
2023-02-15 12:28:19 +00:00
|
|
|
transaction.MultiTransactionID,
|
2021-09-09 14:28:54 +00:00
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-15 12:28:19 +00:00
|
|
|
func (tm *TransactionManager) DeletePending(chainID uint64, hash common.Hash) error {
|
2021-09-09 14:28:54 +00:00
|
|
|
_, err := tm.db.Exec(`DELETE FROM pending_transactions WHERE network_id = ? AND hash = ?`, chainID, hash)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-15 12:28:19 +00:00
|
|
|
func (tm *TransactionManager) Watch(ctx context.Context, transactionHash common.Hash, client *chain.ClientWithFallback) error {
|
2021-09-09 14:28:54 +00:00
|
|
|
watchTxCommand := &watchTransactionCommand{
|
|
|
|
hash: transactionHash,
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
|
|
|
|
commandContext, cancel := context.WithTimeout(ctx, 10*time.Minute)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
return watchTxCommand.Command()(commandContext)
|
|
|
|
}
|
|
|
|
|
2023-06-02 20:08:45 +00:00
|
|
|
const multiTransactionColumns = "from_address, from_asset, from_amount, to_address, to_asset, to_amount, type, timestamp"
|
2022-07-15 08:53:56 +00:00
|
|
|
|
2023-03-01 19:35:19 +00:00
|
|
|
func insertMultiTransaction(db *sql.DB, multiTransaction *MultiTransaction) (MultiTransactionIDType, error) {
|
|
|
|
insert, err := db.Prepare(fmt.Sprintf(`INSERT OR REPLACE INTO multi_transactions (%s)
|
2023-06-02 20:08:45 +00:00
|
|
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?)`, multiTransactionColumns))
|
2022-07-15 08:53:56 +00:00
|
|
|
if err != nil {
|
2023-03-01 19:35:19 +00:00
|
|
|
return 0, err
|
2022-07-15 08:53:56 +00:00
|
|
|
}
|
|
|
|
result, err := insert.Exec(
|
|
|
|
multiTransaction.FromAddress,
|
|
|
|
multiTransaction.FromAsset,
|
|
|
|
multiTransaction.FromAmount.String(),
|
|
|
|
multiTransaction.ToAddress,
|
|
|
|
multiTransaction.ToAsset,
|
2023-06-02 20:08:45 +00:00
|
|
|
multiTransaction.ToAmount.String(),
|
2022-07-15 08:53:56 +00:00
|
|
|
multiTransaction.Type,
|
|
|
|
time.Now().Unix(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
2023-03-01 19:35:19 +00:00
|
|
|
return 0, err
|
2022-07-15 08:53:56 +00:00
|
|
|
}
|
|
|
|
defer insert.Close()
|
|
|
|
multiTransactionID, err := result.LastInsertId()
|
2023-03-01 19:35:19 +00:00
|
|
|
return MultiTransactionIDType(multiTransactionID), err
|
|
|
|
}
|
|
|
|
|
2023-06-02 20:08:45 +00:00
|
|
|
func (tm *TransactionManager) InsertMultiTransaction(multiTransaction *MultiTransaction) (MultiTransactionIDType, error) {
|
|
|
|
return insertMultiTransaction(tm.db, multiTransaction)
|
|
|
|
}
|
|
|
|
|
2023-06-12 10:49:32 +00:00
|
|
|
func (tm *TransactionManager) CreateMultiTransactionFromCommand(ctx context.Context, command *MultiTransactionCommand, data []*bridge.TransactionBridge, bridges map[string]bridge.Bridge, password string) (*MultiTransactionCommandResult, error) {
|
|
|
|
multiTransaction := &MultiTransaction{
|
|
|
|
FromAddress: command.FromAddress,
|
|
|
|
ToAddress: command.ToAddress,
|
|
|
|
FromAsset: command.FromAsset,
|
|
|
|
ToAsset: command.ToAsset,
|
|
|
|
FromAmount: command.FromAmount,
|
2023-06-14 07:32:55 +00:00
|
|
|
ToAmount: new(hexutil.Big),
|
2023-06-12 10:49:32 +00:00
|
|
|
Type: command.Type,
|
|
|
|
}
|
|
|
|
|
2023-03-01 19:35:19 +00:00
|
|
|
selectedAccount, err := tm.getVerifiedWalletAccount(multiTransaction.FromAddress.Hex(), password)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
multiTransactionID, err := insertMultiTransaction(tm.db, multiTransaction)
|
2022-07-15 08:53:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
hashes := make(map[uint64][]types.Hash)
|
2022-09-13 07:10:59 +00:00
|
|
|
for _, tx := range data {
|
|
|
|
hash, err := bridges[tx.BridgeName].Send(tx, selectedAccount)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-15 12:28:19 +00:00
|
|
|
pendingTransaction := PendingTransaction{
|
2022-09-13 07:10:59 +00:00
|
|
|
Hash: common.Hash(hash),
|
|
|
|
Timestamp: uint64(time.Now().Unix()),
|
2022-11-22 10:12:16 +00:00
|
|
|
Value: bigint.BigInt{Int: multiTransaction.FromAmount.ToInt()},
|
2022-09-13 07:10:59 +00:00
|
|
|
From: common.Address(tx.From()),
|
|
|
|
To: common.Address(tx.To()),
|
|
|
|
Data: tx.Data().String(),
|
|
|
|
Type: WalletTransfer,
|
|
|
|
ChainID: tx.ChainID,
|
2023-03-01 19:35:19 +00:00
|
|
|
MultiTransactionID: multiTransactionID,
|
2022-11-22 10:12:16 +00:00
|
|
|
Symbol: multiTransaction.FromAsset,
|
2023-02-15 12:28:19 +00:00
|
|
|
}
|
|
|
|
err = tm.AddPending(pendingTransaction)
|
2022-09-13 07:10:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-07-15 08:53:56 +00:00
|
|
|
}
|
2022-09-13 07:10:59 +00:00
|
|
|
hashes[tx.ChainID] = append(hashes[tx.ChainID], hash)
|
2022-07-15 08:53:56 +00:00
|
|
|
}
|
|
|
|
|
2023-06-12 10:49:32 +00:00
|
|
|
return &MultiTransactionCommandResult{
|
2023-03-01 19:35:19 +00:00
|
|
|
ID: int64(multiTransactionID),
|
2022-07-15 08:53:56 +00:00
|
|
|
Hashes: hashes,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-03-01 19:35:19 +00:00
|
|
|
func (tm *TransactionManager) GetMultiTransactions(ctx context.Context, ids []MultiTransactionIDType) ([]*MultiTransaction, error) {
|
|
|
|
placeholders := make([]string, len(ids))
|
|
|
|
args := make([]interface{}, len(ids))
|
|
|
|
for i, v := range ids {
|
|
|
|
placeholders[i] = "?"
|
|
|
|
args[i] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt, err := tm.db.Prepare(fmt.Sprintf(`SELECT rowid, %s
|
|
|
|
FROM multi_transactions
|
|
|
|
WHERE rowid in (%s)`,
|
|
|
|
multiTransactionColumns,
|
|
|
|
strings.Join(placeholders, ",")))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
rows, err := stmt.Query(args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var multiTransactions []*MultiTransaction
|
|
|
|
for rows.Next() {
|
|
|
|
multiTransaction := &MultiTransaction{}
|
|
|
|
var fromAmount string
|
2023-06-02 20:08:45 +00:00
|
|
|
var toAmount string
|
2023-03-01 19:35:19 +00:00
|
|
|
err := rows.Scan(
|
|
|
|
&multiTransaction.ID,
|
|
|
|
&multiTransaction.FromAddress,
|
|
|
|
&multiTransaction.FromAsset,
|
|
|
|
&fromAmount,
|
|
|
|
&multiTransaction.ToAddress,
|
|
|
|
&multiTransaction.ToAsset,
|
2023-06-02 20:08:45 +00:00
|
|
|
&toAmount,
|
2023-03-01 19:35:19 +00:00
|
|
|
&multiTransaction.Type,
|
|
|
|
&multiTransaction.Timestamp,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
multiTransaction.FromAmount = new(hexutil.Big)
|
|
|
|
_, ok := (*big.Int)(multiTransaction.FromAmount).SetString(fromAmount, 0)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("failed to convert fromAmount to big.Int: " + fromAmount)
|
|
|
|
}
|
|
|
|
|
2023-06-02 20:08:45 +00:00
|
|
|
multiTransaction.ToAmount = new(hexutil.Big)
|
|
|
|
_, ok = (*big.Int)(multiTransaction.ToAmount).SetString(toAmount, 0)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("failed to convert toAmount to big.Int: " + toAmount)
|
|
|
|
}
|
|
|
|
|
2023-03-01 19:35:19 +00:00
|
|
|
multiTransactions = append(multiTransactions, multiTransaction)
|
|
|
|
}
|
|
|
|
|
|
|
|
return multiTransactions, nil
|
|
|
|
}
|
|
|
|
|
2022-07-15 08:53:56 +00:00
|
|
|
func (tm *TransactionManager) getVerifiedWalletAccount(address, password string) (*account.SelectedExtKey, error) {
|
|
|
|
exists, err := tm.accountsDB.AddressExists(types.HexToAddress(address))
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to query db for a given address", "address", address, "error", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
log.Error("failed to get a selected account", "err", transactions.ErrInvalidTxSender)
|
|
|
|
return nil, transactions.ErrAccountDoesntExist
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := tm.gethManager.VerifyAccountPassword(tm.config.KeyStoreDir, address, password)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to verify account", "account", address, "error", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &account.SelectedExtKey{
|
|
|
|
Address: key.Address,
|
|
|
|
AccountKey: key,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-09-09 14:28:54 +00:00
|
|
|
type watchTransactionCommand struct {
|
2023-02-20 09:32:45 +00:00
|
|
|
client *chain.ClientWithFallback
|
2021-09-09 14:28:54 +00:00
|
|
|
hash common.Hash
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *watchTransactionCommand) Command() async.Command {
|
|
|
|
return async.FiniteCommand{
|
|
|
|
Interval: 10 * time.Second,
|
|
|
|
Runable: c.Run,
|
|
|
|
}.Run
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *watchTransactionCommand) Run(ctx context.Context) error {
|
|
|
|
requestContext, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
_, isPending, err := c.client.TransactionByHash(requestContext, c.hash)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Watching transaction error", "error", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if isPending {
|
|
|
|
return errors.New("transaction is pending")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|