Because x/feemarket
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
feemarket
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.
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:
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()
An x/feemarket
query client can then be created using the created gRPC connection.
This client exposes all queries that the x/feemarket
module exposes.
// Create FeeMarketClient with underlying gRPC connection
feeMarketClient := feemarkettypes.NewQueryClient(cc)
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 := feeMarketClient.GasPrice(ctx, &feemarkettypes.GasPriceRequest{
Denom: denom,
})
if err != nil {
panic(err)
}
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 "tip" in addition to the minimum fee:
feeAmount=gasPrice * gasLimit + tip
This will be paid to the block proposer and 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)
The actual amount of fee deducted from the fee payer is based on gas consumed, not gasLimit
. The total amount deducted (fee + tip
) will be equal to the amount of fee specified on your transaction.
The amount consumed is equal to the inferredTip + gasPrice * gasConsumed
, where inferredTip = feeAmount - gasLimit * gasPrice
(This may be different than the tip you specified when building the transaction because the gasPrice
on chain may have changed since when you queried it.)
The Osmosis Blockchain has a similar EIP-1559 feemarket 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