Skip to content

Commit

Permalink
Fix: make generateQuote() separate func, continue quoting on err
Browse files Browse the repository at this point in the history
  • Loading branch information
dwasse committed Mar 13, 2024
1 parent f6e4834 commit 6d4d33e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 50 deletions.
4 changes: 4 additions & 0 deletions services/rfq/relayer/inventory/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ func (i *inventoryManagerImpl) GetCommittableBalance(ctx context.Context, chainI
// the gas token may not be registered in the inventory tokens map,
// but it is always tracked in gasBalances.
if balance == nil && token == chain.EthAddress {
gasBalance, ok := i.gasBalances[chainID]
if !ok || gasBalance == nil {
return nil, fmt.Errorf("could not get gas balance for chain %d", chainID)
}

Check warning on line 90 in services/rfq/relayer/inventory/manager.go

View check run for this annotation

Codecov / codecov/patch

services/rfq/relayer/inventory/manager.go#L87-L90

Added lines #L87 - L90 were not covered by tests
balance = i.gasBalances[chainID]
}
return balance, nil
Expand Down
120 changes: 70 additions & 50 deletions services/rfq/relayer/quoter/quoter.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,73 +265,93 @@ func (m *Manager) prepareAndSubmitQuotes(ctx context.Context, inv map[int]map[co
// 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

Check warning on line 295 in services/rfq/relayer/quoter/quoter.go

View check run for this annotation

Codecov / codecov/patch

services/rfq/relayer/quoter/quoter.go#L291-L295

Added lines #L291 - L295 were not covered by tests
}
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)
}

Check warning on line 310 in services/rfq/relayer/quoter/quoter.go

View check run for this annotation

Codecov / codecov/patch

services/rfq/relayer/quoter/quoter.go#L309-L310

Added lines #L309 - L310 were not covered by tests
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
}

Check warning on line 320 in services/rfq/relayer/quoter/quoter.go

View check run for this annotation

Codecov / codecov/patch

services/rfq/relayer/quoter/quoter.go#L319-L320

Added lines #L319 - L320 were not covered by tests

// 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)
}

Check warning on line 326 in services/rfq/relayer/quoter/quoter.go

View check run for this annotation

Codecov / codecov/patch

services/rfq/relayer/quoter/quoter.go#L325-L326

Added lines #L325 - L326 were not covered by tests
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)
}

Check warning on line 330 in services/rfq/relayer/quoter/quoter.go

View check run for this annotation

Codecov / codecov/patch

services/rfq/relayer/quoter/quoter.go#L329-L330

Added lines #L329 - L330 were not covered by tests
originRFQAddr, err := m.config.GetRFQAddress(origin)
if err != nil {
return nil, fmt.Errorf("error getting RFQ address: %w", err)
}

Check warning on line 334 in services/rfq/relayer/quoter/quoter.go

View check run for this annotation

Codecov / codecov/patch

services/rfq/relayer/quoter/quoter.go#L333-L334

Added lines #L333 - L334 were not covered by tests

// Build the quote
destAmount, err := m.getDestAmount(ctx, quoteAmount, chainID)
if err != nil {
return nil, fmt.Errorf("error getting dest amount: %w", err)
}

Check warning on line 340 in services/rfq/relayer/quoter/quoter.go

View check run for this annotation

Codecov / codecov/patch

services/rfq/relayer/quoter/quoter.go#L339-L340

Added lines #L339 - L340 were not covered by tests
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
}

// 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(
Expand Down

0 comments on commit 6d4d33e

Please sign in to comment.