forked from optionality/clone-factory
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclone-contract.js
53 lines (49 loc) · 2.67 KB
/
clone-contract.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const evm = require('@optionality.io/evm-asm');
const contract = (bytes = 20) => [
// contract code
evm.label('code'),
evm.calldatasize(), // cds
evm.returndatasize(), // 0 cds // per EIP-211
evm.returndatasize(), // 0 0 cds // per EIP-211
evm.calldatacopy(), //
evm.returndatasize(), // 0 // per EIP-211
evm.returndatasize(), // 0 0 // per EIP-211
evm.returndatasize(), // 0 0 0 // per EIP-211
evm.calldatasize(), // cds 0 0 0
evm.returndatasize(), // 0 cds 0 0 0 // per EIP-211
evm.label('address'),
evm['push' + bytes]('0x' + 'be'.repeat(bytes)), // bebe 0 cds 0 0 0
evm.gas(), // gas bebe 0 cds 0 0 0 // all gas for delegate
evm.delegatecall(), // s 0
evm.returndatasize(), // rds s 0
evm.dup3(), // 0 rds s 0
evm.dup1(), // 0
evm.returndatacopy(), // s 0
evm.swap1(), // 0 s
evm.returndatasize(), // rds 0 s
evm.swap2(), // s 0 rds
evm.push1('return-code'), // return address (in code address space)
evm.label('returnpush'), //
evm.jumpi(), // return if zero
evm.revert(),
evm.jumpdest('return'),
evm.return(),
// end label
evm.label('codeend')
]
const loader = (bytes = 20) => [
// initialization code
evm.returndatasize(), // 0 // Per EIP-211 this is a 0
evm.push1('codeend-code'), // codelen 0
evm.dup1(), // codelen codelen 0
evm.push1('code'), // codeoff codelen codelen 0
evm.returndatasize(), // 0 codeoff codelen codelen 0
evm.codecopy(), // codelen 0
evm.dup2(), // 0 codelen 0
evm.return(), // 0
...contract(bytes)
]
module.exports = {
loader: (bytes = 20) => evm.program(loader(bytes)),
contract: (bytes = 20) => evm.program(contract(bytes))
}