From 88c8a689c2c38f5b624993bfe3a7c48c1981e022 Mon Sep 17 00:00:00 2001 From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com> Date: Mon, 29 Mar 2021 03:12:57 +0530 Subject: [PATCH 1/5] raw function added --- contracts/connectors/compound/events.sol | 1 - contracts/connectors/compound/main.sol | 22 ++++++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/contracts/connectors/compound/events.sol b/contracts/connectors/compound/events.sol index 7d5ac529..7e3561e5 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, diff --git a/contracts/connectors/compound/main.sol b/contracts/connectors/compound/main.sol index 2838d47e..9b0ded06 100644 --- a/contracts/connectors/compound/main.sol +++ b/contracts/connectors/compound/main.sol @@ -15,15 +15,15 @@ abstract contract CompoundResolver is Events, Helpers { * @param getId Get token amount at this ID from `InstaMemory` Contract. * @param setId Set token amount at this ID in `InstaMemory` Contract. */ - 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"); enterMarket(cToken); @@ -38,8 +38,18 @@ 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); + } + + 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); } /** From 12652a2ca15e5cd243f7f9c574ed4bcad9e5ee9b Mon Sep 17 00:00:00 2001 From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com> Date: Mon, 29 Mar 2021 03:25:46 +0530 Subject: [PATCH 2/5] updated compound with raw functions --- contracts/connectors/compound/events.sol | 5 - contracts/connectors/compound/main.sol | 113 +++++++++++++++++------ 2 files changed, 83 insertions(+), 35 deletions(-) diff --git a/contracts/connectors/compound/events.sol b/contracts/connectors/compound/events.sol index 7e3561e5..4e4b5033 100644 --- a/contracts/connectors/compound/events.sol +++ b/contracts/connectors/compound/events.sol @@ -11,7 +11,6 @@ contract Events { event LogWithdraw( address indexed token, - string tokenId, address cToken, uint256 tokenAmt, uint256 getId, @@ -20,7 +19,6 @@ contract Events { event LogBorrow( address indexed token, - string tokenId, address cToken, uint256 tokenAmt, uint256 getId, @@ -29,7 +27,6 @@ contract Events { event LogPayback( address indexed token, - string tokenId, address cToken, uint256 tokenAmt, uint256 getId, @@ -38,7 +35,6 @@ contract Events { event LogDepositCToken( address indexed token, - string tokenId, address cToken, uint256 tokenAmt, uint256 cTokenAmt, @@ -48,7 +44,6 @@ contract Events { event LogWithdrawCToken( address indexed token, - string tokenId, address cToken, uint256 tokenAmt, uint256 cTokenAmt, diff --git a/contracts/connectors/compound/main.sol b/contracts/connectors/compound/main.sol index 9b0ded06..5b4a3dce 100644 --- a/contracts/connectors/compound/main.sol +++ b/contracts/connectors/compound/main.sol @@ -59,15 +59,15 @@ abstract contract CompoundResolver is Events, Helpers { * @param getId Get token amount at this ID from `InstaMemory` Contract. * @param setId Set token amount at this ID in `InstaMemory` Contract. */ - function withdraw( - string calldata tokenId, + function withdrawRaw( + 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"); CTokenInterface cTokenContract = CTokenInterface(cToken); @@ -82,8 +82,18 @@ 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); + } + + 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); } /** @@ -93,22 +103,33 @@ abstract contract CompoundResolver is Events, Helpers { * @param getId Get token amount at this ID from `InstaMemory` Contract. * @param setId Set token amount at this ID in `InstaMemory` Contract. */ - function borrow( - string calldata tokenId, + function borrowRaw( + 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"); 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 = "LogBorrow(address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode(token, cToken, _amt, getId, setId); + } + + function borrow( + 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) = borrowRaw(token, cToken, amt, getId, setId); } /** @@ -118,14 +139,15 @@ abstract contract CompoundResolver is Events, Helpers { * @param getId Get token amount at this ID from `InstaMemory` Contract. * @param setId Set token amount at this ID in `InstaMemory` Contract. */ - 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"); CTokenInterface cTokenContract = CTokenInterface(cToken); @@ -142,8 +164,18 @@ 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); + } + + function payback( + 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) = paybackRaw(token, cToken, amt, getId, setId); } /** @@ -153,14 +185,15 @@ abstract contract CompoundResolver is Events, Helpers { * @param getId Get token amount at this ID from `InstaMemory` Contract. * @param setId Set ctoken amount at this ID in `InstaMemory` Contract. */ - function depositCToken( - string calldata tokenId, + function depositCTokenRaw( + 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"); enterMarket(cToken); @@ -186,8 +219,18 @@ 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); + } + + 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); } /** @@ -197,14 +240,14 @@ abstract contract CompoundResolver is Events, Helpers { * @param getId Get ctoken amount at this ID from `InstaMemory` Contract. * @param setId Set token amount at this ID in `InstaMemory` Contract. */ - 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"); CTokenInterface cTokenContract = CTokenInterface(cToken); @@ -222,8 +265,18 @@ 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); + } + + 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, amt, getId, setId); } /** From 815f731079733d24cd8782664542a72c7d9d150c Mon Sep 17 00:00:00 2001 From: Mubaris NK Date: Mon, 29 Mar 2021 10:39:27 +0530 Subject: [PATCH 3/5] Add comments --- contracts/connectors/compound/main.sol | 48 ++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/contracts/connectors/compound/main.sol b/contracts/connectors/compound/main.sol index 5b4a3dce..b7781334 100644 --- a/contracts/connectors/compound/main.sol +++ b/contracts/connectors/compound/main.sol @@ -42,6 +42,14 @@ abstract contract CompoundResolver is Events, Helpers { _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, @@ -86,6 +94,14 @@ abstract contract CompoundResolver is Events, Helpers { _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, @@ -122,6 +138,14 @@ abstract contract CompoundResolver is Events, Helpers { _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, @@ -168,6 +192,14 @@ abstract contract CompoundResolver is Events, Helpers { _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, @@ -223,6 +255,14 @@ abstract contract CompoundResolver is Events, Helpers { _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, @@ -269,6 +309,14 @@ abstract contract CompoundResolver is Events, Helpers { _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, From 26fa1d348ce17c89427a703fdd074c3aaafe53f5 Mon Sep 17 00:00:00 2001 From: Mubaris NK Date: Mon, 29 Mar 2021 21:10:40 +0530 Subject: [PATCH 4/5] Update docs and minor fix --- contracts/connectors/compound/main.sol | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/contracts/connectors/compound/main.sol b/contracts/connectors/compound/main.sol index b7781334..3a299c51 100644 --- a/contracts/connectors/compound/main.sol +++ b/contracts/connectors/compound/main.sol @@ -10,7 +10,8 @@ import { CETHInterface, CTokenInterface, LiquidateData } from "./interface.sol"; abstract contract CompoundResolver is Events, Helpers { /** * @dev Deposit ETH/ERC20_Token. - * @param tokenId 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 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. @@ -62,7 +63,8 @@ abstract contract CompoundResolver is Events, Helpers { /** * @dev Withdraw ETH/ERC20_Token. - * @param tokenId token id of the token to withdraw.(For eg: ETH-A) + * @param token The address of the token to withdraw. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param cToken The address of the corresponding cToken. * @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. @@ -114,7 +116,8 @@ abstract contract CompoundResolver is Events, Helpers { /** * @dev Borrow ETH/ERC20_Token. - * @param tokenId token id of the token to borrow.(For eg: DAI-A) + * @param token The address of the token to borrow. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param cToken The address of the corresponding cToken. * @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. @@ -158,7 +161,8 @@ abstract contract CompoundResolver is Events, Helpers { /** * @dev Payback borrowed ETH/ERC20_Token. - * @param tokenId 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 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. @@ -212,7 +216,8 @@ abstract contract CompoundResolver is Events, Helpers { /** * @dev Deposit ETH/ERC20_Token. - * @param tokenId token id of the token to depositCToken.(For eg: DAI-A) + * @param token The address of the token to deposit. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param cToken The address of the corresponding cToken. * @param amt token amount to depositCToken. * @param getId Get token amount at this ID from `InstaMemory` Contract. * @param setId Set ctoken amount at this ID in `InstaMemory` Contract. @@ -275,7 +280,8 @@ abstract contract CompoundResolver is Events, Helpers { /** * @dev Withdraw CETH/CERC20_Token using cToken Amt. - * @param tokenId token id of the token to withdraw CToken.(For eg: ETH-A) + * @param token The address of the token to withdraw. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param cToken The address of the corresponding cToken. * @param cTokenAmt ctoken amount to withdrawCToken. * @param getId Get ctoken amount at this ID from `InstaMemory` Contract. * @param setId Set token amount at this ID in `InstaMemory` Contract. @@ -324,7 +330,7 @@ abstract contract CompoundResolver is Events, Helpers { uint setId ) external payable returns (string memory _eventName, bytes memory _eventParam) { (address token, address cToken) = compMapping.getMapping(tokenId); - (_eventName, _eventParam) = withdrawCTokenRaw(token, cToken, amt, getId, setId); + (_eventName, _eventParam) = withdrawCTokenRaw(token, cToken, cTokenAmt, getId, setId); } /** From 3757d743f798de46971d1048cca2cec2f00d8a12 Mon Sep 17 00:00:00 2001 From: Mubaris NK Date: Mon, 29 Mar 2021 21:23:21 +0530 Subject: [PATCH 5/5] Add raw liquidation --- contracts/connectors/compound/interface.sol | 8 -- contracts/connectors/compound/main.sol | 110 +++++++++++++------- 2 files changed, 72 insertions(+), 46 deletions(-) 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 3a299c51..5f06d05d 100644 --- a/contracts/connectors/compound/main.sol +++ b/contracts/connectors/compound/main.sol @@ -5,7 +5,7 @@ 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 { /** @@ -25,7 +25,7 @@ abstract contract CompoundResolver is Events, Helpers { ) public payable returns (string memory _eventName, bytes memory _eventParam) { uint _amt = getUint(getId, amt); - 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) { @@ -78,7 +78,7 @@ abstract contract CompoundResolver is Events, Helpers { ) public payable returns (string memory _eventName, bytes memory _eventParam) { uint _amt = getUint(getId, amt); - 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)) { @@ -131,7 +131,7 @@ abstract contract CompoundResolver is Events, Helpers { ) public payable returns (string memory _eventName, bytes memory _eventParam) { uint _amt = getUint(getId, amt); - require(token != address(0) && cToken != address(0), "ctoken mapping not found"); + require(token != address(0) && cToken != address(0), "invalid token/ctoken address"); enterMarket(cToken); require(CTokenInterface(cToken).borrow(_amt) == 0, "borrow-failed"); @@ -176,7 +176,7 @@ abstract contract CompoundResolver is Events, Helpers { ) public payable returns (string memory _eventName, bytes memory _eventParam) { uint _amt = getUint(getId, amt); - 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; @@ -231,7 +231,7 @@ abstract contract CompoundResolver is Events, Helpers { ) public payable returns (string memory _eventName, bytes memory _eventParam) { uint _amt = getUint(getId, amt); - require(token != address(0) && cToken != address(0), "ctoken mapping not found"); + require(token != address(0) && cToken != address(0), "invalid token/ctoken address"); enterMarket(cToken); @@ -294,7 +294,7 @@ abstract contract CompoundResolver is Events, Helpers { uint setId ) public payable returns (string memory _eventName, bytes memory _eventParam) { uint _cAmt = getUint(getId, cTokenAmt); - 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); @@ -336,6 +336,62 @@ abstract contract CompoundResolver is Events, Helpers { /** * @dev Liquidate a position. * @param borrower Borrower's Address. + * @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 Get token amount at this ID from `InstaMemory` Contract. + * @param setId Set token amount at this ID in `InstaMemory` Contract. + */ + 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. + * @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. @@ -350,38 +406,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 );