wagmi 中 estimateMaxPriorityFeePerGas 详解:在 @wagmi/core 中估算最大优先费(max priority fee per gas)

wagmi 中 estimateMaxPriorityFeePerGas 详解:在 @wagmi/core 中估算最大优先费(max priority fee per gas) wagmi 中 estimateMaxPriorityFeePerGas 详解在 wagmi/core 中估算最大优先费max priority fee per gas【免费下载链接】wagmiReactive primitives for Ethereum apps项目地址: https://gitcode.com/GitHub_Trending/wa/wagmi本篇技术指南围绕 wagmi 提供的estimateMaxPriorityFeePerGasaction 展开介绍如何在wagmi/core中获取下一区块内交易大概率被打包所需的最大优先费估算值以 wei 为单位完整覆盖导入方式、调用示例、chainId参数、返回类型、错误类型与 TanStack Query 集成方式。读完本文后你可以直接在自己的 wagmi 应用中读取 EIP-1559 优先费估算并结合源码理解其底层调用链。一、功能定位EIP-1559 下的优先费估算在 EIP-1559 的 gas 模型中一笔交易的费用由两部分构成基础费base fee由网络自动确定、随区块被销毁优先费priority fee即小费 tip用户为激励验证者优先打包交易而额外支付的部分。estimateMaxPriorityFeePerGas返回的正是这笔优先费的上限估算值单位 wei表示为了让交易在下一个区块中被包含进去需要设置的最大优先费大概是多少。它对应wagmi/core中一个独立的异步 action是构建交易参数尤其是与estimateFeesPerGas配合使用时的重要基础。从源码看该 action 位于 packages/core/src/actions/estimateMaxPriorityFeePerGas.ts是对 viem 同名 action 的 wagmi 封装export async function estimateMaxPriorityFeePerGas config extends Config, chainId extends config[chains][number][id] config[chains][number][id], ( config: config, parameters: EstimateMaxPriorityFeePerGasParametersconfig, chainId {}, ): PromiseEstimateMaxPriorityFeePerGasReturnType { const { chainId } parameters const client config.getClient({ chainId }) const action getAction( client, viem_estimateMaxPriorityFeePerGas, estimateMaxPriorityFeePerGas, ) return action({ chain: client.chain }) }它通过config.getClient({ chainId })按链解析客户端再借助getAction工具把 viem 的estimateMaxPriorityFeePerGas挂到对应 client 上执行最后以client.chain作为链上下文完成调用。二、导入与基础用法1. 导入在wagmi/core中导入该 action 及其配套类型import { estimateMaxPriorityFeePerGas } from wagmi/core import { type EstimateMaxPriorityFeePerGasParameters, type EstimateMaxPriorityFeePerGasReturnType, type EstimateMaxPriorityFeePerGasErrorType, } from wagmi/core2. 基础调用最低限度只需要传入通过createConfig创建的config对象import { estimateMaxPriorityFeePerGas } from wagmi/core import { config } from ./config const result await estimateMaxPriorityFeePerGas(config)返回的result是一个bigint即下一区块所需最大优先费的估算值wei。示例中的config使用createConfig创建配置了mainnet与sepolia两条链完整写法参见 site/snippets/core/config.tsimport { createConfig, http } from wagmi/core import { mainnet, sepolia } from wagmi/core/chains export const config createConfig({ chains: [mainnet, sepolia], transports: { [mainnet.id]: http(), [sepolia.id]: http(), }, })仓库测试 packages/core/src/actions/estimateMaxPriorityFeePerGas.test.ts 中即用该配置验证了默认调用可正常解析出值test(default, async () { await expect(estimateMaxPriorityFeePerGas(config)).resolves.toBeDefined() })三、参数详解chainIdEstimateMaxPriorityFeePerGasParameters在 viem 参数基础上通过UnionLooseOmit去掉了chain字段并注入了 wagmi 的ChainIdParameter其核心参数为参数类型说明chainIdconfig[chains][number][id] \| undefined获取数据时使用的链 IDchainId用于显式指定在哪条链上执行估算不传时使用config的默认链。显式指定示例配合wagmi/core/chains中导出的链定义import { estimateMaxPriorityFeePerGas } from wagmi/core import { mainnet } from wagmi/core/chains import { config } from ./config const result await estimateMaxPriorityFeePerGas(config, { chainId: mainnet.id, })测试中同样覆盖了指定chainId的路径test(parameters: chainId, async () { await expect( estimateMaxPriorityFeePerGas(config, { chainId: chain.mainnet2.id, }), ).resolves.toBeDefined() })调用链说明从源码可见chainId只用于config.getClient({ chainId })选择客户端真正的估算请求由 viem 的estimateMaxPriorityFeePerGas在client.chain上发出。也就是说 wagmi 层只负责解析客户端 路由参数底层协议估算逻辑全部交给 viem因此两种调用方式最终都返回同一语义的结果Promisebigint单位 wei。四、返回类型EstimateMaxPriorityFeePerGasReturnType直接复用了 viem 的返回类型export type EstimateMaxPriorityFeePerGasReturnType viem_EstimateMaxPriorityFeePerGasReturnType其值为bigint即下一区块中交易被包含所需最大优先费的估算值wei。实际使用时通常需要配合formatGwei/formatWei之类的格式化工具转成可读单位再填入交易参数如maxPriorityFeePerGas。五、错误类型与 TanStack Query 集成1. 错误类型EstimateMaxPriorityFeePerGasErrorType同样是 viem 错误类型的别名export type EstimateMaxPriorityFeePerGasErrorType viem_EstimateMaxPriorityFeePerGasErrorType在wagmi/core中它涵盖底层 RPC 调用可能抛出的错误如链未配置、RPC 请求失败等便于在try/catch或 TanStack Query 的错误通道中做类型化处理。2. TanStack Querywagmi/core/querywagmi 为每个 action 都提供了 Query 集成层。从 packages/core/src/query/estimateMaxPriorityFeePerGas.ts 可以看到它暴露了如下工具import { type EstimateMaxPriorityFeePerGasData, type EstimateMaxPriorityFeePerGasOptions, type EstimateMaxPriorityFeePerGasQueryFnData, type EstimateMaxPriorityFeePerGasQueryKey, estimateMaxPriorityFeePerGasQueryKey, estimateMaxPriorityFeePerGasQueryOptions, } from wagmi/core/query其核心是estimateMaxPriorityFeePerGasQueryOptions它把参数收敛进 queryKey[estimateMaxPriorityFeePerGas, options]并用queryFn内部调用 actionreturn { ...options.query, queryFn: async (context) { const [, { scopeKey: _, ...parameters }] context.queryKey return estimateMaxPriorityFeePerGas(config, parameters) }, queryKey: estimateMaxPriorityFeePerGasQueryKey(options), }测试 packages/core/src/query/estimateMaxPriorityFeePerGas.test.ts 确认了 queryKey 的形态// 默认queryKey 为 [estimateMaxPriorityFeePerGas, {}] // 指定 chainId: 1 时queryKey 为 [estimateMaxPriorityFeePerGas, { chainId: 1 }]3. React HookuseEstimateMaxPriorityFeePerGas在 React 框架适配层wagmi/react中useEstimateMaxPriorityFeePerGas封装了上述 Query 层未显式传chainId时会自动回退到当前激活链useChainId()const chainId useChainId({ config }) const options estimateMaxPriorityFeePerGasQueryOptions(config, { ...parameters, chainId: parameters.chainId ?? chainId, }) return useQuery(options)这意味着在组件中你可以这样读取优先费估算import { useEstimateMaxPriorityFeePerGas } from wagmi const { data, isPending, error } useEstimateMaxPriorityFeePerGas() // data: bigint | undefined —— 最大优先费估算值wei六、适用前提与限制说明适用链仅适用于支持 EIP-1559或类似优先费机制的网络在非 EIP-1559 网络上该估算语义可能不适用请以实际 RPC 返回为准。依赖 viem本 action 是 viem 同名 action 的薄封装实际 RPC 逻辑由 viem 完成具体底层实现细节可参考 viem 的estimateMaxPriorityFeePerGas文档。返回单位返回值始终为 weibigint展示给用户前需自行格式化。实测参考仓库测试对默认调用与chainId显式指定两条路径均有覆盖见 estimateMaxPriorityFeePerGas.test.ts 与 estimateMaxPriorityFeePerGas.test.ts可作为集成验证依据。七、总结estimateMaxPriorityFeePerGas是 wagmi 中获取 EIP-1559 最大优先费估算的核心 action使用await estimateMaxPriorityFeePerGas(config, { chainId? })参数chainId可选决定在哪个链上估算返回bigintwei类型的安全估算值错误处理EstimateMaxPriorityFeePerGasErrorType类型化错误生态集成wagmi/core/query提供 Query 工具React 侧对应useEstimateMaxPriorityFeePerGasHook。在构建 EIP-1559 交易时将它与estimateFeesPerGas总费用估算配合使用即可得到一套完整的maxFeePerGasmaxPriorityFeePerGas参数组合。【免费下载链接】wagmiReactive primitives for Ethereum apps项目地址: https://gitcode.com/GitHub_Trending/wa/wagmi创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考