Skip to content

Commit

Permalink
feat: add permit2 support in the ESR
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperpawlowski committed Oct 17, 2024
1 parent 83481a4 commit d8e7b06
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
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

0 comments on commit d8e7b06

Please sign in to comment.