-
Notifications
You must be signed in to change notification settings - Fork 12
/
lib.rs
100 lines (80 loc) · 2.35 KB
/
lib.rs
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use std::vec::Vec;
extern "C" {
fn ethereum_useGas(amount: u64);
fn ethereum_getAddress(resultOffset: *const u32);
fn ethereum_getBalance(addressOffset: *const u32, resultOffset: *const u32);
fn ethereum_revert(dataOffset: *const u32, length: u32) -> !;
fn ethereum_return(dataOffset: *const u32, length: u32) -> !;
fn ethereum_callDataCopy(resultOffset: *const u32, dataOffset: u32, length: u32);
fn ethereum_callDataSize() -> u32;
fn ethereum_storageLoad(keyOffset: *const u32, resultOffset: *const u32);
fn ethereum_storageStore(keyOffset: *const u32, valueOffset: *const u32);
}
pub fn consume_gas(amount: u64) {
unsafe {
ethereum_useGas(amount);
}
}
pub fn current_address() -> Vec<u8> {
let mut ret: Vec<u8> = Vec::new();
ret.reserve(20);
unsafe {
ethereum_getAddress(ret.as_mut_ptr() as *const u32);
}
return ret;
}
pub fn external_balance(address: Vec<u8>) -> Vec<u8> {
let mut ret: Vec<u8> = Vec::new();
ret.reserve(16);
unsafe {
ethereum_getBalance(address.as_ptr() as *const u32, ret.as_mut_ptr() as *const u32);
}
return ret;
}
#[warn(non_snake_case)]
pub fn calldata_copy(from: usize, length: usize) -> Vec<u8> {
let mut ret: Vec<u8> = Vec::new();
ret.reserve(length);
unsafe {
ethereum_callDataCopy(ret.as_mut_ptr() as *const u32, from as u32, length as u32);
}
return ret;
}
pub fn calldata_size() -> usize {
unsafe {
return ethereum_callDataSize() as usize;
}
}
pub fn revert() -> ! {
unsafe {
ethereum_revert(0 as *const u32, 0 as u32);
}
}
pub fn revert_data(data: Vec<u8>) -> ! {
unsafe {
ethereum_revert(data.as_ptr() as *const u32, data.len() as u32);
}
}
pub fn finish() -> ! {
unsafe {
ethereum_return(0 as *const u32, 0 as u32);
}
}
pub fn finish_data(data: Vec<u8>) -> ! {
unsafe {
ethereum_return(data.as_ptr() as *const u32, data.len() as u32);
}
}
pub fn storage_load(key: Vec<u8>) -> Vec<u8> {
let mut ret: Vec<u8> = Vec::new();
ret.reserve(32);
unsafe {
ethereum_storageLoad(key.as_ptr() as *const u32, ret.as_mut_ptr() as *const u32);
}
return ret;
}
pub fn storage_store(key: Vec<u8>, value: Vec<u8>) {
unsafe {
ethereum_storageStore(key.as_ptr() as *const u32, value.as_ptr() as *const u32);
}
}