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: gas spending protection #261

Open
wants to merge 4 commits into
base: main
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
6 changes: 3 additions & 3 deletions relayer/chains/evm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ var (
type Config struct {
provider.CommonConfig `json:",inline" yaml:",inline"`
WebsocketUrl string `json:"websocket-url" yaml:"websocket-url"`
GasMin uint64 `json:"gas-min" yaml:"gas-min"`
GasLimit uint64 `json:"gas-limit" yaml:"gas-limit"`
GasPriceCap uint64 `json:"gas-price-cap" yaml:"gas-price-cap"`
GasAdjustment uint64 `json:"gas-adjustment" yaml:"gas-adjustment"`
BlockBatchSize uint64 `json:"block-batch-size" yaml:"block-batch-size"`
}
Expand Down Expand Up @@ -257,7 +256,8 @@ func (p *Provider) GetTransationOpts(ctx context.Context) (*bind.TransactOpts, e
if err != nil {
p.log.Warn("failed to get gas tip", zap.Error(err))
}
txOpts.GasFeeCap = gasPrice.Mul(gasPrice, big.NewInt(2))
// add 20% buffer to gas fee cap
txOpts.GasFeeCap = gasPrice.Mul(gasPrice, big.NewInt(120)).Div(gasPrice, big.NewInt(100))
txOpts.GasTipCap = gasTip
return txOpts, nil
}
Expand Down
11 changes: 4 additions & 7 deletions relayer/chains/evm/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package evm
import (
"context"
"fmt"
"math/big"
"strings"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
Expand Down Expand Up @@ -56,16 +57,12 @@ func (p *Provider) SendTransaction(ctx context.Context, opts *bind.TransactOpts,
return nil, fmt.Errorf("failed to estimate gas: %w", err)
}

if gasLimit > p.cfg.GasLimit {
return nil, fmt.Errorf("gas limit exceeded: %d", gasLimit)
}
opts.GasLimit = gasLimit + (gasLimit * p.cfg.GasAdjustment / 100)

if gasLimit < p.cfg.GasMin {
return nil, fmt.Errorf("gas price less than minimum: %d", gasLimit)
if opts.GasFeeCap.Cmp(new(big.Int).SetUint64(p.cfg.GasPriceCap)) > 0 {
return nil, fmt.Errorf("gas fee cap is too high")
}

opts.GasLimit = gasLimit + (gasLimit * p.cfg.GasAdjustment / 100)

p.log.Info("transaction info",
zap.Uint64("gas_cap", opts.GasFeeCap.Uint64()),
zap.Uint64("gas_tip", opts.GasTipCap.Uint64()),
Expand Down
Loading