forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_region.fe
More file actions
59 lines (51 loc) · 1.65 KB
/
Copy pathcode_region.fe
File metadata and controls
59 lines (51 loc) · 1.65 KB
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
54
55
56
57
58
59
use std::evm::{Create, Evm, RawOps, mem}
#[contract_init(Foo)]
fn init() uses (evm: mut Evm) {
{ // create child contract
let len = evm.code_region_len(child_init)
let offset = evm.code_region_offset(child_init)
let dest = mem::alloc(len)
evm.codecopy(dest: dest, offset: offset, len: len)
evm.create2_raw(value: 0, offset: dest, len: len, salt: 0x1234)
}
let len = evm.code_region_len(runtime)
let offset = evm.code_region_offset(runtime)
let dest = mem::alloc(len)
evm.codecopy(dest: dest, offset: offset, len: len)
evm.return_data(offset: dest, len: len)
}
#[contract_runtime(Foo)]
fn runtime() uses (evm: mut Evm) {
with (RawOps = evm) {
match read_selector() {
0x12345678 => { transfer() }
0x23456789 => { balance() }
_ => {}
}
}
evm.return_data(offset: 0, len: 0)
}
#[contract_init(Child)]
fn child_init() uses (evm: mut Evm) {
let len = evm.code_region_len(child_runtime)
let offset = evm.code_region_offset(child_runtime)
let dest = mem::alloc(len)
evm.codecopy(dest: dest, offset: offset, len: len)
evm.return_data(offset: dest, len: len)
}
#[contract_runtime(Child)]
fn child_runtime() uses (evm: mut Evm) {
evm.calldatacopy(dest: 0, offset: 0, len: 4)
evm.return_data(offset: 0, len: 4)
}
fn balance() uses (ops: RawOps) {
ops.return_data(offset: 0, len: 0)
}
fn transfer() uses (ops: RawOps) {
ops.return_data(offset: 0, len: 0)
}
pub fn read_selector() -> u256 uses (ops: RawOps) {
let word = ops.calldataload(0)
// Shift right by 224 bits to keep only the first four bytes.
word >> 224
}