forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.fe
More file actions
175 lines (145 loc) · 4.28 KB
/
Copy pathcontext.fe
File metadata and controls
175 lines (145 loc) · 4.28 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use ingot::evm
use ingot::error::{
ERROR_INSUFFICIENT_FUNDS_TO_SEND_VALUE,
ERROR_FAILED_SEND_VALUE,
Error
}
use ingot::buf::{
RawCallBuffer,
MemoryBuffer,
MemoryBufferReader,
MemoryBufferWriter
}
struct OutOfReachMarker {}
// ctx.emit(my_event) should be the only way to emit an event. We achieve this by defining the
// private `OutOfReachMarker` here to which only the `Context` has access.
// Now there is no way to call `emit` directly on an Emittable.
pub trait Emittable {
fn emit(self, _ val: OutOfReachMarker);
}
pub struct CalldataReader {
cur_offset: u256
len: u256
pub unsafe fn new(len: u256) -> CalldataReader {
return CalldataReader(cur_offset: 0, len)
}
pub fn remainder(self) -> u256 {
return self.len - self.cur_offset
}
pub fn advance(mut self, len: u256) -> u256 {
self.cur_offset += len
assert self.cur_offset <= self.len
return self.cur_offset
}
fn read_n(mut self, len: u256) -> u256 {
unsafe {
let value: u256 = evm::call_data_load(offset: self.cur_offset)
self.advance(len)
return evm::shr(bits: 256 - len * 8, value)
}
}
pub fn read_u8(mut self) -> u8 {
return u8(self.read_n(len: 1))
}
pub fn read_u16(mut self) -> u16 {
return u16(self.read_n(len: 2))
}
pub fn read_u32(mut self) -> u32 {
return u32(self.read_n(len: 4))
}
pub fn read_u64(mut self) -> u64 {
return u64(self.read_n(len: 8))
}
pub fn read_u128(mut self) -> u128 {
return u128(self.read_n(len: 16))
}
pub fn read_u256(mut self) -> u256 {
unsafe {
let value: u256 = evm::call_data_load(offset: self.cur_offset)
self.advance(len: 32)
return value
}
}
}
pub struct Context {
pub fn base_fee(self) -> u256 {
unsafe { return evm::base_fee() }
}
pub fn block_coinbase(self) -> address {
unsafe { return evm::coinbase() }
}
pub fn prevrandao(self) -> u256 {
unsafe { return evm::prevrandao() }
}
pub fn block_number(self) -> u256 {
unsafe { return evm::block_number() }
}
pub fn block_timestamp(self) -> u256 {
unsafe { return evm::timestamp() }
}
pub fn chain_id(self) -> u256 {
unsafe { return evm::chain_id() }
}
pub fn msg_sender(self) -> address {
unsafe { return evm::caller() }
}
pub fn msg_value(self) -> u256 {
unsafe { return evm::call_value() }
}
pub fn tx_gas_price(self) -> u256 {
unsafe { return evm::gas_price() }
}
pub fn tx_origin(self) -> address {
unsafe { return evm::origin() }
}
pub fn msg_sig(self) -> u256 {
unsafe { return evm::shr(bits: 224, value: evm::call_data_load(offset: 0)) }
}
pub fn balance_of(self, _ account: address) -> u256 {
unsafe { return evm::balance_of(account) }
}
pub fn self_balance(self) -> u256 {
unsafe { return evm::balance() }
}
pub fn self_address(self) -> address {
unsafe { return address(__address()) }
}
pub fn calldata_reader(self) -> CalldataReader {
unsafe {
let len: u256 = evm::call_data_size()
return CalldataReader::new(len)
}
}
pub fn send_value(mut self, to: address, wei: u256) {
unsafe {
if evm::balance() < wei {
revert Error(code: ERROR_INSUFFICIENT_FUNDS_TO_SEND_VALUE)
}
let mut buf: RawCallBuffer = RawCallBuffer::new(input_len: 0, output_len: 0)
let success: bool = evm::call(gas: evm::gas_remaining(), addr: to, value: wei,
buf)
if not success {
revert Error(code: ERROR_FAILED_SEND_VALUE)
}
}
}
/// Makes a call to the given address.
pub fn raw_call(
self,
addr: address,
value: u256,
mut buf: RawCallBuffer
) -> bool {
unsafe {
return evm::call(
gas: evm::gas_remaining(),
addr,
value,
buf
)
}
}
pub fn emit<T: Emittable>(mut self, _ val: T) {
val.emit(OutOfReachMarker())
}
}