mirror of
				https://github.com/Instadapp/dsa-connectors.git
				synced 2024-07-29 22:37:00 +00:00 
			
		
		
		
	Add Aave
This commit is contained in:
		
							parent
							
								
									aa67b788a3
								
							
						
					
					
						commit
						ff35db4fd4
					
				| 
						 | 
				
			
			@ -1,6 +1,5 @@
 | 
			
		|||
pragma solidity ^0.7.0;
 | 
			
		||||
 | 
			
		||||
import { InstaMapping } from "../../common/interfaces.sol";
 | 
			
		||||
import { DSMath } from "../../common/math.sol";
 | 
			
		||||
import { Basic } from "../../common/basic.sol";
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -32,14 +31,14 @@ abstract contract Helpers is DSMath, Basic {
 | 
			
		|||
        isSupply;
 | 
			
		||||
        if(_supplyLen > 0) {
 | 
			
		||||
            for (uint i = 0; i < _supplyLen; i++) {
 | 
			
		||||
                ctokens[i] = InstaMapping(getMappingAddr()).cTokenMapping(supplyTokens[i]);
 | 
			
		||||
                ctokens[i] = instaMapping.cTokenMapping(supplyTokens[i]);
 | 
			
		||||
            }
 | 
			
		||||
            isSupply = true;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if(_borrowLen > 0) {
 | 
			
		||||
            for (uint i = 0; i < _borrowLen; i++) {
 | 
			
		||||
                ctokens[_supplyLen + i] = InstaMapping(getMappingAddr()).cTokenMapping(borrowTokens[i]);
 | 
			
		||||
                ctokens[_supplyLen + i] = instaMapping.cTokenMapping(borrowTokens[i]);
 | 
			
		||||
            }
 | 
			
		||||
            isBorrow = true;
 | 
			
		||||
        }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,6 @@
 | 
			
		|||
pragma solidity ^0.7.0;
 | 
			
		||||
 | 
			
		||||
import { TokenInterface , MemoryInterface, InstaMapping } from "../../common/interfaces.sol";
 | 
			
		||||
import { TokenInterface } from "../../common/interfaces.sol";
 | 
			
		||||
import { Stores } from "../../common/stores.sol";
 | 
			
		||||
import { ComptrollerInterface, COMPInterface } from "./interface.sol";
 | 
			
		||||
import { Helpers } from "./helpers.sol";
 | 
			
		||||
| 
						 | 
				
			
			@ -21,7 +21,7 @@ abstract contract CompResolver is Events, Helpers {
 | 
			
		|||
 | 
			
		||||
        setUint(setId, amt);
 | 
			
		||||
 | 
			
		||||
        eventName = "LogClaimedComp(uint256,uint256)";
 | 
			
		||||
        _eventName = "LogClaimedComp(uint256,uint256)";
 | 
			
		||||
        _eventParam = abi.encode(amt, setId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -34,7 +34,7 @@ abstract contract CompResolver is Events, Helpers {
 | 
			
		|||
        uint _len = tokens.length;
 | 
			
		||||
        address[] memory ctokens = new address[](_len);
 | 
			
		||||
        for (uint i = 0; i < _len; i++) {
 | 
			
		||||
            ctokens[i] = InstaMapping(getMappingAddr()).cTokenMapping(tokens[i]);
 | 
			
		||||
            ctokens[i] = instaMapping.cTokenMapping(tokens[i]);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        TokenInterface compToken = TokenInterface(getCompTokenAddress());
 | 
			
		||||
| 
						 | 
				
			
			@ -45,7 +45,7 @@ abstract contract CompResolver is Events, Helpers {
 | 
			
		|||
 | 
			
		||||
        setUint(setId, amt);
 | 
			
		||||
 | 
			
		||||
        eventName = "LogClaimedComp(uint256,uint256)";
 | 
			
		||||
        _eventName = "LogClaimedComp(uint256,uint256)";
 | 
			
		||||
        _eventParam = abi.encode(amt, setId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -69,7 +69,7 @@ abstract contract CompResolver is Events, Helpers {
 | 
			
		|||
 | 
			
		||||
        setUint(setId, amt);
 | 
			
		||||
 | 
			
		||||
        eventName = "LogClaimedComp(uint256,uint256)";
 | 
			
		||||
        _eventName = "LogClaimedComp(uint256,uint256)";
 | 
			
		||||
        _eventParam = abi.encode(amt, setId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -83,8 +83,8 @@ abstract contract CompResolver is Events, Helpers {
 | 
			
		|||
 | 
			
		||||
        compToken.delegate(delegatee);
 | 
			
		||||
 | 
			
		||||
        eventName = "LogClaimedComp(uint256,uint256)";
 | 
			
		||||
        _eventParam = abi.encode(amt, setId);
 | 
			
		||||
        _eventName = "LogDelegate(address)";
 | 
			
		||||
        _eventParam = abi.encode(delegatee);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										9
									
								
								contracts/connectors/aave/events.sol
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								contracts/connectors/aave/events.sol
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,9 @@
 | 
			
		|||
pragma solidity ^0.7.0;
 | 
			
		||||
 | 
			
		||||
contract Events {
 | 
			
		||||
    event LogDeposit(address indexed token, uint256 tokenAmt, uint256 getId, uint256 setId);
 | 
			
		||||
    event LogWithdraw(address indexed token, uint256 tokenAmt, uint256 getId, uint256 setId);
 | 
			
		||||
    event LogBorrow(address indexed token, uint256 tokenAmt, uint256 getId, uint256 setId);
 | 
			
		||||
    event LogPayback(address indexed token, uint256 tokenAmt, uint256 getId, uint256 setId);
 | 
			
		||||
    event LogEnableCollateral(address[] tokens);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										42
									
								
								contracts/connectors/aave/helpers.sol
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								contracts/connectors/aave/helpers.sol
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,42 @@
 | 
			
		|||
pragma solidity ^0.7.0;
 | 
			
		||||
 | 
			
		||||
import { DSMath } from "../../common/math.sol";
 | 
			
		||||
import { Basic } from "../../common/basic.sol";
 | 
			
		||||
import { AaveProviderInterface, AaveInterface } from "./interface.sol";
 | 
			
		||||
 | 
			
		||||
abstract contract Helpers is DSMath, Basic {
 | 
			
		||||
    /**
 | 
			
		||||
     * @dev Aave Provider
 | 
			
		||||
    */
 | 
			
		||||
    AaveProviderInterface constant internal aaveProvider = AaveProviderInterface(0x24a42fD28C976A61Df5D00D0599C34c4f90748c8);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @dev Aave Referral Code
 | 
			
		||||
    */
 | 
			
		||||
    uint16 constant internal referralCode = 3228;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @dev Checks if collateral is enabled for an asset
 | 
			
		||||
    */
 | 
			
		||||
    function getIsColl(AaveInterface aave, address token) internal view returns (bool isCol) {
 | 
			
		||||
        (, , , , , , , , , isCol) = aave.getUserReserveData(token, address(this));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @dev Get total collateral balance for an asset
 | 
			
		||||
     * @param aave Aave Interface
 | 
			
		||||
     * @param token token address of the collateral.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
 | 
			
		||||
    */
 | 
			
		||||
    function getWithdrawBalance(AaveInterface aave, address token) internal view returns (uint bal) {
 | 
			
		||||
        (bal, , , , , , , , , ) = aave.getUserReserveData(token, address(this));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @dev Get total debt balance & fee for an asset
 | 
			
		||||
     * @param aave Aave Interface
 | 
			
		||||
     * @param token token address of the debt.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
 | 
			
		||||
    */
 | 
			
		||||
    function getPaybackBalance(AaveInterface aave, address token) internal view returns (uint bal, uint fee) {
 | 
			
		||||
        (, bal, , , , , fee, , , ) = aave.getUserReserveData(token, address(this));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										41
									
								
								contracts/connectors/aave/interface.sol
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								contracts/connectors/aave/interface.sol
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,41 @@
 | 
			
		|||
pragma solidity ^0.7.0;
 | 
			
		||||
 | 
			
		||||
interface AaveInterface {
 | 
			
		||||
    function deposit(address _reserve, uint256 _amount, uint16 _referralCode) external payable;
 | 
			
		||||
    function redeemUnderlying(
 | 
			
		||||
        address _reserve,
 | 
			
		||||
        address payable _user,
 | 
			
		||||
        uint256 _amount,
 | 
			
		||||
        uint256 _aTokenBalanceAfterRedeem
 | 
			
		||||
    ) external;
 | 
			
		||||
    function setUserUseReserveAsCollateral(address _reserve, bool _useAsCollateral) external;
 | 
			
		||||
    function getUserReserveData(address _reserve, address _user) external view returns (
 | 
			
		||||
        uint256 currentATokenBalance,
 | 
			
		||||
        uint256 currentBorrowBalance,
 | 
			
		||||
        uint256 principalBorrowBalance,
 | 
			
		||||
        uint256 borrowRateMode,
 | 
			
		||||
        uint256 borrowRate,
 | 
			
		||||
        uint256 liquidityRate,
 | 
			
		||||
        uint256 originationFee,
 | 
			
		||||
        uint256 variableBorrowIndex,
 | 
			
		||||
        uint256 lastUpdateTimestamp,
 | 
			
		||||
        bool usageAsCollateralEnabled
 | 
			
		||||
    );
 | 
			
		||||
    function borrow(address _reserve, uint256 _amount, uint256 _interestRateMode, uint16 _referralCode) external;
 | 
			
		||||
    function repay(address _reserve, uint256 _amount, address payable _onBehalfOf) external payable;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
interface AaveProviderInterface {
 | 
			
		||||
    function getLendingPool() external view returns (address);
 | 
			
		||||
    function getLendingPoolCore() external view returns (address);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
interface AaveCoreInterface {
 | 
			
		||||
    function getReserveATokenAddress(address _reserve) external view returns (address);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
interface ATokenInterface {
 | 
			
		||||
    function redeem(uint256 _amount) external;
 | 
			
		||||
    function balanceOf(address _user) external view returns(uint256);
 | 
			
		||||
    function principalBalanceOf(address _user) external view returns(uint256);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										159
									
								
								contracts/connectors/aave/main.sol
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										159
									
								
								contracts/connectors/aave/main.sol
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,159 @@
 | 
			
		|||
pragma solidity ^0.7.0;
 | 
			
		||||
 | 
			
		||||
import { TokenInterface } from "../../common/interfaces.sol";
 | 
			
		||||
import { Stores } from "../../common/stores.sol";
 | 
			
		||||
import { Helpers } from "./helpers.sol";
 | 
			
		||||
import { Events } from "./events.sol";
 | 
			
		||||
import { AaveInterface, AaveCoreInterface, ATokenInterface } from "./interface.sol";
 | 
			
		||||
 | 
			
		||||
abstract contract AaveResolver is Events, Helpers {
 | 
			
		||||
    /**
 | 
			
		||||
     * @dev Deposit ETH/ERC20_Token.
 | 
			
		||||
     * @param token token address to deposit.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
 | 
			
		||||
     * @param amt token amount to deposit.
 | 
			
		||||
     * @param getId Get token amount at this ID from `InstaMemory` Contract.
 | 
			
		||||
     * @param setId Set token amount at this ID in `InstaMemory` Contract.
 | 
			
		||||
    */
 | 
			
		||||
    function deposit(
 | 
			
		||||
        address token,
 | 
			
		||||
        uint amt,
 | 
			
		||||
        uint getId,
 | 
			
		||||
        uint setId
 | 
			
		||||
    ) external payable returns (string memory _eventName, bytes memory _eventParam) {
 | 
			
		||||
        uint _amt = getUint(getId, amt);
 | 
			
		||||
        AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
 | 
			
		||||
 | 
			
		||||
        uint ethAmt;
 | 
			
		||||
        if (token == ethAddr) {
 | 
			
		||||
            _amt = _amt == uint(-1) ? address(this).balance : _amt;
 | 
			
		||||
            ethAmt = _amt;
 | 
			
		||||
        } else {
 | 
			
		||||
            TokenInterface tokenContract = TokenInterface(token);
 | 
			
		||||
            _amt = _amt == uint(-1) ? tokenContract.balanceOf(address(this)) : _amt;
 | 
			
		||||
            tokenContract.approve(aaveProvider.getLendingPoolCore(), _amt);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        aave.deposit{value: ethAmt}(token, _amt, referralCode);
 | 
			
		||||
 | 
			
		||||
        if (!getIsColl(aave, token)) aave.setUserUseReserveAsCollateral(token, true);
 | 
			
		||||
 | 
			
		||||
        setUint(setId, _amt);
 | 
			
		||||
 | 
			
		||||
        _eventName = "LogDeposit(address,uint256,uint256,uint256)";
 | 
			
		||||
        _eventParam = abi.encode(token, _amt, getId, setId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @dev Withdraw ETH/ERC20_Token.
 | 
			
		||||
     * @param token token address to withdraw.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
 | 
			
		||||
     * @param amt token amount to withdraw.
 | 
			
		||||
     * @param getId Get token amount at this ID from `InstaMemory` Contract.
 | 
			
		||||
     * @param setId Set token amount at this ID in `InstaMemory` Contract.
 | 
			
		||||
    */
 | 
			
		||||
    function withdraw(
 | 
			
		||||
        address token,
 | 
			
		||||
        uint amt,
 | 
			
		||||
        uint getId,
 | 
			
		||||
        uint setId
 | 
			
		||||
    ) external payable returns (string memory _eventName, bytes memory _eventParam) {
 | 
			
		||||
        uint _amt = getUint(getId, amt);
 | 
			
		||||
        AaveCoreInterface aaveCore = AaveCoreInterface(aaveProvider.getLendingPoolCore());
 | 
			
		||||
        ATokenInterface atoken = ATokenInterface(aaveCore.getReserveATokenAddress(token));
 | 
			
		||||
        TokenInterface tokenContract = TokenInterface(token);
 | 
			
		||||
 | 
			
		||||
        uint initialBal = token == ethAddr ? address(this).balance : tokenContract.balanceOf(address(this));
 | 
			
		||||
        atoken.redeem(_amt);
 | 
			
		||||
        uint finalBal = token == ethAddr ? address(this).balance : tokenContract.balanceOf(address(this));
 | 
			
		||||
 | 
			
		||||
        _amt = sub(finalBal, initialBal);
 | 
			
		||||
        setUint(setId, _amt);
 | 
			
		||||
 | 
			
		||||
        _eventName = "LogWithdraw(address,uint256,uint256,uint256)";
 | 
			
		||||
        _eventParam = abi.encode(token, _amt, getId, setId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @dev Borrow ETH/ERC20_Token.
 | 
			
		||||
     * @param token token address to borrow.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
 | 
			
		||||
     * @param amt token amount to borrow.
 | 
			
		||||
     * @param getId Get token amount at this ID from `InstaMemory` Contract.
 | 
			
		||||
     * @param setId Set token amount at this ID in `InstaMemory` Contract.
 | 
			
		||||
    */
 | 
			
		||||
    function borrow(
 | 
			
		||||
        address token,
 | 
			
		||||
        uint amt,
 | 
			
		||||
        uint getId,
 | 
			
		||||
        uint setId
 | 
			
		||||
    ) external payable returns (string memory _eventName, bytes memory _eventParam) {
 | 
			
		||||
        uint _amt = getUint(getId, amt);
 | 
			
		||||
        AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
 | 
			
		||||
        aave.borrow(token, _amt, 2, referralCode);
 | 
			
		||||
        setUint(setId, _amt);
 | 
			
		||||
 | 
			
		||||
        _eventName = "LogBorrow(address,uint256,uint256,uint256)";
 | 
			
		||||
        _eventParam = abi.encode(token, _amt, getId, setId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @dev Payback borrowed ETH/ERC20_Token.
 | 
			
		||||
     * @param token token address to payback.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
 | 
			
		||||
     * @param amt token amount to payback.
 | 
			
		||||
     * @param getId Get token amount at this ID from `InstaMemory` Contract.
 | 
			
		||||
     * @param setId Set token amount at this ID in `InstaMemory` Contract.
 | 
			
		||||
    */
 | 
			
		||||
    function payback(
 | 
			
		||||
        address token,
 | 
			
		||||
        uint amt,
 | 
			
		||||
        uint getId,
 | 
			
		||||
        uint setId
 | 
			
		||||
    ) external payable returns (string memory _eventName, bytes memory _eventParam) {
 | 
			
		||||
        uint _amt = getUint(getId, amt);
 | 
			
		||||
        AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
 | 
			
		||||
 | 
			
		||||
        if (_amt == uint(-1)) {
 | 
			
		||||
            uint fee;
 | 
			
		||||
            (_amt, fee) = getPaybackBalance(aave, token);
 | 
			
		||||
            _amt = add(_amt, fee);
 | 
			
		||||
        }
 | 
			
		||||
        uint ethAmt;
 | 
			
		||||
        if (token == ethAddr) {
 | 
			
		||||
            ethAmt = _amt;
 | 
			
		||||
        } else {
 | 
			
		||||
            TokenInterface(token).approve(aaveProvider.getLendingPoolCore(), _amt);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        aave.repay{value: ethAmt}(token, _amt, payable(address(this)));
 | 
			
		||||
 | 
			
		||||
        setUint(setId, _amt);
 | 
			
		||||
 | 
			
		||||
        _eventName = "LogPayback(address,uint256,uint256,uint256)";
 | 
			
		||||
        _eventParam = abi.encode(token, _amt, getId, setId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @dev Enable collateral
 | 
			
		||||
     * @param tokens Array of tokens to enable collateral
 | 
			
		||||
    */
 | 
			
		||||
    function enableCollateral(
 | 
			
		||||
        address[] calldata tokens
 | 
			
		||||
    ) external payable returns (string memory _eventName, bytes memory _eventParam) {
 | 
			
		||||
        uint _length = tokens.length;
 | 
			
		||||
        require(_length > 0, "0-tokens-not-allowed");
 | 
			
		||||
 | 
			
		||||
        AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
 | 
			
		||||
 | 
			
		||||
        for (uint i = 0; i < _length; i++) {
 | 
			
		||||
            address token = tokens[i];
 | 
			
		||||
            if (getWithdrawBalance(aave, token) > 0 && !getIsColl(aave, token)) {
 | 
			
		||||
                aave.setUserUseReserveAsCollateral(token, true);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        _eventName = "LogEnableCollateral(address[]);";
 | 
			
		||||
        _eventParam = abi.encode(tokens);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
contract ConnectAave is AaveResolver {
 | 
			
		||||
    string public name = "Aave-v1.1";
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -57,6 +57,10 @@ abstract contract StakingHelper is DSMath, Stores {
 | 
			
		|||
    stakingToken = TokenInterface(stakingData.stakingToken);
 | 
			
		||||
    rewardToken = TokenInterface(stakingData.rewardToken);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function getMappingAddr() internal virtual view returns (address) {
 | 
			
		||||
    return 0x772590F33eD05b0E83553650BF9e75A04b337526; // InstaMapping Address
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
abstract contract Staking is StakingHelper {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user