1 of 32

ERC-4337: Payment, validation and Deployment

How to pay for your transaction, validate your transaction and deploy your AA contract in ERC-4337

Apr 24 2023 | POPOP Taipei

Equal access to the Tokenized World

2 of 32

Nic Lin

imToken Labs

3 of 32

How to pay for your transaction in ERC-4337?

4 of 32

How to pay for your transaction in ERC-4337?

  • ERC-4337 transaction payment is made in ETH, just like normal ETH transactions

User

Bundler

ETH

ERC20

Proposer

ETH

5 of 32

How to pay for your transaction in ERC-4337?

  • ERC-4337 transaction payment is made in ETH, just like normal ETH transactions
    • But you can not use `approve`/`transferFrom` with ETH
    • So user has to deposit ETH to Entrypoint and pay Entrypoint with the deposited ETH

User

Bundler

ETH

Entrypoint

ETH

6 of 32

How to pay for your transaction in ERC-4337?

  • User can deposit via
    • transfer ETH directly to Entrypoint
    • or call `Entrypoint.depositTo(address account)`

User

Entrypoint

ETH

depositTo(User)

ETH

7 of 32

How to pay for your transaction in ERC-4337?

  • During validation, Entrypoint will calculate total gas fee required:
    • `maxFeePerGas * (callGasLimit + verificationGasLimit + preVerificationGas) `

User

Gas price I’m willing to pay

Max gas for my execution

Max gas for validating my transaction

Gas for Entrypoint processing overhead

8 of 32

How to pay for your transaction in ERC-4337?

  • During validation, Entrypoint will calculate total gas fee required:
    • `maxFeePerGas * (callGasLimit + verificationGasLimit + preVerificationGas)`
  • If `balanceOf(user)` not enough, Entrypoint will pass the amount short to user’s AA contract during `validateUserOp` (validation step for user’s AA contract)
    • `validateUserOp(..., missingAccountFunds)`
    • User will have to deposit enough ETH during `validateUserOp`

Entrypoint

User

User’s AA contract

validateUserOp(...)

ETH

Your deposit not enough, please deposit

9 of 32

How to pay for your transaction in ERC-4337?

Entrypoint

User

User’s AA contract

validateUserOp(...)

ETH

Your deposit not enough, please deposit

Bundler

This is my UserOp, I’m willing to pay��`maxFeePerGas * (callGasLimit + verificationGasLimit + preVerificationGas)` ��ETH for this op.

10 of 32

How to pay for your transaction in ERC-4337?

11 of 32

How to validate your transaction in ERC-4337?

12 of 32

How to validate your transaction in ERC-4337?

  • Entrypoint calls validateUserOp function on user’s AA contract
    • If the call succeed and returns `0`, validation passed
    • If the call reverted or returns `1`, validation failed

Entrypoint

User’s AA contract

validateUserOp(...)

returns 0

User authorized 👍

Entrypoint

User’s AA contract

validateUserOp(...)

returns 1 or revert

User did NOT authorize 👎

13 of 32

How to validate your transaction in ERC-4337?

  • validateUserOp(userOp, userOpHash, …)
    • User’s AA contract should verify `userOp.signature` against `userOpHash`
      • User can sign and verify using any signing algorithm

Entrypoint

User’s AA contract

validateUserOp(...)

returns 0

Signature checked.

User authorized 👍

ECDSA

14 of 32

How to validate your transaction in ERC-4337?

  • If validation succeed, Entrypoint calls user’s AA contract again to execute what user want to execute

Entrypoint

User’s AA contract

validateUserOp(...)

returns 0

User authorized 👍

execute(...)

15 of 32

How to validate your transaction in ERC-4337?

  • IMPORTANT: you need to validate caller in your execute function
    • DO NOT assume only Entrypoint will call your AA contract

Entrypoint

User’s AA contract

execute(...)

Entrypoint want me to execute, it must have called validateUserOp already

swap

16 of 32

How to validate your transaction in ERC-4337?

  • IMPORTANT: you need to validate caller in your execute function
    • DO NOT assume only Entrypoint will call your AA contract

User’s AA contract

execute(...)

You are not Entrypoint

17 of 32

How to validate your transaction in ERC-4337?

18 of 32

How to deploy your AA contract in ERC-4337?

19 of 32

How to deploy your AA contract in ERC-4337?

  • 1. User pre-deploys the AA contract himself
  • 2. Entrypoint deploys user’s AA contract when user executes his first transaction

20 of 32

How to deploy your AA contract in ERC-4337?

  • 1. User pre-deploys the AA contract himself
  • 2. Entrypoint deploys user’s AA contract when user executes his first transaction

21 of 32

How to deploy your AA contract in ERC-4337?

  • Entrypoint deploys user’s AA contract when user executes his first transaction
    • User needs to inform Entrypoint of deployment information

22 of 32

How to deploy your AA contract in ERC-4337?

  • Entrypoint deploys user’s AA contract when user executes his first transaction
    • User needs to inform Entrypoint of deployment information
      • If user does not inform and user’s contract has not been deployed
        • Transaction failure

Entrypoint

User

User’s AA contract

Please execute my UserOp

check contract exists

Not exist

23 of 32

How to deploy your AA contract in ERC-4337?

  • Entrypoint deploys user’s AA contract when user executes his first transaction
    • User needs to inform Entrypoint of deployment information
      • If user does not inform and user’s contract has not been deployed
        • Transaction failure
      • If user provide deployment information while user’s contract is already deployed
        • Transaction failure

Entrypoint

User

Please deploy my AA contract and execute my UserOp

deploy

Already exist

User’s AA contract

24 of 32

How to deploy your AA contract in ERC-4337?

  • Entrypoint deploys user’s AA contract when user executes his first transaction
    • User needs to inform Entrypoint of deployment information
      • Factory of user’s AA contract and calldata for the factory

Entrypoint

User

Please deploy my AA contract, this is the factory address and data to call the factory for my contract deployment

Factory of user’s AA contract

25 of 32

How to deploy your AA contract in ERC-4337?

  • Entrypoint deploys user’s AA contract when user executes his first transaction
    • User needs to inform Entrypoint of deployment information
      • Factory of user’s AA contract and calldata for the factory

User

In the initcode field of my UserOp:

UserOp {

initcode: [20 bytes factory address][calldata for factory]

}

26 of 32

How to deploy your AA contract in ERC-4337?

  • Entrypoint deploys user’s AA contract when user executes his first transaction
    • User needs to inform Entrypoint of deployment information
      • Factory of user’s AA contract and calldata for the factory

UserOp {

initcode: [20 bytes factory address][calldata for factory]

}

For example:

ABI encode of “createAccount(owner,salt)”

27 of 32

How to deploy your AA contract in ERC-4337?

  • Entrypoint deploys user’s AA contract when user executes his first transaction
    • User needs to inform Entrypoint of deployment information
      • If deployed contract address does not match the sender in transaction
        • Transaction failure

Entrypoint

Bob

Please deploy my AA contract and execute my UserOp

deploy

This is not Bob’s AA contract

Alice’s AA contract

28 of 32

How to deploy your AA contract in ERC-4337?

  • Entrypoint deploys user’s AA contract when user executes his first transaction
    • User needs to inform Entrypoint of deployment information
      • If deployed contract address does not match the sender in transaction
        • Transaction failure
      • User needs to precompute his AA contract address using factory
        • set this address as the sender in transaction

Bob

Where will my contract be deployed at?

Factory of Bob’s AA contract

getAddress(owner,salt)

29 of 32

How to deploy your AA contract in ERC-4337?

Bob

Where will my contract be deployed at?

Factory of Bob’s AA contract

getAddress(owner,salt)

0x1234beef

30 of 32

How to deploy your AA contract in ERC-4337?

Entrypoint

Bob

Please deploy my AA contract and execute my UserOp

deploy

UserOp {

sender: 0x1234beef

initcode: …

}

User’s AA contract

Factory of Bob’s AA contract

deploy

31 of 32

How to deploy your AA contract in ERC-4337?

32 of 32

Thank you

Apr 24 2023 | POPOP Taipei

Equal access to the Tokenized World