---
eip: 2831
label: EIP-2831
title: Transaction Replacement Message Type
status: Stagnant
type: Standards Track
category: Interface
created: 2020-07-26
requires: 1193
official: https://eips.ethereum.org/EIPS/eip-2831
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2831.md
canonical: https://eips.huatuo.xyz/ja/eip-2831/
lang: ja
tldr: EIP-1193 のウォレットプロバイダー API の停滞中の拡張。保留中のトランザクションが置き換えられると tx_speedup、tx_cancel、tx_replacement イベントを発行し、dapp がポーリングせずに追跡できるようにする。
tags: [wallet, provider, interface, dapp, stagnant]
---

# EIP-2831: Transaction Replacement Message Type

> An extension to the JavaScript Ethereum Provider API (EIP-1193) this creates a new message type in the event a transaction replacement occurs.

**TL;DR:** EIP-1193 のウォレットプロバイダー API の停滞中の拡張。保留中のトランザクションが置き換えられると tx_speedup、tx_cancel、tx_replacement イベントを発行し、dapp がポーリングせずに追跡できるようにする。

## 何を解決するのか

ウォレットでは、保留中のトランザクションを「スピードアップ」または「キャンセル」できます。内部では、どちらも同じ手口です。**同じ nonce** で、より高い手数料の新しいトランザクションをブロードキャストし、元のトランザクションがマイニングされる前にそれを置き換えます。

元のトランザクションを要求した dapp が知っているのは、古いハッシュだけです。置き換えられるとそのハッシュは二度と確定せず、dapp が新しいハッシュを知る標準的な方法はありません。開発者はブロックごとにポーリングしたり、推測したりするしかありませんでした。EIP-2831 は、プロバイダーが dapp に単に知らせるようにすることを提案します。

## 仕組み

[EIP-1193](/eip-1193/) のプロバイダー（`window.ethereum`）に、Node.js の `EventEmitter` スタイルの `provider.on(...)` で使える 3 つのイベントを追加します。

| イベント | 意味 | 置き換え後も維持しなければならないもの |
| --- | --- | --- |
| `tx_speedup` | 同じトランザクションで、より高い手数料 | nonce、`to`、`value`、`data` |
| `tx_cancel` | 元のトランザクションを無効化 | nonce、同じ from/to、value はゼロ、data なし |
| `tx_replacement` | まったく別のトランザクション | nonce のみ |

どのイベントも、同じペイロードを渡します。

```ts
interface TxReplacementInfo {
  readonly oldTx: string;  // hash of the superseded transaction
  readonly newTx: string;  // hash of the replacement
  readonly nonce: string;
  readonly from: string;
}
```

仕様は、置き換えを「同じ nonce で、ガス価格が 10 % 高いもの」と定義しています。これは、仕様が書かれた当時の一般的なノードの mempool ルールを反映したものです。イベントに対応していないプロバイダーは、購読を無視するか、エラーを出すべきです。

## サンプルコード

ブラウザの dapp で置き換えを待ち受ける例（この提案を実装したプロバイダーを前提とします）:

```ts
const ethereum = (window as any).ethereum;

let watched = await ethereum.request({
  method: "eth_sendTransaction",
  params: [{ from: account, to: recipient, value: "0x2386f26fc10000" }],
});

function onReplaced(kind: string) {
  return ({ oldTx, newTx, nonce }: { oldTx: string; newTx: string; nonce: string }) => {
    if (oldTx !== watched) return;
    console.log(`${kind}: ${oldTx} -> ${newTx} (nonce ${nonce})`);
    watched = newTx; // keep tracking the transaction that may actually confirm
  };
}

ethereum.on("tx_speedup", onReplaced("sped up"));
ethereum.on("tx_cancel", onReplaced("cancelled"));
ethereum.on("tx_replacement", onReplaced("replaced"));
```

## 知っておくべきこと

- ステータス: **Stagnant**。どのウォレットがこれらのイベントを発行すると思い込んではいけません。機能検出を行い、フォールバックを用意してください。
- どこでも動く堅牢なフォールバック: 送信者の nonce を監視します。その nonce のトランザクションがマイニングされたら、それがどのハッシュだったかを調べます。ethers v6 のようなライブラリは、`tx.wait()` の間にこの方法で置き換えを検出し、キャンセルまたは置き換えを説明する `TRANSACTION_REPLACED` エラーをスローします。
- [EIP-1559](/eip-1559/) のトランザクションでは、「ガス価格を上げる」とは `maxFeePerGas` と `maxPriorityFeePerGas` の両方を引き上げることを意味します。必要な引き上げ幅は、プロトコルのルールではなくノードの mempool ポリシーです。
- 仕様の付録にある例は、未定義の `message.data` から分割代入しています。上のように、リスナーの引数を直接使ってください。


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-2831
