mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
commit
6f551d4846
62
contracts/connectors/cream/events.sol
Normal file
62
contracts/connectors/cream/events.sol
Normal file
|
@ -0,0 +1,62 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
contract Events {
|
||||
event LogDeposit(
|
||||
address indexed token,
|
||||
address cToken,
|
||||
uint256 tokenAmt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
|
||||
event LogWithdraw(
|
||||
address indexed token,
|
||||
address cToken,
|
||||
uint256 tokenAmt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
|
||||
event LogBorrow(
|
||||
address indexed token,
|
||||
address cToken,
|
||||
uint256 tokenAmt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
|
||||
event LogPayback(
|
||||
address indexed token,
|
||||
address cToken,
|
||||
uint256 tokenAmt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
|
||||
event LogDepositCToken(
|
||||
address indexed token,
|
||||
address cToken,
|
||||
uint256 tokenAmt,
|
||||
uint256 cTokenAmt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
|
||||
event LogWithdrawCToken(
|
||||
address indexed token,
|
||||
address cToken,
|
||||
uint256 tokenAmt,
|
||||
uint256 cTokenAmt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
|
||||
event LogLiquidate(
|
||||
address indexed borrower,
|
||||
address indexed tokenToPay,
|
||||
address indexed tokenInReturn,
|
||||
uint256 tokenAmt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
}
|
36
contracts/connectors/cream/helpers.sol
Normal file
36
contracts/connectors/cream/helpers.sol
Normal file
|
@ -0,0 +1,36 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
import { DSMath } from "../../common/math.sol";
|
||||
import { Basic } from "../../common/basic.sol";
|
||||
import { ComptrollerInterface, CreamMappingInterface } from "./interface.sol";
|
||||
|
||||
abstract contract Helpers is DSMath, Basic {
|
||||
/**
|
||||
* @dev Cream Comptroller
|
||||
*/
|
||||
ComptrollerInterface internal constant troller = ComptrollerInterface(0x3d5BC3c8d13dcB8bF317092d84783c2697AE9258);
|
||||
|
||||
/**
|
||||
* @dev Cream Mapping
|
||||
*/
|
||||
// TODO: wait for the cream mapping contract address
|
||||
CreamMappingInterface internal constant creamMapping = CreamMappingInterface();
|
||||
|
||||
/**
|
||||
* @dev enter cream market
|
||||
*/
|
||||
function enterMarket(address cToken) internal {
|
||||
address[] memory markets = troller.getAssetsIn(address(this));
|
||||
bool isEntered = false;
|
||||
for (uint i = 0; i < markets.length; i++) {
|
||||
if (markets[i] == cToken) {
|
||||
isEntered = true;
|
||||
}
|
||||
}
|
||||
if (!isEntered) {
|
||||
address[] memory toEnter = new address[](1);
|
||||
toEnter[0] = cToken;
|
||||
troller.enterMarkets(toEnter);
|
||||
}
|
||||
}
|
||||
}
|
36
contracts/connectors/cream/interface.sol
Normal file
36
contracts/connectors/cream/interface.sol
Normal file
|
@ -0,0 +1,36 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
interface CTokenInterface {
|
||||
function mint(uint mintAmount) external returns (uint);
|
||||
function redeem(uint redeemTokens) external returns (uint);
|
||||
function borrow(uint borrowAmount) external returns (uint);
|
||||
function repayBorrow(uint repayAmount) external returns (uint);
|
||||
function repayBorrowBehalf(address borrower, uint repayAmount) external returns (uint); // For ERC20
|
||||
function liquidateBorrow(address borrower, uint repayAmount, address cTokenCollateral) external returns (uint);
|
||||
|
||||
function borrowBalanceCurrent(address account) external returns (uint);
|
||||
function redeemUnderlying(uint redeemAmount) external returns (uint);
|
||||
function exchangeRateCurrent() external returns (uint);
|
||||
|
||||
function balanceOf(address owner) external view returns (uint256 balance);
|
||||
}
|
||||
|
||||
interface CETHInterface {
|
||||
function mint() external payable;
|
||||
function repayBorrow() external payable;
|
||||
function repayBorrowBehalf(address borrower) external payable;
|
||||
function liquidateBorrow(address borrower, address cTokenCollateral) external payable;
|
||||
}
|
||||
|
||||
interface ComptrollerInterface {
|
||||
function enterMarkets(address[] calldata cTokens) external returns (uint[] memory);
|
||||
function exitMarket(address cTokenAddress) external returns (uint);
|
||||
function getAssetsIn(address account) external view returns (address[] memory);
|
||||
function getAccountLiquidity(address account) external view returns (uint, uint, uint);
|
||||
function claimComp(address) external;
|
||||
}
|
||||
|
||||
interface CreamMappingInterface {
|
||||
function cTokenMapping(string calldata tokenId) external view returns (address);
|
||||
function getMapping(string calldata tokenId) external view returns (address, address);
|
||||
}
|
435
contracts/connectors/cream/main.sol
Normal file
435
contracts/connectors/cream/main.sol
Normal file
|
@ -0,0 +1,435 @@
|
|||
pragma solidity ^0.7.0;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
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 CreamResolver is Events, Helpers {
|
||||
/**
|
||||
* @dev Deposit ETH/ERC20_Token.
|
||||
* @notice Deposit a token to Cream 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");
|
||||
|
||||
enterMarket(cToken);
|
||||
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;
|
||||
tokenContract.approve(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 Cream 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) = creamMapping.getMapping(tokenId);
|
||||
(_eventName, _eventParam) = depositRaw(token, cToken, amt, getId, setId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Withdraw ETH/ERC20_Token.
|
||||
* @notice Withdraw deposited token from Cream
|
||||
* @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 Cream
|
||||
* @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) = creamMapping.getMapping(tokenId);
|
||||
(_eventName, _eventParam) = withdrawRaw(token, cToken, amt, getId, setId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Borrow ETH/ERC20_Token.
|
||||
* @notice Borrow a token using Cream
|
||||
* @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 Cream
|
||||
* @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) = creamMapping.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");
|
||||
tokenContract.approve(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) = creamMapping.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");
|
||||
|
||||
enterMarket(cToken);
|
||||
|
||||
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;
|
||||
tokenContract.approve(cToken, _amt);
|
||||
require(ctokenContract.mint(_amt) == 0, "deposit-ctoken-failed.");
|
||||
}
|
||||
|
||||
uint _cAmt;
|
||||
|
||||
{
|
||||
uint finalBal = ctokenContract.balanceOf(address(this));
|
||||
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) = creamMapping.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) = creamMapping.getMapping(tokenId);
|
||||
(_eventName, _eventParam) = withdrawCTokenRaw(token, cToken, cTokenAmt, getId, setId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Liquidate a position.
|
||||
* @notice 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 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 shortfall) = troller.getAccountLiquidity(borrower);
|
||||
require(shortfall != 0, "account-cannot-be-liquidated");
|
||||
_amt = _amt == uint(-1) ? cTokenContract.borrowBalanceCurrent(borrower) : _amt;
|
||||
}
|
||||
|
||||
if (tokenToPay == ethAddr) {
|
||||
require(address(this).balance >= _amt, "not-enough-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.
|
||||
*/
|
||||
function liquidate(
|
||||
address borrower,
|
||||
string calldata tokenIdToPay,
|
||||
string calldata tokenIdInReturn,
|
||||
uint256 amt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
) external payable returns (string memory _eventName, bytes memory _eventParam) {
|
||||
(address tokenToPay, address cTokenToPay) = creamMapping.getMapping(tokenIdToPay);
|
||||
(address tokenInReturn, address cTokenColl) = creamMapping.getMapping(tokenIdInReturn);
|
||||
|
||||
(_eventName, _eventParam) = liquidateRaw(
|
||||
borrower,
|
||||
tokenToPay,
|
||||
cTokenToPay,
|
||||
tokenInReturn,
|
||||
cTokenColl,
|
||||
amt,
|
||||
getId,
|
||||
setId
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
contract ConnectV2Cream is CreamResolver {
|
||||
string public name = "Cream-v1";
|
||||
}
|
137
contracts/mapping/cream.sol
Normal file
137
contracts/mapping/cream.sol
Normal file
|
@ -0,0 +1,137 @@
|
|||
pragma solidity ^0.7.0;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
interface IndexInterface {
|
||||
function master() external view returns (address);
|
||||
}
|
||||
|
||||
interface ConnectorsInterface {
|
||||
function chief(address) external view returns (bool);
|
||||
}
|
||||
|
||||
interface CTokenInterface {
|
||||
function isCToken() external view returns (bool);
|
||||
function underlying() external view returns (address);
|
||||
}
|
||||
|
||||
abstract contract Helpers {
|
||||
|
||||
struct TokenMap {
|
||||
address ctoken;
|
||||
address token;
|
||||
}
|
||||
|
||||
event LogCTokenAdded(string indexed name, address indexed token, address indexed ctoken);
|
||||
event LogCTokenUpdated(string indexed name, address indexed token, address indexed ctoken);
|
||||
|
||||
ConnectorsInterface public immutable connectors;
|
||||
|
||||
// InstaIndex Address.
|
||||
IndexInterface public constant instaIndex = IndexInterface(0x2971AdFa57b20E5a416aE5a708A8655A9c74f723);
|
||||
|
||||
address public constant ethAddr = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
|
||||
|
||||
mapping (string => TokenMap) public cTokenMapping;
|
||||
|
||||
modifier isChief {
|
||||
require(msg.sender == instaIndex.master() || connectors.chief(msg.sender), "not-an-chief");
|
||||
_;
|
||||
}
|
||||
|
||||
constructor(address _connectors) {
|
||||
connectors = ConnectorsInterface(_connectors);
|
||||
}
|
||||
|
||||
function _addCtokenMapping(
|
||||
string[] memory _names,
|
||||
address[] memory _tokens,
|
||||
address[] memory _ctokens
|
||||
) internal {
|
||||
require(_names.length == _tokens.length, "addCtokenMapping: not same length");
|
||||
require(_names.length == _ctokens.length, "addCtokenMapping: not same length");
|
||||
|
||||
for (uint i = 0; i < _ctokens.length; i++) {
|
||||
TokenMap memory _data = cTokenMapping[_names[i]];
|
||||
|
||||
require(_data.ctoken == address(0), "addCtokenMapping: mapping added already");
|
||||
require(_data.token == address(0), "addCtokenMapping: mapping added already");
|
||||
|
||||
require(_tokens[i] != address(0), "addCtokenMapping: _tokens address not vaild");
|
||||
require(_ctokens[i] != address(0), "addCtokenMapping: _ctokens address not vaild");
|
||||
|
||||
CTokenInterface _ctokenContract = CTokenInterface(_ctokens[i]);
|
||||
|
||||
require(_ctokenContract.isCToken(), "addCtokenMapping: not a cToken");
|
||||
if (_tokens[i] != ethAddr) {
|
||||
require(_ctokenContract.underlying() == _tokens[i], "addCtokenMapping: mapping mismatch");
|
||||
}
|
||||
|
||||
cTokenMapping[_names[i]] = TokenMap(
|
||||
_ctokens[i],
|
||||
_tokens[i]
|
||||
);
|
||||
emit LogCTokenAdded(_names[i], _tokens[i], _ctokens[i]);
|
||||
}
|
||||
}
|
||||
|
||||
function updateCtokenMapping(
|
||||
string[] calldata _names,
|
||||
address[] memory _tokens,
|
||||
address[] calldata _ctokens
|
||||
) external {
|
||||
require(msg.sender == instaIndex.master(), "not-master");
|
||||
|
||||
require(_names.length == _tokens.length, "updateCtokenMapping: not same length");
|
||||
require(_names.length == _ctokens.length, "updateCtokenMapping: not same length");
|
||||
|
||||
for (uint i = 0; i < _ctokens.length; i++) {
|
||||
TokenMap memory _data = cTokenMapping[_names[i]];
|
||||
|
||||
require(_data.ctoken != address(0), "updateCtokenMapping: mapping does not exist");
|
||||
require(_data.token != address(0), "updateCtokenMapping: mapping does not exist");
|
||||
|
||||
require(_tokens[i] != address(0), "updateCtokenMapping: _tokens address not vaild");
|
||||
require(_ctokens[i] != address(0), "updateCtokenMapping: _ctokens address not vaild");
|
||||
|
||||
CTokenInterface _ctokenContract = CTokenInterface(_ctokens[i]);
|
||||
|
||||
require(_ctokenContract.isCToken(), "updateCtokenMapping: not a cToken");
|
||||
if (_tokens[i] != ethAddr) {
|
||||
require(_ctokenContract.underlying() == _tokens[i], "addCtokenMapping: mapping mismatch");
|
||||
}
|
||||
|
||||
cTokenMapping[_names[i]] = TokenMap(
|
||||
_ctokens[i],
|
||||
_tokens[i]
|
||||
);
|
||||
emit LogCTokenUpdated(_names[i], _tokens[i], _ctokens[i]);
|
||||
}
|
||||
}
|
||||
|
||||
function addCtokenMapping(
|
||||
string[] memory _names,
|
||||
address[] memory _tokens,
|
||||
address[] memory _ctokens
|
||||
) external isChief {
|
||||
_addCtokenMapping(_names, _tokens, _ctokens);
|
||||
}
|
||||
|
||||
function getMapping(string memory _tokenId) external view returns (address, address) {
|
||||
TokenMap memory _data = cTokenMapping[_tokenId];
|
||||
return (_data.token, _data.ctoken);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
contract InstaCreamMapping is Helpers {
|
||||
string constant public name = "Cream-Mapping-v1.1";
|
||||
|
||||
constructor(
|
||||
address _connectors,
|
||||
string[] memory _ctokenNames,
|
||||
address[] memory _tokens,
|
||||
address[] memory _ctokens
|
||||
) Helpers(_connectors) {
|
||||
_addCtokenMapping(_ctokenNames, _tokens, _ctokens);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user