Skip to content

Commit

Permalink
tasks: add min amount out and erc4626 parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
lgalende committed Nov 22, 2023
1 parent 5d3dc02 commit 3c906a2
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ interface IERC4626Connector {
*/
function getToken(address erc4626) external view returns (address);

/**
* @dev Tells the underlying token of an ERC4626
*/
function getToken(address erc4626) external view returns (address);

/**
* @dev Deposits assets to the underlying ERC4626
* @param erc4626 Address of the ERC4626 to join
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,8 @@ import './IBaseERC4626Task.sol';
* @dev ERC4626 exiter task interface
*/
interface IERC4626Exiter is IBaseERC4626Task {
/**
* The token is not the ERC4626 token
*/
error TaskTokenNotERC4626(address token, address erc4626);

/**
* @dev Executes the ERC4626 exiter task
*/
function call(address token, uint256 amount) external;
function call(address erc4626, uint256 amount, uint256 minAmountOut) external;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import './IBaseERC4626Task.sol';
*/
interface IERC4626Joiner is IBaseERC4626Task {
/**
* @dev The token is not the ERC4626 underlying token
* The ERC4626 reference is zero
*/
error TaskTokenNotUnderlying(address token, address underlying);
error TaskERC4626Zero();

/**
* @dev Executes the ERC4626 joiner task
*/
function call(address toke, uint256 amount) external;
function call(address erc4626, address token, uint256 amount, uint256 minAmountOut) external;
}
28 changes: 18 additions & 10 deletions packages/tasks/contracts/liquidity/erc4626/ERC4626Exiter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

pragma solidity ^0.8.0;

import '@mimic-fi/v3-connectors/contracts/interfaces/liquidity/erc4626/IERC4626Connector.sol';
import '@mimic-fi/v3-connectors/contracts/interfaces/erc4626/IERC4626Connector.sol';

import './BaseERC4626Task.sol';
import '../../interfaces/liquidity/erc4626/IERC4626Exiter.sol';
Expand Down Expand Up @@ -60,26 +60,34 @@ contract ERC4626Exiter is IERC4626Exiter, BaseERC4626Task {
}

/**
* @dev Executes the ERC4626 exiter task
* @param token Address of the token to be exited with
* @dev Executes the ERC4626 exiter task. Note that the ERC4626 is also the token.
* @param erc4626 Address of the ERC4626 to be exited
* @param amount Amount of shares to be exited with
* @param minAmountOut Minimum amount of assets willing to receive
*/
function call(address token, uint256 amount) external override authP(authParams(token, amount)) {
if (amount == 0) amount = getTaskAmount(token);
_beforeERC4626Exiter(token, amount);
bytes memory connectorData = abi.encodeWithSelector(IERC4626Connector.exit.selector, amount);
function call(address erc4626, uint256 amount, uint256 minAmountOut)
external
override
authP(authParams(erc4626, amount))
{
if (amount == 0) amount = getTaskAmount(erc4626);
_beforeERC4626Exiter(erc4626, amount);
bytes memory connectorData = abi.encodeWithSelector(
IERC4626Connector.exit.selector,
erc4626,
amount,
minAmountOut
);
bytes memory result = ISmartVault(smartVault).execute(connector, connectorData);
(address tokenOut, uint256 amountOut) = abi.decode(result, (address, uint256));
_afterERC4626Exiter(token, amount, tokenOut, amountOut);
_afterERC4626Exiter(erc4626, amount, tokenOut, amountOut);
}

/**
* @dev Before ERC4626 exiter hook
*/
function _beforeERC4626Exiter(address token, uint256 amount) internal virtual {
_beforeBaseERC4626Task(token, amount);
address erc4626 = IERC4626Connector(connector).erc4626();
if (token != erc4626) revert TaskTokenNotERC4626(token, erc4626);
}

/**
Expand Down
25 changes: 18 additions & 7 deletions packages/tasks/contracts/liquidity/erc4626/ERC4626Joiner.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

pragma solidity ^0.8.0;

import '@mimic-fi/v3-connectors/contracts/interfaces/liquidity/erc4626/IERC4626Connector.sol';
import '@mimic-fi/v3-connectors/contracts/interfaces/erc4626/IERC4626Connector.sol';

import './BaseERC4626Task.sol';
import '../../interfaces/liquidity/erc4626/IERC4626Joiner.sol';
Expand Down Expand Up @@ -61,13 +61,25 @@ contract ERC4626Joiner is IERC4626Joiner, BaseERC4626Task {

/**
* @dev Executes the ERC4626 joiner task
* @param erc4626 Address of the ERC4626 to be joined
* @param token Address of the token to be joined with
* @param amount Amount of assets to be joined with
* @param minAmountOut Minimum amount of shares willing to receive
*/
function call(address token, uint256 amount) external override authP(authParams(token, amount)) {
function call(address erc4626, address token, uint256 amount, uint256 minAmountOut)
external
override
authP(authParams(erc4626, token, amount))
{
if (amount == 0) amount = getTaskAmount(token);
_beforeERC4626Joiner(token, amount);
bytes memory connectorData = abi.encodeWithSelector(IERC4626Connector.join.selector, amount);
_beforeERC4626Joiner(erc4626, token, amount);
bytes memory connectorData = abi.encodeWithSelector(
IERC4626Connector.join.selector,
erc4626,
token,
amount,
minAmountOut
);
bytes memory result = ISmartVault(smartVault).execute(connector, connectorData);
(address tokenOut, uint256 amountOut) = abi.decode(result, (address, uint256));
_afterERC4626Joiner(token, amount, tokenOut, amountOut);
Expand All @@ -76,10 +88,9 @@ contract ERC4626Joiner is IERC4626Joiner, BaseERC4626Task {
/**
* @dev Before ERC4626 joiner hook
*/
function _beforeERC4626Joiner(address token, uint256 amount) internal virtual {
function _beforeERC4626Joiner(address erc4626, address token, uint256 amount) internal virtual {
_beforeBaseERC4626Task(token, amount);
address underlying = IERC4626Connector(connector).getToken();
if (token != underlying) revert TaskTokenNotUnderlying(token, underlying);
if (erc4626 == address(0)) revert TaskERC4626Zero();
}

/**
Expand Down
31 changes: 14 additions & 17 deletions packages/tasks/contracts/test/liquidity/ERC4626ConnectorMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,26 @@
pragma solidity ^0.8.0;

contract ERC4626ConnectorMock {
address public erc4626;
address public immutable tokenOut;

address public getToken;
event LogJoin(address erc4626, address token, uint256 amount, uint256 minAmountOut);

event LogJoin(uint256 amount);
event LogExit(address erc4626, uint256 amount, uint256 minAmountOut);

event LogExit(uint256 amount);

function setERC4626(address newERC4626) external {
erc4626 = newERC4626;
}

function setToken(address newToken) external {
getToken = newToken;
constructor(address _tokenOut) {
tokenOut = _tokenOut;
}

function join(uint256 assets) external returns (address, uint256) {
emit LogJoin(assets);
return (erc4626, assets);
function join(address erc4626, address token, uint256 assets, uint256 minSharesOut)
external
returns (address, uint256)
{
emit LogJoin(erc4626, token, assets, minSharesOut);
return (erc4626, minSharesOut);
}

function exit(uint256 shares) external returns (address, uint256) {
emit LogExit(shares);
return (getToken, shares);
function exit(address erc4626, uint256 shares, uint256 minAssetsOut) external returns (address, uint256) {
emit LogExit(erc4626, shares, minAssetsOut);
return (tokenOut, minAssetsOut);
}
}
Loading

0 comments on commit 3c906a2

Please sign in to comment.