-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathERC20.vy
807 lines (674 loc) · 29.4 KB
/
ERC20.vy
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
# @version ^0.3.9
"""
@title Modern and Gas-Efficient ERC-20 + EIP-2612 Implementation
@custom:contract-name ERC20
@license GNU Affero General Public License v3.0
@author pcaversaccio
@notice These functions implement the ERC-20
standard interface:
- https://eips.ethereum.org/EIPS/eip-20.
In addition, the following functions have
been added for convenience:
- `name` (`external` `view` function),
- `symbol` (`external` `view` function),
- `decimals` (`external` `view` function),
- `increase_allowance` (`external` function),
- `decrease_allowance` (`external` function),
- `burn` (`external` function),
- `burn_from` (`external` function),
- `is_minter` (`external` `view` function),
- `mint` (`external` function),
- `set_minter` (`external` function),
- `permit` (`external` function),
- `nonces` (`external` `view` function),
- `DOMAIN_SEPARATOR` (`external` `view` function),
- `eip712Domain` (`external` `view` function),
- `owner` (`external` `view` function),
- `transfer_ownership` (`external` function),
- `renounce_ownership` (`external` function),
- `_before_token_transfer` (`internal` function),
- `_after_token_transfer` (`internal` function).
The `permit` function implements approvals via
EIP-712 secp256k1 signatures:
https://eips.ethereum.org/EIPS/eip-2612.
In addition, this contract also implements the EIP-5267
function `eip712Domain`:
https://eips.ethereum.org/EIPS/eip-5267.
The implementation is inspired by OpenZeppelin's
implementation here:
https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol,
as well as by ApeAcademy's implementation here:
https://github.com/ApeAcademy/ERC20/blob/main/%7B%7Bcookiecutter.project_name%7D%7D/contracts/Token.vy.
@custom:security This ERC-20 implementation allows the commonly known
address poisoning attack, where `transferFrom` instructions
are executed from arbitrary addresses with an `amount` of 0.
However, this poisoning attack is not an on-chain vulnerability.
All assets are safe. It is an off-chain log interpretation issue.
The main reason why we do not disallow address poisonig is that
we do not want to potentially break any DeFi composability.
This issue has been extensively discussed here:
https://github.com/pcaversaccio/snekmate/issues/51,
as well as in the OpenZeppelin repository:
https://github.com/OpenZeppelin/openzeppelin-contracts/issues/3931.
"""
# @dev We import and implement the `ERC20` interface,
# which is a built-in interface of the Vyper compiler.
from vyper.interfaces import ERC20
implements: ERC20
# @dev We import and implement the `ERC20Detailed` interface,
# which is a built-in interface of the Vyper compiler.
from vyper.interfaces import ERC20Detailed
implements: ERC20Detailed
# @dev We import and implement the `IERC20Permit`
# interface, which is written using standard Vyper
# syntax.
import interfaces.IERC20Permit as IERC20Permit
implements: IERC20Permit
# @dev We import and implement the `IERC5267` interface,
# which is written using standard Vyper syntax.
from ..utils.interfaces.IERC5267 import IERC5267
implements: IERC5267
# @dev Returns the decimals places of the token.
# The default value is 18.
# @notice If you declare a variable as `public`,
# Vyper automatically generates an `external`
# getter function for the variable. Furthermore,
# to preserve consistency with the interface for
# the optional metadata functions of the ERC-20
# standard, we use lower case letters for the
# `immutable` and `constant` variables `name`,
# `symbol`, and `decimals`.
decimals: public(constant(uint8)) = 18
# @dev Constant used as part of the ECDSA recovery function.
_MALLEABILITY_THRESHOLD: constant(bytes32) = 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0
# @dev The 32-byte type hash for the EIP-712 domain separator.
_TYPE_HASH: constant(bytes32) = keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)")
# @dev The 32-byte type hash of the `permit` function.
_PERMIT_TYPE_HASH: constant(bytes32) = keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)")
# @dev Returns the name of the token.
# @notice See comment on lower case letters
# above at `decimals`.
name: public(immutable(String[25]))
# @dev Returns the symbol of the token.
# @notice See comment on lower case letters
# above at `decimals`.
symbol: public(immutable(String[5]))
# @dev Caches the domain separator as an `immutable`
# value, but also stores the corresponding chain ID
# to invalidate the cached domain separator if the
# chain ID changes.
_CACHED_DOMAIN_SEPARATOR: immutable(bytes32)
_CACHED_CHAIN_ID: immutable(uint256)
# @dev Caches `self` to `immutable` storage to avoid
# potential issues if a vanilla contract is used in
# a `delegatecall` context.
_CACHED_SELF: immutable(address)
# @dev `immutable` variables to store the (hashed)
# name and (hashed) version during contract creation.
_NAME: immutable(String[50])
_HASHED_NAME: immutable(bytes32)
_VERSION: immutable(String[20])
_HASHED_VERSION: immutable(bytes32)
# @dev Returns the amount of tokens owned by an `address`.
balanceOf: public(HashMap[address, uint256])
# @dev Returns the remaining number of tokens that a
# `spender` will be allowed to spend on behalf of
# `owner` through `transferFrom`. This is zero by
# default. This value changes when `approve`,
# `increase_allowance`, `decrease_allowance`, or
# `transferFrom` are called.
allowance: public(HashMap[address, HashMap[address, uint256]])
# @dev Returns the amount of tokens in existence.
totalSupply: public(uint256)
# @dev Returns the address of the current owner.
owner: public(address)
# @dev Returns `True` if an `address` has been
# granted the minter role.
is_minter: public(HashMap[address, bool])
# @dev Returns the current on-chain tracked nonce
# of `address`.
nonces: public(HashMap[address, uint256])
# @dev Emitted when `amount` tokens are moved
# from one account (`owner`) to another (`to`).
# Note that the parameter `amount` may be zero.
event Transfer:
owner: indexed(address)
to: indexed(address)
amount: uint256
# @dev Emitted when the allowance of a `spender`
# for an `owner` is set by a call to `approve`.
# The parameter `amount` is the new allowance.
event Approval:
owner: indexed(address)
spender: indexed(address)
amount: uint256
# @dev May be emitted to signal that the domain could
# have changed.
event EIP712DomainChanged:
pass
# @dev Emitted when the ownership is transferred
# from `previous_owner` to `new_owner`.
event OwnershipTransferred:
previous_owner: indexed(address)
new_owner: indexed(address)
# @dev Emitted when the status of a `minter`
# address is changed.
event RoleMinterChanged:
minter: indexed(address)
status: bool
@external
@payable
def __init__(name_: String[25], symbol_: String[5], initial_supply_: uint256, name_eip712_: String[50], version_eip712_: String[20]):
"""
@dev To omit the opcodes for checking the `msg.value`
in the creation-time EVM bytecode, the constructor
is declared as `payable`.
@notice The initial supply of the token as well
as the `owner` role will be assigned to
the `msg.sender`.
@param name_ The maximum 25-character user-readable
string name of the token.
@param symbol_ The maximum 5-character user-readable
string symbol of the token.
@param initial_supply_ The initial supply of the token.
@param name_eip712_ The maximum 50-character user-readable
string name of the signing domain, i.e. the name
of the dApp or protocol.
@param version_eip712_ The maximum 20-character current
main version of the signing domain. Signatures
from different versions are not compatible.
"""
initial_supply: uint256 = initial_supply_ * 10 ** convert(decimals, uint256)
name = name_
symbol = symbol_
self._transfer_ownership(msg.sender)
self.is_minter[msg.sender] = True
log RoleMinterChanged(msg.sender, True)
if (initial_supply != empty(uint256)):
self._before_token_transfer(empty(address), msg.sender, initial_supply)
self.totalSupply = initial_supply
self.balanceOf[msg.sender] = initial_supply
log Transfer(empty(address), msg.sender, initial_supply)
self._after_token_transfer(empty(address), msg.sender, initial_supply)
_NAME = name_eip712_
_VERSION = version_eip712_
_HASHED_NAME = keccak256(name_eip712_)
_HASHED_VERSION = keccak256(version_eip712_)
_CACHED_DOMAIN_SEPARATOR = self._build_domain_separator()
_CACHED_CHAIN_ID = chain.id
_CACHED_SELF = self
@external
def transfer(to: address, amount: uint256) -> bool:
"""
@dev Moves `amount` tokens from the caller's
account to `to`.
@notice Note that `to` cannot be the zero address.
Also, the caller must have a balance of at
least `amount`.
@param to The 20-byte receiver address.
@param amount The 32-byte token amount to be transferred.
@return bool The verification whether the transfer succeeded
or failed. Note that the function reverts instead
of returning `False` on a failure.
"""
self._transfer(msg.sender, to, amount)
return True
@external
def approve(spender: address, amount: uint256) -> bool:
"""
@dev Sets `amount` as the allowance of `spender`
over the caller's tokens.
@notice WARNING: Note that if `amount` is the maximum
`uint256`, the allowance is not updated on
`transferFrom`. This is semantically equivalent
to an infinite approval. Also, `spender` cannot
be the zero address.
IMPORTANT: Beware that changing an allowance
with this method brings the risk that someone
may use both the old and the new allowance by
unfortunate transaction ordering. One possible
solution to mitigate this race condition is to
first reduce the spender's allowance to 0 and
set the desired amount afterwards:
https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729.
@param spender The 20-byte spender address.
@param amount The 32-byte token amount that is
allowed to be spent by the `spender`.
@return bool The verification whether the approval operation
succeeded or failed. Note that the function reverts
instead of returning `False` on a failure.
"""
self._approve(msg.sender, spender, amount)
return True
@external
def transferFrom(owner: address, to: address, amount: uint256) -> bool:
"""
@dev Moves `amount` tokens from `owner`
to `to` using the allowance mechanism.
The `amount` is then deducted from the
caller's allowance.
@notice Note that `owner` and `to` cannot
be the zero address. Also, `owner`
must have a balance of at least `amount`.
Eventually, the caller must have allowance
for `owner`'s tokens of at least `amount`.
WARNING: The function does not update the
allowance if the current allowance is the
maximum `uint256`.
@param owner The 20-byte owner address.
@param to The 20-byte receiver address.
@param amount The 32-byte token amount to be transferred.
@return bool The verification whether the transfer succeeded
or failed. Note that the function reverts instead
of returning `False` on a failure.
"""
self._spend_allowance(owner, msg.sender, amount)
self._transfer(owner, to, amount)
return True
@external
def increase_allowance(spender: address, added_amount: uint256) -> bool:
"""
@dev Atomically increases the allowance granted to
`spender` by the caller.
@notice This is an alternative to `approve` that can
be used as a mitigation for the problems
described in https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729.
Note that `spender` cannot be the zero address.
@param spender The 20-byte spender address.
@param added_amount The 32-byte token amount that is
added atomically to the allowance of the `spender`.
@return bool The verification whether the allowance increase
operation succeeded or failed. Note that the function
reverts instead of returning `False` on a failure.
"""
self._approve(msg.sender, spender, self.allowance[msg.sender][spender] + added_amount)
return True
@external
def decrease_allowance(spender: address, subtracted_amount: uint256) -> bool:
"""
@dev Atomically decreases the allowance granted to
`spender` by the caller.
@notice This is an alternative to `approve` that can
be used as a mitigation for the problems
described in https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729.
Note that `spender` cannot be the zero address.
Also, `spender` must have an allowance for
the caller of at least `subtracted_amount`.
@param spender The 20-byte spender address.
@param subtracted_amount The 32-byte token amount that is
subtracted atomically from the allowance of the `spender`.
@return bool The verification whether the allowance decrease
operation succeeded or failed. Note that the function
reverts instead of returning `False` on a failure.
"""
current_allowance: uint256 = self.allowance[msg.sender][spender]
assert current_allowance >= subtracted_amount, "ERC20: decreased allowance below zero"
self._approve(msg.sender, spender, unsafe_sub(current_allowance, subtracted_amount))
return True
@external
def burn(amount: uint256):
"""
@dev Destroys `amount` tokens from the caller.
@param amount The 32-byte token amount to be destroyed.
"""
self._burn(msg.sender, amount)
@external
def burn_from(owner: address, amount: uint256):
"""
@dev Destroys `amount` tokens from `owner`,
deducting from the caller's allowance.
@notice Note that `owner` cannot be the
zero address. Also, the caller must
have an allowance for `owner`'s tokens
of at least `amount`.
@param owner The 20-byte owner address.
@param amount The 32-byte token amount to be destroyed.
"""
self._spend_allowance(owner, msg.sender, amount)
self._burn(owner, amount)
@external
def mint(owner: address, amount: uint256):
"""
@dev Creates `amount` tokens and assigns them to `owner`.
@notice Only authorised minters can access this function.
Note that `owner` cannot be the zero address.
@param amount The 32-byte token amount to be created.
"""
assert self.is_minter[msg.sender], "AccessControl: access is denied"
self._mint(owner, amount)
@external
def set_minter(minter: address, status: bool):
"""
@dev Adds or removes an address `minter` to/from the
list of allowed minters. Note that only the
`owner` can add or remove `minter` addresses.
Also, the `minter` cannot be the zero address.
Eventually, the `owner` cannot remove himself
from the list of allowed minters.
@param minter The 20-byte minter address.
@param status The Boolean variable that sets the status.
"""
self._check_owner()
assert minter != empty(address), "AccessControl: minter is the zero address"
assert minter != self.owner, "AccessControl: minter is owner address"
self.is_minter[minter] = status
log RoleMinterChanged(minter, status)
@external
def permit(owner: address, spender: address, amount: uint256, deadline: uint256, v: uint8, r: bytes32, s: bytes32):
"""
@dev Sets `amount` as the allowance of `spender`
over `owner`'s tokens, given `owner`'s signed
approval.
@notice Note that `spender` cannot be the zero address.
Also, `deadline` must be a block timestamp in
the future. `v`, `r`, and `s` must be a valid
secp256k1 signature from `owner` over the
EIP-712-formatted function arguments. Eventually,
the signature must use `owner`'s current nonce.
@param owner The 20-byte owner address.
@param spender The 20-byte spender address.
@param amount The 32-byte token amount that is
allowed to be spent by the `spender`.
@param deadline The 32-byte block timestamp up
which the `spender` is allowed to spend `amount`.
@param v The secp256k1 1-byte signature parameter `v`.
@param r The secp256k1 32-byte signature parameter `r`.
@param s The secp256k1 32-byte signature parameter `s`.
"""
assert block.timestamp <= deadline, "ERC20Permit: expired deadline"
current_nonce: uint256 = self.nonces[owner]
self.nonces[owner] = unsafe_add(current_nonce, 1)
struct_hash: bytes32 = keccak256(_abi_encode(_PERMIT_TYPE_HASH, owner, spender, amount, current_nonce, deadline))
hash: bytes32 = self._hash_typed_data_v4(struct_hash)
signer: address = self._recover_vrs(hash, convert(v, uint256), convert(r, uint256), convert(s, uint256))
assert signer == owner, "ERC20Permit: invalid signature"
self._approve(owner, spender, amount)
@external
@view
def DOMAIN_SEPARATOR() -> bytes32:
"""
@dev Returns the domain separator for the current chain.
@return bytes32 The 32-byte domain separator.
"""
return self._domain_separator_v4()
@external
@view
def eip712Domain() -> (bytes1, String[50], String[20], uint256, address, bytes32, DynArray[uint256, 128]):
"""
@dev Returns the fields and values that describe the domain
separator used by this contract for EIP-712 signatures.
@notice The bits in the 1-byte bit map are read from the least
significant to the most significant, and fields are indexed
in the order that is specified by EIP-712, identical to the
order in which they are listed in the function type.
@return bytes1 The 1-byte bit map where bit `i` is set to 1
if and only if domain field `i` is present (`0 ≤ i ≤ 4`).
@return String The maximum 50-character user-readable string name
of the signing domain, i.e. the name of the dApp or protocol.
@return String The maximum 20-character current main version of
the signing domain. Signatures from different versions are
not compatible.
@return uint256 The 32-byte EIP-155 chain ID.
@return address The 20-byte address of the verifying contract.
@return bytes32 The 32-byte disambiguation salt for the protocol.
@return DynArray The 32-byte array of EIP-712 extensions.
"""
# Note that `\x0f` equals `01111`.
return (convert(b"\x0f", bytes1), _NAME, _VERSION, chain.id, self, empty(bytes32), empty(DynArray[uint256, 128]))
@external
def transfer_ownership(new_owner: address):
"""
@dev Transfers the ownership of the contract
to a new account `new_owner`.
@notice Note that this function can only be
called by the current `owner`. Also,
the `new_owner` cannot be the zero address.
WARNING: The ownership transfer also removes
the previous owner's minter role and assigns
the minter role to `new_owner` accordingly.
@param new_owner The 20-byte address of the new owner.
"""
self._check_owner()
assert new_owner != empty(address), "Ownable: new owner is the zero address"
self.is_minter[msg.sender] = False
log RoleMinterChanged(msg.sender, False)
self._transfer_ownership(new_owner)
self.is_minter[new_owner] = True
log RoleMinterChanged(new_owner, True)
@external
def renounce_ownership():
"""
@dev Leaves the contract without an owner.
@notice Renouncing ownership will leave the
contract without an owner, thereby
removing any functionality that is
only available to the owner. Note
that the `owner` is also removed from
the list of allowed minters.
WARNING: All other existing `minter`
addresses will still be able to create
new tokens. Consider removing all non-owner
minter addresses first via `set_minter`
before calling `renounce_ownership`.
"""
self._check_owner()
self.is_minter[msg.sender] = False
log RoleMinterChanged(msg.sender, False)
self._transfer_ownership(empty(address))
@internal
def _transfer(owner: address, to: address, amount: uint256):
"""
@dev Moves `amount` tokens from the owner's
account to `to`.
@notice Note that `owner` and `to` cannot be
the zero address. Also, `owner` must
have a balance of at least `amount`.
@param owner The 20-byte owner address.
@param to The 20-byte receiver address.
@param amount The 32-byte token amount to be transferred.
"""
assert owner != empty(address), "ERC20: transfer from the zero address"
assert to != empty(address), "ERC20: transfer to the zero address"
self._before_token_transfer(owner, to, amount)
owner_balanceOf: uint256 = self.balanceOf[owner]
assert owner_balanceOf >= amount, "ERC20: transfer amount exceeds balance"
self.balanceOf[owner] = unsafe_sub(owner_balanceOf, amount)
self.balanceOf[to] = unsafe_add(self.balanceOf[to], amount)
log Transfer(owner, to, amount)
self._after_token_transfer(owner, to, amount)
@internal
def _mint(owner: address, amount: uint256):
"""
@dev Creates `amount` tokens and assigns
them to `owner`, increasing the
total supply.
@notice This is an `internal` function without
access restriction. Note that `owner`
cannot be the zero address.
@param owner The 20-byte owner address.
@param amount The 32-byte token amount to be created.
"""
assert owner != empty(address), "ERC20: mint to the zero address"
self._before_token_transfer(empty(address), owner, amount)
self.totalSupply += amount
self.balanceOf[owner] = unsafe_add(self.balanceOf[owner], amount)
log Transfer(empty(address), owner, amount)
self._after_token_transfer(empty(address), owner, amount)
@internal
def _burn(owner: address, amount: uint256):
"""
@dev Destroys `amount` tokens from `owner`,
reducing the total supply.
@notice Note that `owner` cannot be the
zero address. Also, `owner` must
have at least `amount` tokens.
@param owner The 20-byte owner address.
@param amount The 32-byte token amount to be destroyed.
"""
assert owner != empty(address), "ERC20: burn from the zero address"
self._before_token_transfer(owner, empty(address), amount)
account_balance: uint256 = self.balanceOf[owner]
assert account_balance >= amount, "ERC20: burn amount exceeds balance"
self.balanceOf[owner] = unsafe_sub(account_balance, amount)
self.totalSupply = unsafe_sub(self.totalSupply, amount)
log Transfer(owner, empty(address), amount)
self._after_token_transfer(owner, empty(address), amount)
@internal
def _approve(owner: address, spender: address, amount: uint256):
"""
@dev Sets `amount` as the allowance of `spender`
over the `owner`'s tokens.
@notice Note that `owner` and `spender` cannot
be the zero address.
@param owner The 20-byte owner address.
@param spender The 20-byte spender address.
@param amount The 32-byte token amount that is
allowed to be spent by the `spender`.
"""
assert owner != empty(address), "ERC20: approve from the zero address"
assert spender != empty(address), "ERC20: approve to the zero address"
self.allowance[owner][spender] = amount
log Approval(owner, spender, amount)
@internal
def _spend_allowance(owner: address, spender: address, amount: uint256):
"""
@dev Updates `owner`'s allowance for `spender`
based on spent `amount`.
@notice WARNING: Note that it does not update the
allowance `amount` in case of infinite
allowance. Also, it reverts if not enough
allowance is available.
@param owner The 20-byte owner address.
@param spender The 20-byte spender address.
@param amount The 32-byte token amount that is
allowed to be spent by the `spender`.
"""
current_allowance: uint256 = self.allowance[owner][spender]
if (current_allowance != max_value(uint256)):
# The following line allows the commonly known address
# poisoning attack, where `transferFrom` instructions
# are executed from arbitrary addresses with an `amount`
# of 0. However, this poisoning attack is not an on-chain
# vulnerability. All assets are safe. It is an off-chain
# log interpretation issue.
assert current_allowance >= amount, "ERC20: insufficient allowance"
self._approve(owner, spender, unsafe_sub(current_allowance, amount))
@internal
def _before_token_transfer(owner: address, to: address, amount: uint256):
"""
@dev Hook that is called before any transfer of tokens.
This includes minting and burning.
@notice The calling conditions are:
- when `owner` and `to` are both non-zero,
`amount` of `owner`'s tokens will be
transferred to `to`,
- when `owner` is zero, `amount` tokens will
be minted for `to`,
- when `to` is zero, `amount` of `owner`'s
tokens will be burned,
- `owner` and `to` are never both zero.
@param owner The 20-byte owner address.
@param to The 20-byte receiver address.
@param amount The 32-byte token amount to be transferred.
"""
pass
@internal
def _after_token_transfer(owner: address, to: address, amount: uint256):
"""
@dev Hook that is called after any transfer of tokens.
This includes minting and burning.
@notice The calling conditions are:
- when `owner` and `to` are both non-zero,
`amount` of `owner`'s tokens has been
transferred to `to`,
- when `owner` is zero, `amount` tokens
have been minted for `to`,
- when `to` is zero, `amount` of `owner`'s
tokens have been burned,
- `owner` and `to` are never both zero.
@param owner The 20-byte owner address.
@param to The 20-byte receiver address.
@param amount The 32-byte token amount that has
been transferred.
"""
pass
@internal
def _check_owner():
"""
@dev Sourced from {Ownable-_check_owner}.
@notice See {Ownable-_check_owner} for
the function docstring.
"""
assert msg.sender == self.owner, "Ownable: caller is not the owner"
@internal
def _transfer_ownership(new_owner: address):
"""
@dev Sourced from {Ownable-_transfer_ownership}.
@notice See {Ownable-_transfer_ownership} for
the function docstring.
"""
old_owner: address = self.owner
self.owner = new_owner
log OwnershipTransferred(old_owner, new_owner)
@internal
@view
def _domain_separator_v4() -> bytes32:
"""
@dev Sourced from {EIP712DomainSeparator-domain_separator_v4}.
@notice See {EIP712DomainSeparator-domain_separator_v4}
for the function docstring.
"""
if (self == _CACHED_SELF and chain.id == _CACHED_CHAIN_ID):
return _CACHED_DOMAIN_SEPARATOR
else:
return self._build_domain_separator()
@internal
@view
def _build_domain_separator() -> bytes32:
"""
@dev Sourced from {EIP712DomainSeparator-_build_domain_separator}.
@notice See {EIP712DomainSeparator-_build_domain_separator}
for the function docstring.
"""
return keccak256(_abi_encode(_TYPE_HASH, _HASHED_NAME, _HASHED_VERSION, chain.id, self))
@internal
@view
def _hash_typed_data_v4(struct_hash: bytes32) -> bytes32:
"""
@dev Sourced from {EIP712DomainSeparator-hash_typed_data_v4}.
@notice See {EIP712DomainSeparator-hash_typed_data_v4}
for the function docstring.
"""
return self._to_typed_data_hash(self._domain_separator_v4(), struct_hash)
@internal
@pure
def _to_typed_data_hash(domain_separator: bytes32, struct_hash: bytes32) -> bytes32:
"""
@dev Sourced from {ECDSA-to_typed_data_hash}.
@notice See {ECDSA-to_typed_data_hash} for the
function docstring.
"""
return keccak256(concat(b"\x19\x01", domain_separator, struct_hash))
@internal
@pure
def _recover_vrs(hash: bytes32, v: uint256, r: uint256, s: uint256) -> address:
"""
@dev Sourced from {ECDSA-_recover_vrs}.
@notice See {ECDSA-_recover_vrs} for the
function docstring.
"""
return self._try_recover_vrs(hash, v, r, s)
@internal
@pure
def _try_recover_vrs(hash: bytes32, v: uint256, r: uint256, s: uint256) -> address:
"""
@dev Sourced from {ECDSA-_try_recover_vrs}.
@notice See {ECDSA-_try_recover_vrs} for the
function docstring.
"""
if (s > convert(_MALLEABILITY_THRESHOLD, uint256)):
raise "ECDSA: invalid signature 's' value"
signer: address = ecrecover(hash, v, r, s)
if (signer == empty(address)):
raise "ECDSA: invalid signature"
return signer