---
title: "使用者操作費用如何估算與收取"
description: "了解 bundler 的誘因機制、bundler 如何估算使用者操作的 gas 費用，以及發送者如何在 Arbitrum 和 Optimism 上估算使用者操作費用。"
---

# 使用者操作費用如何估算與收取

在我們關於 ERC-4337 gas 估算的系列文章中,從 [ERC-4337 gas 估算如何運作](https://www.alchemy.com/blog/erc-4337-gas-estimation) 開始,我們已經討論過「User Operation Flow」的第 2 步 `eth\_estimateUserOperationGas`,以及其中的複雜情況,包括 [gas token 轉帳問題](https://www.alchemy.com/blog/dummy-signatures-and-gas-token-transfers) 和 [估算 L2 gas](https://www.alchemy.com/blog/l2-gas-and-signature-aggregators) 的挑戰。這篇文章將聚焦於流程的第 3 步,詳細說明 gas 費用是如何被估算與收取的。

在這篇文章中,我們假設所使用的鏈支援 EIP-1559。理解 [EIP-1559](https://eips.ethereum.org/EIPS/eip-1559) 是掌握 user operation 中費用欄位的必要背景知識。

User operation 的費用欄位與 EIP-1559 交易相同:

- `maxFeePerGas`:使用者出價願意支付的最高總金額（base fee \+ priority fee）
- `maxPriorityFeePerGas`:使用者出價的最高 priority fee 或「小費」

Entry point 使用以下公式從這兩個欄位 [計算出最終的 gas price](https://github.com/eth-infinitism/account-abstraction/blob/033b4be2a606defd5cd5226bdde3afcea21db5fa/contracts/core/EntryPoint.sol#L600):

如你所見,`maxFeePerGas` 是 gas price 的絕對上限,而 `maxPriorityFeePerGas` 則是在該區塊 base fee 之上的小費。

## Bundle 交易

當 [bundler 將 bundle 交易發送](https://www.alchemy.com/overviews/what-is-a-bundler) 給 entry point 時,它們同樣受到由 EIP-1559 機制推導出的 gas price 約束。Bundler 會設定一個 `maxFeePerGas` 和一個 `maxPriorityFeePerGas`。最終的 gas price 會根據這兩者以及該交易被納入區塊的 base fee 計算出來。

Bundler 以某個 gas price 提交交易,然後依照 bundle 中每個 user operation 所設定的 gas price（可能不同）獲得補償。如果 user operation 的 gas price 較高,bundler 會獲利;反之,bundler 則會在該 user operation 上虧損。`maxPriorityFeePerGas` 不只是給 bundler 的小費,也在幫助 bundler 補償它為了讓 bundle 交易被納入而付給區塊建構者（block builder）的小費（除非 bundler 本身就是 builder）。

### Bundler 如何在財務上獲得誘因？

Bundling 可能是一項成本高昂的工作,主要是因為需要進行複雜的模擬以確保 bundler 的安全性。為了讓 bundler 能夠回收這些運算成本,並激勵一個健康的 user operation 市場（這能透過降低納入時間來改善使用者體驗）,bundler 必須以超額收益的形式獲得財務誘因。

這種收益可以有多種形式：priority fee、MEV、以及鏈下交易。

#### 1. Priority fee

Bundler 可能會要求傳送者在其 operation 中包含比 bundler 在其交易上支付的 priority fee 更高的金額。想在高壅塞期間提高被納入 bundle 機率的傳送者,可以提高其 priority fee 出價。

#### 2. MEV 收益

由於 bundler 對 bundle 中的順序擁有決定權,它們可以從 mempool 中的 operation 提取 MEV。它們可以自由地進行 back-run 與夾擊（sandwich）operation 以賺取這部分利潤。

#### 3. 鏈下

Bundler 可以運行私有 mempool（具備如 MEV 保護等功能）,並與 user operation 傳送者進行鏈下交易。費用會透過某種鏈下機制來強制執行。

為了確保網路健康,我們認為對於大多數 user operation 而言,(1) 是必要的選項。

並非每種 user operation 類型都能提取到正向的 MEV。如果我們只依賴這個誘因,bundler 將不會接手大多數的 user operation。鏈下交易具有中心化的傾向,許多使用者會想避免這種方式。這就剩下了 Priority Fee。

Alchemy 的 Rundler 目前定義了一個動態的 bundling 所需 priority fee。這個費用是根據當前估算的網路費用計算的。如果一個 user operation 出價低於估算的網路費用加上給 bundler 的一小部分額外小費,它就不會被 bundle,而會留在 mempool 中。

<CalloutBlock>

💡 當 P2P 網路出現時,情況會更像一個市場,類似於交易的市場。傳送者可以分析先前已上鏈的 user operation,來判斷最低價格大概是多少。Bundler 可以根據當前 mempool 的內容向使用者提供建議。MEV 搜尋者也可能會從 mempool 中挑選具有高額相關 MEV 的 user operation,即使這些 operation 支付較少（甚至為零）的 gas（甚至可能要求搜尋者反過來 [付錢給使用者](https://github.com/blunt-instruments/MevWallet)）。這種 MEV 市場可以透過 [Orderflow Auction](https://frontier.tech/the-orderflow-auction-design-space)（OFA）來促成。

</CalloutBlock>

## Bundler 如何估算 gas 費用？

在 [bundling](https://www.alchemy.com/overviews/what-is-account-abstraction) 之前,bundler 必須對其將要提交的費用進行自己的估算,以確保該 bundle 交易能透過 bundle 市場被納入。通常 bundler 會針對下一個區塊進行納入的建構,並出一個足夠合理的價格以確保被納入。

要估算 base fee,bundler 可以向網路查詢當前區塊的 base fee。通常至少加上 25% 的餘裕（這是接下來 2 個區塊中費用可能上漲的最大幅度）是可取的,以確保交易不會卡住。

要估算 priority fee,bundler 可以使用 `eth\_maxPriorityFeePerGas`。與 base fee 類似,為此加上一定百分比的餘裕是可取的,以確保交易會被納入。

估算出這些費用後,bundler 便可以查詢 mempool,並篩選出出價足以被納入（包含給 bundler 的小額「小費」）的 user operation。

<CalloutBlock>

💡 這個章節描述的是使用 bundle 市場來提交其 bundle 的 Bundler。Bundler 必須使用 bundle 市場（例如 [Flashbots](https://www.alchemy.com/dapps/flashbots)）,以確保其 user operation 的 bundle 不會被搶跑（front-run）,導致在鏈上被 revert,讓 bundler 自己承擔 gas 費用。

如果 bundler 本身是區塊建構者的一部分,情況則會不同。

</CalloutBlock>

### 傳送者應該如何估算 user operation 的費用？

在客戶端,傳送者必須估算 bundling 所需的費用。如果估算過低,其 user operation 可能會卡在 mempool 中。如果估算過高,尤其是 priority fee 部分,可能會為納入而多付了錢。

**在與 p2p 網路推出前的 Rundler 互動時,我們建議採用以下方法：**

1. 估算 base fee。加上你可以接受的餘裕,以確保在價格對你不利時仍能被上鏈。我們建議至少加上 25%。
1. 使用 `eth\_maxPriorityFeePerGas` 估算 priority fee。加上你可以接受的餘裕。我們建議至少加上 10%。
1. 將與網路相關的 bundler「小費」餘裕加到 priority fee 上。
1. 將 priority fee 加到 base fee 上得到 `maxFeePerGas`,`maxPriorityFeePerGas` 則是第 2 步驟中估算出的費用。

### Arbitrum 費用估算

Arbitrum sequencer 會忽略 priority fee,`eth\_maxPriorityFeePerGas ` 始終回傳 0。為了補償 bundler,我們要求 priority fee 必須是 bundler 估算的最大 base fee 的固定百分比。我們目前收取 5%。

### Optimism 費用估算

Optimism 有一個邊緣情況,我們在先前 [估算 L2 gas](https://www.alchemy.com/blog/l2-gas-and-signature-aggregators) 的文章中有描述過,這會影響費用估算。

在 Optimism 上,我們設定 `maxPriorityFeePerGas = l1\_gas / l2\_base\_fee`。然而,由於在一般的 Optimism 交易中,priority fee 只適用於 L2 gas 的部分,它們可能比 L2 base fee 高出許多數量級。因此,我們要求 bundle 的 priority fee 必須是估算 base fee 的固定百分比。我們目前收取 5%。

<CalloutBlock>

💡 更多關於 Alchemy 的 [bundler 費用邏輯](https://www.alchemy.com/docs/reference/bundler-api-quickstart) 詳情,可以在我們的文件中找到。

</CalloutBlock>

## P2p mempool

一旦 P2P mempool 開始運行,將會出現一個獨立的 user operation 費用市場。這可能會大幅簡化 user operation 的費用估算。傳送者不再需要像前述那樣進行靜態的 priority fee 計算,而是可以使用費用歷史記錄來估算其出價。單一 bundler 選擇 user operation 所使用的標準已不再重要,重要的只是_某個_ bundler 認為該 user operation 有足夠的價值值得 bundle。

Rundler 將支援一個類似於 `eth\_feeHistory` 的 `rundler\_userOperationFeeHistory` 方法（或類似的方法,也可能被標準化）,使用者可以查詢此方法,根據先前已被 bundle 的 operation 來估算自己的出價。

🦀

## 繼續閱讀

如果你錯過了這個關於 ERC-4337 gas 估算的技術探討系列的前三篇文章,可以了解 ERC-4337 gas 估算如何運作,以及我們如何克服各種複雜情況,為 user operation 提供準確的 gas 估算。

- [ERC-4337 Gas 估算如何運作](https://www.alchemy.com/blog/erc-4337-gas-estimation)
- [Dummy Signature 與 Gas Token 轉帳](https://www.alchemy.com/blog/dummy-signatures-and-gas-token-transfers)
- [L2 Gas 與簽名聚合器](https://www.alchemy.com/blog/l2-gas-and-signature-aggregators)
