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] estimate tx fee #12

Merged
merged 6 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix baseFee
  • Loading branch information
nkitlabs committed Nov 19, 2024
commit 2f20735c0cb3b22e7b823e00acdba0c40d3bfdbf
14 changes: 14 additions & 0 deletions relayer/chains/evm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Client interface {
Query(ctx context.Context, gethAddr gethcommon.Address, data []byte) ([]byte, error)
EstimateGas(ctx context.Context, msg ethereum.CallMsg) (uint64, error)
EstimateGasPrice(ctx context.Context) (*big.Int, error)
EstimateBaseFee(ctx context.Context) (*big.Int, error)
EstimateGasTipCap(ctx context.Context) (*big.Int, error)
BroadcastTx(ctx context.Context, tx *gethtypes.Transaction) (string, error)
GetBalance(ctx context.Context, gethAddr gethcommon.Address) (*big.Int, error)
Expand Down Expand Up @@ -336,6 +337,19 @@ func (c *client) EstimateGasPrice(ctx context.Context) (*big.Int, error) {
return gasPrice, nil
}

// EstimateBaseFee estimates the current base fee on the EVM chain.
func (c *client) EstimateBaseFee(ctx context.Context) (*big.Int, error) {
newCtx, cancel := context.WithTimeout(ctx, c.QueryTimeout)
defer cancel()

latestHeader, err := c.client.HeaderByNumber(newCtx, nil)
if err != nil {
return nil, err
}

return latestHeader.BaseFee, nil
}

// EstimateGasTipCap estimates the current gas tip cap on the EVM chain.
func (c *client) EstimateGasTipCap(ctx context.Context) (*big.Int, error) {
newCtx, cancel := context.WithTimeout(ctx, c.QueryTimeout)
Expand Down
17 changes: 10 additions & 7 deletions relayer/chains/evm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,12 @@ func (cp *EVMChainProvider) EstimateGas(ctx context.Context) (GasInfo, error) {
return GasInfo{}, err
}

return cp.BumpAndBoundGas(
ctx,
NewGasEIP1559Info(priorityFee, big.NewInt(int64(cp.Config.MaxBaseFee))),
1.0,
)
baseFee, err := cp.Client.EstimateBaseFee(ctx)
if err != nil {
return GasInfo{}, err
}

return cp.BumpAndBoundGas(ctx, NewGasEIP1559Info(priorityFee, baseFee), 1.0)
default:
return GasInfo{}, fmt.Errorf("unsupported gas type: %v", cp.GasType)
}
Expand Down Expand Up @@ -458,8 +459,10 @@ func (cp *EVMChainProvider) BumpAndBoundGas(
newPriorityFee = maxPriorityFee
}

// this can be fixed value, no need to query from chain.
newBaseFee := big.NewInt(int64(cp.Config.MaxBaseFee))
newBaseFee := gasInfo.GasBaseFee
if newBaseFee.Cmp(big.NewInt(int64(cp.Config.MaxBaseFee))) > 0 {
newBaseFee = big.NewInt(int64(cp.Config.MaxBaseFee))
}

return NewGasEIP1559Info(newPriorityFee, newBaseFee), nil
default:
Expand Down
Loading