-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathBitcoinLSTToken.sol
279 lines (239 loc) · 10.3 KB
/
BitcoinLSTToken.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
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
// SPDX-License-Identifier: Apache2.0
pragma solidity 0.8.4;
import './interface/IBitcoinLSTToken.sol';
import "./interface/IParamSubscriber.sol";
import "./System.sol";
contract BitcoinLSTToken is IBitcoinLSTToken, System, IParamSubscriber {
mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) private _allowances;
uint256 private _totalSupply;
string public name; // fancy name
uint8 public decimals; // How many decimals to show.
string public symbol; // An identifier
modifier onlyBtcLSTStake() {
require(msg.sender == BTCLST_STAKE_ADDR, "only invoked by bitcoin lst stake");
_;
}
/*********************** Init ********************************/
function init() external onlyNotInit {
name = "Core BTC LST";
symbol = "lstBTC";
decimals = 8;
alreadyInit = true;
}
/// @dev See {IERC20-totalSupply}.
function totalSupply() public view virtual override returns (uint256) {
return _totalSupply;
}
/// @dev See {IERC20-balanceOf}.
function balanceOf(address account) public view virtual override returns (uint256) {
return _balances[account];
}
/// @dev See {IERC20-allowance}.
function allowance(address owner, address spender) public view virtual override returns (uint256) {
return _allowances[owner][spender];
}
/// @dev Deprecated See {IERC20-approve}.
/// NOTE: If `amount` is the maximum `uint256`, the allowance is not updated on
/// `transferFrom`. This is semantically equivalent to an infinite approval.
///
/// Requirements:
/// - `spender` cannot be the zero address.
function approve(address spender, uint256 amount) public virtual override returns (bool) {
address owner = msg.sender;
_approve(owner, spender, amount);
return true;
}
/// @dev Atomically increases the allowance granted to `spender` by the caller.
/// This is an alternative to {approve} that can be used as a mitigation for
/// problems described in {IERC20-approve}.
///
/// Emits an {Approval} event indicating the updated allowance.
///
/// Requirements:
/// - `spender` cannot be the zero address.
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
address owner = msg.sender;
_approve(owner, spender, allowance(owner, spender) + addedValue);
return true;
}
/// @dev Atomically decreases the allowance granted to `spender` by the caller.
/// This is an alternative to {approve} that can be used as a mitigation for
/// problems described in {IERC20-approve}.
///
/// Emits an {Approval} event indicating the updated allowance.
///
/// Requirements:
/// - `spender` cannot be the zero address.
/// - `spender` must have allowance for the caller of at least
/// `subtractedValue`.
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
address owner = msg.sender;
uint256 currentAllowance = allowance(owner, spender);
require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
unchecked {
_approve(owner, spender, currentAllowance - subtractedValue);
}
return true;
}
/// @dev Moves `amount` of tokens from `from` to `to`.
/// This internal function is equivalent to {transfer}, and can be used to
/// e.g. implement automatic token fees, slashing mechanisms, etc.
///
/// Emits a {Transfer} event.
///
/// Requirements:
/// - `from` cannot be the zero address.
/// - `to` cannot be the zero address.
/// - `from` must have a balance of at least `amount`.
function _transfer(address from, address to, uint256 amount) internal virtual {
require(from != address(0), "ERC20: transfer from the zero address");
require(to != address(0), "ERC20: transfer to the zero address");
_beforeTokenTransfer(from, to, amount);
uint256 fromBalance = _balances[from];
require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
unchecked {
_balances[from] = fromBalance - amount;
// Overflow not possible: the sum of all balances is capped by totalSupply, and the sum is preserved by
// decrementing then incrementing.
_balances[to] += amount;
}
emit Transfer(from, to, amount);
_afterTokenTransfer(from, to, amount);
}
/// @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.
/// This internal function is equivalent to `approve`, and can be used to
/// e.g. set automatic allowances for certain subsystems, etc.
///
/// Emits an {Approval} event.
///
/// Requirements:
/// - `owner` cannot be the zero address.
/// - `spender` cannot be the zero address.
function _approve(address owner, address spender, uint256 amount) internal virtual {
require(owner != address(0), "ERC20: approve from the zero address");
require(spender != address(0), "ERC20: approve to the zero address");
_allowances[owner][spender] = amount;
emit Approval(owner, spender, amount);
}
/// @dev Updates `owner` s allowance for `spender` based on spent `amount`.
/// Does not update the allowance amount in case of infinite allowance.
/// Revert if not enough allowance is available.
///
/// Might emit an {Approval} event.
function _spendAllowance(address owner, address spender, uint256 amount) internal virtual {
uint256 currentAllowance = allowance(owner, spender);
if (currentAllowance != type(uint256).max) {
require(currentAllowance >= amount, "ERC20: insufficient allowance");
unchecked {
_approve(owner, spender, currentAllowance - amount);
}
}
}
/// @dev Hook that is called before any transfer of tokens. This includes
/// minting and burning.
///
/// Calling conditions:
/// - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
/// will be transferred to `to`.
/// - when `from` is zero, `amount` tokens will be minted for `to`.
/// - when `to` is zero, `amount` of ``from``'s tokens will be burned.
/// - `from` and `to` are never both zero.
///
/// To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual {}
/// @dev Hook that is called after any transfer of tokens. This includes
/// minting and burning.
///
/// Calling conditions:
/// - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
/// has been transferred to `to`.
/// - when `from` is zero, `amount` tokens have been minted for `to`.
/// - when `to` is zero, `amount` of ``from``'s tokens have been burned.
/// - `from` and `to` are never both zero.
///
/// To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
function _afterTokenTransfer(address from, address to, uint256 amount) internal virtual {}
/// @dev Creates `amount` tokens and assigns them to `account`, increasing
/// the total supply.
///
/// Emits a {Transfer} event with `from` set to the zero address.
///
/// Requirements:
/// - `account` cannot be the zero address.
function mint(address account, uint256 amount) external override onlyBtcLSTStake {
require(account != address(0), "ERC20: mint to the zero address");
_beforeTokenTransfer(address(0), account, amount);
_totalSupply += amount;
unchecked {
// Overflow not possible: balance + amount is at most totalSupply + amount, which is checked above.
_balances[account] += amount;
}
emit Transfer(address(0), account, amount);
_afterTokenTransfer(address(0), account, amount);
}
/// @dev Destroys `amount` tokens from `account`, reducing the
/// total supply.
///
/// Emits a {Transfer} event with `to` set to the zero address.
///
/// Requirements:
/// - `account` cannot be the zero address.
/// - `account` must have at least `amount` tokens.
function burn(address account, uint256 amount) external override onlyBtcLSTStake {
require(account != address(0), "ERC20: burn from the zero address");
_beforeTokenTransfer(account, address(0), amount);
uint256 accountBalance = _balances[account];
require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
unchecked {
_balances[account] = accountBalance - amount;
// Overflow not possible: amount <= accountBalance <= totalSupply.
_totalSupply -= amount;
}
emit Transfer(account, address(0), amount);
_afterTokenTransfer(account, address(0), amount);
}
/// @dev See {IERC20-transfer}.
/// Requirements:
/// - `to` cannot be the zero address.
/// - the caller must have a balance of at least `amount`.
function transfer(address to, uint256 amount) public override returns (bool) {
address owner = msg.sender;
_transfer(owner, to, amount);
_onTransfer(owner, to, amount);
return true;
}
/// @dev See {IERC20-transferFrom}.
/// Emits an {Approval} event indicating the updated allowance. This is not
/// required by the EIP. See the note at the beginning of {ERC20}.
///
/// NOTE: Does not update the allowance if the current allowance
/// is the maximum `uint256`.
///
/// Requirements:
/// - `from` and `to` cannot be the zero address.
/// - `from` must have a balance of at least `amount`.
/// - the caller must have allowance for ``from``'s tokens of at least
/// `amount`.
function transferFrom(address from, address to, uint256 amount) public override returns (bool) {
address spender = msg.sender;
_spendAllowance(from, spender, amount);
_transfer(from, to, amount);
_onTransfer(from, to, amount);
return true;
}
function _onTransfer(address from, address to, uint256 amount) internal {
(bool success, ) = BTCLST_STAKE_ADDR.call(
abi.encodeWithSignature("onTokenTransfer(address,address,uint256)",
from, to, amount)
);
require(success, "call lstStake.onTokenTransfer failed.");
}
/*********************** Param update ********************************/
/// Update parameters through governance vote
/// @param key The name of the parameter
/// @param value the new value set to the parameter
function updateParam(string calldata key, bytes calldata value) external view override onlyInit onlyGov {
revert UnsupportedGovParam(key);
}
}