-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathERC20Capped.sol
56 lines (47 loc) · 1.4 KB
/
ERC20Capped.sol
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
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/extensions/ERC20Capped.sol)
pragma solidity ^0.8.20;
import {ERC20} from "../ERC20.sol";
/**
* @dev Extension of {ERC20} that adds a cap to the supply of tokens.
*/
abstract contract ERC20Capped is ERC20 {
uint256 private immutable _cap;
/**
* @dev Total supply cap has been exceeded.
*/
error ERC20ExceededCap(uint256 increasedSupply, uint256 cap);
/**
* @dev The supplied cap is not a valid cap.
*/
error ERC20InvalidCap(uint256 cap);
/**
* @dev Sets the value of the `cap`. This value is immutable, it can only be
* set once during construction.
*/
constructor(uint256 cap_) {
if (cap_ == 0) {
revert ERC20InvalidCap(0);
}
_cap = cap_;
}
/**
* @dev Returns the cap on the token's total supply.
*/
function cap() public view virtual returns (uint256) {
return _cap;
}
/**
* @dev See {ERC20-_update}.
*/
function _update(address from, address to, uint256 value) internal virtual override {
super._update(from, to, value);
if (from == address(0)) {
uint256 maxSupply = cap();
uint256 supply = totalSupply();
if (supply > maxSupply) {
revert ERC20ExceededCap(supply, maxSupply);
}
}
}
}