-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy patheth_contracts.nim
296 lines (257 loc) · 10.7 KB
/
eth_contracts.nim
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import macros
{.push cdecl, importc.}
proc useGas*(amount: int64)
## Subtracts an amount to the gas counter
##
## Parameters:
## `amount` - the amount to subtract to the gas counter
proc getAddress*(resultOffset: pointer)
## Gets address of currently executing account and stores it in memory at
## the given offset.
##
## Parameters:
## `resultOffset` the memory offset at which the address is to be stored (`address`)
proc getExternalBalance*(addressOffset, resultOffset: pointer)
## Gets balance of the given account and loads it into memory at the given offset.
##
## Parameters:
## `addressOffset` the memory offset to load the address from (`address`)
# `resultOffset` the memory offset to load the balance into (`u128`)
proc getBlockHash*(number: int64, resultOffset: pointer)
## Gets the hash of one of the 256 most recent complete blocks.
##
## Parameters:
## `number` which block to load
## `resultOffset` the memory offset to load the hash into (`u256`)
proc call*(gas: int64, addressOffset, valueOffset, dataOffset: pointer,
dataLength: int32): int32
## Sends a message with arbitrary data to a given address path
##
## Parameters:
## `gas` **i64** the gas limit
## `addressOffset` the memory offset to load the address from (`address`)
## `valueOffset` the memory offset to load the value from (`u128`)
## `dataOffset` the memory offset to load data from (`bytes`)
## `dataLength` the length of data
## Returns:
## 0 on success, 1 on failure and 2 on `revert`
proc callDataCopy*(resultOffset: pointer, dataOffset, length: int32)
## Copies the input data in current environment to memory. This pertains to
## the input data passed with the message call instruction or transaction.
##
## Parameters:
## `resultOffset` the memory offset to load data into (`bytes`)
## `dataOffset` the offset in the input data
## `length` the length of data to copy
proc getCallDataSize*(): int32
## Get size of input data in current environment. This pertains to the input
## data passed with the message call instruction or transaction.
##
## Returns: call data size
proc callCode*(gas: int64, addressOffset, valueOffset, dataOffset: pointer,
dataLength: int32): int32
## Message-call into this account with an alternative account's code.
##
## Parameters:
## `gas` the gas limit
## `addressOffset` the memory offset to load the address from (`address`)
## `valueOffset` the memory offset to load the value from (`u128`)
## `dataOffset` the memory offset to load data from (`bytes`)
## `dataLength` the length of data
##
## Returns: 0 on success, 1 on failure and 2 on `revert`
proc callDelegate*(gas: int64, addressOffset, dataOffset: pointer,
dataLength: int32)
## Message-call into this account with an alternative account’s code, but
## persisting the current values for sender and value.
##
## Parameters:
## `gas` the gas limit
## `addressOffset` the memory offset to load the address from (`address`)
## `dataOffset` the memory offset to load data from (`bytes`)
## `dataLength` the length of data
##
## Returns: 0 on success, 1 on failure and 2 on `revert`
proc callStatic*(gas: int64, addressOffset, dataOffset: pointer,
dataLength: int32)
## Sends a message with arbitrary data to a given address path, but disallow
## state modifications. This includes `log`, `create`, `selfdestruct` and `call`
## with a non-zero value.
##
## Parameters:
## `gas` the gas limit
## `addressOffset` the memory offset to load the address from (`address`)
## `dataOffset` the memory offset to load data from (`bytes`)
## `dataLength` the length of data
##
## Returns: 0 on success, 1 on failure and 2 on `revert`
proc storageStore*(pathOffset, valueOffset: pointer)
## Store 256-bit a value in memory to persistent storage
##
## Parameters:
## `pathOffset` the memory offset to load the path from (`u256`)
## `valueOffset` the memory offset to load the value from (`u256`)
proc storageLoad*(pathOffset, valueOffset: pointer)
## Loads a 256-bit a value to memory from persistent storage
##
## Parameters:
## `pathOffset` the memory offset to load the path from (`u256`)
## `resultOffset` the memory offset to store the result at (`u256`)
proc getCaller*(resultOffset: pointer)
## Gets caller address and loads it into memory at the given offset. This is
## the address of the account that is directly responsible for this execution.
##
## Parameters:
## `resultOffset` the memory offset to load the address into (`address`)
proc getCallValue*(resultOffset: pointer)
## Gets the deposited value by the instruction/transaction responsible for
## this execution and loads it into memory at the given location.
##
## Parameters:
## `resultOffset` the memory offset to load the value into (`u128`)
proc codeCopy*(resultOffset: pointer, codeOffset, length: int32)
## Copies the code running in current environment to memory.
##
## Parameters:
## `resultOffset` the memory offset to load the result into (`bytes`)
## `codeOffset` the offset within the code
## `length` the length of code to copy
proc getCodeSize*(): int32
## Gets the size of code running in current environment.
proc getBlockCoinbase*(resultOffset: pointer)
## Gets the block’s beneficiary address and loads into memory.
##
## Parameters:
## `resultOffset` the memory offset to load the coinbase address into (`address`)
proc create*(valueOffset, dataOffset: pointer, length: int32, resultOffset: pointer): int32
## Creates a new contract with a given value.
##
## Parameters:
## `valueOffset` the memory offset to load the value from (`u128`)
## `dataOffset` the memory offset to load the code for the new contract from (`bytes`)
## `length` the data length
## `resultOffset` the memory offset to write the new contract address to (`address`)
##
## Note: `create` will clear the return buffer in case of success or may fill
## it with data coming from `revert`.
##
## Returns: 0 on success, 1 on failure and 2 on `revert`
proc getBlockDifficulty*(resultOffset: pointer)
## Get the block’s difficulty.
##
## Parameters:
## `resultOffset` the memory offset to load the difficulty into (`u256`)
proc externalCodeCopy*(addressOffset, resultOffset: pointer, codeOffset, length: int32)
## Copies the code of an account to memory.
##
## Parameters:
## `addressOffset` the memory offset to load the address from (`address`)
## `resultOffset` the memory offset to load the result into (`bytes`)
## `codeOffset` the offset within the code
## `length` the length of code to copy
proc getExternalCodeSize*(addressOffset: pointer): int32
## Get size of an account’s code.
##
## Parameters:
## `addressOffset` the memory offset to load the address from (`address`)
proc getGasLeft*(): int64
## Returns the current gasCounter
proc getBlockGasLimit*(): int64
## Get the block’s gas limit.
proc getTxGasPrice*(valueOffset: pointer)
## Gets price of gas in current environment.
##
## Parameters:
## `valueOffset` the memory offset to write the value to (`u128`)
proc log*(dataOffset: pointer, length, numberOfTopics: int32,
topic1, topic2, topic3, topic4: pointer)
## Creates a new log in the current environment
##
## Parameters:
## `dataOffset` the memory offset to load data from (`bytes`)
## `length` the data length
## `numberOfTopics` the number of topics following (0 to 4)
## `topic1` the memory offset to load topic1 from (`u256`)
## `topic2` the memory offset to load topic2 from (`u256`)
## `topic3` the memory offset to load topic3 from (`u256`)
## `topic4` the memory offset to load topic4 from (`u256`)
proc getBlockNumber*(): int64
## Get the block’s number.
proc getTxOrigin*(resultOffset: pointer)
## Gets the execution's origination address and loads it into memory at the
## given offset. This is the sender of original transaction; it is never an
## account with non-empty associated code.
##
## Parameters:
## `resultOffset` the memory offset to load the origin address from (`address`)
proc finish*(dataOffset: pointer, length: int32) {.noreturn.}
## Set the returning output data for the execution. This will cause a trap and
## the execution will be aborted immediately.
##
## Note: multiple invocations will overwrite the previous data.
##
## Parameters:
## `dataOffset` the memory offset of the output data (`bytes`)
## `length` the length of the output data
proc revert*(dataOffset: pointer, length: int32) {.noreturn.}
## Set the returning output data for the execution. This will cause a trap and
## the execution will be aborted immediately.
##
## Note: multiple invocations will overwrite the previous data.
##
## Parameters:
## `dataOffset` the memory offset of the output data (`bytes`)
## `length` the length of the output data
proc getReturnDataSize*(): int32
## Get size of current return data buffer to memory. This contains the return
## data from the last executed `call`, `callCode`, `callDelegate`, `callStatic`
## or `create`.
##
## Note: `create` only fills the return data buffer in case of a failure.
proc returnDataCopy*(resultOffset: pointer, dataOffset, length: int32)
## Copies the current return data buffer to memory. This contains the return data
## from last executed `call`, `callCode`, `callDelegate`, `callStatic` or `create`.
##
## Note: `create` only fills the return data buffer in case of a failure.
##
## Parameters:
## `resultOffset` the memory offset to load data into (`bytes`)
## `dataOffset` the offset in the return data
## `length` the length of data to copy
proc selfDestruct*(addressOffset: pointer)
## Mark account for later deletion and give the remaining balance to the specified
## beneficiary address. This will cause a trap and the execution will be aborted
## immediately.
##
## Parameters:
## `addressOffset` the memory offset to load the address from (`address`)
proc getBlockTimestamp*(): int64
## Get the block’s timestamp.
{.pop.}
proc callDataCopy*[T](res: var T, offset: int) {.inline.} =
callDataCopy(addr res, offset.int32, sizeof(res).int32)
proc storageLoad*[N](path: array[N, byte], res: pointer) {.inline.} =
when path.len < 32:
type PaddedPath {.packed.} = object
padding: array[32 - path.len, byte]
p: array[N, byte]
var p: PaddedPath
p.p = path
storageLoad(addr p, res)
else:
storageLoad(unsafeAddr path[0], res)
proc storageStore*[N](path: array[N, byte], res: pointer) {.inline.} =
when path.len < 32:
type PaddedPath {.packed.} = object
padding: array[32 - path.len, byte]
p: array[N, byte]
var p: PaddedPath
p.p = path
storageStore(addr p, res)
else:
storageStore(unsafeAddr path[0], res)
macro exportwasm*(p: untyped): untyped =
expectKind(p, nnkProcDef)
result = p
result.addPragma(newIdentNode("exportc"))
result.addPragma(newColonExpr(newIdentNode("codegenDecl"), newLit("__attribute__ ((visibility (\"default\"))) $# $#$#")))