Skip to content

Commit

Permalink
Merge pull request #32 from dongrie/estimate-gas
Browse files Browse the repository at this point in the history
Add estimate gas
  • Loading branch information
siburu authored Jan 31, 2024
2 parents 1d2a2d4 + fac1f10 commit d1da2f1
Show file tree
Hide file tree
Showing 6 changed files with 406 additions and 70 deletions.
29 changes: 29 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,32 @@ func searchRevertReason(result *callFrame) (string, error) {
}
return "", fmt.Errorf("revert reason not found")
}

func (cl *ETHClient) EstimateGasFromTx(ctx context.Context, tx *gethtypes.Transaction) (uint64, error) {
from, err := gethtypes.LatestSignerForChainID(tx.ChainId()).Sender(tx)
if err != nil {
return 0, err
}
to := tx.To()
value := tx.Value()
gasTipCap := tx.GasTipCap()
gasFeeCap := tx.GasFeeCap()
gasPrice := tx.GasPrice()
data := tx.Data()
accessList := tx.AccessList()
callMsg := ethereum.CallMsg{
From: from,
To: to,
GasPrice: gasPrice,
GasTipCap: gasTipCap,
GasFeeCap: gasFeeCap,
Value: value,
Data: data,
AccessList: accessList,
}
estimatedGas, err := cl.EstimateGas(ctx, callMsg)
if err != nil {
return 0, err
}
return estimatedGas, nil
}
5 changes: 2 additions & 3 deletions pkg/relay/ethereum/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ func (chain *Chain) CallOpts(ctx context.Context, height int64) *bind.CallOpts {

func (chain *Chain) TxOpts(ctx context.Context) *bind.TransactOpts {
return &bind.TransactOpts{
From: chain.signer.Address(),
GasLimit: 6382056,
Signer: chain.signer.Sign,
From: chain.signer.Address(),
Signer: chain.signer.Sign,
}
}
9 changes: 9 additions & 0 deletions pkg/relay/ethereum/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ func (c ChainConfig) Validate() error {
if c.MaxRetryForInclusion == 0 {
errs = append(errs, fmt.Errorf("config attribute \"max_retry_for_inclusion\" is zero"))
}
if c.GasEstimateRate.Numerator == 0 {
errs = append(errs, fmt.Errorf("config attribute \"gas_estimate_rate.numerator\" is zero"))
}
if c.GasEstimateRate.Denominator == 0 {
errs = append(errs, fmt.Errorf("config attribute \"gas_estimate_rate.denominator\" is zero"))
}
if c.MaxGasLimit == 0 {
errs = append(errs, fmt.Errorf("config attribute \"max_gas_limit\" is zero"))
}
if c.Signer == nil {
errs = append(errs, fmt.Errorf("config attribute \"signer\" is empty"))
} else if err := c.Signer.GetCachedValue().(SignerConfig).Validate(); err != nil {
Expand Down
Loading

0 comments on commit d1da2f1

Please sign in to comment.