Skip to content

Commit

Permalink
feat(wallet)_: enrich status changed and tx update signals' payload w…
Browse files Browse the repository at this point in the history
…ith tx route data
  • Loading branch information
saledjenic committed Nov 6, 2024
1 parent 6166824 commit 4a6f483
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 44 deletions.
21 changes: 21 additions & 0 deletions services/wallet/responses/router_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package responses
import (
"github.com/status-im/status-go/errors"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/services/wallet/requests"
"github.com/status-im/status-go/services/wallet/wallettypes"
)

Expand Down Expand Up @@ -72,3 +73,23 @@ func NewRouterSentTransaction(sendArgs *wallettypes.SendTxArgs, hash types.Hash,
ApprovalTx: approvalTx,
}
}

func (sd *SendDetails) UpdateFields(inputParams requests.RouteInputParams) {
sd.SendType = int(inputParams.SendType)
sd.FromAddress = types.Address(inputParams.AddrFrom)
sd.ToAddress = types.Address(inputParams.AddrTo)
sd.FromToken = inputParams.TokenID
sd.ToToken = inputParams.ToTokenID
if inputParams.AmountIn != nil {
sd.FromAmount = inputParams.AmountIn.String()
}
if inputParams.AmountOut != nil {
sd.ToAmount = inputParams.AmountOut.String()
}
sd.OwnerTokenBeingSent = inputParams.TokenIDIsOwnerToken
sd.Username = inputParams.Username
sd.PublicKey = inputParams.PublicKey
if inputParams.PackID != nil {
sd.PackID = inputParams.PackID.String()
}
}
25 changes: 2 additions & 23 deletions services/wallet/routeexecution/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"

"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/logutils"

status_common "github.com/status-im/status-go/common"
Expand Down Expand Up @@ -81,7 +80,7 @@ func (m *Manager) BuildTransactionsFromRoute(ctx context.Context, buildInputPara

m.buildInputParams = buildInputParams

updateFields(response.SendDetails, routeInputParams)
response.SendDetails.UpdateFields(routeInputParams)

// notify client that sending transactions started (has 3 steps, building txs, signing txs, sending txs)
signal.SendWalletEvent(signal.RouterSendingTransactionsStarted, response.SendDetails)
Expand Down Expand Up @@ -143,7 +142,7 @@ func (m *Manager) SendRouterTransactionsWithSignatures(ctx context.Context, send
return
}

updateFields(response.SendDetails, routeInputParams)
response.SendDetails.UpdateFields(routeInputParams)

err = m.transactionManager.ValidateAndAddSignaturesToRouterTransactions(sendInputParams.Signatures)
if err != nil {
Expand Down Expand Up @@ -219,23 +218,3 @@ func (m *Manager) SendRouterTransactionsWithSignatures(ctx context.Context, send
}
}()
}

func updateFields(sd *responses.SendDetails, inputParams requests.RouteInputParams) {
sd.SendType = int(inputParams.SendType)
sd.FromAddress = types.Address(inputParams.AddrFrom)
sd.ToAddress = types.Address(inputParams.AddrTo)
sd.FromToken = inputParams.TokenID
sd.ToToken = inputParams.ToTokenID
if inputParams.AmountIn != nil {
sd.FromAmount = inputParams.AmountIn.String()
}
if inputParams.AmountOut != nil {
sd.ToAmount = inputParams.AmountOut.String()
}
sd.OwnerTokenBeingSent = inputParams.TokenIDIsOwnerToken
sd.Username = inputParams.Username
sd.PublicKey = inputParams.PublicKey
if inputParams.PackID != nil {
sd.PackID = inputParams.PackID.String()
}
}
29 changes: 29 additions & 0 deletions services/wallet/routeexecution/storage/route_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ func (db *DB) GetRouteData(uuid string) (*wallettypes.RouteData, error) {
return getRouteData(db.db, uuid)
}

func (db *DB) GetRouteDataByHash(chainID uint64, txHash types.Hash) (*wallettypes.RouteData, error) {
uuid, err := getUuidForTxOnChain(db.db, chainID, txHash)
if err != nil {
return nil, err
}
return db.GetRouteData(uuid)
}

func putRouteInputParams(creator sqlite.StatementCreator, p *requests.RouteInputParams) error {
q := sq.Replace("route_input_parameters").
SetMap(sq.Eq{"route_input_params_json": &sqlite.JSONBlob{Data: p}})
Expand Down Expand Up @@ -372,3 +380,24 @@ func getPathTransactions(creator sqlite.StatementCreator, uuid string, pathIdx i

return txs, nil
}

func getUuidForTxOnChain(creator sqlite.StatementCreator, chainID uint64, txHash types.Hash) (string, error) {
var uuid string
q := sq.Select("uuid").
From("route_path_transactions").
Where(sq.Eq{"chain_id": chainID, "tx_hash": txHash[:]})

query, args, err := q.ToSql()
if err != nil {
return uuid, err
}

stmt, err := creator.Prepare(query)
if err != nil {
return uuid, err
}
defer stmt.Close()

err = stmt.QueryRow(args...).Scan(&uuid)
return uuid, err
}
8 changes: 8 additions & 0 deletions services/wallet/routeexecution/storage/route_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ func Test_PutRouteData(t *testing.T) {
readRouteData, err := routeDB.GetRouteData(routeData.RouteInputParams.Uuid)
require.NoError(t, err)
require.EqualExportedValues(t, routeData, readRouteData)

for _, pathData := range routeData.PathsData {
for _, txData := range pathData.TransactionsData {
readRouteData, err = routeDB.GetRouteDataByHash(txData.ChainID, txData.TxHash)
require.NoError(t, err)
require.EqualExportedValues(t, routeData, readRouteData)
}
}
})
}
}
4 changes: 2 additions & 2 deletions services/wallet/wallettypes/route_db_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewRouteData(routeInputParams *requests.RouteInputParams,
if td.IsApprovalPlaced() {
transactionsData = append(transactionsData, &TransactionData{
ChainID: td.RouterPath.FromChain.ChainID,
TxHash: td.ApprovalHashToSign,
TxHash: td.ApprovalTxSentHash,
IsApproval: true,
TxArgs: td.ApprovalTxArgs,
Tx: td.ApprovalTx,
Expand All @@ -48,7 +48,7 @@ func NewRouteData(routeInputParams *requests.RouteInputParams,
if td.IsTxPlaced() {
transactionsData = append(transactionsData, &TransactionData{
ChainID: td.RouterPath.FromChain.ChainID,
TxHash: td.TxHashToSign,
TxHash: td.TxSentHash,
IsApproval: false,
TxArgs: td.TxArgs,
Tx: td.Tx,
Expand Down
16 changes: 8 additions & 8 deletions services/wallet/wallettypes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ type SendTxArgs struct {

// additional data - version SendTxArgsVersion0
MultiTransactionID wallet_common.MultiTransactionIDType `json:"multiTransactionID"`
Symbol string `json:"-"`
Symbol string `json:"symbol"`
// additional data - version SendTxArgsVersion1
ValueOut *hexutil.Big `json:"-"`
FromChainID uint64 `json:"-"`
ToChainID uint64 `json:"-"`
FromTokenID string `json:"-"`
ToTokenID string `json:"-"`
ToContractAddress types.Address `json:"-"` // represents address of the contract that needs to be used in order to send assets, like ERC721 or ERC1155 tx
SlippagePercentage float32 `json:"-"`
ValueOut *hexutil.Big `json:"valueOut"`
FromChainID uint64 `json:"fromChainID"`
ToChainID uint64 `json:"toChainID"`
FromTokenID string `json:"fromTokenID"`
ToTokenID string `json:"toTokenID"`
ToContractAddress types.Address `json:"toContractAddress"` // represents address of the contract that needs to be used in order to send assets, like ERC721 or ERC1155 tx
SlippagePercentage float32 `json:"slippagePercentage"`
}

// Valid checks whether this structure is filled in correctly.
Expand Down
55 changes: 44 additions & 11 deletions transactions/pendingtxtracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ import (
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/p2p"
ethrpc "github.com/ethereum/go-ethereum/rpc"
ethTypes "github.com/status-im/status-go/eth-node/types"

"github.com/status-im/status-go/logutils"
"github.com/status-im/status-go/rpc"
"github.com/status-im/status-go/services/rpcfilters"
"github.com/status-im/status-go/services/wallet/bigint"
"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/responses"
"github.com/status-im/status-go/services/wallet/routeexecution/storage"
"github.com/status-im/status-go/services/wallet/walletevent"
)

Expand Down Expand Up @@ -59,8 +62,10 @@ const (
)

type TxIdentity struct {
ChainID common.ChainID `json:"chainId"`
Hash eth.Hash `json:"hash"`
ChainID common.ChainID `json:"chainId"`
Hash eth.Hash `json:"hash"`
SendDetails *responses.SendDetails `json:"sendDetails"`
SentTransactions []*responses.RouterSentTransaction `json:"sentTransactions"`
}

type PendingTxUpdatePayload struct {
Expand All @@ -75,9 +80,10 @@ type StatusChangedPayload struct {

// PendingTxTracker implements StatusService in common/status_node_service.go
type PendingTxTracker struct {
db *sql.DB
trackedTxDB *DB
rpcClient rpc.ClientInterface
db *sql.DB
routeExecutionStorage *storage.DB
trackedTxDB *DB
rpcClient rpc.ClientInterface

rpcFilter *rpcfilters.Service
eventFeed *event.Feed
Expand All @@ -88,12 +94,13 @@ type PendingTxTracker struct {

func NewPendingTxTracker(db *sql.DB, rpcClient rpc.ClientInterface, rpcFilter *rpcfilters.Service, eventFeed *event.Feed, checkInterval time.Duration) *PendingTxTracker {
tm := &PendingTxTracker{
db: db,
trackedTxDB: NewDB(db),
rpcClient: rpcClient,
eventFeed: eventFeed,
rpcFilter: rpcFilter,
logger: logutils.ZapLogger().Named("PendingTxTracker"),
db: db,
routeExecutionStorage: storage.NewDB(db),
trackedTxDB: NewDB(db),
rpcClient: rpcClient,
eventFeed: eventFeed,
rpcFilter: rpcFilter,
logger: logutils.ZapLogger().Named("PendingTxTracker"),
}
tm.taskRunner = NewConditionalRepeater(checkInterval, func(ctx context.Context) bool {
return tm.fetchAndUpdateDB(ctx)
Expand Down Expand Up @@ -335,6 +342,28 @@ func (tm *PendingTxTracker) updateDBStatus(ctx context.Context, chainID common.C
return res, nil
}

func (tm *PendingTxTracker) updateTxIdentity(txIdentity *TxIdentity, chainID uint64, txHash ethTypes.Hash) {
routeData, err := tm.routeExecutionStorage.GetRouteDataByHash(chainID, txHash)
if err != nil {
tm.logger.Warn("Missing tx data ", zap.Stringer("hash", txHash), zap.Error(err))
}
if routeData != nil {
if routeData.RouteInputParams != nil {
txIdentity.SendDetails = &responses.SendDetails{}
txIdentity.SendDetails.UpdateFields(*routeData.RouteInputParams)
}

for _, pd := range routeData.PathsData {
for _, td := range pd.TransactionsData {
if td.TxArgs == nil {
continue
}
txIdentity.SentTransactions = append(txIdentity.SentTransactions, responses.NewRouterSentTransaction(td.TxArgs, td.TxHash, td.IsApproval))
}
}
}
}

func (tm *PendingTxTracker) emitNotifications(chainID common.ChainID, changes []txStatusRes) {
if tm.eventFeed != nil {
for _, change := range changes {
Expand All @@ -346,6 +375,8 @@ func (tm *PendingTxTracker) emitNotifications(chainID common.ChainID, changes []
Status: change.Status,
}

tm.updateTxIdentity(&payload.TxIdentity, chainID.ToUint(), ethTypes.Hash(change.hash))

jsonPayload, err := json.Marshal(payload)
if err != nil {
tm.logger.Error("Failed to marshal pending transaction status", zap.Stringer("hash", change.hash), zap.Error(err))
Expand Down Expand Up @@ -688,6 +719,8 @@ func (tm *PendingTxTracker) addPending(transaction *PendingTransaction) error {
}

func (tm *PendingTxTracker) notifyPendingTransactionListeners(payload PendingTxUpdatePayload, addresses []eth.Address, timestamp uint64) {
tm.updateTxIdentity(&payload.TxIdentity, payload.ChainID.ToUint(), ethTypes.Hash(payload.Hash))

jsonPayload, err := json.Marshal(payload)
if err != nil {
tm.logger.Error("Failed to marshal PendingTxUpdatePayload", zap.Stringer("hash", payload.Hash), zap.Error(err))
Expand Down

0 comments on commit 4a6f483

Please sign in to comment.