Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add permit2 support in the ESR #281

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/Synths/EulerSavingsRate.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ pragma solidity ^0.8.0;

import {Context} from "openzeppelin-contracts/utils/Context.sol";
import {Math} from "openzeppelin-contracts/utils/math/Math.sol";
import {IERC20} from "openzeppelin-contracts/token/ERC20/IERC20.sol";
import {IERC20 as IERC20_OZ} from "openzeppelin-contracts/token/ERC20/IERC20.sol";
import {ERC20} from "openzeppelin-contracts/token/ERC20/ERC20.sol";
import {ERC4626} from "openzeppelin-contracts/token/ERC20/extensions/ERC4626.sol";
import {EVCUtil} from "ethereum-vault-connector/utils/EVCUtil.sol";
import {SafeERC20Lib, IERC20} from "../EVault/shared/lib/SafeERC20Lib.sol";

/// @title EulerSavingsRate
/// @custom:security-contact [email protected]
Expand All @@ -28,6 +29,8 @@ contract EulerSavingsRate is EVCUtil, ERC4626 {

uint256 public constant INTEREST_SMEAR = 2 weeks;

address public immutable PERMIT2;

struct ESRSlot {
uint40 lastInterestUpdate;
uint40 interestSmearEnd;
Expand All @@ -40,6 +43,7 @@ contract EulerSavingsRate is EVCUtil, ERC4626 {
/// @notice The total assets accounted for in the vault.
uint256 internal _totalAssets;

error InvalidAsset();
error Reentrancy();

event Gulped(uint256 gulped, uint256 interestLeft);
Expand All @@ -53,11 +57,13 @@ contract EulerSavingsRate is EVCUtil, ERC4626 {
esrSlot.locked = UNLOCKED;
}

constructor(address _evc, address _asset, string memory _name, string memory _symbol)
constructor(address _evc, address _permit2, address _asset, string memory _name, string memory _symbol)
EVCUtil(_evc)
ERC4626(IERC20(_asset))
ERC4626(IERC20_OZ(_asset))
ERC20(_name, _symbol)
{
if (_asset.code.length == 0) revert InvalidAsset();
PERMIT2 = _permit2;
esrSlot.locked = UNLOCKED;
}

Expand Down Expand Up @@ -116,7 +122,9 @@ contract EulerSavingsRate is EVCUtil, ERC4626 {
}

function _deposit(address caller, address receiver, uint256 assets, uint256 shares) internal override {
super._deposit(caller, receiver, assets, shares);
SafeERC20Lib.safeTransferFrom(IERC20(asset()), caller, address(this), assets, PERMIT2);
_mint(receiver, shares);
emit Deposit(caller, receiver, assets, shares);
_totalAssets = _totalAssets + assets;
}

Expand Down
7 changes: 5 additions & 2 deletions test/unit/esr/lib/ESRTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import "forge-std/Test.sol";
import {EthereumVaultConnector as EVC} from "ethereum-vault-connector/EthereumVaultConnector.sol";
import {EulerSavingsRate} from "../../../../src/Synths/EulerSavingsRate.sol";
import {MockToken} from "./MockToken.sol";
import {DeployPermit2} from "permit2/test/utils/DeployPermit2.sol";

contract ESRTest is Test {
contract ESRTest is Test, DeployPermit2 {
address permit2;
EVC public evc;
EulerSavingsRate public esr;
MockToken public asset;
Expand All @@ -18,9 +20,10 @@ contract ESRTest is Test {
string public constant SYMBOL = "ESR";

function setUp() public virtual {
permit2 = deployPermit2();
asset = new MockToken();
evc = new EVC();
esr = new EulerSavingsRate(address(evc), address(asset), NAME, SYMBOL);
esr = new EulerSavingsRate(address(evc), permit2, address(asset), NAME, SYMBOL);

// Set a non zero timestamp
vm.warp(420);
Expand Down
Loading