EIPs Explained

EIP-2831 · Stagnant · Interface · 2020-07-26

EIP-2831: Transaction Replacement Message Type

A stagnant extension to the EIP-1193 wallet provider API that emits tx_speedup, tx_cancel and tx_replacement events when a pending transaction is replaced, so dapps can follow it instead of polling.

What problem does it solve?

Wallets let users "speed up" or "cancel" a pending transaction. Under the hood both are the same trick: broadcast a new transaction with the same nonce and a higher fee, so it replaces the original before it is mined.

The dapp that requested the original transaction only knows the old hash. Once it is replaced, that hash never confirms, and the dapp has no standard way to learn the new one. Developers ended up polling block by block or guessing. EIP-2831 proposes that the provider simply tell the dapp.

How it works

It adds three events to the EIP-1193 provider (window.ethereum), using the Node.js EventEmitter style provider.on(...):

Event Meaning Replacement must keep
tx_speedup Same transaction, higher fee nonce, to, value, data
tx_cancel Original nullified nonce, same from/to, zero value, no data
tx_replacement Entirely different transaction nonce only

Every event delivers the same payload:

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

The spec defines a replacement as same nonce plus a 10 % higher gas price, which reflects common node mempool rules at the time it was written. Providers that do not support the events should ignore the subscription or surface an error.

Sample code

Listening for replacements in a browser dapp (assumes a provider that implements this proposal):

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"));

Things to know

  • Status: Stagnant. Do not assume any wallet emits these events; feature-detect and fall back.
  • A robust fallback that works everywhere: watch the sender's nonce. Once a transaction with that nonce is mined, look up which hash it was. Libraries such as ethers v6 detect replacements this way during tx.wait() and throw a TRANSACTION_REPLACED error describing the cancellation or replacement.
  • With EIP-1559 transactions, "higher gas price" means raising both maxFeePerGas and maxPriorityFeePerGas; the exact bump required is a node mempool policy, not a protocol rule.
  • The example in the spec's appendix destructures from an undefined message.data; use the listener argument directly as shown above.

#wallet #provider #interface #dapp #stagnant

Details

Authors
Gregory Markou (@GregTheGreek)
Status
Stagnant
Type
Standards Track / Interface
Created
2020-07-26
Requires

Machine-readable: Markdown · JSON