Skip to content

Commit

Permalink
Merge pull request ethereum-optimism#66 from ethereum-optimism/m/depo…
Browse files Browse the repository at this point in the history
…sit-contract
  • Loading branch information
maurelian authored Dec 20, 2021
2 parents 1ff7153 + 995893c commit 713c2b5
Show file tree
Hide file tree
Showing 17 changed files with 10,803 additions and 45 deletions.
53 changes: 8 additions & 45 deletions deposits.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ L1 transaction deposits are [deposit transactions][deposit-transaction-type] gen
corresponding `TransactionDeposited` event emitted by the [deposit feed
contract][deposit-feed-contract] on L1.

1. `from` is unchanged from the emitted value (though it may have been transformed to an in
1. `from` is unchanged from the emitted value (though it may have been transformed to an alias in
the deposit feed contract).
2. `to` may be either:
1. any 20-byte address (including the zero-address)
Expand All @@ -189,53 +189,10 @@ The deposit feed handles two special cases:

1. A contract creation deposit, which is indicated by setting the `isCreation` flag to `true`.
In the event that the `to` address is non-zero, the contract will revert.
2. A call from a contract account, in which case the `from` value is transformed to its L2 .
2. A call from a contract account, in which case the `from` value is transformed to its L2 [alias][address-aliasing].

> **TODO** Define if/how ETH withdrawals occur.
A solidity-like pseudocode implementation demonstrates the functionality:

```solidity
contract DepositFeed {
event TransactionDeposited(
address indexed from,
address indexed to,
uint256 value,
uint256 gasLimit,
bool isCreation,
bytes _data
);
function depositTransaction(
address to,
uint256 value,
uint256 gasLimit,
bool isCreation,
bytes memory _data
) external payable {
address from;
if (msg.sender == tx.origin) {
from = msg.sender;
} else {
from = msg.sender + 0x1111000000000000000000000000000000001111;
}
if(isCreation && _to != address(0)) {
revert('Contract creation deposits must not specify a recipient address.');
} else {
emit TransactionDeposited(
msg.sender,
to,
msg.value,
isCreation,
initCode
);
}
}
}
```

#### Address aliasing

[address-aliasing]: #address-aliasing
Expand All @@ -245,3 +202,9 @@ If the caller is not a contract, the address will be ed by adding
has the same address as a contract on L2 but doesn't have the same code. We can safely ignore
this for EOAs because they're guaranteed to have the same "code" (i.e. no code at all). This also
makes it possible for users to interact with contracts on L2 even when the Sequencer is down.

#### Reference Implementation

A reference implementation of the Deposit Feed contract can be found in [DepositFeed.sol].

[DepositFeed.sol]: /packages/contracts/contracts/DepositFeed.sol
5 changes: 5 additions & 0 deletions packages/contracts/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
artifacts
cache
typechain
coverage*
24 changes: 24 additions & 0 deletions packages/contracts/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module.exports = {
env: {
browser: false,
es2021: true,
mocha: true,
node: true,
},
plugins: ['@typescript-eslint'],
extends: [
'standard',
'plugin:prettier/recommended',
'plugin:node/recommended',
],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 12,
},
rules: {
'node/no-unsupported-features/es-syntax': [
'error',
{ ignores: ['modules'] },
],
},
}
3 changes: 3 additions & 0 deletions packages/contracts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
artifacts
cache
typechain
3 changes: 3 additions & 0 deletions packages/contracts/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hardhat.config.ts
scripts
test
6 changes: 6 additions & 0 deletions packages/contracts/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
artifacts
cache
typechain
coverage*
gasReporterOutput.json
24 changes: 24 additions & 0 deletions packages/contracts/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module.exports = {
$schema: 'http://json.schemastore.org/prettierrc',
trailingComma: 'es5',
tabWidth: 2,
semi: false,
singleQuote: true,
arrowParens: 'always',
overrides: [
{
files: '*.sol',
options: {
// These options are native to Prettier.
printWidth: 100,
tabWidth: 4,
useTabs: false,
singleQuote: false,
bracketSpacing: true,
// These options are specific to the Solidity Plugin
explicitTypes: 'always',
compiler: '0.8.10',
},
},
],
}
16 changes: 16 additions & 0 deletions packages/contracts/.solhint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "solhint:recommended",
"plugins": ["prettier"],
"rules": {
"prettier/prettier": "error",
"compiler-version": "off",
"code-complexity": ["warn", 5],
"max-line-length": ["error", 100],
"func-param-name-mixedcase": "error",
"modifier-name-mixedcase": "error",
"ordering": "warn",
"avoid-low-level-calls": "off",
"reason-string": "off",
"avoid-tx-origin": "off"
}
}
1 change: 1 addition & 0 deletions packages/contracts/.solhintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
17 changes: 17 additions & 0 deletions packages/contracts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Contracts for OVM 1.0

## Usage

Install with yarn (v1), and Node.js (14+).

```shell
yarn
```

### Running Tests

Tests are executed via `yarn`:

```shell
yarn test
```
51 changes: 51 additions & 0 deletions packages/contracts/contracts/DepositFeed.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//SPDX-License-Identifier: MIT
pragma solidity 0.8.10;

/**
* @title DepositFeed
*/
contract DepositFeed {
// Constant for address aliasing
uint160 private constant OFFSET = uint160(0x1111000000000000000000000000000000001111);

/**
* Event with the parameters required to derive transactions on L2.
*/
event TransactionDeposited(
address indexed from,
address indexed to,
uint256 value,
uint256 gasLimit,
bool isCreation,
bytes data
);

/**
* Accepts deposits of ETH and data, and emits a TransactionDeposited event for use in deriving
* deposit transactions.
* @param _to The L2 destination address.
* @param _gasLimit The L2 gasLimit.
* @param _isCreation Whether or not the transaction should be contract creation.
* @param _data The input data.
*/
function depositTransaction(
address _to,
uint256 _gasLimit,
bool _isCreation,
bytes memory _data
) external payable {
if (_isCreation && _to != address(0)) {
revert("Contract creation deposits must not specify a recipient address.");
}

address from = msg.sender;
// Transform the from-address to its alias if the caller is a contract.
if (msg.sender != tx.origin) {
unchecked {
from = address(uint160(msg.sender) + OFFSET);
}
}

emit TransactionDeposited(from, _to, msg.value, _gasLimit, _isCreation, _data);
}
}
19 changes: 19 additions & 0 deletions packages/contracts/contracts/test/DummyContract.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

/**
* For use in testing with a call from a contract rather than an EOA.
*/
contract Dummy {
/**
* Forwards a call.
* @param _target Address to call
* @param _data Data to forward
*/
function forward(address _target, bytes calldata _data) external payable {
(bool success, bytes memory ret) = _target.call{ value: msg.value }(_data);
// Silence the 'Return value of low-level calls not used' warning.
success;
ret;
}
}
24 changes: 24 additions & 0 deletions packages/contracts/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { HardhatUserConfig, task } from 'hardhat/config'
import '@nomiclabs/hardhat-waffle'
import '@typechain/hardhat'
import 'hardhat-gas-reporter'
import 'solidity-coverage'

task('accounts', 'Prints the list of accounts', async (taskArgs, hre) => {
const accounts = await hre.ethers.getSigners()

for (const account of accounts) {
console.log(account.address)
}
})

const config: HardhatUserConfig = {
solidity: '0.8.10',
networks: {},
gasReporter: {
enabled: process.env.REPORT_GAS !== undefined,
currency: 'USD',
},
}

export default config
53 changes: 53 additions & 0 deletions packages/contracts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "@eth-optimism/specs-contracts",
"version": "0.1.0",
"description": "Contracts for Optimism Specs",
"main": "index.js",
"license": "MIT",
"dependencies": {
"hardhat": "^2.7.1"
},
"scripts": {
"test": "hardhat test",
"lint:ts:check": "eslint .",
"lint:contracts:check": "yarn solhint -f table 'contracts/**/*.sol'",
"lint:check": "yarn lint:contracts:check && yarn lint:ts:check",
"lint:ts:fix": "prettier --write .",
"lint:contracts:fix": "prettier --write 'contracts/**/*.sol'",
"lint:fix": "yarn lint:contracts:fix && yarn lint:ts:fix",
"lint": "yarn lint:fix && yarn lint:check"
},
"devDependencies": {
"@eth-optimism/core-utils": "^0.7.3",
"@nomiclabs/hardhat-ethers": "^2.0.0",
"@nomiclabs/hardhat-etherscan": "^2.1.3",
"@nomiclabs/hardhat-waffle": "^2.0.0",
"@typechain/ethers-v5": "^7.0.1",
"@typechain/hardhat": "^2.3.0",
"@types/chai": "^4.2.21",
"@types/mocha": "^9.0.0",
"@types/node": "^12.0.0",
"@typescript-eslint/eslint-plugin": "^4.29.1",
"@typescript-eslint/parser": "^4.29.1",
"chai": "^4.2.0",
"dotenv": "^10.0.0",
"eslint": "^7.29.0",
"eslint-config-prettier": "^8.3.0",
"eslint-config-standard": "^16.0.3",
"eslint-plugin-import": "^2.23.4",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.4.0",
"eslint-plugin-promise": "^5.1.0",
"ethereum-waffle": "^3.0.0",
"ethers": "^5.0.0",
"hardhat-gas-reporter": "^1.0.4",
"prettier": "^2.3.2",
"prettier-plugin-solidity": "^1.0.0-beta.13",
"solhint": "^3.3.6",
"solhint-plugin-prettier": "^0.0.5",
"solidity-coverage": "^0.7.16",
"ts-node": "^10.1.0",
"typechain": "^5.1.2",
"typescript": "^4.5.2"
}
}
Loading

0 comments on commit 713c2b5

Please sign in to comment.