Skip to content

Unsigned ethers v6 type 2 transaction #4739

Open
@mrosendin

Description

@mrosendin

Suggestion

I cannot find an example anywhere for the use case where a client creates an unsigned type 2 (e.g., EIP-1559 / Dynamic Fee) transaction object. A typical use case might be to send the unsigned tx via API to a digital asset custodian for signing.

I have been testing with op-geth and get typed transaction too short or other similar errors. It would be beneficial to document the use case of deploying a contract.

const myContractFactory = new ethers.ContractFactory(
    MyContract.abi,
    MyContract.bytecode,
    provider // instantiation omitted
)

// Prepare the deployment data
const deployTransaction = await myContractFactory.getDeployTransaction(
  deployParam1, // omitted
  deployParam2 // omitted
)

const WALLET_ADDRESS = "..." // omitted

// Get the gas price and nonce
const feeData = await provider.getFeeData()
const gasPrice = feeData.gasPrice || ethers.parseUnits('20', 'gwei') // Set a default gas price if null

// Prepare the transaction parameters
const nonce = await provider.getTransactionCount(WALLET_ADDRESS)
const gasLimit = 3000000
const chainId = 84532

const txRequest = {
    nonce: nonce,
    to: ethers.ZeroAddress,
    maxFeePerGas: feeData.maxFeePerGas,
    maxPriorityFeePerGas: feeData.maxPriorityFeePerGas,
    gasLimit: gasLimit,
    data: deployTransaction.data,
    chainId: chainId,
    value: 0,
    type: 2, // EIP-1559 tx envelope, see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2718.md
    accessList: [],
}

When op-geth tries to RLP decode, I get this error:

{
  error: 'BAD_GATEWAY',
  status: 502,
  description: 'decompiler failed to decompile transaction: rpc error: code = Unknown desc = typed transaction too short\n' +
    'error rlp decoding dynamic transaction\n' +
    'failed to decompile transaction'
}

Furthermore, if I instead try to manually RLP-encode the transaction, I get back a different error (maybe due to the ordering of the fields?)

const rlpFields = [
  ethers.toBeHex(chainId),
  ethers.toBeHex(Number(nonce)).replace(/^0x0+/, "0x"), // Remove leading 0 padding
  ethers.toBeHex(Number(feeData.maxPriorityFeePerGas)),
  ethers.toBeHex(Number(feeData.maxFeePerGas)),
  ethers.toBeHex(gasLimit).replace(/^0x0+/, "0x"), // Remove leading 0 padding
  tx.to,
  ethers.toBeHex(value),
  tx.data,
  [], // accessList
];
  
//  Data below submitted without the leading `0x` or you get error "encoding/hex: invalid byte: U+0078 'x'"
ethers.encodeRlp(rlpFields).slice(2)

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationDocumentation related issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions