-
Notifications
You must be signed in to change notification settings - Fork 30
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
RFQ relayer continues generating quotes if one fails #2264
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -265,73 +265,93 @@ | |
// Essentially, if we know a destination chain token balance, then we just need to find which tokens are bridgeable to it. | ||
// We can do this by looking at the quotableTokens map, and finding the key that matches the destination chain token. | ||
// Generates quotes for a given chain ID, address, and balance. | ||
func (m *Manager) generateQuotes(ctx context.Context, chainID int, address common.Address, balance *big.Int) ([]model.PutQuoteRequest, error) { | ||
func (m *Manager) generateQuotes(parentCtx context.Context, chainID int, address common.Address, balance *big.Int) (quotes []model.PutQuoteRequest, err error) { | ||
ctx, span := m.metricsHandler.Tracer().Start(parentCtx, "generateQuotes", trace.WithAttributes( | ||
attribute.Int(metrics.Origin, chainID), | ||
attribute.String("address", address.String()), | ||
attribute.String("balance", balance.String()), | ||
)) | ||
defer func() { | ||
metrics.EndSpanWithErr(span, err) | ||
}() | ||
|
||
destRFQAddr, err := m.config.GetRFQAddress(chainID) | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting destination RFQ address: %w", err) | ||
} | ||
|
||
destTokenID := fmt.Sprintf("%d-%s", chainID, address.Hex()) | ||
|
||
var quotes []model.PutQuoteRequest | ||
quotes = []model.PutQuoteRequest{} | ||
for keyTokenID, itemTokenIDs := range m.quotableTokens { | ||
for _, tokenID := range itemTokenIDs { | ||
//nolint:nestif | ||
if tokenID == destTokenID { | ||
// Parse token info | ||
originStr := strings.Split(keyTokenID, "-")[0] | ||
origin, err := strconv.Atoi(originStr) | ||
if err != nil { | ||
return nil, fmt.Errorf("error converting origin chainID: %w", err) | ||
} | ||
originTokenAddr := common.HexToAddress(strings.Split(keyTokenID, "-")[1]) | ||
|
||
// Calculate the quote amount for this route | ||
quoteAmount, err := m.getQuoteAmount(ctx, origin, chainID, address, balance) | ||
// don't quote if gas exceeds quote | ||
if errors.Is(err, errMinGasExceedsQuoteAmount) { | ||
quoteAmount = big.NewInt(0) | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Calculate the fee for this route | ||
destToken, err := m.config.GetTokenName(uint32(chainID), address.Hex()) | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting dest token ID: %w", err) | ||
} | ||
fee, err := m.feePricer.GetTotalFee(ctx, uint32(origin), uint32(chainID), destToken, true) | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting total fee: %w", err) | ||
} | ||
originRFQAddr, err := m.config.GetRFQAddress(origin) | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting RFQ address: %w", err) | ||
} | ||
|
||
// Build the quote | ||
destAmount, err := m.getDestAmount(ctx, quoteAmount, chainID) | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting dest amount: %w", err) | ||
} | ||
quote := model.PutQuoteRequest{ | ||
OriginChainID: origin, | ||
OriginTokenAddr: originTokenAddr.Hex(), | ||
DestChainID: chainID, | ||
DestTokenAddr: address.Hex(), | ||
DestAmount: destAmount.String(), | ||
MaxOriginAmount: quoteAmount.String(), | ||
FixedFee: fee.String(), | ||
OriginFastBridgeAddress: originRFQAddr, | ||
DestFastBridgeAddress: destRFQAddr, | ||
quote, quoteErr := m.generateQuote(ctx, keyTokenID, chainID, address, balance, destRFQAddr) | ||
if quoteErr != nil { | ||
// continue generating quotes even if one fails | ||
span.AddEvent("error generating quote", trace.WithAttributes( | ||
attribute.String("key_token_id", keyTokenID), | ||
)) | ||
continue | ||
Comment on lines
+291
to
+295
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The loop within Would you like assistance in writing tests to cover the new logic introduced in the |
||
} | ||
quotes = append(quotes, quote) | ||
quotes = append(quotes, *quote) | ||
} | ||
} | ||
} | ||
return quotes, nil | ||
} | ||
|
||
func (m *Manager) generateQuote(ctx context.Context, keyTokenID string, chainID int, address common.Address, balance *big.Int, destRFQAddr string) (quote *model.PutQuoteRequest, err error) { | ||
// Parse token info | ||
originStr := strings.Split(keyTokenID, "-")[0] | ||
origin, err := strconv.Atoi(originStr) | ||
if err != nil { | ||
return nil, fmt.Errorf("error converting origin chainID: %w", err) | ||
} | ||
Comment on lines
+310
to
+311
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Several error handling blocks within the I can help create tests that cover the error handling scenarios within the Also applies to: 319-320, 325-326, 329-330, 333-334, 339-340 |
||
originTokenAddr := common.HexToAddress(strings.Split(keyTokenID, "-")[1]) | ||
|
||
// Calculate the quote amount for this route | ||
quoteAmount, err := m.getQuoteAmount(ctx, origin, chainID, address, balance) | ||
// don't quote if gas exceeds quote | ||
if errors.Is(err, errMinGasExceedsQuoteAmount) { | ||
quoteAmount = big.NewInt(0) | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Calculate the fee for this route | ||
destToken, err := m.config.GetTokenName(uint32(chainID), address.Hex()) | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting dest token ID: %w", err) | ||
} | ||
fee, err := m.feePricer.GetTotalFee(ctx, uint32(origin), uint32(chainID), destToken, true) | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting total fee: %w", err) | ||
} | ||
originRFQAddr, err := m.config.GetRFQAddress(origin) | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting RFQ address: %w", err) | ||
} | ||
|
||
// Build the quote | ||
destAmount, err := m.getDestAmount(ctx, quoteAmount, chainID) | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting dest amount: %w", err) | ||
} | ||
quote = &model.PutQuoteRequest{ | ||
OriginChainID: origin, | ||
OriginTokenAddr: originTokenAddr.Hex(), | ||
DestChainID: chainID, | ||
DestTokenAddr: address.Hex(), | ||
DestAmount: destAmount.String(), | ||
MaxOriginAmount: quoteAmount.String(), | ||
FixedFee: fee.String(), | ||
OriginFastBridgeAddress: originRFQAddr, | ||
DestFastBridgeAddress: destRFQAddr, | ||
} | ||
return quote, nil | ||
} | ||
trajan0x marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// getQuoteAmount calculates the quote amount for a given route. | ||
func (m *Manager) getQuoteAmount(parentCtx context.Context, origin, dest int, address common.Address, balance *big.Int) (quoteAmount *big.Int, err error) { | ||
ctx, span := m.metricsHandler.Tracer().Start(parentCtx, "getQuoteAmount", trace.WithAttributes( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The added logic for handling gas balances when the gas token is not found in the inventory tokens map is a good improvement. It ensures that gas balances are properly managed, addressing a potential gap in the system's inventory management capabilities. However, it appears these changes are not covered by tests.
Ensuring test coverage for this new logic is crucial for validating its correctness and reliability. Consider adding unit tests that cover scenarios where the gas token is not found in the inventory tokens map, as well as cases where the gas balance is successfully retrieved or when it fails.