generated from ZeframLou/foundry-template
-
Notifications
You must be signed in to change notification settings - Fork 29
/
EulerETokenMock.sol
59 lines (43 loc) · 2.03 KB
/
EulerETokenMock.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
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.4;
import {ERC20} from "solmate/tokens/ERC20.sol";
import {FixedPointMathLib} from "solmate/utils/FixedPointMathLib.sol";
import {EulerMock} from "./EulerMock.sol";
import {IEulerEToken} from "../../../euler/external/IEulerEToken.sol";
contract EulerETokenMock is IEulerEToken, ERC20("EulerETokenMock", "eMOCK", 18) {
using FixedPointMathLib for uint256;
EulerMock public euler;
ERC20 public underlying;
constructor(ERC20 underlying_, EulerMock euler_) {
euler = euler_;
underlying = underlying_;
}
function balanceOfUnderlying(address account) external view returns (uint256) {
uint256 supply = totalSupply; // Saves an extra SLOAD if totalSupply is non-zero.
uint256 shares = balanceOf[account];
return supply == 0 ? shares : shares.mulDivDown(totalAssets(), supply);
}
function deposit(uint256, uint256 amount) external override {
// call EulerMock to transfer tokens from sender
euler.transferTokenFrom(underlying, msg.sender, address(this), amount);
// mint shares
_mint(msg.sender, convertToShares(amount));
}
function withdraw(uint256, uint256 amount) external override {
// burn shares
_burn(msg.sender, previewWithdraw(amount));
// transfer tokens to sender
underlying.transfer(msg.sender, amount);
}
function convertToShares(uint256 assets) public view virtual returns (uint256) {
uint256 supply = totalSupply; // Saves an extra SLOAD if totalSupply is non-zero.
return supply == 0 ? assets : assets.mulDivDown(supply, totalAssets());
}
function previewWithdraw(uint256 assets) public view virtual returns (uint256) {
uint256 supply = totalSupply; // Saves an extra SLOAD if totalSupply is non-zero.
return supply == 0 ? assets : assets.mulDivUp(supply, totalAssets());
}
function totalAssets() public view virtual returns (uint256) {
return underlying.balanceOf(address(this));
}
}