generated from huff-language/huff-project-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ERC20.huff
558 lines (466 loc) · 32.1 KB
/
ERC20.huff
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/* Imports */
#include "./utils/HashMap.huff"
/// @author devtooligan
/// @title ERC20.huff
/// @notice Adapted from Solmate
/// https://github.com/transmissions11/solmate/blob/main/src/tokens/ERC20.sol
// TODO: All revert strings (and related tests) have not been added.
// TODO: Use Jtriley's error utils
/*//////////////////////////////////////////////////////////////////
// INTERFACE
//////////////////////////////////////////////////////////////////*/
#define function allowance(address,address) view returns (uint256)
#define function approve(address,uint256) nonpayable returns () // these returns sb updated
#define function balanceOf(address) view returns (uint256)
#define function DOMAIN_SEPARATOR() view returns (bytes32)
#define function nonces(address) view returns (uint256)
#define function permit(address,address,uint256,uint256,uint8,bytes32,bytes32) nonpayable returns ()
#define function totalSupply() view returns (uint256)
#define function transfer(address,uint256) nonpayable returns ()
#define function transferFrom(address,address,uint256) nonpayable returns ()
/*//////////////////////////////////////////////////////////////////
// EVENTS
//////////////////////////////////////////////////////////////////*/
#define event Approval(address,address,uint256)
#define event Transfer(address,address,uint256)
// todo: use __EVENT_HASH fn
/*//////////////////////////////////////////////////////////////////
// METADATA
//////////////////////////////////////////////////////////////////*/
#define function name() nonpayable returns (string)
#define constant META_NAME = 0x546f6b656e000000000000000000000000000000000000000000000000000000 // "Token"
#define constant META_NAME_LENGTH = 0x05
#define function symbol() nonpayable returns (string)
#define constant META_SYMBOL = 0x544B4E0000000000000000000000000000000000000000000000000000000000 // "TKN"
#define constant META_SYMBOL_LENGTH = 0x03
#define function decimals() nonpayable returns (uint256)
#define constant META_DECIMALS = 0x12 // 18
/*//////////////////////////////////////////////////////////////////
// ERC20 STORAGE
//////////////////////////////////////////////////////////////////*/
#define constant TOTAL_SUPPLY_SLOT = FREE_STORAGE_POINTER()
#define constant BALANCE_SLOT = FREE_STORAGE_POINTER()
#define constant APPROVAL_SLOT = FREE_STORAGE_POINTER()
/*//////////////////////////////////////////////////////////////////
// EIP-2612 STORAGE
//////////////////////////////////////////////////////////////////*/
// TODO: consider making EIP-2612 it's own separate module
#define constant INITIAL_CHAIN_ID = FREE_STORAGE_POINTER()
#define constant INITIAL_DOMAIN_SEPARATOR = FREE_STORAGE_POINTER()
#define constant NONCE_SLOT = FREE_STORAGE_POINTER()
#define constant PERMIT_TYPEHASH = 0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9 // keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)")
#define constant X_1901 = 0x1901000000000000000000000000000000000000000000000000000000000000
/*//////////////////////////////////////////////////////////////////
// UTILITY CONSTANTS
//////////////////////////////////////////////////////////////////*/
#define constant UINT_256_MAX = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
#define constant ERROR_SIG = 0x08c379a000000000000000000000000000000000000000000000000000000000
/*//////////////////////////////////////////////////////////////////
// CONSTRUCTOR
//////////////////////////////////////////////////////////////////*/
#define macro CONSTRUCTOR_ERC20() = takes(0) returns (0) {
chainid [INITIAL_CHAIN_ID] sstore // []
COMPUTE_DOMAIN_SEPARATOR() // [DOMAIN SEPARATOR]
[INITIAL_DOMAIN_SEPARATOR] sstore // []
}
/*//////////////////////////////////////////////////////////////////
// ERC20 LOGIC
//////////////////////////////////////////////////////////////////*/
#define macro APPROVE() = takes (0) returns (0) {
NON_PAYABLE()
0x24 calldataload // [value]
0x04 calldataload // [to, value]
caller // [from, to, value]
[APPROVAL_SLOT] // [slot, from, to, value]
STORE_ELEMENT_FROM_SLOT_AND_KEYS(0x00) // []
// Emit the Approval event
0x24 calldataload // [value]
0x00 mstore // []
0x04 calldataload // [to]
caller // [from, to]
__EVENT_HASH(Approval) // [sig, from, to]
0x20 0x00 // [0, 32, sig, from, to]
log3 // []
0x01 0x00 mstore // return 01 for true
0x20 0x00 return
}
/* Transfer Functions */
#define macro TRANSFER() = takes(0) returns(1) {
NON_PAYABLE()
// Setup the stack for the transfer function.
0x04 calldataload // [to]
caller // [from, to]
0x24 calldataload // [value, from, to]
// Update the balances of the sender and recipient.
_TRANSFER_TAKE_FROM() // [value, from, to]
_TRANSFER_GIVE_TO() // [value, from, to]
// Emit the transfer event. // TODO consider macro
0x00 mstore // [from, to]
__EVENT_HASH(Transfer) // [sig, from, to]
0x20 0x00 // [0, 32, sig, from, to]
log3 // []
// Return "1" to represent a succesful transfer.
0x01 0x00 mstore
0x20 0x00 return
}
#define macro TRANSFER_FROM() = takes(0) returns(1) { // TODO: is self?
NON_PAYABLE()
// Setup the stack for the transfer function.
0x24 calldataload // [to]
0x04 calldataload // [from, to]
caller // [msg.sender, from, to]
dup2 // [from, msg.sender, from, to]
[APPROVAL_SLOT] // [slot, from, msg.sender, from, to]
// check for max approval
LOAD_ELEMENT_FROM_SLOT_AND_KEYS(0x00) // [approved, from, to]
dup1 // [approved, approved, from, to]
0x44 calldataload // [value, approved, approved, from, to]
// check isOwner
dup4 // [from, value, approved, approved, from, to]
caller // [msg.sender, from, value, approved, approved, from, to]
eq // [msg.sender == from, value, approved, approved, from, to]
approved1 jumpi // [value, approved, approved, from, to]
// check max approval
dup2 // [approved, value, approved, approved, from, to]
[UINT_256_MAX] // [type(uint).max, approved, value, approved, approved, from, to]
eq // [type(uint).max == approved, value, approved, approved, from, to]
approved1 jumpi // [value, approved, approved, from, to]
// check has approval
gt // [value > approved, approved, from, to]
insufficientApproval jumpi // [approved, from, to]
// adjust approval
0x44 calldataload // [value, approved, from, to]
swap1 // [approved, value, from, to]
sub // [approved - value => newApprovalValue, from, to]
caller // [msg.sender, newApprovalValue, from, to]
dup3 // [from, msg.sender, newApprovalValue, from, to]
[APPROVAL_SLOT] // [slot, from, msg.sender, newApprovalValue, from, to]
STORE_ELEMENT_FROM_SLOT_AND_KEYS(0x00) // [from, to]
approved2 jump // [from, to]
approved1: // [value, approved, approved, from, to]
pop pop pop // [from, to]
approved2: // [from, to]
0x44 calldataload // [value, from, to]
// Update the balances of the sender and recipient.
_TRANSFER_TAKE_FROM() // [value, from, to]
_TRANSFER_GIVE_TO() // [value, from, to]
// Emit the transfer event. // TODO consider macro
0x00 mstore // [from, to]
__EVENT_HASH(Transfer) // [sig, from, to]
0x20 0x00 // [0, 32, sig, from, to]
log3 // []
// Return "1" to represent a succesful transfer.
0x01 0x00 mstore
0x20 0x00 return
insufficientApproval:
0x00 0x00 revert
}
#define macro _TRANSFER_TAKE_FROM() = takes(3) returns (3) {
// input stack: [value, from, to]
dup2 [BALANCE_SLOT] LOAD_ELEMENT_FROM_KEYS(0x00) // [balance, value, from, to] // [from, value, from, to]
dup1 // [balance, balance, value, from, to]
dup3 // [value, balance, balance, value, from, to]
gt // [value>balance, balance, value, from, to]
iszero // [value<=balance, balance, value, from, to]
valid jumpi // [balance, value, from, to]
// insufficient balance
0x00 0x00 revert
// Update the sender's balance.
valid:
dup2 // [value, balance, value, from, to]
swap1 // [balance, value, value, from, to]
sub // [balance - value, value, from, to]
dup3 // [from, balance-value, value, from, to]
[BALANCE_SLOT] STORE_ELEMENT_FROM_KEYS(0x00) // [value, from, to]
}
// TODO: OVERFLOW CHECK?
#define macro _TRANSFER_GIVE_TO() = takes(3) returns (0) {
// input stack: [value, from, to]
dup3 // [to, value, from, to]
dup2 // [value, to, value, from, to]
swap1 // [to, value, value, from, to]
[BALANCE_SLOT] LOAD_ELEMENT_FROM_KEYS(0x00) // [balance, value, value, from, to]
add // [balance+value, value, from, to]
dup4 // [to, balance+value, value, from, to]
[BALANCE_SLOT] STORE_ELEMENT_FROM_KEYS(0x00) // [value, from, to]
}
/*//////////////////////////////////////////////////////////////////
// EIP-2612 LOGIC
//////////////////////////////////////////////////////////////////*/
#define macro COMPUTE_DOMAIN_SEPARATOR() = takes (0) returns (1) {
[PERMIT_TYPEHASH] // [DOMAIN_SEPARATOR_3
0x00 mstore // []
[META_NAME] 0x20 mstore // []
0x20 0x20 sha3 // [name hash]
0x20 mstore // []
0x31 // ["1"] 0x31 is hex for ascii for 1
0x40 mstore // []
0x02 0x40 sha3 // [hash of "1"]
0x20 0x40 mstore // []
chainid // [chainid]
0x60 mstore // []
address // [address(this)]
0x80 mstore // []
0xA0 0x00 // [loc, len]
sha3 // [hash]
}
#define macro DOMAIN_SEPARATOR() = takes (0) returns (0) {
NON_PAYABLE()
_DOMAIN_SEPARATOR()
0x00 mstore // [domain separator]
0x20 0x00 return // []
}
#define macro _DOMAIN_SEPARATOR() = takes (0) returns (1) {
chainid // [chainid]
[INITIAL_CHAIN_ID] sload // [INITIAL_CHAIN_ID, chainid]
eq // [INITIAL_CHAIN_ID == chainid]
useInitial jumpi // []
COMPUTE_DOMAIN_SEPARATOR() // [computed domain separator]
done jump
useInitial:
[INITIAL_DOMAIN_SEPARATOR] sload // [INITIAL_DOMAIN_SEPARATOR]
done:
}
#define macro PERMIT() = takes (0) returns (0) {
NON_PAYABLE()
// permit() fn sig for reference:
// address owner, // 0x04
// address spender, // 0x24
// uint256 value, // 0x44
// uint256 deadline, // 0x64
// uint8 v, // 0x84
// bytes32 r, // 0xa4
// bytes32 s)// 0xc4
// check deadline
0x64 calldataload // [deadline]
dup1 // [deadline, deadline]
timestamp // [timestamp, deadline, deadline]
gt // [timestamp > deadline, deadline]
expired jumpi // [deadline]
// calc inner kec
0x04 calldataload // [owner, deadline]
_NONCE_PLUS_PLUS() // [nonce, deadline]
0x44 calldataload // [value, nonce, deadline]
0x24 calldataload // [spender, value, nonce, deadline]
0x04 calldataload // [owner, spender, value, nonce, deadline]
[PERMIT_TYPEHASH] // [permit hash, owner, spender, value, nonce, deadline]
0x00 mstore // [owner, spender, value, nonce, deadline]
0x20 mstore // [spender, value, nonce, deadline]
0x40 mstore // [value, nonce, deadline]
0x60 mstore // [nonce, deadline]
0x80 mstore // [deadline]
0xa0 mstore // []
0xc0 0x00 // [loc, len]
sha3 // [inner hash]
// calc and mstore outer kec
_DOMAIN_SEPARATOR() // [DOMAIN_SEPARATOR, inner hash]
[X_1901] // [0x1901, DOMAIN_SEPARATOR, inner hash]
0x00 mstore // [DOMAIN_SEPARATOR, inner hash]
0x02 mstore // note: abi.encodePacked // [inner hash]
0x22 mstore // []
0x42 0x00 // [loc, len]
sha3 // [outer hash]
// memory layout:
0x00 mstore // [] 0x00 outer hash
0x84 calldataload // [v]
0x20 mstore // [] 0x00 outerhash 0x20 v
0xa4 calldataload // [r]
0x40 mstore // [] 0x00 outerhash 0x20 v 0x40 r
0xc4 calldataload // [s]
0x60 mstore // [] 0x00 outerhash 0x20 v 0x40 r 0x60 s
// prepare stack for later
0x44 calldataload // [value]
0x24 calldataload // [spender, value]
// ecrecover
0x20 // [32, spender, value]
0x80 // [128, 32, spender, value]
0x80 // [128, 128, 32, spender, value]
0x00 // [0, 128, 128, 32, spender, value]
0x1 // [ecrecover precompile address, 0, 128, 128, 32, spender, value]
0xFFFFFFFF // [gas, ecrecover precompile address, 0, 128, 128, 32, spender, value]
staticcall // [success, spender, value]
pop // TODO: check for success // [spender, value]
0x80 mload // [recovered address, spender, value]
// check for recovered 0 address
dup1 // [recovered address, recovered address, spender, value]
0x00 eq // [recovered address == 0, recovered address, spender, value]
invalidSigner jumpi // [recovered address, spender, value]
// check for address is owner
dup1 // [recovered address, recovered address, spender, value]
0x04 calldataload // [owner, recovered address, recovered address, spender, value]
eq // [owner == recovered address, recovered address, spender, value]
iszero // [owner != recovered address, recovered address, spender, value]
invalidSigner jumpi // [recovered address, spender, value]
[APPROVAL_SLOT] // [slot, recovered address, spender, value]
STORE_ELEMENT_FROM_SLOT_AND_KEYS(0x00) // []
// Emit the Approval event
0x44 calldataload // [value]
0x00 mstore // []
0x24 calldataload // [to]
0x04 calldataload // [from, to]
__EVENT_HASH(Approval) // [sig, from, to]
0x20 0x00 // [0, 32, sig, from, to]
log3 // []
0x00 0x00 return
expired:
0x5045524D49545F444541444C494E455F45585049524544000000000000000000 // ["PERMIT_DEADLINE_EXPIRED"]
0x17 // [23 (length), "PERMIT_DEADLINE_EXPIRED"]
REVERT_WITH_REASON(0x00)
invalidSigner: // todo: add revert strings
0x494E56414C49445F5349474E4552000000000000000000000000000000000000 // ["INVALID_SIGNER"]
0x0e // [14 (length), "INVALID_SIGNER"]
REVERT_WITH_REASON(0x00)
}
// Takes an address off the stack, returns the current nonce for that address onto the stack.
// Increments the nonce for next time,
#define macro _NONCE_PLUS_PLUS() = takes (1) returns (1) {
// starting stack // [account]
dup1 // [account, account]
[NONCE_SLOT] LOAD_ELEMENT_FROM_KEYS(0x00) // [currentNonce, account]
dup1 // [currentNonce, currentNonce, account]
0x01 // [1, currentNonce, currentNonce, account]
add // [nextNonce, currentNonce, account]
dup3 // [account, nextNonce, currentNonce, account]
[NONCE_SLOT] STORE_ELEMENT_FROM_KEYS(0x00) // [currentNonce, account]
swap1 // clean up stack // [account, currentNonce]
pop // clean up stack // [currentNonce]
}
#define macro NONCES() = takes (0) returns (0) {
0x04 calldataload // [account]
[NONCE_SLOT] LOAD_ELEMENT_FROM_KEYS(0x00) // [nonce]
0x00 mstore // []
0x20 0x00 return // []
}
/* Metadata */
#define macro NAME() = takes (0) returns (0) {
NON_PAYABLE()
0x20 0x00 mstore
[META_NAME_LENGTH] 0x20 mstore
[META_NAME] 0x40 mstore
0x60 0x00 return
}
#define macro SYMBOL() = takes (0) returns (0) {
NON_PAYABLE()
0x20 0x00 mstore
[META_SYMBOL_LENGTH] 0x20 mstore
[META_SYMBOL] 0x40 mstore
0x60 0x00 return
}
#define macro DECIMALS() = takes (0) returns (0) {
NON_PAYABLE()
[META_DECIMALS] 0x00 mstore
0x20 0x00 return
}
/* Accounting Functions */
#define macro BALANCE_OF() = takes (0) returns (0) {
NON_PAYABLE()
0x04 calldataload // [account]
[BALANCE_SLOT] LOAD_ELEMENT_FROM_KEYS(0x00) // [balance]
0x00 mstore // []
0x20 0x00 return // []
}
#define macro TOTAL_SUPPLY() = takes (0) returns (0) {
NON_PAYABLE()
[TOTAL_SUPPLY_SLOT] sload // [supply]
0x00 mstore // []
0x20 0x00 return // []
}
/* Approval Functions */
#define macro ALLOWANCE() = takes (0) returns (0) {
NON_PAYABLE()
0x24 calldataload // [to]
0x04 calldataload // [from, to]
[APPROVAL_SLOT] // [slot, from, to]
LOAD_ELEMENT_FROM_SLOT_AND_KEYS(0x00) // [allowance]
0x00 mstore // []
0x20 0x00 return
}
/*//////////////////////////////////////////////////////////////////
// MINT/BURN LOGIC
//////////////////////////////////////////////////////////////////*/
#define macro _BURN() = takes(3) returns (0) {
// [value, from, to]
_TRANSFER_TAKE_FROM() // [value, from, to]
dup1 // [value, value, from, to]
[TOTAL_SUPPLY_SLOT] sload // [supply,value,value,from,to]
sub // [supply-value,value,from,to]
[TOTAL_SUPPLY_SLOT] sstore // [value,from,to]
// Emit the transfer event.
0x00 mstore // [from, to]
__EVENT_HASH(Transfer) // [sig, from, to]
0x20 0x00 // [0, 32, sig, from, to]
log3 // []
}
#define macro _MINT() = takes(0) returns (0) {
_TRANSFER_GIVE_TO() // [value, from, to]
// Update totalSupply
dup1 // [value, value, from, to]
[TOTAL_SUPPLY_SLOT] sload // [supply,value,value,from,to]
add // [supply+value,value,from,to]
[TOTAL_SUPPLY_SLOT] sstore // [value,from,to]
// Emit the transfer event.
0x00 mstore // [from, to]
__EVENT_HASH(Transfer) // [sig, from, to]
0x20 0x00 // [0, 32, sig, from, to]
log3 // []
}
/* Utility Functions */
#define macro NON_PAYABLE() = takes(0) returns (0) {
callvalue iszero // [msg.value == 0]
novalue jumpi // []
// TODO: Add revert string
0x00 0x00 revert
novalue:
}
#define macro REVERT_WITH_REASON(mem_ptr) = takes(2) returns(0) {
// NOTE: String must be < 32 bytes
// [len, "str"]
[ERROR_SIG] <mem_ptr> mstore // [len, "str"] 0x00 error sig
0x20 <mem_ptr> 0x04 add mstore // [len, "str"] 0x00 error sig 0x04 offset
<mem_ptr> 0x24 add mstore // ["str"] 0x00 error sig 0x04 offset 0x24 len
<mem_ptr> 0x44 add mstore // [] 0x00 error sig 0x04 offset 0x24 len 0x44: "string"
0x64 0x00 revert
}
// Main Macro
#define macro MAIN_ERC20() = takes(1) returns (1) {
// Identify which function is being called.
// [func sig]
dup1 __FUNC_SIG(transfer) eq transferJump jumpi
dup1 __FUNC_SIG(transferFrom) eq transferFromJump jumpi
dup1 __FUNC_SIG(balanceOf) eq balanceOfJump jumpi
dup1 __FUNC_SIG(totalSupply) eq totalSupplyJump jumpi
dup1 __FUNC_SIG(approve) eq approveJump jumpi
dup1 __FUNC_SIG(allowance) eq allowanceJump jumpi
dup1 __FUNC_SIG(permit) eq permitJump jumpi
dup1 __FUNC_SIG(nonces) eq noncesJump jumpi
dup1 __FUNC_SIG(name) eq nameJump jumpi
dup1 __FUNC_SIG(symbol) eq symbolJump jumpi
dup1 __FUNC_SIG(decimals) eq decimalsJump jumpi
dup1 __FUNC_SIG(DOMAIN_SEPARATOR) eq domainSeparatorJump jumpi
unknown jump
allowanceJump:
ALLOWANCE()
approveJump:
APPROVE()
balanceOfJump:
BALANCE_OF()
decimalsJump:
DECIMALS()
domainSeparatorJump:
DOMAIN_SEPARATOR()
nameJump:
NAME()
noncesJump:
NONCES()
permitJump:
PERMIT()
symbolJump:
SYMBOL()
totalSupplyJump:
TOTAL_SUPPLY()
transferFromJump:
TRANSFER_FROM()
transferJump:
TRANSFER()
unknown:
}