Closed3

EIP-1559 なトランザクションを投げてみる

odanodan

以下 Ethers.js の場合

Documentation

サポートされたバージョン

v5.4.0 から EIP-1559 がサポートされた
https://github.com/ethers-io/ethers.js/blob/4e9394554b1f05400ea4e0529d33e1d4efe78247/CHANGELOG.md#ethersv540-2021-06-26-0150

トランザクションの投げ方

sendTransactiontype というオプションを渡せて、これによってレガシーか EIP-1559 かを設定できる
デフォルトでは EIP-1559 なトランザクションとして、トランザクションが投げられる

async function sendLegacyTx(wallet: Wallet) {
  return wallet.sendTransaction({
    to: wallet.address,
    type: 0,
  });
}

async function sendEIP1559Tx(wallet: Wallet) {
  return wallet.sendTransaction({
    to: wallet.address,
    // type が undefined だと 2 (EIP-1559) としてトランザクションが作成される
    // type: 2,
  });
}

実際に投げたトランザクション

odanodan

Web3.js の場合

サポートされたバージョン

v1.5.1 でサポートされた
https://github.com/ChainSafe/web3.js/releases/tag/v1.5.1

トランザクションの投げ方

sendTransactiontype というパラメータが渡せて 0x2 を渡すと EIP-1559 なトランザクションになる。デフォルトでは Legacy なトランザクションが投げられる

async function sendLegacyTx(web3: Web3, account: Account) {
  const from = account.address;
  const to = account.address;
  const tx = await web3.eth.sendTransaction({
    from,
    to,
    gas: "21000",
  });

  return tx;
}

async function sendEIP1559Tx(web3: Web3, account: Account) {
  const from = account.address;
  const to = account.address;
  // sendTransaction の型定義が EIP-1559 に対応していないので as any 必須
  const tx = await web3.eth.sendTransaction({
    from,
    to,
    type: "0x2",
    gas: "21000",
  } as any);

  return tx;
}

実際に投げたトランザクション

Legacy https://rinkeby.etherscan.io/tx/0xde0217561dae071f8b62023c08ebca7d6ccbfc0b38bd4c4cd18eb05cb1460ea3

EIP-1559 https://rinkeby.etherscan.io/tx/0xfcaa150282ee1fc5449a1d51c44042d5020d5a82dcc2b26c2cbdce6439749871

このスクラップは2022/08/06にクローズされました