Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rfq-relayer): insert record even for transactions with invalid tokens #3473

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions services/rfq/relayer/reldb/base/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ type RequestForQuote struct {
SendChainGas bool
// RelayNonce is the nonce for the relay transaction.
RelayNonce uint64
// Reason is the reason why a transaction may not be processed.
Reason string
}

// Rebalance is the event model for a rebalance action.
Expand Down Expand Up @@ -137,6 +139,7 @@ func FromQuoteRequest(request reldb.QuoteRequest) RequestForQuote {
Status: request.Status,
BlockNumber: request.BlockNumber,
RelayNonce: request.RelayNonce,
Reason: request.Reason,
}
}

Expand Down Expand Up @@ -225,6 +228,7 @@ func (r RequestForQuote) ToQuoteRequest() (*reldb.QuoteRequest, error) {
OriginTxHash: common.HexToHash(r.OriginTxHash.String),
DestTxHash: common.HexToHash(r.DestTxHash.String),
RelayNonce: r.RelayNonce,
Reason: r.Reason,
}, nil
}

Expand Down
1 change: 1 addition & 0 deletions services/rfq/relayer/reldb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ type QuoteRequest struct {
DestTxHash common.Hash
// RelayNonce is the nonce for the relay transaction.
RelayNonce uint64
Reason string
}

// GetOriginIDPair gets the origin chain id and token address pair.
Expand Down
39 changes: 25 additions & 14 deletions services/rfq/relayer/service/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,32 +91,43 @@
return fmt.Errorf("could not make call: %w", err)
}

dbReq := reldb.QuoteRequest{
BlockNumber: req.Raw.BlockNumber,
RawRequest: req.Request,
TransactionID: req.TransactionId,
Sender: req.Sender,
Transaction: bridgeTx,
Status: reldb.Seen,
OriginTxHash: req.Raw.TxHash,
}

Check warning on line 103 in services/rfq/relayer/service/handlers.go

View check run for this annotation

Codecov / codecov/patch

services/rfq/relayer/service/handlers.go#L94-L103

Added lines #L94 - L103 were not covered by tests
// TODO: you can just pull these out of inventory. If they don't exist mark as invalid.
originDecimals, destDecimals, err := r.getDecimalsFromBridgeTx(ctx, bridgeTx)
// can't use errors.is here
if err != nil && strings.Contains(err.Error(), "no contract code at given address") {
logger.Warnf("invalid token, skipping")
dbReq.Status = reldb.WillNotProcess
dbReq.Reason = "invalid token (no contract code at address)"
err = r.db.StoreQuoteRequest(ctx, dbReq)
if err != nil {
return fmt.Errorf("could not store db request: %w", err)
}

Check warning on line 113 in services/rfq/relayer/service/handlers.go

View check run for this annotation

Codecov / codecov/patch

services/rfq/relayer/service/handlers.go#L108-L113

Added lines #L108 - L113 were not covered by tests
return nil
}

if err != nil || originDecimals == nil || destDecimals == nil {
dbReq.Status = reldb.WillNotProcess
dbReq.Reason = fmt.Sprintf("could not get decimals: %s", err.Error())
err = r.db.StoreQuoteRequest(ctx, dbReq)
if err != nil {
return fmt.Errorf("could not store db request: %w", err)
}

Check warning on line 122 in services/rfq/relayer/service/handlers.go

View check run for this annotation

Codecov / codecov/patch

services/rfq/relayer/service/handlers.go#L117-L122

Added lines #L117 - L122 were not covered by tests
Comment on lines +117 to +122
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve error handling consistency.

The error handling can be improved in two ways:

  1. Avoid duplicating the error message between Reason and returned error
  2. Use consistent error wrapping patterns
 dbReq.Status = reldb.WillNotProcess
-dbReq.Reason = fmt.Sprintf("could not get decimals: %s", err.Error())
+dbReq.Reason = fmt.Sprintf("could not get decimals: %v", err)
 err = r.db.StoreQuoteRequest(ctx, dbReq)
 if err != nil {
     return fmt.Errorf("could not store db request: %w", err)
 }
-return fmt.Errorf("could not get decimals: %w", err)
+return fmt.Errorf("%s: %w", dbReq.Reason, err)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
dbReq.Status = reldb.WillNotProcess
dbReq.Reason = fmt.Sprintf("could not get decimals: %s", err.Error())
err = r.db.StoreQuoteRequest(ctx, dbReq)
if err != nil {
return fmt.Errorf("could not store db request: %w", err)
}
dbReq.Status = reldb.WillNotProcess
dbReq.Reason = fmt.Sprintf("could not get decimals: %v", err)
err = r.db.StoreQuoteRequest(ctx, dbReq)
if err != nil {
return fmt.Errorf("could not store db request: %w", err)
}
return fmt.Errorf("%s: %w", dbReq.Reason, err)

return fmt.Errorf("could not get decimals: %w", err)
}
dbReq.OriginTokenDecimals = *originDecimals
dbReq.DestTokenDecimals = *destDecimals

Check warning on line 126 in services/rfq/relayer/service/handlers.go

View check run for this annotation

Codecov / codecov/patch

services/rfq/relayer/service/handlers.go#L125-L126

Added lines #L125 - L126 were not covered by tests

dbReq := reldb.QuoteRequest{
BlockNumber: req.Raw.BlockNumber,
RawRequest: req.Request,
OriginTokenDecimals: *originDecimals,
DestTokenDecimals: *destDecimals,
TransactionID: req.TransactionId,
Sender: req.Sender,
Transaction: bridgeTx,
Status: reldb.Seen,
OriginTxHash: req.Raw.TxHash,
}
err = r.db.StoreQuoteRequest(ctx, dbReq)
if err != nil {
return fmt.Errorf("could not get db: %w", err)
return fmt.Errorf("could not store db request: %w", err)

Check warning on line 130 in services/rfq/relayer/service/handlers.go

View check run for this annotation

Codecov / codecov/patch

services/rfq/relayer/service/handlers.go#L130

Added line #L130 was not covered by tests
}

// immediately forward the request to handleSeen
Expand Down
Loading