mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
Morpho-Aave contracts set up
This commit is contained in:
parent
9976f844dd
commit
1bfd45527e
|
@ -4,26 +4,22 @@ pragma experimental ABIEncoderV2;
|
|||
|
||||
contract Events {
|
||||
event LogDeposit(
|
||||
uint256 pool,
|
||||
address tokenAddress,
|
||||
address poolTokenAddress,
|
||||
uint256 amount,
|
||||
uint256 maxGasForMatching,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
|
||||
event LogBorrow(
|
||||
uint256 pool,
|
||||
bool isETH,
|
||||
address poolTokenAddress,
|
||||
uint256 amount,
|
||||
uint256 maxGasForMatching,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
|
||||
event LogWithdraw(
|
||||
uint256 pool,
|
||||
bool isETH,
|
||||
address poolTokenAddress,
|
||||
uint256 amt,
|
||||
|
@ -32,7 +28,6 @@ contract Events {
|
|||
);
|
||||
|
||||
event LogPayback(
|
||||
uint256 pool,
|
||||
bool isETH,
|
||||
address poolTokenAddress,
|
||||
uint256 amt,
|
||||
|
@ -40,9 +35,5 @@ contract Events {
|
|||
uint256 setId
|
||||
);
|
||||
|
||||
event LogClaimed(
|
||||
uint256 pool,
|
||||
address[] tokenAddresses,
|
||||
bool tradeForMorphoToken
|
||||
);
|
||||
event LogClaimed(address[] tokenAddresses, bool tradeForMorphoToken);
|
||||
}
|
|
@ -7,13 +7,9 @@ import "../../common/basic.sol";
|
|||
import "../../common/interfaces.sol";
|
||||
|
||||
abstract contract Helpers is Stores, Basic {
|
||||
IMorphoCore public constant morphoCompound =
|
||||
IMorphoCore(0x8888882f8f843896699869179fB6E4f7e3B58888);
|
||||
IMorphoCore public constant morphoAave =
|
||||
IMorphoCore(0x777777c9898D384F785Ee44Acfe945efDFf5f3E0);
|
||||
|
||||
enum Underlying {
|
||||
AAVEV2,
|
||||
COMPOUNDV2
|
||||
}
|
||||
IMorphoAaveLens public constant morphoAaveLens =
|
||||
IMorphoAaveLens(0x507fA343d0A90786d86C7cd885f5C49263A91FF4);
|
||||
}
|
55
contracts/mainnet/connectors/morpho-aave/interface.sol
Normal file
55
contracts/mainnet/connectors/morpho-aave/interface.sol
Normal file
|
@ -0,0 +1,55 @@
|
|||
//SPDX-License-Identifier: MIT
|
||||
pragma solidity ^0.7.0;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
interface IMorphoCore {
|
||||
function supply(
|
||||
address _poolTokenAddress,
|
||||
address _onBehalf,
|
||||
uint256 _amount
|
||||
) external;
|
||||
|
||||
function borrow(address _poolTokenAddress, uint256 _amount) external;
|
||||
|
||||
function withdraw(address _poolTokenAddress, uint256 _amount) external;
|
||||
|
||||
function repay(
|
||||
address _poolTokenAddress,
|
||||
address _onBehalf,
|
||||
uint256 _amount
|
||||
) external;
|
||||
|
||||
function liquidate(
|
||||
address _poolTokenBorrowedAddress,
|
||||
address _poolTokenCollateralAddress,
|
||||
address _borrower,
|
||||
uint256 _amount
|
||||
) external;
|
||||
|
||||
function claimRewards(
|
||||
address[] calldata _tokenAddresses,
|
||||
bool _tradeForMorphoToken
|
||||
) external;
|
||||
}
|
||||
|
||||
interface IMorphoAaveLens {
|
||||
function _getCurrentBorrowBalanceInOf(address _poolToken, address _user)
|
||||
external
|
||||
view
|
||||
returns (
|
||||
address underlyingToken,
|
||||
uint256 balanceInP2P,
|
||||
uint256 balanceOnPool,
|
||||
uint256 totalBalance
|
||||
);
|
||||
|
||||
function _getCurrentSupplyBalanceInOf(address _poolToken, address _user)
|
||||
external
|
||||
view
|
||||
returns (
|
||||
address underlyingToken,
|
||||
uint256 balanceInP2P,
|
||||
uint256 balanceOnPool,
|
||||
uint256 totalBalance
|
||||
);
|
||||
}
|
222
contracts/mainnet/connectors/morpho-aave/main.sol
Normal file
222
contracts/mainnet/connectors/morpho-aave/main.sol
Normal file
|
@ -0,0 +1,222 @@
|
|||
//SPDX-License-Identifier: MIT
|
||||
pragma solidity ^0.7.0;
|
||||
pragma experimental ABIEncoderV2;
|
||||
import "./helpers.sol";
|
||||
import "./events.sol";
|
||||
|
||||
abstract contract MorphoAave is Helpers, Events {
|
||||
/**
|
||||
* @dev Deposit ETH/ERC20_Token.
|
||||
* @notice Deposit a token to Morpho Aave for lending / collaterization.
|
||||
* @param _tokenAddress The address of underlying token to deposit.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param _poolTokenAddress The address of aToken to deposit.(For ETH: aWETH address)
|
||||
* @param _amount The amount of the token (in underlying) to deposit. (For max: `uint256(-1)`)
|
||||
* @param _getId ID to retrieve amt.
|
||||
* @param _setId ID stores the amount of tokens deposited.
|
||||
*/
|
||||
function deposit(
|
||||
address _tokenAddress,
|
||||
address _poolTokenAddress,
|
||||
uint256 _amount,
|
||||
// uint256 _maxGasForMatching, // optional
|
||||
uint256 _getId,
|
||||
uint256 _setId
|
||||
)
|
||||
external
|
||||
payable
|
||||
returns (string memory _eventName, bytes memory _eventParam)
|
||||
{
|
||||
uint256 _amt = getUint(_getId, _amount);
|
||||
|
||||
bool _isETH = _tokenAddress == ethAddr;
|
||||
|
||||
TokenInterface _tokenContract = _isETH
|
||||
? TokenInterface(wethAddr)
|
||||
: TokenInterface(_tokenAddress);
|
||||
|
||||
if (_amt == uint256(-1)) {
|
||||
_amt = _isETH
|
||||
? address(this).balance
|
||||
: _tokenContract.balanceOf(address(this));
|
||||
}
|
||||
|
||||
if (_isETH) convertEthToWeth(_isETH, _tokenContract, _amt);
|
||||
|
||||
approve(_tokenContract, address(morphoAave), _amt);
|
||||
|
||||
morphoAave.supply(_poolTokenAddress, address(this), _amt);
|
||||
|
||||
setUint(_setId, _amt);
|
||||
|
||||
_eventName = "LogDeposit(address,address,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(
|
||||
_tokenAddress,
|
||||
_poolTokenAddress,
|
||||
_amt,
|
||||
// _maxGasForMatching,
|
||||
_getId,
|
||||
_setId
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Borrow ETH/ERC20_Token.
|
||||
* @notice Borrow a token from Morpho Aave.
|
||||
* @param _tokenAddress The address of underlying token to borrow.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param _poolTokenAddress The address of aToken to borrow.(For ETH: aWETH address)
|
||||
* @param _amount The amount of the token (in underlying) to borrow.
|
||||
* @param _getId ID to retrieve amt.
|
||||
* @param _setId ID stores the amount of tokens borrowed.
|
||||
*/
|
||||
function borrow(
|
||||
address _tokenAddress,
|
||||
address _poolTokenAddress,
|
||||
uint256 _amount,
|
||||
// uint256 _maxGasForMatching,
|
||||
uint256 _getId,
|
||||
uint256 _setId
|
||||
)
|
||||
external
|
||||
payable
|
||||
returns (string memory _eventName, bytes memory _eventParam)
|
||||
{
|
||||
uint256 _amt = getUint(_getId, _amount);
|
||||
|
||||
bool _isETH = _tokenAddress == ethAddr;
|
||||
|
||||
morphoAave.borrow(_poolTokenAddress, _amt);
|
||||
|
||||
if (_isETH) convertWethToEth(_isETH, TokenInterface(wethAddr), _amt);
|
||||
|
||||
setUint(_setId, _amt);
|
||||
|
||||
_eventName = "LogBorrow(bool,address,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(
|
||||
_isETH,
|
||||
_poolTokenAddress,
|
||||
_amt,
|
||||
// _maxGasForMatching,
|
||||
_getId,
|
||||
_setId
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Withdraw ETH/ERC20_Token.
|
||||
* @notice Withdraw a token from Morpho Aave.
|
||||
* @param _tokenAddress The address of underlying token to withdraw.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param _poolTokenAddress The address of aToken to withdraw.(For ETH: aWETH address)
|
||||
* @param _amount The amount of the token (in underlying) to withdraw. (For max: `uint256(-1)`)
|
||||
* @param _getId ID to retrieve amt.
|
||||
* @param _setId ID stores the amount of tokens withdrawed.
|
||||
*/
|
||||
function withdraw(
|
||||
address _tokenAddress,
|
||||
address _poolTokenAddress,
|
||||
uint256 _amount,
|
||||
uint256 _getId,
|
||||
uint256 _setId
|
||||
)
|
||||
external
|
||||
payable
|
||||
returns (string memory _eventName, bytes memory _eventParam)
|
||||
{
|
||||
uint256 _amt = getUint(_getId, _amount);
|
||||
bool _isETH = _tokenAddress == ethAddr;
|
||||
address _token = _isETH ? wethAddr : _tokenAddress;
|
||||
|
||||
if (_amt == uint256(-1))
|
||||
(, , , _amt) = morphoAaveLens._getCurrentSupplyBalanceInOf(
|
||||
_poolTokenAddress,
|
||||
address(this)
|
||||
);
|
||||
|
||||
morphoAave.withdraw(_poolTokenAddress, _amt);
|
||||
|
||||
convertWethToEth(_isETH, TokenInterface(_token), _amt);
|
||||
|
||||
setUint(_setId, _amt);
|
||||
|
||||
_eventName = "LogWithdraw(bool,address,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(
|
||||
_isETH,
|
||||
_poolTokenAddress,
|
||||
_amt,
|
||||
_getId,
|
||||
_setId
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Payback ETH/ERC20_Token.
|
||||
* @notice Payback a token to Morpho Aave.
|
||||
* @param _tokenAddress The address of underlying token to payback.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param _poolTokenAddress The address of aToken to payback.(For ETH: aWETH address)
|
||||
* @param _amount The amount of the token (in underlying) to payback. (For max: `uint256(-1)`)
|
||||
* @param _getId ID to retrieve amt.
|
||||
* @param _setId ID stores the amount of tokens paid back.
|
||||
*/
|
||||
function payback(
|
||||
address _tokenAddress,
|
||||
address _poolTokenAddress,
|
||||
uint256 _amount,
|
||||
uint256 _getId,
|
||||
uint256 _setId
|
||||
)
|
||||
external
|
||||
payable
|
||||
returns (string memory _eventName, bytes memory _eventParam)
|
||||
{
|
||||
bool _isETH = _tokenAddress == ethAddr;
|
||||
uint256 _amt = getUint(_getId, _amount);
|
||||
address _token = _isETH ? wethAddr : _tokenAddress;
|
||||
|
||||
if (_amt == uint256(-1)) {
|
||||
(, , , _amt) = morphoAaveLens._getCurrentBorrowBalanceInOf(
|
||||
_poolTokenAddress,
|
||||
address(this)
|
||||
);
|
||||
}
|
||||
|
||||
if (_isETH) convertEthToWeth(_isETH, TokenInterface(_token), _amt);
|
||||
|
||||
approve(TokenInterface(_token), address(morphoAave), _amt);
|
||||
|
||||
morphoAave.repay(_poolTokenAddress, address(this), _amt);
|
||||
|
||||
setUint(_setId, _amt);
|
||||
|
||||
_eventName = "LogPayback(bool,address,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(
|
||||
_isETH,
|
||||
_poolTokenAddress,
|
||||
_amt,
|
||||
_getId,
|
||||
_setId
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Claim rewards.
|
||||
* @notice Claim rewards for the given assets from underlying protocol.
|
||||
* @param _poolTokenAddresses The assets to claim rewards from (aToken or variable debt token).(For ETH: aToken or variable debt token address of WETH)
|
||||
* @param _tradeForMorphoToken Whether or not to trade reward tokens for MORPHO tokens.
|
||||
*/
|
||||
function claim(
|
||||
address[] calldata _poolTokenAddresses, //todo: eth will be claimed as weth currently?
|
||||
bool _tradeForMorphoToken
|
||||
)
|
||||
external
|
||||
payable
|
||||
returns (string memory _eventName, bytes memory _eventParam)
|
||||
{
|
||||
morphoAave.claimRewards(_poolTokenAddresses, _tradeForMorphoToken);
|
||||
|
||||
_eventName = "LogClaimed(address[],bool)";
|
||||
_eventParam = abi.encode(_poolTokenAddresses, _tradeForMorphoToken);
|
||||
}
|
||||
}
|
||||
|
||||
contract ConnectV2MorphoAave is MorphoAave {
|
||||
string public constant name = "Morpho-Aave-v1.0";
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
//SPDX-License-Identifier: MIT
|
||||
pragma solidity ^0.7.0;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
interface IMorphoCore {
|
||||
function supply(
|
||||
address _poolTokenAddress,
|
||||
address _onBehalf,
|
||||
uint256 _amount,
|
||||
uint256 _maxGasForMatching
|
||||
) external;
|
||||
|
||||
function borrow(
|
||||
address _poolTokenAddress,
|
||||
uint256 _amount,
|
||||
uint256 _maxGasForMatching
|
||||
) external;
|
||||
|
||||
function withdraw(
|
||||
address _poolTokenAddress,
|
||||
uint256 _amount
|
||||
) external;
|
||||
|
||||
function repay(
|
||||
address _poolTokenAddress,
|
||||
address _onBehalf,
|
||||
uint256 _amount
|
||||
) external;
|
||||
|
||||
function liquidate(
|
||||
address _poolTokenBorrowedAddress,
|
||||
address _poolTokenCollateralAddress,
|
||||
address _borrower,
|
||||
uint256 _amount
|
||||
) external;
|
||||
|
||||
// (For AAVEV2: (aToken or variable debt token), COMPOUNDV2: cToken addresses)
|
||||
function claimRewards(
|
||||
address[] _tokenAddresses,
|
||||
bool _tradeForMorphoToken,
|
||||
) external;
|
||||
}
|
|
@ -1,176 +0,0 @@
|
|||
//SPDX-License-Identifier: MIT
|
||||
pragma solidity ^0.7.0;
|
||||
pragma experimental ABIEncoderV2;
|
||||
import './helpers.sol';
|
||||
import './events.sol';
|
||||
import 'hardhat/console.sol';
|
||||
|
||||
abstract contract Morpho is Helpers, Events {
|
||||
|
||||
function deposit (
|
||||
Underlying _pool,
|
||||
address _tokenAddress,
|
||||
address _poolTokenAddress, // if address weth (send eth)
|
||||
uint256 _amount, // if max, check balance
|
||||
uint256 _maxGasForMatching, // optional
|
||||
uint256 _getId,
|
||||
uint256 _setId
|
||||
) external payable returns(string memory _eventName, bytes memory _eventParam) {
|
||||
|
||||
require(_pool == Underlying.AAVEV2 || _pool == Underlying.COMPOUNDV2, 'underlying-protocol-not-supported');
|
||||
|
||||
uint256 _amt = getUint(_getId, _amount);
|
||||
|
||||
bool _isETH = _tokenAddress == ethAddr;
|
||||
|
||||
TokenInterface _tokenContract = _isETH ? TokenInterface(wethAddr) : TokenInterface(_tokenAddress);
|
||||
|
||||
if(_amt == uint256(-1)) {
|
||||
_amt = _isETH ? address(this).balance : _tokenContract.balanceOf(address(this));
|
||||
}
|
||||
|
||||
if(_isETH) convertEthToWeth(_isETH, _tokenContract, _amt);
|
||||
|
||||
if(_pool == Underlying.AAVEV2) {
|
||||
approve(_tokenContract, morphoAave, _amt);
|
||||
morphoAave.supply(_poolTokenAddress, address(this), _amt, _maxGasForMatching);
|
||||
} else {
|
||||
approve(_tokenContract, morphoCompound, _amt);
|
||||
morphoCompound.supply(_poolTokenAddress, address(this), _amt, _maxGasForMatching);
|
||||
}
|
||||
|
||||
setUint(_setId, _amt);
|
||||
|
||||
_eventName = "LogDeposit(uint256,address,address,uint256,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(
|
||||
_pool,
|
||||
_tokenAddress,
|
||||
_poolTokenAddress,
|
||||
_amt,
|
||||
_maxGasForMatching,
|
||||
_getId,
|
||||
_setId
|
||||
);
|
||||
}
|
||||
|
||||
function borrow (
|
||||
Underlying _pool,
|
||||
address _tokenAddress,
|
||||
address _poolTokenAddress, //todo: dTokenaAddress? // if address weth (send eth)
|
||||
uint256 _amount,
|
||||
uint256 _maxGasForMatching,
|
||||
uint256 _getId,
|
||||
uint256 _setId
|
||||
) external payable returns(string memory _eventName, bytes memory _eventParam) {
|
||||
|
||||
require(_pool == Underlying.AAVEV2 || _pool == Underlying.COMPOUNDV2, 'protocol-not-supported');
|
||||
|
||||
uint256 _amt = getUint(_getId, _amount);
|
||||
|
||||
bool _isETH = _tokenAddress == ethAddr;
|
||||
|
||||
if(_pool == Underlying.AAVEV2) {
|
||||
morphoAave.borrow(_poolTokenAddress, _amt, _maxGasForMatching);
|
||||
} else {
|
||||
morphoCompound.borrow(_poolTokenAddress, _amt, _maxGasForMatching);
|
||||
}
|
||||
|
||||
if(_isETH) convertWethToEth(_isETH, tokenInterface(wethAddr), _amt);
|
||||
|
||||
setUint(_setId, _amt);
|
||||
|
||||
_eventName = "LogBorrow(uint256,address,uint256,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(
|
||||
_pool,
|
||||
_poolTokenAddress,
|
||||
_amt,
|
||||
_maxGasForMatching,
|
||||
_getId,
|
||||
_setId
|
||||
);
|
||||
}
|
||||
|
||||
function withdraw (
|
||||
Underlying _pool,
|
||||
address _tokenAddress,
|
||||
address _poolTokenAddress, // if address weth (send eth)
|
||||
uint256 _amount, // amount max
|
||||
uint256 _getId,
|
||||
uint256 _setId
|
||||
) external payable returns(string memory _eventName, bytes memory _eventParam) {
|
||||
|
||||
require(_pool == Underlying.AAVEV2 || _pool == Underlying.COMPOUNDV2, 'protocol not supported');
|
||||
|
||||
uint256 _amt = getUint(_getId, _amount);
|
||||
bool _isETH = _tokenAddress == ethAddr;
|
||||
address _token = _isETH ? wethAddr : _tokenAddress;
|
||||
|
||||
if (_amt == uint256(-1)) _amt = _poolTokenAddress.balanceOf(address(this));
|
||||
|
||||
if(_pool == Underlying.AAVEV2) {
|
||||
morphoAave.withdraw(_poolTokenAddress, _amt);
|
||||
} else {
|
||||
morphoCompound.withdraw(_poolTokenAddress, _amt);
|
||||
}
|
||||
|
||||
convertWethToEth(_isETH, TokenInterface(_token), _amt);
|
||||
|
||||
setUint(_setId, _amt);
|
||||
|
||||
_eventName = "LogWithdraw(uint256,bool,address,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(_pool, _isETH, _poolTokenAddress, _amt, _getId, _setId);
|
||||
|
||||
}
|
||||
|
||||
function payback (
|
||||
Underlying _pool,
|
||||
address _tokenAddress,
|
||||
address _poolTokenAddress, // if address weth (send eth)
|
||||
uint256 _amount, // max value
|
||||
uint256 _getId,
|
||||
uint256 _setId
|
||||
) external payable returns(string memory _eventName, bytes memory _eventParam) {
|
||||
|
||||
require(_pool == Underlying.AAVEV2 || _pool == Underlying.COMPOUNDV2, 'protocol not supported');
|
||||
|
||||
bool _isETH = _tokenAddress == ethAddr;
|
||||
uint256 _amt = getUint(_getId, _amount);
|
||||
address _token = _isETH ? wethAddr : _tokenAddress;
|
||||
|
||||
if(_amt == uint256(-1)) {
|
||||
_amt = _isETH ? _amt = address(this).balance : TokenInterface(_token).balanceOf(address(this));
|
||||
}
|
||||
|
||||
if (_isETH) convertEthToWeth(_isETH, TokenInterface(_token), _amt);
|
||||
|
||||
_pool == Underlying.AAVEV2
|
||||
? morphoAave.repay(_poolTokenAddress, address(this), _amt)
|
||||
: morphoCompound.repay(_poolTokenAddress, address(this), _amt);
|
||||
|
||||
setUint(_setId, _amt);
|
||||
|
||||
_eventName = "LogPayback(uint256,bool,address,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(_pool, _isETH, _poolTokenAddress, _amt, _getId, _setId);
|
||||
}
|
||||
|
||||
function claim (
|
||||
Underlying _pool,
|
||||
address[] _tokenAddresses, //todo: eth will be claimed as weth currently?
|
||||
bool _tradeForMorphoToken
|
||||
) external payable returns(string memory _eventName, bytes memory _eventParam) {
|
||||
|
||||
require(_pool == Underlying.AAVEV2 || _pool == Underlying.COMPOUNDV2, 'protocol not supported');
|
||||
|
||||
_pool == Underlying.AAVEV2
|
||||
? morphoAave.claim(_tokenAddresses, _tradeForMorphoToken)
|
||||
: morphoCompound.claim(_tokenAddresses, _tradeForMorphoToken);
|
||||
|
||||
_eventName = "LogClaimed(uint256,address[],bool)";
|
||||
_eventParam = abi.encode(_pool, _tokenAddresses, _tradeForMorphoToken);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
contract ConnectV2Morpho is Morpho {
|
||||
string public constant name = "Morpho-v1.0";
|
||||
}
|
Loading…
Reference in New Issue
Block a user