Skip to content

Commit

Permalink
chore(wallet)_: route execution related types moved to services/walle…
Browse files Browse the repository at this point in the history
…t/wallettypes
  • Loading branch information
saledjenic committed Nov 5, 2024
1 parent 5917270 commit 1a92352
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 60 deletions.
8 changes: 5 additions & 3 deletions services/wallet/routeexecution/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ import (
walletCommon "github.com/status-im/status-go/services/wallet/common"
"github.com/status-im/status-go/services/wallet/requests"
"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/router"
"github.com/status-im/status-go/services/wallet/router/sendtype"
"github.com/status-im/status-go/services/wallet/transfer"
"github.com/status-im/status-go/services/wallet/wallettypes"
"github.com/status-im/status-go/signal"
)

type Manager struct {
router *router.Router
transactionManager *transfer.TransactionManager
transferController *transfer.Controller
db *DB
db *storage.DB

// Local data used for storage purposes
buildInputParams *requests.RouterBuildTransactionsParams
Expand All @@ -39,7 +41,7 @@ func NewManager(walletDB *sql.DB, router *router.Router, transactionManager *tra
router: router,
transactionManager: transactionManager,
transferController: transferController,
db: NewDB(walletDB),
db: storage.NewDB(walletDB),
}
}

Expand Down Expand Up @@ -189,7 +191,7 @@ func (m *Manager) SendRouterTransactionsWithSignatures(ctx context.Context, send
// don't overwrite err since we want to process it in the deferred function
var tmpErr error
routerTransactions := m.transactionManager.GetRouterTransactions()
routeData := NewRouteData(&routeInputParams, m.buildInputParams, routerTransactions)
routeData := wallettypes.NewRouteData(&routeInputParams, m.buildInputParams, routerTransactions)
tmpErr = m.db.PutRouteData(routeData)
if tmpErr != nil {
log.Error("Error storing route data", "error", tmpErr)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package routeexecution
package storage

import (
"database/sql"
Expand All @@ -8,6 +8,7 @@ import (
"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/router/routes"
"github.com/status-im/status-go/services/wallet/wallettypes"
"github.com/status-im/status-go/sqlite"
)

Expand All @@ -19,7 +20,7 @@ func NewDB(db *sql.DB) *DB {
return &DB{db: db}
}

func (db *DB) PutRouteData(routeData *RouteData) (err error) {
func (db *DB) PutRouteData(routeData *wallettypes.RouteData) (err error) {
var tx *sql.Tx
tx, err = db.db.Begin()
if err != nil {
Expand Down Expand Up @@ -48,10 +49,18 @@ func (db *DB) PutRouteData(routeData *RouteData) (err error) {
return
}

func (db *DB) GetRouteData(uuid string) (*RouteData, error) {
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 @@ -92,7 +101,7 @@ func putBuildTxParams(creator sqlite.StatementCreator, p *requests.RouterBuildTr
return err
}

func putPathsData(creator sqlite.StatementCreator, uuid string, d []*PathData) error {
func putPathsData(creator sqlite.StatementCreator, uuid string, d []*wallettypes.PathData) error {
for i, pathData := range d {
if err := putPathData(creator, uuid, i, pathData); err != nil {
return err
Expand All @@ -101,7 +110,7 @@ func putPathsData(creator sqlite.StatementCreator, uuid string, d []*PathData) e
return nil
}

func putPathData(creator sqlite.StatementCreator, uuid string, pathIdx int, d *PathData) (err error) {
func putPathData(creator sqlite.StatementCreator, uuid string, pathIdx int, d *wallettypes.PathData) (err error) {
err = putPath(creator, uuid, pathIdx, d.Path)
if err != nil {
return
Expand Down Expand Up @@ -150,7 +159,7 @@ func putPathTransaction(
uuid string,
pathIdx int,
txIdx int,
txData *TransactionData,
txData *wallettypes.TransactionData,
) error {
q := sq.Replace("route_path_transactions").
SetMap(sq.Eq{
Expand Down Expand Up @@ -181,7 +190,7 @@ func putPathTransaction(

func putSentTransaction(
creator sqlite.StatementCreator,
txData *TransactionData,
txData *wallettypes.TransactionData,
) error {
q := sq.Replace("sent_transactions").
SetMap(sq.Eq{
Expand All @@ -206,7 +215,7 @@ func putSentTransaction(
return err
}

func getRouteData(creator sqlite.StatementCreator, uuid string) (*RouteData, error) {
func getRouteData(creator sqlite.StatementCreator, uuid string) (*wallettypes.RouteData, error) {
routeInputParams, err := getRouteInputParams(creator, uuid)
if err != nil {
return nil, err
Expand All @@ -222,7 +231,7 @@ func getRouteData(creator sqlite.StatementCreator, uuid string) (*RouteData, err
return nil, err
}

return &RouteData{
return &wallettypes.RouteData{
RouteInputParams: routeInputParams,
BuildInputParams: buildTxParams,
PathsData: pathsData,
Expand Down Expand Up @@ -271,16 +280,16 @@ func getBuildTxParams(creator sqlite.StatementCreator, uuid string) (*requests.R
return &p, err
}

func getPathsData(creator sqlite.StatementCreator, uuid string) ([]*PathData, error) {
var pathsData []*PathData
func getPathsData(creator sqlite.StatementCreator, uuid string) ([]*wallettypes.PathData, error) {
var pathsData []*wallettypes.PathData

paths, err := getPaths(creator, uuid)
if err != nil {
return nil, err
}

for pathIdx, path := range paths {
pathData := &PathData{Path: path}
pathData := &wallettypes.PathData{Path: path}
txs, err := getPathTransactions(creator, uuid, pathIdx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -329,12 +338,12 @@ func getPaths(creator sqlite.StatementCreator, uuid string) ([]*routes.Path, err
return paths, nil
}

func getPathTransactions(creator sqlite.StatementCreator, uuid string, pathIdx int) ([]*TransactionData, error) {
txs := make([]*TransactionData, 0)
func getPathTransactions(creator sqlite.StatementCreator, uuid string, pathIdx int) ([]*wallettypes.TransactionData, error) {
txs := make([]*wallettypes.TransactionData, 0)
q := sq.Select("rpt.is_approval", "rpt.chain_id", "rpt.tx_hash", "rpt.tx_args_json", "st.tx_json").
From("route_path_transactions rpt").
LeftJoin(`sent_transactions st ON
rpt.chain_id = st.chain_id AND
LeftJoin(`sent_transactions st ON
rpt.chain_id = st.chain_id AND
rpt.tx_hash = st.tx_hash`).
Where(sq.Eq{"uuid": uuid, "path_idx": pathIdx}).
OrderBy("tx_idx ASC")
Expand All @@ -357,7 +366,7 @@ func getPathTransactions(creator sqlite.StatementCreator, uuid string, pathIdx i
defer rows.Close()

for rows.Next() {
var tx TransactionData
var tx wallettypes.TransactionData
var txHash sql.RawBytes
err = rows.Scan(&tx.IsApproval, &tx.ChainID, &txHash, &sqlite.JSONBlob{Data: &tx.TxArgs}, &sqlite.JSONBlob{Data: &tx.Tx})
if err != nil {
Expand All @@ -371,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
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package routeexecution_test
package storage_test

import (
"encoding/json"

"github.com/status-im/status-go/services/wallet/requests"
"github.com/status-im/status-go/services/wallet/transfer"
"github.com/status-im/status-go/services/wallet/wallettypes"
)

type dbTestData struct {
name string
routeInputParams requests.RouteInputParams
buildInputParams *requests.RouterBuildTransactionsParams
transactionDetails []*transfer.RouterTransactionDetails
transactionDetails []*wallettypes.RouterTransactionDetails
}

func createDBTestData(name string, routeInputParamsJSON string, buildInputParamsJSON string, transactionDetailsJSON string) dbTestData {
Expand All @@ -28,7 +28,7 @@ func createDBTestData(name string, routeInputParamsJSON string, buildInputParams
panic(err)
}

transactionDetails := make([]*transfer.RouterTransactionDetails, 0)
transactionDetails := make([]*wallettypes.RouterTransactionDetails, 0)
err = json.Unmarshal([]byte(transactionDetailsJSON), &transactionDetails)
if err != nil {
panic(err)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package routeexecution_test
package storage_test

import (
"testing"

"github.com/status-im/status-go/services/wallet/routeexecution"
"github.com/status-im/status-go/services/wallet/routeexecution/storage"
"github.com/status-im/status-go/services/wallet/wallettypes"
"github.com/status-im/status-go/t/helpers"
"github.com/status-im/status-go/walletdatabase"
"github.com/stretchr/testify/require"
Expand All @@ -16,17 +17,25 @@ func Test_PutRouteData(t *testing.T) {
require.NoError(t, err)
defer closeFn()

routeDB := routeexecution.NewDB(walletDB)
routeDB := storage.NewDB(walletDB)

for _, tt := range testData {
t.Run(tt.name, func(t *testing.T) {
routeData := routeexecution.NewRouteData(&tt.routeInputParams, tt.buildInputParams, tt.transactionDetails)
routeData := wallettypes.NewRouteData(&tt.routeInputParams, tt.buildInputParams, tt.transactionDetails)
err := routeDB.PutRouteData(routeData)
require.NoError(t, err)

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)
}
}
})
}
}
25 changes: 1 addition & 24 deletions services/wallet/transfer/transaction_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/status-im/status-go/params"
wallet_common "github.com/status-im/status-go/services/wallet/common"
"github.com/status-im/status-go/services/wallet/router/pathprocessor"
"github.com/status-im/status-go/services/wallet/router/routes"
"github.com/status-im/status-go/services/wallet/wallettypes"
"github.com/status-im/status-go/transactions"
)
Expand All @@ -30,28 +29,6 @@ type TransactionDescription struct {
signature []byte
}

type RouterTransactionDetails struct {
RouterPath *routes.Path
TxArgs *wallettypes.SendTxArgs
Tx *ethTypes.Transaction
TxHashToSign types.Hash
TxSignature []byte
TxSentHash types.Hash
ApprovalTxArgs *wallettypes.SendTxArgs
ApprovalTx *ethTypes.Transaction
ApprovalHashToSign types.Hash
ApprovalSignature []byte
ApprovalTxSentHash types.Hash
}

func (rtd *RouterTransactionDetails) IsTxPlaced() bool {
return rtd.TxSentHash != types.Hash(wallet_common.ZeroHash())
}

func (rtd *RouterTransactionDetails) IsApprovalPlaced() bool {
return rtd.ApprovalTxSentHash != types.Hash(wallet_common.ZeroHash())
}

type TransactionManager struct {
storage MultiTransactionStorage
gethManager *account.GethManager
Expand All @@ -67,7 +44,7 @@ type TransactionManager struct {
transactionsForKeycardSigning map[common.Hash]*TransactionDescription

// used in a new approach
routerTransactions []*RouterTransactionDetails
routerTransactions []*wallettypes.RouterTransactionDetails
}

type MultiTransactionStorage interface {
Expand Down
6 changes: 3 additions & 3 deletions services/wallet/transfer/transaction_manager_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (tm *TransactionManager) buildApprovalTxForPath(path *routes.Path, addressF
approvalTxHash := signer.Hash(builtApprovalTx)
usedNonces[path.FromChain.ChainID] = int64(usedNonce)

tm.routerTransactions = append(tm.routerTransactions, &RouterTransactionDetails{
tm.routerTransactions = append(tm.routerTransactions, &wallettypes.RouterTransactionDetails{
RouterPath: path,
ApprovalTxArgs: approavalSendArgs,
ApprovalTx: builtApprovalTx,
Expand Down Expand Up @@ -191,7 +191,7 @@ func (tm *TransactionManager) buildTxForPath(path *routes.Path, pathProcessors m
txHash := signer.Hash(builtTx)
usedNonces[path.FromChain.ChainID] = int64(usedNonce)

tm.routerTransactions = append(tm.routerTransactions, &RouterTransactionDetails{
tm.routerTransactions = append(tm.routerTransactions, &wallettypes.RouterTransactionDetails{
RouterPath: path,
TxArgs: sendArgs,
Tx: builtTx,
Expand Down Expand Up @@ -354,6 +354,6 @@ func (tm *TransactionManager) SendRouterTransactions(ctx context.Context, multiT
return
}

func (tm *TransactionManager) GetRouterTransactions() []*RouterTransactionDetails {
func (tm *TransactionManager) GetRouterTransactions() []*wallettypes.RouterTransactionDetails {
return tm.routerTransactions
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package routeexecution
package wallettypes

import (
ethTypes "github.com/ethereum/go-ethereum/core/types"
"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/router/routes"
"github.com/status-im/status-go/services/wallet/transfer"
"github.com/status-im/status-go/services/wallet/wallettypes"
)

// These structs oontain all route execution data
Expand All @@ -26,13 +24,13 @@ type TransactionData struct {
ChainID uint64
TxHash types.Hash
IsApproval bool
TxArgs *wallettypes.SendTxArgs
TxArgs *SendTxArgs
Tx *ethTypes.Transaction
}

func NewRouteData(routeInputParams *requests.RouteInputParams,
buildInputParams *requests.RouterBuildTransactionsParams,
transactionDetails []*transfer.RouterTransactionDetails) *RouteData {
transactionDetails []*RouterTransactionDetails) *RouteData {

pathDataPerProcessorName := make(map[string]*PathData)
pathsData := make([]*PathData, 0, len(transactionDetails))
Expand Down
Loading

0 comments on commit 1a92352

Please sign in to comment.