Skip to content

Commit

Permalink
reduced origin min gas balance when quoting a route w/ origin gas token
Browse files Browse the repository at this point in the history
  • Loading branch information
parodime committed Jan 9, 2025
1 parent 15c2598 commit 8b05383
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
21 changes: 19 additions & 2 deletions services/rfq/relayer/inventory/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ type Manager interface {
ApproveAllTokens(ctx context.Context) error
// HasSufficientGas checks if there is sufficient gas for a given route.
HasSufficientGas(ctx context.Context, chainID int, gasValue *big.Int) (bool, error)
// HasSufficientGasWithMult checks if there is sufficient gas for a given route with an optional threshold multiplier applied.
HasSufficientGasWithMult(ctx context.Context, chainID int, gasValue *big.Int, thresholdMultiplier *float64) (bool, error)
// Rebalance attempts any rebalances that could be executed across all supported tokens and chains.
Rebalance(ctx context.Context) error
// GetTokenMetadata gets the metadata for a token.
Expand Down Expand Up @@ -438,6 +440,11 @@ func (i *inventoryManagerImpl) approve(parentCtx context.Context, tokenAddr, con

// HasSufficientGas checks if there is sufficient gas for a given route.
func (i *inventoryManagerImpl) HasSufficientGas(parentCtx context.Context, chainID int, gasValue *big.Int) (sufficient bool, err error) {
return i.HasSufficientGasWithMult(parentCtx, chainID, gasValue, nil)
}

// HasSufficientGasWithMult checks if there is sufficient gas for a given route with an optional threshold multiplier applied.
func (i *inventoryManagerImpl) HasSufficientGasWithMult(parentCtx context.Context, chainID int, gasValue *big.Int, thresholdMultiplier *float64) (sufficient bool, err error) {
ctx, span := i.handler.Tracer().Start(parentCtx, "HasSufficientGas", trace.WithAttributes(
attribute.Int(metrics.ChainID, chainID),
))
Expand All @@ -447,17 +454,27 @@ func (i *inventoryManagerImpl) HasSufficientGas(parentCtx context.Context, chain

gasThreshRaw, err := i.cfg.GetMinGasToken(chainID)
if err != nil {
return false, fmt.Errorf("error getting min gas token on origin: %w", err)
return false, fmt.Errorf("error getting min gas token: %w", err)
}
gasThresh := core.CopyBigInt(gasThreshRaw)
if gasValue != nil {
gasThresh = new(big.Int).Add(gasThresh, gasValue)
span.SetAttributes(attribute.String("gas_value", gasValue.String()))
}

// If param supplied, apply threshold multiplier before comparing
if thresholdMultiplier != nil {
if *thresholdMultiplier < 0 || *thresholdMultiplier > 2 {
return false, fmt.Errorf("thresholdMultiplier out of range: %f", *thresholdMultiplier)
}
gasThreshFloat := new(big.Float).SetInt(gasThresh)
gasThreshFloat.Mul(gasThreshFloat, big.NewFloat(*thresholdMultiplier))
gasThresh, _ = gasThreshFloat.Int(nil)
}

gasBalance, err := i.GetCommittableBalance(ctx, chainID, util.EthAddress, SkipDBCache())
if err != nil {
return false, fmt.Errorf("error getting committable gas on origin: %w", err)
return false, fmt.Errorf("error getting committable gas: %w", err)
}

sufficient = gasBalance.Cmp(gasThresh) >= 0
Expand Down
22 changes: 22 additions & 0 deletions services/rfq/relayer/inventory/mocks/manager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion services/rfq/relayer/quoter/quoter.go
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,17 @@ func (m *Manager) getOriginAmount(parentCtx context.Context, input QuoteInput) (
// First, check if we have enough gas to complete the a bridge for this route
// If not, set the quote amount to zero to make sure a stale quote won't be used
// TODO: handle in-flight gas; for now we can set a high min_gas_token
sufficentGasOrigin, err := m.inventoryManager.HasSufficientGas(ctx, input.OriginChainID, nil)

// if the origin token is native gas, require less minimum gas on the origin chain.
// as origin gas gets critically low, this will discourage the relayer from quoting most routes *except* those that will replenish the deficit gas.
var sufficentGasOrigin bool
if input.OriginTokenAddr.Hex() == "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" {
multiplier := 0.65
sufficentGasOrigin, err = m.inventoryManager.HasSufficientGasWithMult(ctx, input.OriginChainID, nil, &multiplier)
} else {
sufficentGasOrigin, err = m.inventoryManager.HasSufficientGas(ctx, input.OriginChainID, nil)
}

if err != nil {
return nil, fmt.Errorf("error checking sufficient gas: %w", err)
}
Expand Down

0 comments on commit 8b05383

Please sign in to comment.