feat(wallet) report new multi-transactions updates

Updates status-desktop #11233
This commit is contained in:
Stefan 2023-07-07 15:23:30 +01:00 committed by Stefan Dunca
parent 2a935100a0
commit 3ce851b84b
3 changed files with 52 additions and 8 deletions

View File

@ -94,7 +94,7 @@ func NewService(
tokenManager := token.NewTokenManager(db, rpcClient, rpcClient.NetworkManager) tokenManager := token.NewTokenManager(db, rpcClient, rpcClient.NetworkManager)
savedAddressesManager := &SavedAddressesManager{db: db} savedAddressesManager := &SavedAddressesManager{db: db}
pendingTxManager := transactions.NewTransactionManager(db, rpcFilterSrvc.TransactionSentToUpstreamEvent(), walletFeed) pendingTxManager := transactions.NewTransactionManager(db, rpcFilterSrvc.TransactionSentToUpstreamEvent(), walletFeed)
transactionManager := transfer.NewTransactionManager(db, gethManager, transactor, config, accountsDB, pendingTxManager) transactionManager := transfer.NewTransactionManager(db, gethManager, transactor, config, accountsDB, pendingTxManager, walletFeed)
transferController := transfer.NewTransferController(db, rpcClient, accountFeed, walletFeed, transactionManager, pendingTxManager, transferController := transfer.NewTransferController(db, rpcClient, accountFeed, walletFeed, transactionManager, pendingTxManager,
tokenManager, config.WalletConfig.LoadAllTransfers) tokenManager, config.WalletConfig.LoadAllTransfers)
cryptoCompare := cryptocompare.NewClient() cryptoCompare := cryptocompare.NewClient()

View File

@ -11,6 +11,7 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/status-im/status-go/account" "github.com/status-im/status-go/account"
"github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/eth-node/types"
@ -19,6 +20,7 @@ import (
"github.com/status-im/status-go/services/wallet/bigint" "github.com/status-im/status-go/services/wallet/bigint"
"github.com/status-im/status-go/services/wallet/bridge" "github.com/status-im/status-go/services/wallet/bridge"
wallet_common "github.com/status-im/status-go/services/wallet/common" wallet_common "github.com/status-im/status-go/services/wallet/common"
"github.com/status-im/status-go/services/wallet/walletevent"
"github.com/status-im/status-go/transactions" "github.com/status-im/status-go/transactions"
) )
@ -26,6 +28,9 @@ type MultiTransactionIDType int64
const ( const (
NoMultiTransactionID = MultiTransactionIDType(0) NoMultiTransactionID = MultiTransactionIDType(0)
// EventMTTransactionUpdate is emitted when a multi-transaction is updated (added or deleted)
EventMTTransactionUpdate walletevent.EventType = "multi-transaction-update"
) )
type TransactionManager struct { type TransactionManager struct {
@ -35,12 +40,18 @@ type TransactionManager struct {
config *params.NodeConfig config *params.NodeConfig
accountsDB *accounts.Database accountsDB *accounts.Database
pendingManager *transactions.TransactionManager pendingManager *transactions.TransactionManager
eventFeed *event.Feed
} }
func NewTransactionManager(db *sql.DB, gethManager *account.GethManager, transactor *transactions.Transactor, func NewTransactionManager(
config *params.NodeConfig, accountsDB *accounts.Database, db *sql.DB,
pendingTxManager *transactions.TransactionManager) *TransactionManager { gethManager *account.GethManager,
transactor *transactions.Transactor,
config *params.NodeConfig,
accountsDB *accounts.Database,
pendingTxManager *transactions.TransactionManager,
eventFeed *event.Feed,
) *TransactionManager {
return &TransactionManager{ return &TransactionManager{
db: db, db: db,
gethManager: gethManager, gethManager: gethManager,
@ -48,6 +59,7 @@ func NewTransactionManager(db *sql.DB, gethManager *account.GethManager, transac
config: config, config: config,
accountsDB: accountsDB, accountsDB: accountsDB,
pendingManager: pendingTxManager, pendingManager: pendingTxManager,
eventFeed: eventFeed,
} }
} }
@ -147,6 +159,7 @@ func rowsToMultiTransactions(rows *sql.Rows) ([]*MultiTransaction, error) {
return multiTransactions, nil return multiTransactions, nil
} }
// insertMultiTransaction inserts a multi transaction into the database and updates multi-transaction ID and timestamp
func insertMultiTransaction(db *sql.DB, multiTransaction *MultiTransaction) (MultiTransactionIDType, error) { func insertMultiTransaction(db *sql.DB, multiTransaction *MultiTransaction) (MultiTransactionIDType, error) {
insert, err := db.Prepare(fmt.Sprintf(`INSERT INTO multi_transactions (%s) insert, err := db.Prepare(fmt.Sprintf(`INSERT INTO multi_transactions (%s)
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, multiTransactionColumns)) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, multiTransactionColumns))
@ -183,7 +196,34 @@ func insertMultiTransaction(db *sql.DB, multiTransaction *MultiTransaction) (Mul
} }
func (tm *TransactionManager) InsertMultiTransaction(multiTransaction *MultiTransaction) (MultiTransactionIDType, error) { func (tm *TransactionManager) InsertMultiTransaction(multiTransaction *MultiTransaction) (MultiTransactionIDType, error) {
return insertMultiTransaction(tm.db, multiTransaction) return tm.insertMultiTransactionAndNotify(tm.db, multiTransaction, nil)
}
func (tm *TransactionManager) insertMultiTransactionAndNotify(db *sql.DB, multiTransaction *MultiTransaction, chainIDs []uint64) (MultiTransactionIDType, error) {
id, err := insertMultiTransaction(db, multiTransaction)
if err != nil {
publishMultiTransactionUpdatedEvent(db, multiTransaction, tm.eventFeed, chainIDs)
}
return id, err
}
// publishMultiTransactionUpdatedEvent notify listeners of new multi transaction (used in activity history)
func publishMultiTransactionUpdatedEvent(db *sql.DB, multiTransaction *MultiTransaction, eventFeed *event.Feed, chainIDs []uint64) {
publishFn := func(chainID uint64) {
eventFeed.Send(walletevent.Event{
Type: EventMTTransactionUpdate,
ChainID: chainID,
Accounts: []common.Address{multiTransaction.FromAddress, multiTransaction.ToAddress},
At: int64(multiTransaction.Timestamp),
})
}
if len(chainIDs) > 0 {
for _, chainID := range chainIDs {
publishFn(chainID)
}
} else {
publishFn(0)
}
} }
func updateMultiTransaction(db *sql.DB, multiTransaction *MultiTransaction) error { func updateMultiTransaction(db *sql.DB, multiTransaction *MultiTransaction) error {
@ -228,7 +268,11 @@ func (tm *TransactionManager) CreateMultiTransactionFromCommand(ctx context.Cont
multiTransaction := multiTransactionFromCommand(command) multiTransaction := multiTransactionFromCommand(command)
multiTransactionID, err := insertMultiTransaction(tm.db, multiTransaction) chainIDs := make([]uint64, 0, len(data))
for _, tx := range data {
chainIDs = append(chainIDs, tx.ChainID)
}
multiTransactionID, err := tm.insertMultiTransactionAndNotify(tm.db, multiTransaction, chainIDs)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -16,7 +16,7 @@ import (
func setupTestTransactionDB(t *testing.T) (*TransactionManager, func()) { func setupTestTransactionDB(t *testing.T) (*TransactionManager, func()) {
db, err := appdatabase.SetupTestMemorySQLDB("wallet-transfer-transaction-tests") db, err := appdatabase.SetupTestMemorySQLDB("wallet-transfer-transaction-tests")
require.NoError(t, err) require.NoError(t, err)
return &TransactionManager{db, nil, nil, nil, nil, nil}, func() { return &TransactionManager{db, nil, nil, nil, nil, nil, nil}, func() {
require.NoError(t, db.Close()) require.NoError(t, db.Close())
} }
} }