Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document setup flow #80

Merged
merged 3 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ about: Bug report about the Safe smart contracts
## Prerequisites

- First, many thanks for taking part in the community and helping us improve. We appreciate that a lot.
- Support questions are better asked on our Discord: https://chat.gnosis-safe.io
- Support questions are better asked on our Discord: https://chat.safe.global
- Please ensure the issue isn't already reported.

*Please delete the above section and the instructions in the sections below before submitting*

## Description

Please describe considely the bug you have found, and what you expect instead.
Please describe concisely the bug you have found, and what you expect instead.

## Environment

Expand Down
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ about: Suggest an idea or feature request for the Safe smart contracts project
## Prerequisites

- First, many thanks for taking part in the community and helping us improve. We appreciate that a lot.
- Support questions are better asked on our Discord: https://chat.gnosis-safe.io
- Support questions are better asked on our Discord: https://chat.safe.global
- Please ensure the issue isn't already reported.

*Please delete the above section and the instructions in the sections below before submitting*
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci_4337.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Coveralls
uses: coverallsapp/github-action@master
with:
path-to-lcov: './4337/coverage/lcov.info'
path-to-lcov: '4337/coverage/lcov.info'
github-token: ${{ secrets.GITHUB_TOKEN }}
lint:
runs-on: ubuntu-latest
Expand Down
65 changes: 60 additions & 5 deletions 4337/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Safe Module/Fallback handler for EIP4337 Support

The diagram below outlines the flow that is triggered when a user operation is submitted to via the entrypoint. Additionally the gas overhead compared to a native implementation is mentioned.
## Execution Flow

The diagram below outlines the flow that is triggered when a user operation is submitted to the entrypoint. Additionally the gas overhead compared to a native implementation is mentioned.

```mermaid
sequenceDiagram
Expand All @@ -13,7 +15,7 @@ sequenceDiagram
B->>+E: Submit User Operations
E->>+P: Validate User Operation
P-->>S: Load Safe logic
Note over P, M: Gas overhead for calls and storage access
Note over P, M: Gas overhead for calls and storage access
P->>+M: Forward validation
Note over P, M: Load fallback handler ~2100 gas<br>Intital module access ~2600 gas
M->>P: Check signatures
Expand All @@ -27,7 +29,7 @@ sequenceDiagram
end
M-->>-P: Validation response
P-->>-E: Validation response
Note over P, M: Total gas overhead<br>Without fee payment ~4.900 gas<br>With fee payment ~7.200 gas
Note over P, M: Total gas overhead<br>Without fee payment ~4.900 gas<br>With fee payment ~7.200 gas
Note over B, T: This execution flow is similar<br>for native 4337 support<br>therefore there is no gas overhead
E->>+P: executeTransactionFromModule
P-->>S: Load Safe logic
Expand All @@ -38,6 +40,59 @@ The gas overhead is based on [EIP-2929](https://eips.ethereum.org/EIPS/eip-2929)

Note: The gas overhead is only the very base line using storage access and call costs. As also additional costs occur to handle the storage access and calls the actual gas overhead is higher. The tests indicate an overhead of ~11.6k gas when a fee payment is required and 7.8k gas otherwise. This shows that there is still room for optimization in the contracts used in the tests.

## Setup Flow

If the Account that the Entry Point should interact with is not deployed yet it is necessary to provide the necessary initialization data (`initCode`) to deploy it.

Important to note that [ERC-4337](https://eips.ethereum.org/EIPS/eip-4337#first-time-account-creation) defined the `initCode` as the following:

> The initCode field (if non-zero length) is parsed as a 20-byte address, followed by "calldata" to pass to this address.

The `initCode` for the Safe with a 4337 module enabled is composed in the following way:

```solidity
/** Enable Modules **/
bytes memory enableModuleCalldata = abi.encodeWithSignature("enableModule", 4337_MODULE_ADDRESS);
bytes memory enableEntryPointCalldata = abi.encodeWithSignature("enableModule", ENTRY_POINT_ADDRESS);
bytes memory initExecutor = MULTISEND_ADDRESS;
// Might be more gas efficient to use delegate call to the singleton here, but also more error prone.
bytes memory initData = abi.encodePacked(
uint8(0), sender, uint256(0), enableModuleCalldata.length, enableModuleCalldata,
uint8(0), sender, uint256(0), enableEntryPointCalldata.length, enableEntryPointCalldata
);

/** Setup Safe **/
// We do not want to use any payment logic therefore this is all set to 0
bytes memory setupData = abi.encodeWithSignature("setup", owners, threhsold, initExecutor, initData, address(0), 0, address(0));

/** Deploy Proxy **/
bytes memory deployData = abi.encodeWithSignature("createProxyWithNonce", SAFE_SINGLETON_ADDRESS, setupData, salt);

/** Encode for 4337 **/
bytes memory initCode = abi.encodePacked(SAFE_PROXY_FACTORY_ADDRESS, deployData);
```

The diagram below outlines the flow that is triggered by the initialization data to deploy a Safe with the 4337 module enabled.

```mermaid
sequenceDiagram
actor B as Bundler
participant E as Entry Point
participant F as Safe Proxy Factory
participant P as Safe Proxy
participant M as MultiSend
B->>+E: Submit User Operations with `initCode`
E->>+F: Deploy Proxy with `deployData`
F->>+P: Create Proxy
F->>P: Setup Proxy with `setupData`
P->>+M: Execute transaction batch
M->>P: Enable 4337 module
M->>P: Enable Entry Point as module
deactivate M
deactivate P
F->>-E: Return Account Address
```

## Usage

### Install requirements with yarn:
Expand All @@ -61,7 +116,7 @@ npx hardhat run scripts/runOp.ts --network goerli

### Deploy

> :warning: **Make sure to use the correct commit when deploying the contracts.** Any change (even comments) within the contract files will result in different addresses. The tagged versions that are used by the Safe team can be found in the [releases](https://github.com/5afe/eip4337-diatomic/releases).
> :warning: **Make sure to use the correct commit when deploying the contracts.** Any change (even comments) within the contract files will result in different addresses. The tagged versions that are used by the Safe team can be found in the [releases](https://github.com/safe-modules/releases).

This will deploy the contracts deterministically and verify the contracts on etherscan using [Solidity 0.7.6](https://github.com/ethereum/solidity/releases/tag/v0.7.6) by default.

Expand Down Expand Up @@ -109,7 +164,7 @@ npx hardhat --network <network> etherscan-verify

## Documentation

- [Safe developer portal](http://docs.gnosis-safe.io)
- [Safe developer portal](http://docs.safe.global)

## Security and Liability

Expand Down
6 changes: 3 additions & 3 deletions 4337/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@safeglobal/safe-eip4337",
"version": "1.0.0",
"description": "Safe Module for EIP-4337 support",
"homepage": "https://github.com/5afe/eip4337-diatomic/",
"homepage": "https://github.com/safe-global/safe-modules/4337",
"license": "GPL-3.0",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down Expand Up @@ -31,7 +31,7 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/5afe/eip4337-diatomic.git"
"url": "git+https://github.com/safe-global/safe-modules.git"
},
"keywords": [
"Ethereum",
Expand All @@ -40,7 +40,7 @@
],
"author": "@safe-global",
"bugs": {
"url": "https://github.com/5afe/eip4337-diatomic/issues"
"url": "https://github.com/safe-global/safe-modules/issues"
},
"devDependencies": {
"@nomiclabs/hardhat-ethers": "^2.0.6",
Expand Down
Loading