-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAlchemicTokenV2.sol
227 lines (190 loc) · 7.52 KB
/
AlchemicTokenV2.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
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
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.11;
import {AccessControl} from "@openzeppelin/contracts/access/AccessControl.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import {IllegalArgument, IllegalState, Unauthorized} from "./base/Errors.sol";
import {IERC3156FlashBorrower} from "./interfaces/IERC3156FlashBorrower.sol";
import {IERC3156FlashLender} from "./interfaces/IERC3156FlashLender.sol";
/// @title AlchemicTokenV2
/// @author Alchemix Finance
///
/// @notice This is the contract for version two alchemic tokens.
contract AlchemicTokenV2 is AccessControl, ReentrancyGuard, ERC20, IERC3156FlashLender {
/// @notice The identifier of the role which maintains other roles.
bytes32 public constant ADMIN_ROLE = keccak256("ADMIN");
/// @notice The identifier of the role which allows accounts to mint tokens.
bytes32 public constant SENTINEL_ROLE = keccak256("SENTINEL");
/// @notice The expected return value from a flash mint receiver
bytes32 public constant CALLBACK_SUCCESS = keccak256("ERC3156FlashBorrower.onFlashLoan");
/// @notice The maximum number of basis points needed to represent 100%.
uint256 public constant BPS = 10000;
/// @notice A set of addresses which are whitelisted for minting new tokens.
mapping(address => bool) public whitelisted;
/// @notice A set of addresses which are paused from minting new tokens.
mapping(address => bool) public paused;
/// @notice Fee for flash minting
uint256 public flashMintFee;
/// @notice Max flash mint amount
uint256 public maxFlashLoanAmount;
/// @notice An event which is emitted when a minter is paused from minting.
///
/// @param minter The address of the minter which was paused.
/// @param state A flag indicating if the alchemist is paused or unpaused.
event Paused(address minter, bool state);
/// @notice An event which is emitted when the flash mint fee is updated.
///
/// @param fee The new flash mint fee.
event SetFlashMintFee(uint256 fee);
constructor(string memory _name, string memory _symbol, uint256 _flashFee) ERC20(_name, _symbol) {
_setupRole(ADMIN_ROLE, msg.sender);
_setupRole(SENTINEL_ROLE, msg.sender);
_setRoleAdmin(SENTINEL_ROLE, ADMIN_ROLE);
_setRoleAdmin(ADMIN_ROLE, ADMIN_ROLE);
flashMintFee = _flashFee;
}
/// @dev A modifier which checks that the caller has the admin role.
modifier onlyAdmin() {
if (!hasRole(ADMIN_ROLE, msg.sender)) {
revert Unauthorized();
}
_;
}
/// @dev A modifier which checks that the caller has the sentinel role.
modifier onlySentinel() {
if(!hasRole(SENTINEL_ROLE, msg.sender)) {
revert Unauthorized();
}
_;
}
/// @dev A modifier which checks if whitelisted for minting.
modifier onlyWhitelisted() {
if(!whitelisted[msg.sender]) {
revert Unauthorized();
}
_;
}
/// @notice Sets the flash minting fee.
///
/// @notice This function reverts if `msg.sender` is not an admin.
///
/// @param newFee The new flash mint fee.
function setFlashFee(uint256 newFee) external onlyAdmin {
flashMintFee = newFee;
emit SetFlashMintFee(flashMintFee);
}
/// @notice Mints tokens to `a recipient.`
///
/// @notice This function reverts if `msg.sender` is not whitelisted.
/// @notice This function reverts if `msg.sender` is paused.
/// @notice This function reverts if `msg.sender` has exceeded their mintable ceiling.
///
/// @param recipient The address to mint the tokens to.
/// @param amount The amount of tokens to mint.
function mint(address recipient, uint256 amount) external onlyWhitelisted {
if (paused[msg.sender]) {
revert IllegalState();
}
_mint(recipient, amount);
}
/// @notice Sets `minter` as whitelisted to mint.
///
/// @notice This function reverts if `msg.sender` is not an admin.
///
/// @param minter The account to permit to mint.
/// @param state A flag indicating if the minter should be able to mint.
function setWhitelist(address minter, bool state) external onlyAdmin {
whitelisted[minter] = state;
}
/// @notice Sets `sentinel` as a sentinel.
///
/// @notice This function reverts if `msg.sender` is not an admin.
///
/// @param sentinel The address to set as a sentinel.
function setSentinel(address sentinel) external onlyAdmin {
_setupRole(SENTINEL_ROLE, sentinel);
}
/// @notice Pauses `minter` from minting tokens.
///
/// @notice This function reverts if `msg.sender` is not a sentinel.
///
/// @param minter The address to set as paused or unpaused.
/// @param state A flag indicating if the minter should be paused or unpaused.
function pauseMinter(address minter, bool state) external onlySentinel {
paused[minter] = state;
emit Paused(minter, state);
}
/// @notice Burns `amount` tokens from `msg.sender`.
///
/// @param amount The amount of tokens to be burned.
function burn(uint256 amount) external {
_burn(msg.sender, amount);
}
/// @dev Destroys `amount` tokens from `account`, deducting from the caller's allowance.
///
/// @param account The address the burn tokens from.
/// @param amount The amount of tokens to burn.
function burnFrom(address account, uint256 amount) external {
uint256 newAllowance = allowance(account, msg.sender) - amount;
_approve(account, msg.sender, newAllowance);
_burn(account, amount);
}
/// @notice Adjusts the maximum flashloan amount.
///
/// @param _maxFlashLoanAmount The maximum flashloan amount.
function setMaxFlashLoan(uint _maxFlashLoanAmount) external onlyAdmin {
maxFlashLoanAmount = _maxFlashLoanAmount;
}
/// @notice Gets the maximum amount to be flash loaned of a token.
///
/// @param token The address of the token.
///
/// @return The maximum amount of `token` that can be flashed loaned.
function maxFlashLoan(address token) public view override returns (uint256) {
if (token != address(this)) {
return 0;
}
return maxFlashLoanAmount;
}
/// @notice Gets the flash loan fee of `amount` of `token`.
///
/// @param token The address of the token.`
/// @param amount The amount of `token` to flash mint.
///
/// @return The flash loan fee.
function flashFee(address token, uint256 amount) public view override returns (uint256) {
if (token != address(this)) {
revert IllegalArgument();
}
return amount * flashMintFee / BPS;
}
/// @notice Performs a flash mint (called flash loan to confirm with ERC3156 standard).
///
/// @param receiver The address which will receive the flash minted tokens.
/// @param token The address of the token to flash mint.
/// @param amount How much to flash mint.
/// @param data ABI encoded data to pass to the receiver.
///
/// @return If the flash loan was successful.
function flashLoan(
IERC3156FlashBorrower receiver,
address token,
uint256 amount,
bytes calldata data
) external override nonReentrant returns (bool) {
if (token != address(this)) {
revert IllegalArgument();
}
if (amount > maxFlashLoan(token)) {
revert IllegalArgument();
}
uint256 fee = flashFee(token, amount);
_mint(address(receiver), amount);
if (receiver.onFlashLoan(msg.sender, token, amount, fee, data) != CALLBACK_SUCCESS) {
revert IllegalState();
}
_burn(address(receiver), amount + fee); // Will throw error if not enough to burn
return true;
}
}