flashloan-rs 0.2.3

Minimal Multicall3 Flashloan Module
Documentation
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/// @title ERC20
/// @notice SPDX-License-Identifier: MIT
/// @author asnared <https://github.com/abigger87>
/// @author devtooligan <https://github.com/devtooligan>
/// @notice Modern and gas efficient ERC20 + EIP-2612 implementation
/// @notice Adapted from Solmate (https://github.com/transmissions11/solmate/blob/main/src/tokens/ERC20.sol)

// Imports
#include "../utils/Errors.huff"
#include "../auth/NonPayable.huff"
#include "../data-structures/Hashmap.huff"

// Interface
#define function allowance(address,address) view returns (uint256)
#define function approve(address,uint256) nonpayable returns ()
#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 indexed, address indexed, uint256)
#define event Transfer(address, address, uint256)

// Metadata
#define function decimals() nonpayable returns (uint256)
#define function name() nonpayable returns (string)
#define function symbol() nonpayable returns (string)

// META_NAME = "Token"
#define constant META_NAME = 0x546f6b656e000000000000000000000000000000000000000000000000000000
#define constant META_NAME_LENGTH = 0x05

// META_SYMBOL = "TKN"
#define constant META_SYMBOL = 0x544B4E0000000000000000000000000000000000000000000000000000000000
#define constant META_SYMBOL_LENGTH = 0x03


// ERC20 Storage
#define constant TOTAL_SUPPLY_SLOT = FREE_STORAGE_POINTER()
#define constant BALANCE_SLOT = FREE_STORAGE_POINTER()
#define constant APPROVAL_SLOT = FREE_STORAGE_POINTER()
#define constant DECIMALS_SLOT = FREE_STORAGE_POINTER()

// EIP-2612 STORAGE
#define constant INITIAL_CHAIN_ID = FREE_STORAGE_POINTER()
#define constant INITIAL_DOMAIN_SEPARATOR = FREE_STORAGE_POINTER()
#define constant NONCE_SLOT = FREE_STORAGE_POINTER()

// PERMIT_TYPEHASH = keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)")
#define constant PERMIT_TYPEHASH = 0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9
#define constant X_1901 = 0x1901000000000000000000000000000000000000000000000000000000000000

// Utility Constants
#define constant UINT_256_MAX = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
#define constant ERROR_SIG = 0x08c379a000000000000000000000000000000000000000000000000000000000
#define constant ZERO_ADDRESS = 0x0000000000000000000000000000000000000000

/// @notice Constructor
/// @notice Sets the initial domain separator and chain ID
#define macro ERC20_CONSTRUCTOR() = takes (0) returns (0) {
    // Copy the decimals into memory from the bytecode
    0x20                                    // [size] - byte size to copy
    0x20 codesize sub                       // [offset, size] - offset in the code to copy from
    0x00                                    // [mem, offset, size] - offset in memory to copy to
    codecopy                                // []

    // Copy the decimals from memory into the DECIMALS_SLOT storage location
    0x00 mload                              // [decimals]
    [DECIMALS_SLOT] sstore                  // []

    chainid [INITIAL_CHAIN_ID] sstore       // []
    COMPUTE_DOMAIN_SEPARATOR()              // [DOMAIN SEPARATOR]
    [INITIAL_DOMAIN_SEPARATOR] sstore       // []
}

/// @notice Approve
/// @notice Grants approval to an operator to transfer tokens on behalf of the sender.
#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_KEYS_2D(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                                    // []

    // Return 01 for true
    0x01 0x00 mstore                        // []
    0x20 0x00 return                        // []
}

/// @notice Transfer
/// @notice Non-Payable function that transfers an amount of tokens from the sender to a recipient.
#define macro TRANSFER() = takes (0) returns (0) {
    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.
    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            // []
}

/// @notice Transfer From
/// @notice Non-Payable function that transfers an amount of tokens from an address to a recipient.
#define macro TRANSFER_FROM() = takes (0) returns (0) {
    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_KEYS_2D(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_KEYS_2D(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.
        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                    // []
}

/// @notice Transfers an amount of tokens from
#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]
}

/// @notice Transfers an amount of tokens from one address to another.
#define macro _TRANSFER_GIVE_TO() = takes (3) returns (3) {
    // 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]
}

/// @notice Approve
/// @notice Approves an address to spend an amount of tokens on the caller's behalf
#define macro APPROVE() = takes (0) returns (0) {
    0x24 calldataload                           // [value]
    dup1 0x00 mstore                            // [value]
    0x04 calldataload                           // [to, value]
    caller                                      // [from, to, value]

    // Emit the approval event.
    dup2 dup2                                   // [from, to, from, to, value]
    __EVENT_HASH(APPROVAL_EVENT_SIGNATURE)      // [sig, from, to, from, to, value]
    0x20 0x00                                   // [0, 32, sig, from, to, from, to, value]
    log3                                        // [from, to, value]

    // Store the value at slot = keccak256(from . to)
    STORE_ELEMENT_FROM_KEYS(0x00)
}

/// @notice Domain Separator
/// @notice Returns the EIP-712 domain separator
#define macro DOMAIN_SEPARATOR() = takes (0) returns (0) {
    NON_PAYABLE()                   // []
    _DOMAIN_SEPARATOR()             // [domain separator]
    0x00 mstore                     // [domain separator]
    0x20 0x00 return                // []
}

/// @notice Loads the EIP-712 domain separator
#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:
}

/// @notice Computes the EIP-712 domain separator
#define macro COMPUTE_DOMAIN_SEPARATOR() = takes (0) returns (1) {
    // WARNING: 0x00 - 0x3f (64 bytes): scratch space for hashing methods
    // AS SUCH, WE STORE VARIABLES IN MEMORY STARTING AT 0x40
    [PERMIT_TYPEHASH] 0x40 mstore   // []
    [META_NAME] 0x60 mstore         // []
    0x20 0x60 sha3 0x60 mstore      // []

    // 0x31 is hex for ascii for 1
    0x31 0x80 mstore                // []
    0x02 0x80 sha3 0x80 mstore      // [hash of "1"]

    chainid 0xa0 mstore             // [chainid]
    address 0xc0 mstore             // [address(this)]

    0xA0 0x40 sha3                  // [hash]
}

/// @notice Permit
/// @notice EIP 2612 Signed Approvals
#define macro PERMIT() = takes (0) returns (0) {
    NON_PAYABLE()
    // function permit arg    calldata loc
    //     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]

    // Calculate inner keccak
    // keccak256(
    //      abi.encode(
    //        PERMIT_TYPEHASH,
    //        owner,
    //        spender,
    //        value,
    //        nonces[owner]++,
    //        deadline
    //      )
    // )
    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]

    // Grab the domain separator
    _DOMAIN_SEPARATOR()                     // [domain separator, inner hash]
    [X_1901]                                // [x1901, domain separator, inner hash]

    // Bitwise shifts
    dup3 0xf0 shl                           // [inner hash << 0xf0, x1901, domain separator, inner hash]

    // Create the second word
    dup3 0xf0 shl                           // [domain separator << 0xf0, inner hash << 0xf0, x1901, domain separator, inner hash]
    dup5 0x10 shr or                        // [domain separator << 0xf0 | inner hash >> 0x10, inner hash << 0xf0, x1901, domain separator, inner hash]

    // Create the first word
    dup4 dup4 swap1 0x10 shr or             // [x1901 | domain separator >> 0x10, domain separator << 0xf0 | inner hash >> 0x10, inner hash << 0xf0, x1901, domain separator, inner hash]

    // Prepare memory mstore outer keccak
    0x40 mstore                             // [domain separator << 0xf0 | inner hash >> 0x10, inner hash << 0xf0, x1901, domain separator, inner hash]
    0x60 mstore                             // [inner hash << 0xf0, x1901, domain separator, inner hash]
    0x80 mstore                             // [x1901, domain separator, inner hash]
    0x42 0x40                               // [loc, len, x1901, domain separator, inner hash]
    sha3                                    // [outer hash, x1901, domain separator, inner hash]

    // Store signature in memory                                 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]

    // Revert invalid signer if call failed
    iszero invalidSigner jumpi              // [spender, value]

    // Load the recovered address from memory
    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_KEYS_2D(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                                    // []

    // Stop Execution
    stop                                    // []

    expired:
        0x5045524D49545F444541444C494E455F45585049524544000000000000000000         // ["PERMIT_DEADLINE_EXPIRED"]
        0x17                                                                       // [23 (length), "PERMIT_DEADLINE_EXPIRED"]
        0x00                                                                       // [0, 23 (length), "PERMIT_DEADLINE_EXPIRED"]
        REQUIRE()

    invalidSigner:
        0x494E56414C49445F5349474E4552000000000000000000000000000000000000         // ["INVALID_SIGNER"]
        0x0e                                                                       // [14 (length), "INVALID_SIGNER"]
        0x00                                                                       // [0, 14 (length), "INVALID_SIGNER"]
        REQUIRE()
}

/// @notice Takes an address off the stack, returns the current nonce for that address onto the stack.
/// @notice Increments the nonce for next time,
#define macro _NONCE_PLUS_PLUS() = takes (1) returns (1) {
    // input 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]
}

/// @notice Nonces
/// @notice Returns the current nonce for an account
#define macro NONCES() = takes (0) returns (0) {
    0x04 calldataload                               // [account]
    [NONCE_SLOT] LOAD_ELEMENT_FROM_KEYS(0x00)       // [nonce]
    0x00 mstore                                     // []
    0x20 0x00 return                                // []
}

/// @notice Name
/// @notice Returns the token name string
#define macro NAME() = takes (0) returns (0) {
    NON_PAYABLE()                       // []
    0x20 0x00 mstore                    // []
    [META_NAME_LENGTH] 0x20 mstore      // []
    [META_NAME] 0x40 mstore             // []
    0x60 0x00 return                    // []
}

/// @notice Symbol
/// @notice Returns the symbol of the token
#define macro SYMBOL() = takes (0) returns (0) {
    NON_PAYABLE()                       // []
    0x20 0x00 mstore                    // []
    [META_SYMBOL_LENGTH] 0x20 mstore    // []
    [META_SYMBOL] 0x40 mstore           // []
    0x60 0x00 return                    // []
}

/// @notice Decimals
/// @notice Returns the token decimal representation
#define macro DECIMALS() = takes (0) returns (0) {
    NON_PAYABLE()                       // []
    [DECIMALS_SLOT] sload               // [decimals]
    0x00 mstore                         // []
    0x20 0x00 return                    // []
}

/// @notice Balance Of
/// @notice Returns the token balance of an address
#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                                // []
}

/// @notice Total Supply
/// @notice Returns the total supply of the token
#define macro TOTAL_SUPPLY() = takes (0) returns (0) {
    NON_PAYABLE()                       // []
    [TOTAL_SUPPLY_SLOT] sload           // [supply]
    0x00 mstore                         // []
    0x20 0x00 return                    // []
}

/// @notice Allowance
/// @notice Returns the amount which a spender is allowed to transfer on behalf of an owner
#define macro ALLOWANCE() = takes (0) returns (0) {
    NON_PAYABLE()                       // []
    0x24 calldataload                   // [to]
    0x04 calldataload                   // [from, to]
    [APPROVAL_SLOT]                     // [slot, from, to]
    LOAD_ELEMENT_FROM_KEYS_2D(0x00)     // [allowance]
    0x00 mstore                         // []
    0x20 0x00 return                    // []
}

// MINT/BURN LOGIC

#define macro _BURN() = takes(2) returns (0) {
    // stack input: [value, from]
    [ZERO_ADDRESS]                  // [zero, value, from]
    swap2 swap1                     // [value, from, zero]

    _TRANSFER_TAKE_FROM()           // [value, from, zero]
    dup1                            // [value, value, from, zero]
    [TOTAL_SUPPLY_SLOT] sload       // [supply, value, value, from, zero]
    sub                             // [supply-value, value, from, zero]
    [TOTAL_SUPPLY_SLOT] sstore      // [value, from, zero]

    // Emit the transfer event.
    0x00 mstore                     // [from, zero]
    __EVENT_HASH(Transfer)          // [sig, from, zero]
    0x20 0x00                       // [0, 32, sig, from, zero]
    log3                            // []
}

/// @notice Mints tokens to a specified address
#define macro _MINT() = takes (2) returns (0) {
    // Input stack: [value, to]
    dup2                            // [to, value, to]
    swap1                           // [value, to, to]
    _TRANSFER_GIVE_TO()             // [value, to, to]

    // Update totalSupply
    dup1                            // [value, value, to, to]
    [TOTAL_SUPPLY_SLOT] sload       // [supply, value, value, to, to]
    add                             // [supply + value, value, to, to]
    [TOTAL_SUPPLY_SLOT] sstore      // [value, to, to]

    // Emit the transfer event.
    0x00 mstore                     // [to, to]
    [ZERO_ADDRESS]                  // [address(0), to, to]
    __EVENT_HASH(Transfer)          // [sig, from, to, to]
    0x20 0x00                       // [0, 32, sig, from, to, to]
    log3 pop                        // []
}


// Function Dispatching
#define macro ERC20_MAIN() = takes (1) returns (1) {
    // Identify which function is being called.
    // [func sig]

    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

    dup1 __FUNC_SIG(totalSupply)        eq totalSupplyJump      jumpi
    dup1 __FUNC_SIG(balanceOf)          eq balanceOfJump        jumpi
    dup1 __FUNC_SIG(allowance)          eq allowanceJump        jumpi

    dup1 __FUNC_SIG(transfer)           eq transferJump         jumpi
    dup1 __FUNC_SIG(transferFrom)       eq transferFromJump     jumpi
    dup1 __FUNC_SIG(approve)            eq approveJump          jumpi

    // Bubble up to the parent macro
    no_match jumpi

    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()

    no_match:
}