mirror of
				https://github.com/Instadapp/dsa-connectors.git
				synced 2024-07-29 22:37:00 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			349 lines
		
	
	
		
			14 KiB
		
	
	
	
		
			Solidity
		
	
	
	
	
	
			
		
		
	
	
			349 lines
		
	
	
		
			14 KiB
		
	
	
	
		
			Solidity
		
	
	
	
	
	
//SPDX-License-Identifier: MIT
 | 
						|
pragma solidity ^0.7.0;
 | 
						|
pragma experimental ABIEncoderV2;
 | 
						|
 | 
						|
/**
 | 
						|
 * @title B.Compound.
 | 
						|
 * @dev Lending & Borrowing.
 | 
						|
 */
 | 
						|
 | 
						|
import { TokenInterface } from "../../../common/interfaces.sol";
 | 
						|
import { Stores } from "../../../common/stores.sol";
 | 
						|
import { Helpers } from "./helpers.sol";
 | 
						|
import { Events } from "./events.sol";
 | 
						|
import { CETHInterface, CTokenInterface } from "./interface.sol";
 | 
						|
 | 
						|
abstract contract BCompoundResolver is Events, Helpers {
 | 
						|
    /**
 | 
						|
     * @dev Deposit ETH/ERC20_Token.
 | 
						|
     * @notice Deposit a token to Compound for lending / collaterization.
 | 
						|
     * @param token The address of the token to deposit. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
 | 
						|
     * @param cToken The address of the corresponding cToken.
 | 
						|
     * @param amt The amount of the token to deposit. (For max: `uint256(-1)`)
 | 
						|
     * @param getId ID to retrieve amt.
 | 
						|
     * @param setId ID stores the amount of tokens deposited.
 | 
						|
    */
 | 
						|
    function depositRaw(
 | 
						|
        address token,
 | 
						|
        address cToken,
 | 
						|
        uint256 amt,
 | 
						|
        uint256 getId,
 | 
						|
        uint256 setId
 | 
						|
    ) public payable returns (string memory _eventName, bytes memory _eventParam) {
 | 
						|
        uint _amt = getUint(getId, amt);
 | 
						|
 | 
						|
        require(token != address(0) && cToken != address(0), "invalid token/ctoken address");
 | 
						|
 | 
						|
        if (token == ethAddr) {
 | 
						|
            _amt = _amt == uint(-1) ? address(this).balance : _amt;
 | 
						|
            CETHInterface(cToken).mint{value: _amt}();
 | 
						|
        } else {
 | 
						|
            TokenInterface tokenContract = TokenInterface(token);
 | 
						|
            _amt = _amt == uint(-1) ? tokenContract.balanceOf(address(this)) : _amt;
 | 
						|
            approve(tokenContract, cToken, _amt);
 | 
						|
            require(CTokenInterface(cToken).mint(_amt) == 0, "deposit-failed");
 | 
						|
        }
 | 
						|
        setUint(setId, _amt);
 | 
						|
 | 
						|
        _eventName = "LogDeposit(address,address,uint256,uint256,uint256)";
 | 
						|
        _eventParam = abi.encode(token, cToken, _amt, getId, setId);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @dev Deposit ETH/ERC20_Token using the Mapping.
 | 
						|
     * @notice Deposit a token to Compound for lending / collaterization.
 | 
						|
     * @param tokenId The token id of the token to deposit.(For eg: ETH-A)
 | 
						|
     * @param amt The amount of the token to deposit. (For max: `uint256(-1)`)
 | 
						|
     * @param getId ID to retrieve amt.
 | 
						|
     * @param setId ID stores the amount of tokens deposited.
 | 
						|
    */
 | 
						|
    function deposit(
 | 
						|
        string calldata tokenId,
 | 
						|
        uint256 amt,
 | 
						|
        uint256 getId,
 | 
						|
        uint256 setId
 | 
						|
    ) external payable returns (string memory _eventName, bytes memory _eventParam) {
 | 
						|
        (address token, address cToken) = getMapping(tokenId);
 | 
						|
        (_eventName, _eventParam) = depositRaw(token, cToken, amt, getId, setId);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @dev Withdraw ETH/ERC20_Token.
 | 
						|
     * @notice Withdraw deposited token from Compound
 | 
						|
     * @param token The address of the token to withdraw. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
 | 
						|
     * @param cToken The address of the corresponding cToken.
 | 
						|
     * @param amt The amount of the token to withdraw. (For max: `uint256(-1)`)
 | 
						|
     * @param getId ID to retrieve amt.
 | 
						|
     * @param setId ID stores the amount of tokens withdrawn.
 | 
						|
    */
 | 
						|
    function withdrawRaw(
 | 
						|
        address token,
 | 
						|
        address cToken,
 | 
						|
        uint256 amt,
 | 
						|
        uint256 getId,
 | 
						|
        uint256 setId
 | 
						|
    ) public payable returns (string memory _eventName, bytes memory _eventParam) {
 | 
						|
        uint _amt = getUint(getId, amt);
 | 
						|
        
 | 
						|
        require(token != address(0) && cToken != address(0), "invalid token/ctoken address");
 | 
						|
 | 
						|
        CTokenInterface cTokenContract = CTokenInterface(cToken);
 | 
						|
        if (_amt == uint(-1)) {
 | 
						|
            TokenInterface tokenContract = TokenInterface(token);
 | 
						|
            uint initialBal = token == ethAddr ? address(this).balance : tokenContract.balanceOf(address(this));
 | 
						|
            require(cTokenContract.redeem(cTokenContract.balanceOf(address(this))) == 0, "full-withdraw-failed");
 | 
						|
            uint finalBal = token == ethAddr ? address(this).balance : tokenContract.balanceOf(address(this));
 | 
						|
            _amt = finalBal - initialBal;
 | 
						|
        } else {
 | 
						|
            require(cTokenContract.redeemUnderlying(_amt) == 0, "withdraw-failed");
 | 
						|
        }
 | 
						|
        setUint(setId, _amt);
 | 
						|
 | 
						|
        _eventName = "LogWithdraw(address,address,uint256,uint256,uint256)";
 | 
						|
        _eventParam = abi.encode(token, cToken, _amt, getId, setId);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @dev Withdraw ETH/ERC20_Token using the Mapping.
 | 
						|
     * @notice Withdraw deposited token from Compound
 | 
						|
     * @param tokenId The token id of the token to withdraw.(For eg: ETH-A)
 | 
						|
     * @param amt The amount of the token to withdraw. (For max: `uint256(-1)`)
 | 
						|
     * @param getId ID to retrieve amt.
 | 
						|
     * @param setId ID stores the amount of tokens withdrawn.
 | 
						|
    */
 | 
						|
    function withdraw(
 | 
						|
        string calldata tokenId,
 | 
						|
        uint256 amt,
 | 
						|
        uint256 getId,
 | 
						|
        uint256 setId
 | 
						|
    ) external payable returns (string memory _eventName, bytes memory _eventParam) {
 | 
						|
        (address token, address cToken) = getMapping(tokenId);
 | 
						|
        (_eventName, _eventParam) = withdrawRaw(token, cToken, amt, getId, setId);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @dev Borrow ETH/ERC20_Token.
 | 
						|
     * @notice Borrow a token using Compound
 | 
						|
     * @param token The address of the token to borrow. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
 | 
						|
     * @param cToken The address of the corresponding cToken.
 | 
						|
     * @param amt The amount of the token to borrow.
 | 
						|
     * @param getId ID to retrieve amt.
 | 
						|
     * @param setId ID stores the amount of tokens borrowed.
 | 
						|
    */
 | 
						|
    function borrowRaw(
 | 
						|
        address token,
 | 
						|
        address cToken,
 | 
						|
        uint256 amt,
 | 
						|
        uint256 getId,
 | 
						|
        uint256 setId
 | 
						|
    ) public payable returns (string memory _eventName, bytes memory _eventParam) {
 | 
						|
        uint _amt = getUint(getId, amt);
 | 
						|
 | 
						|
        require(token != address(0) && cToken != address(0), "invalid token/ctoken address");
 | 
						|
 | 
						|
        require(CTokenInterface(cToken).borrow(_amt) == 0, "borrow-failed");
 | 
						|
        setUint(setId, _amt);
 | 
						|
 | 
						|
        _eventName = "LogBorrow(address,address,uint256,uint256,uint256)";
 | 
						|
        _eventParam = abi.encode(token, cToken, _amt, getId, setId);
 | 
						|
    }
 | 
						|
 | 
						|
     /**
 | 
						|
     * @dev Borrow ETH/ERC20_Token using the Mapping.
 | 
						|
     * @notice Borrow a token using Compound
 | 
						|
     * @param tokenId The token id of the token to borrow.(For eg: DAI-A)
 | 
						|
     * @param amt The amount of the token to borrow.
 | 
						|
     * @param getId ID to retrieve amt.
 | 
						|
     * @param setId ID stores the amount of tokens borrowed.
 | 
						|
    */
 | 
						|
    function borrow(
 | 
						|
        string calldata tokenId,
 | 
						|
        uint256 amt,
 | 
						|
        uint256 getId,
 | 
						|
        uint256 setId
 | 
						|
    ) external payable returns (string memory _eventName, bytes memory _eventParam) {
 | 
						|
        (address token, address cToken) = getMapping(tokenId);
 | 
						|
        (_eventName, _eventParam) = borrowRaw(token, cToken, amt, getId, setId);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @dev Payback borrowed ETH/ERC20_Token.
 | 
						|
     * @notice Payback debt owed.
 | 
						|
     * @param token The address of the token to payback. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
 | 
						|
     * @param cToken The address of the corresponding cToken.
 | 
						|
     * @param amt The amount of the token to payback. (For max: `uint256(-1)`)
 | 
						|
     * @param getId ID to retrieve amt.
 | 
						|
     * @param setId ID stores the amount of tokens paid back.
 | 
						|
    */
 | 
						|
    function paybackRaw(
 | 
						|
        address token,
 | 
						|
        address cToken,
 | 
						|
        uint256 amt,
 | 
						|
        uint256 getId,
 | 
						|
        uint256 setId
 | 
						|
    ) public payable returns (string memory _eventName, bytes memory _eventParam) {
 | 
						|
        uint _amt = getUint(getId, amt);
 | 
						|
 | 
						|
        require(token != address(0) && cToken != address(0), "invalid token/ctoken address");
 | 
						|
 | 
						|
        CTokenInterface cTokenContract = CTokenInterface(cToken);
 | 
						|
        _amt = _amt == uint(-1) ? cTokenContract.borrowBalanceCurrent(address(this)) : _amt;
 | 
						|
 | 
						|
        if (token == ethAddr) {
 | 
						|
            require(address(this).balance >= _amt, "not-enough-eth");
 | 
						|
            CETHInterface(cToken).repayBorrow{value: _amt}();
 | 
						|
        } else {
 | 
						|
            TokenInterface tokenContract = TokenInterface(token);
 | 
						|
            require(tokenContract.balanceOf(address(this)) >= _amt, "not-enough-token");
 | 
						|
            approve(tokenContract, cToken, _amt);
 | 
						|
            require(cTokenContract.repayBorrow(_amt) == 0, "repay-failed.");
 | 
						|
        }
 | 
						|
        setUint(setId, _amt);
 | 
						|
 | 
						|
        _eventName = "LogPayback(address,address,uint256,uint256,uint256)";
 | 
						|
        _eventParam = abi.encode(token, cToken, _amt, getId, setId);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @dev Payback borrowed ETH/ERC20_Token using the Mapping.
 | 
						|
     * @notice Payback debt owed.
 | 
						|
     * @param tokenId The token id of the token to payback.(For eg: COMP-A)
 | 
						|
     * @param amt The amount of the token to payback. (For max: `uint256(-1)`)
 | 
						|
     * @param getId ID to retrieve amt.
 | 
						|
     * @param setId ID stores the amount of tokens paid back.
 | 
						|
    */
 | 
						|
    function payback(
 | 
						|
        string calldata tokenId,
 | 
						|
        uint256 amt,
 | 
						|
        uint256 getId,
 | 
						|
        uint256 setId
 | 
						|
    ) external payable returns (string memory _eventName, bytes memory _eventParam) {
 | 
						|
        (address token, address cToken) = getMapping(tokenId);
 | 
						|
        (_eventName, _eventParam) = paybackRaw(token, cToken, amt, getId, setId);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @dev Deposit ETH/ERC20_Token.
 | 
						|
     * @notice Same as depositRaw. The only difference is this method stores cToken amount in set ID.
 | 
						|
     * @param token The address of the token to deposit. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
 | 
						|
     * @param cToken The address of the corresponding cToken.
 | 
						|
     * @param amt The amount of the token to deposit. (For max: `uint256(-1)`)
 | 
						|
     * @param getId ID to retrieve amt.
 | 
						|
     * @param setId ID stores the amount of cTokens received.
 | 
						|
    */
 | 
						|
    function depositCTokenRaw(
 | 
						|
        address token,
 | 
						|
        address cToken,
 | 
						|
        uint256 amt,
 | 
						|
        uint256 getId,
 | 
						|
        uint256 setId
 | 
						|
    ) public payable returns (string memory _eventName, bytes memory _eventParam) {
 | 
						|
        uint _amt = getUint(getId, amt);
 | 
						|
 | 
						|
        require(token != address(0) && cToken != address(0), "invalid token/ctoken address");
 | 
						|
 | 
						|
        CTokenInterface ctokenContract = CTokenInterface(cToken);
 | 
						|
        uint initialBal = ctokenContract.balanceOf(address(this));
 | 
						|
 | 
						|
        if (token == ethAddr) {
 | 
						|
            _amt = _amt == uint(-1) ? address(this).balance : _amt;
 | 
						|
            CETHInterface(cToken).mint{value: _amt}();
 | 
						|
        } else {
 | 
						|
            TokenInterface tokenContract = TokenInterface(token);
 | 
						|
            _amt = _amt == uint(-1) ? tokenContract.balanceOf(address(this)) : _amt;
 | 
						|
            approve(tokenContract, cToken, _amt);
 | 
						|
            require(ctokenContract.mint(_amt) == 0, "deposit-ctoken-failed.");
 | 
						|
        }
 | 
						|
 | 
						|
        uint _cAmt;
 | 
						|
 | 
						|
        {
 | 
						|
            uint finalBal = ctokenContract.balanceOf(address(this));
 | 
						|
            _cAmt = sub(finalBal, initialBal);
 | 
						|
 | 
						|
            setUint(setId, _cAmt);
 | 
						|
        }
 | 
						|
 | 
						|
        _eventName = "LogDepositCToken(address,address,uint256,uint256,uint256,uint256)";
 | 
						|
        _eventParam = abi.encode(token, cToken, _amt, _cAmt, getId, setId);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @dev Deposit ETH/ERC20_Token using the Mapping.
 | 
						|
     * @notice Same as deposit. The only difference is this method stores cToken amount in set ID.
 | 
						|
     * @param tokenId The token id of the token to depositCToken.(For eg: DAI-A)
 | 
						|
     * @param amt The amount of the token to deposit. (For max: `uint256(-1)`)
 | 
						|
     * @param getId ID to retrieve amt.
 | 
						|
     * @param setId ID stores the amount of cTokens received.
 | 
						|
    */
 | 
						|
    function depositCToken(
 | 
						|
        string calldata tokenId,
 | 
						|
        uint256 amt,
 | 
						|
        uint256 getId,
 | 
						|
        uint256 setId
 | 
						|
    ) external payable returns (string memory _eventName, bytes memory _eventParam) {
 | 
						|
        (address token, address cToken) = getMapping(tokenId);
 | 
						|
        (_eventName, _eventParam) = depositCTokenRaw(token, cToken, amt, getId, setId);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @dev Withdraw CETH/CERC20_Token using cToken Amt.
 | 
						|
     * @notice Same as withdrawRaw. The only difference is this method fetch cToken amount in get ID.
 | 
						|
     * @param token The address of the token to withdraw. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
 | 
						|
     * @param cToken The address of the corresponding cToken.
 | 
						|
     * @param cTokenAmt The amount of cTokens to withdraw
 | 
						|
     * @param getId ID to retrieve cTokenAmt 
 | 
						|
     * @param setId ID stores the amount of tokens withdrawn.
 | 
						|
    */
 | 
						|
    function withdrawCTokenRaw(
 | 
						|
        address token,
 | 
						|
        address cToken,
 | 
						|
        uint cTokenAmt,
 | 
						|
        uint getId,
 | 
						|
        uint setId
 | 
						|
    ) public payable returns (string memory _eventName, bytes memory _eventParam) {
 | 
						|
        uint _cAmt = getUint(getId, cTokenAmt);
 | 
						|
        require(token != address(0) && cToken != address(0), "invalid token/ctoken address");
 | 
						|
 | 
						|
        CTokenInterface cTokenContract = CTokenInterface(cToken);
 | 
						|
        TokenInterface tokenContract = TokenInterface(token);
 | 
						|
        _cAmt = _cAmt == uint(-1) ? cTokenContract.balanceOf(address(this)) : _cAmt;
 | 
						|
 | 
						|
        uint withdrawAmt;
 | 
						|
        {
 | 
						|
            uint initialBal = token != ethAddr ? tokenContract.balanceOf(address(this)) : address(this).balance;
 | 
						|
            require(cTokenContract.redeem(_cAmt) == 0, "redeem-failed");
 | 
						|
            uint finalBal = token != ethAddr ? tokenContract.balanceOf(address(this)) : address(this).balance;
 | 
						|
 | 
						|
            withdrawAmt = sub(finalBal, initialBal);
 | 
						|
        }
 | 
						|
 | 
						|
        setUint(setId, withdrawAmt);
 | 
						|
 | 
						|
        _eventName = "LogWithdrawCToken(address,address,uint256,uint256,uint256,uint256)";
 | 
						|
        _eventParam = abi.encode(token, cToken, withdrawAmt, _cAmt, getId, setId);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @dev Withdraw CETH/CERC20_Token using cToken Amt & the Mapping.
 | 
						|
     * @notice Same as withdraw. The only difference is this method fetch cToken amount in get ID.
 | 
						|
     * @param tokenId The token id of the token to withdraw CToken.(For eg: ETH-A)
 | 
						|
     * @param cTokenAmt The amount of cTokens to withdraw
 | 
						|
     * @param getId ID to retrieve cTokenAmt 
 | 
						|
     * @param setId ID stores the amount of tokens withdrawn.
 | 
						|
    */
 | 
						|
    function withdrawCToken(
 | 
						|
        string calldata tokenId,
 | 
						|
        uint cTokenAmt,
 | 
						|
        uint getId,
 | 
						|
        uint setId
 | 
						|
    ) external payable returns (string memory _eventName, bytes memory _eventParam) {
 | 
						|
        (address token, address cToken) = getMapping(tokenId);
 | 
						|
        (_eventName, _eventParam) = withdrawCTokenRaw(token, cToken, cTokenAmt, getId, setId);
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
contract ConnectV2BCompound is BCompoundResolver {
 | 
						|
    string public name = "B.Compound-v1.0";
 | 
						|
}
 |