Gas Price Queries for Integrations
Because x/dynamicfee
uses a dynamic fee, end-users will need to query the module for the current gasPrice
to use when building a transaction.
A summary for the flow to query this information is as follows:
- Create an RPC connection with the chain
- Create a
dynamicfee
client - Submit the
GasPrice
query - Use the
gasPrice
to populate the Tx fee field.
Extensive querying information can be seen in the module spec.
The specific query for GasPrices
can be found here.
Code Snippet
Wallet, relayers, and other users will want to add programmatic ways to query this before building their transactions. Below is an example of how a user could implement this lightweight query in Go:
Create A gRPC Connection
First, a base connection to the chain you are querying must be created.
A chain gRPC (below) or CometBFT ABCI RPC connection can be created:
// Set up gRPC connection to chain
cc, err := grpc.NewClient(endpoint, insecure.NewCredentials())
if err != nil {
panic(err)
}
defer cc.Close()
Create a Dynamicfee Query Client
An x/dynamicfee
query client can then be created using the created gRPC connection.
This client exposes all queries that the x/dynamicfee
module exposes.
// Create DynamicfeeClient with underlying gRPC connection
dynamicfeeClient := dynamicfeetypes.NewQueryClient(cc)
Query Gas Prices
The gas price
(as an sdk.DecCoin
) can be queried using the GasPrice
query. This query requires the desired coin denomination for the fee to be paid with.
The query will return an error if the given denomination is not supported.
gasPrice, err := dynamicfeeClient.GasPrice(ctx, &dynamicfeetypes.GasPriceRequest{
Denom: denom,
})
if err != nil {
panic(err)
}
Using gasPrice
to construct a transaction
There are two ways to construct a transaction with gasPrice
:
- Provide the minimum fee:
feeAmount = gasPrice * gasLimit
(gasLimit
gives the maximum amount of gas a transaction can consume. You can obtain appropriategasLimit
by simulating a transaction to see how much gas it consumes under normal conditions). - Provide a higher fee with respect to the minimum fee:
feeAmount=gasPrice * gasLimit + tip
; this will result in your transaction being placed ahead of others with lower tips (or being included in the block instead of others when the block is full)
Examples of Other EIP-1559 Integrations
The original skip-mev/feemarket
from which this module is based on provides a similar implementation.
Also, the Osmosis Blockchain has a similar EIP-1559 mechanism that has been integrated by wallets and relayers. Below are some examples as to how different projects query the dynamic fee for transactions:
- Keplr Wallet EIP-1559 BaseFee Query
- Cosmos-Relayer EIP-1559 BaseFee Query
- Hermes Relayer EIP-1559 Fee Query
- Note: Hermes also already implements a query
x/feemarket
seen here
- Note: Hermes also already implements a query