Fees
Configure chain-based fee data in Viem
You can modify how fees are derived by using the fees
property on the Chain.
Usage
import { defineChain } from 'viem'
export const example = defineChain({
/* ... */
fees: {
baseFeeMultiplier: 1.2,
defaultPriorityFee: parseGwei('0.01'),
}
})
API
fees.baseFeeMultiplier
- Type:
number
- Default:
1.2
The fee multiplier to use to account for fee fluctuations. Used in the estimateFeesPerGas
Action against the latest block's base fee per gas to derive a final maxFeePerGas
(EIP-1193), or gas price to derive a final gasPrice
(Legacy).
block
: The latest block.client
: The Client instance.request
: The transaction request (if exists).
import { defineChain } from 'viem'
const example = defineChain({
/* ... */
fees: {
baseFeeMultiplier: 1.2,
// or
async baseFeeMultiplier({ block, request }) {
// some async work
return // ...
},
},
})
fees.defaultPriorityFee
- Type:
number | ((args: FeesFnParameters) => Promise<bigint> | bigint)
The default maxPriorityFeePerGas
to use when a priority fee is not defined upon sending a transaction.
Also overrides the return value in the estimateMaxPriorityFeePerGas
Action and maxPriorityFeePerGas
value in estimateFeesPerGas
.
block
: The latest block.client
: The Client instance.request
: The transaction request (if exists).
import { defineChain } from 'viem'
const example = defineChain({
/* ... */
fees: {
defaultPriorityFee: parseGwei('0.01'),
// or
async defaultPriorityFee({ block, request }) {
// some async work
return // ...
},
},
})
fees.estimateFeesPerGas
- Type:
(args: FeesFnParameters) => Promise<EstimateFeesPerGasResponse>
Allows customization of fee per gas values (ie. maxFeePerGas
, maxPriorityFeePerGas
, gasPrice
).
Also overrides the return value in estimateFeesPerGas
.
block
: The latest block.client
: The Client instance.multiply
: A function to apply thebaseFeeMultiplier
to the provided value.request
: The transaction request (if exists).type
: The transaction type (ie.legacy
oreip1559
).
import { defineChain } from 'viem'
const example = defineChain({
/* ... */
fees: {
async estimateFeesPerGas({ client, multiply, type }) {
const gasPrice = // ...
const baseFeePerGas = // ...
const maxPriorityFeePerGas = // ...
if (type === 'legacy') return { gasPrice: multiply(gasPrice) }
return {
maxFeePerGas: multiply(baseFeePerGas) + maxPriorityFeePerGas,
maxPriorityFeePerGas
},
},
},
})