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

reduced origin min gas balance when quoting a route w/ origin gas token #3487

Open
wants to merge 1 commit into
base: feat/relayer-arb-call
Choose a base branch
from
Open
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
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)
Copy link
Collaborator

Choose a reason for hiding this comment

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

My suggestion here would be to add the thresholdMultiplier param to HasSufficientGas and remove HasSufficientGasWithMult- since you already have thresholdMultiplier as a pointer, it serves as optional param. This way we can keep the interface simple

// 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" {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can use util.IsGasToken() here instead of manual check

multiplier := 0.65
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would say this should probably be configurable, and we can default to 0.65 or some other sensible value

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
Loading