Skip to content

Commit c27c773

Browse files
saledjenicalaibe
authored andcommitted
chore(wallet)_: from and to chains added to send details
From and to chains added to SendDetails type that can be used on the client side if the sending fails that we can display appropriate ephemeral notifications, saying sending from which chain has failed.
1 parent 74db631 commit c27c773

File tree

5 files changed

+53
-21
lines changed

5 files changed

+53
-21
lines changed

services/wallet/responses/router_transactions.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ type SendDetails struct {
1515
SendType int `json:"sendType"`
1616
FromAddress types.Address `json:"fromAddress"`
1717
ToAddress types.Address `json:"toAddress"`
18+
FromChain uint64 `json:"fromChain"` // this chain should be used only if en error occurred while sending a transaction
19+
ToChain uint64 `json:"toChain"` // this chain should be used only if en error occurred while sending a transaction
1820
FromToken string `json:"fromToken"`
1921
ToToken string `json:"toToken"`
2022
FromAmount string `json:"fromAmount"` // total amount
@@ -88,7 +90,7 @@ func NewRouterSentTransaction(sendArgs *wallettypes.SendTxArgs, hash types.Hash,
8890
}
8991
}
9092

91-
func (sd *SendDetails) UpdateFields(inputParams requests.RouteInputParams) {
93+
func (sd *SendDetails) UpdateFields(inputParams requests.RouteInputParams, fromChain uint64, toChain uint64) {
9294
sd.SendType = int(inputParams.SendType)
9395
sd.FromAddress = types.Address(inputParams.AddrFrom)
9496
sd.ToAddress = types.Address(inputParams.AddrTo)
@@ -106,4 +108,8 @@ func (sd *SendDetails) UpdateFields(inputParams requests.RouteInputParams) {
106108
if inputParams.PackID != nil {
107109
sd.PackID = inputParams.PackID.String()
108110
}
111+
112+
// Set chain IDs, in case of an error while sending a transaction
113+
sd.FromChain = fromChain
114+
sd.ToChain = toChain
109115
}

services/wallet/routeexecution/manager.go

+17-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/status-im/status-go/services/wallet/routeexecution/storage"
2121
"github.com/status-im/status-go/services/wallet/router"
2222
pathProcessorCommon "github.com/status-im/status-go/services/wallet/router/pathprocessor/common"
23+
"github.com/status-im/status-go/services/wallet/router/routes"
2324
"github.com/status-im/status-go/services/wallet/router/sendtype"
2425
"github.com/status-im/status-go/services/wallet/transfer"
2526
"github.com/status-im/status-go/services/wallet/walletevent"
@@ -88,12 +89,14 @@ func (m *Manager) BuildTransactionsFromRoute(ctx context.Context, buildInputPara
8889

8990
m.buildInputParams = buildInputParams
9091

91-
response.SendDetails.UpdateFields(routeInputParams)
92+
fromChainID, toChainID := route.GetFirstPathChains()
93+
94+
response.SendDetails.UpdateFields(routeInputParams, fromChainID, toChainID)
9295

9396
// notify client that sending transactions started (has 3 steps, building txs, signing txs, sending txs)
9497
signal.SendWalletEvent(signal.RouterSendingTransactionsStarted, response.SendDetails)
9598

96-
response.SigningDetails, err = m.transactionManager.BuildTransactionsFromRoute(
99+
response.SigningDetails, fromChainID, toChainID, err = m.transactionManager.BuildTransactionsFromRoute(
97100
route,
98101
m.router.GetPathProcessors(),
99102
transfer.BuildRouteExtraParams{
@@ -105,6 +108,9 @@ func (m *Manager) BuildTransactionsFromRoute(ctx context.Context, buildInputPara
105108
SlippagePercentage: buildInputParams.SlippagePercentage,
106109
},
107110
)
111+
if err != nil {
112+
response.SendDetails.UpdateFields(routeInputParams, fromChainID, toChainID)
113+
}
108114
}()
109115
}
110116

@@ -114,6 +120,7 @@ func (m *Manager) SendRouterTransactionsWithSignatures(ctx context.Context, send
114120

115121
var (
116122
err error
123+
route routes.Route
117124
routeInputParams requests.RouteInputParams
118125
)
119126
response := &responses.RouterSentTransactions{
@@ -150,16 +157,19 @@ func (m *Manager) SendRouterTransactionsWithSignatures(ctx context.Context, send
150157
m.eventFeed.Send(event)
151158
}()
152159

153-
_, routeInputParams = m.router.GetBestRouteAndAssociatedInputParams()
160+
route, routeInputParams = m.router.GetBestRouteAndAssociatedInputParams()
154161
if routeInputParams.Uuid != sendInputParams.Uuid {
155162
err = ErrCannotResolveRouteId
156163
return
157164
}
158165

159-
response.SendDetails.UpdateFields(routeInputParams)
166+
fromChainID, toChainID := route.GetFirstPathChains()
167+
168+
response.SendDetails.UpdateFields(routeInputParams, fromChainID, toChainID)
160169

161-
err = m.transactionManager.ValidateAndAddSignaturesToRouterTransactions(sendInputParams.Signatures)
170+
fromChainID, toChainID, err = m.transactionManager.ValidateAndAddSignaturesToRouterTransactions(sendInputParams.Signatures)
162171
if err != nil {
172+
response.SendDetails.UpdateFields(routeInputParams, fromChainID, toChainID)
163173
return
164174
}
165175

@@ -194,8 +204,9 @@ func (m *Manager) SendRouterTransactionsWithSignatures(ctx context.Context, send
194204
}
195205
//////////////////////////////////////////////////////////////////////////////
196206

197-
response.SentTransactions, err = m.transactionManager.SendRouterTransactions(ctx, multiTx)
207+
response.SentTransactions, fromChainID, toChainID, err = m.transactionManager.SendRouterTransactions(ctx, multiTx)
198208
if err != nil {
209+
response.SendDetails.UpdateFields(routeInputParams, fromChainID, toChainID)
199210
log.Error("Error sending router transactions", "error", err)
200211
// TODO #16556: Handle partially successful Tx sends?
201212
// Don't return, store whichever transactions were successfully sent

services/wallet/router/routes/route.go

+13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ func (r Route) Copy() Route {
1717
return newRoute
1818
}
1919

20+
// GetFirstPathChains returns the chain IDs (from and to) of the first path in the route
21+
// If certain tx fails or succeeds status--go will send from/to chains along with other tx details to client to let
22+
// thc client know about failed/successful tx. But if an error occurs before the first path, during the preparation
23+
// of the route, the client will not have the chain IDs to know where the tx was supposed to be sent. This function
24+
// is used to get the chain IDs of the first path in the route to send them to the client in case of an error.
25+
func (r Route) GetFirstPathChains() (uint64, uint64) {
26+
if len(r) == 0 {
27+
return 0, 0
28+
}
29+
30+
return r[0].FromChain.ChainID, r[0].ToChain.ChainID
31+
}
32+
2033
func FindBestRoute(routes []Route, tokenPrice float64, nativeTokenPrice float64) Route {
2134
var best Route
2235
bestCost := big.NewFloat(math.Inf(1))

services/wallet/transfer/transaction_manager_route.go

+15-13
Original file line numberDiff line numberDiff line change
@@ -214,19 +214,19 @@ func buildTxForPath(transactor transactions.TransactorIface, path *routes.Path,
214214
}
215215

216216
func (tm *TransactionManager) BuildTransactionsFromRoute(route routes.Route, pathProcessors map[string]pathprocessor.PathProcessor,
217-
params BuildRouteExtraParams) (*responses.SigningDetails, error) {
217+
params BuildRouteExtraParams) (*responses.SigningDetails, uint64, uint64, error) {
218218
if len(route) == 0 {
219-
return nil, ErrNoRoute
219+
return nil, 0, 0, ErrNoRoute
220220
}
221221

222222
accFrom, err := tm.accountsDB.GetAccountByAddress(types.Address(params.AddressFrom))
223223
if err != nil {
224-
return nil, err
224+
return nil, 0, 0, err
225225
}
226226

227227
keypair, err := tm.accountsDB.GetKeypairByKeyUID(accFrom.KeyUID)
228228
if err != nil {
229-
return nil, err
229+
return nil, 0, 0, err
230230
}
231231

232232
response := &responses.SigningDetails{
@@ -246,7 +246,7 @@ func (tm *TransactionManager) BuildTransactionsFromRoute(route routes.Route, pat
246246
if path.ApprovalRequired && !tm.ApprovalPlacedForPath(path.ProcessorName) {
247247
txDetails.ApprovalTxData, err = buildApprovalTxForPath(tm.transactor, path, params.AddressFrom, usedNonces, signer)
248248
if err != nil {
249-
return nil, err
249+
return nil, path.FromChain.ChainID, path.ToChain.ChainID, err
250250
}
251251
response.Hashes = append(response.Hashes, txDetails.ApprovalTxData.HashToSign)
252252

@@ -259,12 +259,12 @@ func (tm *TransactionManager) BuildTransactionsFromRoute(route routes.Route, pat
259259
// build tx for the path
260260
txDetails.TxData, err = buildTxForPath(tm.transactor, path, pathProcessors, usedNonces, signer, params)
261261
if err != nil {
262-
return nil, err
262+
return nil, path.FromChain.ChainID, path.ToChain.ChainID, err
263263
}
264264
response.Hashes = append(response.Hashes, txDetails.TxData.HashToSign)
265265
}
266266

267-
return response, nil
267+
return response, 0, 0, nil
268268
}
269269

270270
func getSignatureForTxHash(txHash string, signatures map[string]requests.SignatureDetails) ([]byte, error) {
@@ -309,26 +309,26 @@ func validateAndAddSignature(txData *wallettypes.TransactionData, signatures map
309309
return nil
310310
}
311311

312-
func (tm *TransactionManager) ValidateAndAddSignaturesToRouterTransactions(signatures map[string]requests.SignatureDetails) error {
312+
func (tm *TransactionManager) ValidateAndAddSignaturesToRouterTransactions(signatures map[string]requests.SignatureDetails) (uint64, uint64, error) {
313313
if len(tm.routerTransactions) == 0 {
314-
return ErrNoTrsansactionsBeingBuilt
314+
return 0, 0, ErrNoTrsansactionsBeingBuilt
315315
}
316316

317317
// check if all transactions have been signed
318318
var err error
319319
for _, desc := range tm.routerTransactions {
320320
err = validateAndAddSignature(desc.ApprovalTxData, signatures)
321321
if err != nil {
322-
return err
322+
return desc.RouterPath.FromChain.ChainID, desc.RouterPath.ToChain.ChainID, err
323323
}
324324

325325
err = validateAndAddSignature(desc.TxData, signatures)
326326
if err != nil {
327-
return err
327+
return desc.RouterPath.FromChain.ChainID, desc.RouterPath.ToChain.ChainID, err
328328
}
329329
}
330330

331-
return nil
331+
return 0, 0, nil
332332
}
333333

334334
func addSignatureAndSendTransaction(
@@ -355,11 +355,13 @@ func addSignatureAndSendTransaction(
355355
return responses.NewRouterSentTransaction(txData.TxArgs, txData.SentHash, isApproval), nil
356356
}
357357

358-
func (tm *TransactionManager) SendRouterTransactions(ctx context.Context, multiTx *MultiTransaction) (transactions []*responses.RouterSentTransaction, err error) {
358+
func (tm *TransactionManager) SendRouterTransactions(ctx context.Context, multiTx *MultiTransaction) (transactions []*responses.RouterSentTransaction, fromChainID uint64, toChainID uint64, err error) {
359359
transactions = make([]*responses.RouterSentTransaction, 0)
360360

361361
// send transactions
362362
for _, desc := range tm.routerTransactions {
363+
fromChainID = desc.RouterPath.FromChain.ChainID
364+
toChainID = desc.RouterPath.ToChain.ChainID
363365
if desc.ApprovalTxData != nil && !desc.IsApprovalPlaced() {
364366
var response *responses.RouterSentTransaction
365367
response, err = addSignatureAndSendTransaction(tm.transactor, desc.ApprovalTxData, multiTx.ID, true)

transactions/pendingtxtracker.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ func (tm *PendingTxTracker) updateTxDetails(txDetails *TxDetails, chainID uint64
358358
}
359359
if routeData != nil {
360360
if routeData.RouteInputParams != nil {
361-
txDetails.SendDetails.UpdateFields(*routeData.RouteInputParams)
361+
txDetails.SendDetails.UpdateFields(*routeData.RouteInputParams, 0, 0)
362362
}
363363

364364
for _, pd := range routeData.PathsData {

0 commit comments

Comments
 (0)