diff --git a/contracts/connectors/compound/events.sol b/contracts/connectors/compound/events.sol index 7d5ac529..4e4b5033 100644 --- a/contracts/connectors/compound/events.sol +++ b/contracts/connectors/compound/events.sol @@ -3,7 +3,6 @@ pragma solidity ^0.7.0; contract Events { event LogDeposit( address indexed token, - string tokenId, address cToken, uint256 tokenAmt, uint256 getId, @@ -12,7 +11,6 @@ contract Events { event LogWithdraw( address indexed token, - string tokenId, address cToken, uint256 tokenAmt, uint256 getId, @@ -21,7 +19,6 @@ contract Events { event LogBorrow( address indexed token, - string tokenId, address cToken, uint256 tokenAmt, uint256 getId, @@ -30,7 +27,6 @@ contract Events { event LogPayback( address indexed token, - string tokenId, address cToken, uint256 tokenAmt, uint256 getId, @@ -39,7 +35,6 @@ contract Events { event LogDepositCToken( address indexed token, - string tokenId, address cToken, uint256 tokenAmt, uint256 cTokenAmt, @@ -49,7 +44,6 @@ contract Events { event LogWithdrawCToken( address indexed token, - string tokenId, address cToken, uint256 tokenAmt, uint256 cTokenAmt, diff --git a/contracts/connectors/compound/interface.sol b/contracts/connectors/compound/interface.sol index bc0e1036..a0abf734 100644 --- a/contracts/connectors/compound/interface.sol +++ b/contracts/connectors/compound/interface.sol @@ -33,12 +33,4 @@ interface ComptrollerInterface { interface CompoundMappingInterface { function cTokenMapping(string calldata tokenId) external view returns (address); function getMapping(string calldata tokenId) external view returns (address, address); -} - -struct LiquidateData { - address tokenToPay; - address tokenInReturn; - address cTokenPay; - address cTokenColl; - CTokenInterface cTokenContract; } \ No newline at end of file diff --git a/contracts/connectors/compound/main.sol b/contracts/connectors/compound/main.sol index dd869de5..5b1da0f4 100644 --- a/contracts/connectors/compound/main.sol +++ b/contracts/connectors/compound/main.sol @@ -5,27 +5,28 @@ 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, LiquidateData } from "./interface.sol"; +import { CETHInterface, CTokenInterface } from "./interface.sol"; abstract contract CompoundResolver is Events, Helpers { /** * @dev Deposit ETH/ERC20_Token. * @notice Deposit a token to Compound for lending / collaterization. - * @param tokenId The token id of the token to deposit.(For eg: ETH-A) + * @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 deposit( - string calldata tokenId, + function depositRaw( + address token, + address cToken, uint256 amt, uint256 getId, uint256 setId - ) external payable returns (string memory _eventName, bytes memory _eventParam) { + ) public payable returns (string memory _eventName, bytes memory _eventParam) { uint _amt = getUint(getId, amt); - (address token, address cToken) = compMapping.getMapping(tokenId); - require(token != address(0) && cToken != address(0), "ctoken mapping not found"); + require(token != address(0) && cToken != address(0), "invalid token/ctoken address"); enterMarket(cToken); if (token == ethAddr) { @@ -39,28 +40,47 @@ abstract contract CompoundResolver is Events, Helpers { } setUint(setId, _amt); - _eventName = "LogDeposit(address,string,address,uint256,uint256,uint256)"; - _eventParam = abi.encode(token, tokenId, cToken, _amt, getId, setId); + _eventName = "LogDeposit(address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode(token, cToken, _amt, getId, setId); } /** - * @dev Withdraw ETH/ERC20_Token. - * @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)`) + * @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 withdrawn. + * @param setId ID stores the amount of tokens deposited. */ - function withdraw( + function deposit( string calldata tokenId, uint256 amt, uint256 getId, uint256 setId ) external payable returns (string memory _eventName, bytes memory _eventParam) { + (address token, address cToken) = compMapping.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); - (address token, address cToken) = compMapping.getMapping(tokenId); - require(token != address(0) && cToken != address(0), "ctoken mapping not found"); + require(token != address(0) && cToken != address(0), "invalid token/ctoken address"); CTokenInterface cTokenContract = CTokenInterface(cToken); if (_amt == uint(-1)) { @@ -74,13 +94,59 @@ abstract contract CompoundResolver is Events, Helpers { } setUint(setId, _amt); - _eventName = "LogWithdraw(address,string,address,uint256,uint256,uint256)"; - _eventParam = abi.encode(token, tokenId, cToken, _amt, getId, setId); + _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) = compMapping.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"); + + enterMarket(cToken); + 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. @@ -92,35 +158,29 @@ abstract contract CompoundResolver is Events, Helpers { uint256 getId, uint256 setId ) external payable returns (string memory _eventName, bytes memory _eventParam) { - uint _amt = getUint(getId, amt); (address token, address cToken) = compMapping.getMapping(tokenId); - require(token != address(0) && cToken != address(0), "ctoken mapping not found"); - - enterMarket(cToken); - require(CTokenInterface(cToken).borrow(_amt) == 0, "borrow-failed"); - setUint(setId, _amt); - - _eventName = "LogBorrow(address,string,address,uint256,uint256,uint256)"; - _eventParam = abi.encode(token, tokenId, cToken, _amt, getId, setId); + (_eventName, _eventParam) = borrowRaw(token, cToken, amt, getId, setId); } /** * @dev Payback borrowed ETH/ERC20_Token. * @notice Payback debt owed. - * @param tokenId The token id of the token to payback.(For eg: COMP-A) + * @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 payback( - string calldata tokenId, + function paybackRaw( + address token, + address cToken, uint256 amt, uint256 getId, uint256 setId - ) external payable returns (string memory _eventName, bytes memory _eventParam) { + ) public payable returns (string memory _eventName, bytes memory _eventParam) { uint _amt = getUint(getId, amt); - (address token, address cToken) = compMapping.getMapping(tokenId); - require(token != address(0) && cToken != address(0), "ctoken mapping not found"); + + require(token != address(0) && cToken != address(0), "invalid token/ctoken address"); CTokenInterface cTokenContract = CTokenInterface(cToken); _amt = _amt == uint(-1) ? cTokenContract.borrowBalanceCurrent(address(this)) : _amt; @@ -136,27 +196,47 @@ abstract contract CompoundResolver is Events, Helpers { } setUint(setId, _amt); - _eventName = "LogPayback(address,string,address,uint256,uint256,uint256)"; - _eventParam = abi.encode(token, tokenId, cToken, _amt, getId, setId); + _eventName = "LogPayback(address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode(token, cToken, _amt, getId, setId); } /** - * @dev Deposit ETH/ERC20_Token. - * @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)`) + * @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 cTokens received. + * @param setId ID stores the amount of tokens paid back. */ - function depositCToken( + function payback( string calldata tokenId, uint256 amt, uint256 getId, uint256 setId ) external payable returns (string memory _eventName, bytes memory _eventParam) { - uint _amt = getUint(getId, amt); (address token, address cToken) = compMapping.getMapping(tokenId); - require(token != address(0) && cToken != address(0), "ctoken mapping not found"); + (_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"); enterMarket(cToken); @@ -181,27 +261,46 @@ abstract contract CompoundResolver is Events, Helpers { setUint(setId, _cAmt); } - _eventName = "LogDepositCToken(address,string,address,uint256,uint256,uint256,uint256)"; - _eventParam = abi.encode(token, tokenId, cToken, _amt, _cAmt, getId, setId); + _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) = compMapping.getMapping(tokenId); + (_eventName, _eventParam) = depositCTokenRaw(token, cToken, amt, getId, setId); } /** * @dev Withdraw CETH/CERC20_Token using cToken Amt. - * @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) + * @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 withdrawCToken( - string calldata tokenId, + function withdrawCTokenRaw( + address token, + address cToken, uint cTokenAmt, uint getId, uint setId - ) external payable returns (string memory _eventName, bytes memory _eventParam) { + ) public payable returns (string memory _eventName, bytes memory _eventParam) { uint _cAmt = getUint(getId, cTokenAmt); - (address token, address cToken) = compMapping.getMapping(tokenId); - require(token != address(0) && cToken != address(0), "ctoken mapping not found"); + require(token != address(0) && cToken != address(0), "invalid token/ctoken address"); CTokenInterface cTokenContract = CTokenInterface(cToken); TokenInterface tokenContract = TokenInterface(token); @@ -218,17 +317,92 @@ abstract contract CompoundResolver is Events, Helpers { setUint(setId, withdrawAmt); - _eventName = "LogWithdrawCToken(address,string,address,uint256,uint256,uint256,uint256)"; - _eventParam = abi.encode(token, tokenId, cToken, withdrawAmt, _cAmt, getId, setId); + _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) = compMapping.getMapping(tokenId); + (_eventName, _eventParam) = withdrawCTokenRaw(token, cToken, cTokenAmt, getId, setId); } /** * @dev Liquidate a position. * @notice Liquidate a position. * @param borrower Borrower's Address. - * @param tokenIdToPay The token id of the token to pay for liquidation.(For eg: ETH-A) - * @param tokenIdInReturn The token id of the token to return for liquidation.(For eg: USDC-A) - * @param amt The amount of tokens to pay for liquidation. (For max: `uint256(-1)`) + * @param tokenToPay The address of the token to pay for liquidation.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param cTokenPay Corresponding cToken address. + * @param tokenInReturn The address of the token to return for liquidation. + * @param cTokenColl Corresponding cToken address. + * @param amt The token amount to pay for liquidation. + * @param getId ID to retrieve amt. + * @param setId ID stores the amount of paid for liquidation. + */ + function liquidateRaw( + address borrower, + address tokenToPay, + address cTokenPay, + address tokenInReturn, + address cTokenColl, + uint256 amt, + uint256 getId, + uint256 setId + ) public payable returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amt); + require(tokenToPay != address(0) && cTokenPay != address(0), "invalid token/ctoken address"); + require(tokenInReturn != address(0) && cTokenColl != address(0), "invalid token/ctoken address"); + + CTokenInterface cTokenContract = CTokenInterface(cTokenPay); + + { + (,, uint shortfal) = troller.getAccountLiquidity(borrower); + require(shortfal != 0, "account-cannot-be-liquidated"); + _amt = _amt == uint(-1) ? cTokenContract.borrowBalanceCurrent(borrower) : _amt; + } + + if (tokenToPay == ethAddr) { + require(address(this).balance >= _amt, "not-enought-eth"); + CETHInterface(cTokenPay).liquidateBorrow{value: _amt}(borrower, cTokenColl); + } else { + TokenInterface tokenContract = TokenInterface(tokenToPay); + require(tokenContract.balanceOf(address(this)) >= _amt, "not-enough-token"); + tokenContract.approve(cTokenPay, _amt); + require(cTokenContract.liquidateBorrow(borrower, _amt, cTokenColl) == 0, "liquidate-failed"); + } + + setUint(setId, _amt); + + _eventName = "LogLiquidate(address,address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode( + address(this), + tokenToPay, + tokenInReturn, + _amt, + getId, + setId + ); + } + + /** + * @dev Liquidate a position using the mapping. + * @notice Liquidate a position using the mapping. + * @param borrower Borrower's Address. + * @param tokenIdToPay token id of the token to pay for liquidation.(For eg: ETH-A) + * @param tokenIdInReturn token id of the token to return for liquidation.(For eg: USDC-A) + * @param amt token amount to pay for liquidation. * @param getId ID to retrieve amt. * @param setId ID stores the amount of paid for liquidation. */ @@ -240,38 +414,16 @@ abstract contract CompoundResolver is Events, Helpers { uint256 getId, uint256 setId ) external payable returns (string memory _eventName, bytes memory _eventParam) { - uint _amt = getUint(getId, amt); + (address tokenToPay, address cTokenToPay) = compMapping.getMapping(tokenIdToPay); + (address tokenInReturn, address cTokenColl) = compMapping.getMapping(tokenIdInReturn); - LiquidateData memory data; - - (data.tokenToPay, data.cTokenPay) = compMapping.getMapping(tokenIdToPay); - (data.tokenInReturn, data.cTokenColl) = compMapping.getMapping(tokenIdInReturn); - data.cTokenContract = CTokenInterface(data.cTokenPay); - - { - (,, uint shortfal) = troller.getAccountLiquidity(borrower); - require(shortfal != 0, "account-cannot-be-liquidated"); - _amt = _amt == uint(-1) ? data.cTokenContract.borrowBalanceCurrent(borrower) : _amt; - } - - if (data.tokenToPay == ethAddr) { - require(address(this).balance >= _amt, "not-enought-eth"); - CETHInterface(data.cTokenPay).liquidateBorrow{value: _amt}(borrower, data.cTokenColl); - } else { - TokenInterface tokenContract = TokenInterface(data.tokenToPay); - require(tokenContract.balanceOf(address(this)) >= _amt, "not-enough-token"); - tokenContract.approve(data.cTokenPay, _amt); - require(data.cTokenContract.liquidateBorrow(borrower, _amt, data.cTokenColl) == 0, "liquidate-failed"); - } - - setUint(setId, _amt); - - _eventName = "LogLiquidate(address,address,address,uint256,uint256,uint256)"; - _eventParam = abi.encode( - address(this), - data.tokenToPay, - data.tokenInReturn, - _amt, + (_eventName, _eventParam) = liquidateRaw( + borrower, + tokenToPay, + cTokenToPay, + tokenInReturn, + cTokenColl, + amt, getId, setId );