This repository was archived by the owner on Dec 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 781
Expand file tree
/
Copy pathStETHMocked.sol
More file actions
276 lines (223 loc) · 8.17 KB
/
Copy pathStETHMocked.sol
File metadata and controls
276 lines (223 loc) · 8.17 KB
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
// SPDX-FileCopyrightText: 2020 Lido <info@lido.fi>
pragma solidity 0.6.12;
/**
* @notice Mock was based on the original StETH contract from the Lido organization
* https://github.com/lidofinance/lido-dao/blob/master/contracts/0.4.24/StETH.sol
* and tries to be as close as possible to the original implementation.
*/
contract StETHMocked {
using LidoSafeMath for uint256;
// use there values like real stETH has
uint256 internal _totalShares = 1608965089698263670456320;
uint256 internal _pooledEther = 1701398689820002221426255;
mapping(address => uint256) private shares;
mapping(address => mapping(address => uint256)) private allowances;
function name() public pure returns (string memory) {
return 'Liquid staked Ether 2.0';
}
function symbol() public pure returns (string memory) {
return 'stETH';
}
function decimals() public pure returns (uint8) {
return 18;
}
function totalSupply() public view returns (uint256) {
return _getTotalPooledEther();
}
function getTotalPooledEther() public view returns (uint256) {
return _getTotalPooledEther();
}
function balanceOf(address _account) public view returns (uint256) {
return getPooledEthByShares(_sharesOf(_account));
}
function transfer(address _recipient, uint256 _amount) public returns (bool) {
_transfer(msg.sender, _recipient, _amount);
return true;
}
function allowance(address _owner, address _spender) public view returns (uint256) {
return allowances[_owner][_spender];
}
function approve(address _spender, uint256 _amount) public returns (bool) {
_approve(msg.sender, _spender, _amount);
return true;
}
function transferFrom(
address _sender,
address _recipient,
uint256 _amount
) public returns (bool) {
uint256 currentAllowance = allowances[_sender][msg.sender];
require(currentAllowance >= _amount, 'TRANSFER_AMOUNT_EXCEEDS_ALLOWANCE');
_transfer(_sender, _recipient, _amount);
_approve(_sender, msg.sender, currentAllowance.sub(_amount));
return true;
}
function increaseAllowance(address _spender, uint256 _addedValue) public returns (bool) {
_approve(msg.sender, _spender, allowances[msg.sender][_spender].add(_addedValue));
return true;
}
function decreaseAllowance(address _spender, uint256 _subtractedValue) public returns (bool) {
uint256 currentAllowance = allowances[msg.sender][_spender];
require(currentAllowance >= _subtractedValue, 'DECREASED_ALLOWANCE_BELOW_ZERO');
_approve(msg.sender, _spender, currentAllowance.sub(_subtractedValue));
return true;
}
function getTotalShares() public view returns (uint256) {
return _getTotalShares();
}
function sharesOf(address _account) public view returns (uint256) {
return _sharesOf(_account);
}
function getSharesByPooledEth(uint256 _ethAmount) public view returns (uint256) {
uint256 totalPooledEther = _getTotalPooledEther();
if (totalPooledEther == 0) {
return 0;
} else {
return _ethAmount.mul(_getTotalShares()).div(totalPooledEther);
}
}
function getPooledEthByShares(uint256 _sharesAmount) public view returns (uint256) {
uint256 totalShares = _getTotalShares();
if (totalShares == 0) {
return 0;
} else {
return _sharesAmount.mul(_getTotalPooledEther()).div(totalShares);
}
}
function mint(address _recipient, uint256 amount) external returns (uint256) {
return _submit(_recipient, amount);
}
function positiveRebase(uint256 amount) external {
_pooledEther = _pooledEther.add(amount);
}
function negativeRebase(uint256 amount) external {
_pooledEther = _pooledEther.sub(amount);
}
function _getTotalPooledEther() internal view returns (uint256) {
return _pooledEther;
}
function _transfer(
address _sender,
address _recipient,
uint256 _amount
) internal {
uint256 _sharesToTransfer = getSharesByPooledEth(_amount);
_transferShares(_sender, _recipient, _sharesToTransfer);
emit Transfer(_sender, _recipient, _amount);
}
function _approve(
address _owner,
address _spender,
uint256 _amount
) internal {
require(_owner != address(0), 'APPROVE_FROM_ZERO_ADDRESS');
require(_spender != address(0), 'APPROVE_TO_ZERO_ADDRESS');
allowances[_owner][_spender] = _amount;
emit Approval(_owner, _spender, _amount);
}
function _getTotalShares() internal view returns (uint256) {
return _totalShares;
}
function _sharesOf(address _account) internal view returns (uint256) {
return shares[_account];
}
function _transferShares(
address _sender,
address _recipient,
uint256 _sharesAmount
) internal {
require(_sender != address(0), 'TRANSFER_FROM_THE_ZERO_ADDRESS');
require(_recipient != address(0), 'TRANSFER_TO_THE_ZERO_ADDRESS');
uint256 currentSenderShares = shares[_sender];
require(_sharesAmount <= currentSenderShares, 'TRANSFER_AMOUNT_EXCEEDS_BALANCE');
shares[_sender] = currentSenderShares.sub(_sharesAmount);
shares[_recipient] = shares[_recipient].add(_sharesAmount);
}
function _mintShares(address _recipient, uint256 _sharesAmount)
internal
returns (uint256 newTotalShares)
{
require(_recipient != address(0), 'MINT_TO_THE_ZERO_ADDRESS');
newTotalShares = _getTotalShares().add(_sharesAmount);
_totalShares = newTotalShares;
shares[_recipient] = shares[_recipient].add(_sharesAmount);
}
function _burnShares(address _account, uint256 _sharesAmount)
internal
returns (uint256 newTotalShares)
{
require(_account != address(0), 'BURN_FROM_THE_ZERO_ADDRESS');
uint256 accountShares = shares[_account];
require(_sharesAmount <= accountShares, 'BURN_AMOUNT_EXCEEDS_BALANCE');
newTotalShares = _getTotalShares().sub(_sharesAmount);
_totalShares = newTotalShares;
shares[_account] = accountShares.sub(_sharesAmount);
}
function _submit(address sender, uint256 deposit) internal returns (uint256) {
require(deposit != 0, 'ZERO_DEPOSIT');
uint256 sharesAmount = getSharesByPooledEth(deposit);
if (sharesAmount == 0) {
sharesAmount = deposit;
}
_mintShares(sender, sharesAmount);
_pooledEther = _pooledEther.add(deposit);
return sharesAmount;
}
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
/**
* @title SafeMath
* @dev Math operations with safety checks that revert on error. Prefix Lido used to not
* conflict with OpenZeppelin SafeMath contract.
*/
library LidoSafeMath {
string private constant ERROR_ADD_OVERFLOW = 'MATH_ADD_OVERFLOW';
string private constant ERROR_SUB_UNDERFLOW = 'MATH_SUB_UNDERFLOW';
string private constant ERROR_MUL_OVERFLOW = 'MATH_MUL_OVERFLOW';
string private constant ERROR_DIV_ZERO = 'MATH_DIV_ZERO';
/**
* @dev Multiplies two numbers, reverts on overflow.
*/
function mul(uint256 _a, uint256 _b) internal pure returns (uint256) {
if (_a == 0) {
return 0;
}
uint256 c = _a * _b;
require(c / _a == _b, ERROR_MUL_OVERFLOW);
return c;
}
/**
* @dev Integer division of two numbers truncating the quotient, reverts on division by zero.
*/
function div(uint256 _a, uint256 _b) internal pure returns (uint256) {
require(_b > 0, ERROR_DIV_ZERO); // Solidity only automatically asserts when dividing by 0
uint256 c = _a / _b;
// assert(_a == _b * c + _a % _b); // There is no case in which this doesn't hold
return c;
}
/**
* @dev Subtracts two numbers, reverts on overflow (i.e. if subtrahend is greater than minuend).
*/
function sub(uint256 _a, uint256 _b) internal pure returns (uint256) {
require(_b <= _a, ERROR_SUB_UNDERFLOW);
uint256 c = _a - _b;
return c;
}
/**
* @dev Adds two numbers, reverts on overflow.
*/
function add(uint256 _a, uint256 _b) internal pure returns (uint256) {
uint256 c = _a + _b;
require(c >= _a, ERROR_ADD_OVERFLOW);
return c;
}
/**
* @dev Divides two numbers and returns the remainder (unsigned integer modulo),
* reverts when dividing by zero.
*/
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
require(b != 0, ERROR_DIV_ZERO);
return a % b;
}
}