mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
Add paraswap connector
This commit is contained in:
parent
53e92dbac3
commit
fd918e0381
|
@ -1,10 +0,0 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
contract Events {
|
||||
event LogDeposit(address indexed token, uint256 tokenAmt, uint256 getId, uint256 setId);
|
||||
event LogWithdraw(address indexed token, uint256 tokenAmt, uint256 getId, uint256 setId);
|
||||
event LogBorrow(address indexed token, uint256 tokenAmt, uint256 indexed rateMode, uint256 getId, uint256 setId);
|
||||
event LogPayback(address indexed token, uint256 tokenAmt, uint256 indexed rateMode, uint256 getId, uint256 setId);
|
||||
event LogEnableCollateral(address[] tokens);
|
||||
event LogSwapRateMode(address indexed token, uint256 rateMode);
|
||||
}
|
|
@ -1,48 +0,0 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
import { DSMath } from "../../../common/math.sol";
|
||||
import { Basic } from "../../../common/basic.sol";
|
||||
import { AaveLendingPoolProviderInterface, AaveDataProviderInterface } from "./interface.sol";
|
||||
|
||||
abstract contract Helpers is DSMath, Basic {
|
||||
/**
|
||||
* @dev Aave Lending Pool Provider
|
||||
*/
|
||||
AaveLendingPoolProviderInterface constant internal aaveProvider = AaveLendingPoolProviderInterface(0xAcc030EF66f9dFEAE9CbB0cd1B25654b82cFA8d5);
|
||||
|
||||
/**
|
||||
* @dev Aave Protocol Data Provider
|
||||
*/
|
||||
AaveDataProviderInterface constant internal aaveData = AaveDataProviderInterface(0xc443AD9DDE3cecfB9dfC5736578f447aFE3590ba);
|
||||
|
||||
/**
|
||||
* @dev Aave Referral Code
|
||||
*/
|
||||
uint16 constant internal referralCode = 3228;
|
||||
|
||||
/**
|
||||
* @dev Checks if collateral is enabled for an asset
|
||||
* @param token token address of the asset.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
*/
|
||||
function getIsColl(address token) internal view returns (bool isCol) {
|
||||
(, , , , , , , , isCol) = aaveData.getUserReserveData(token, address(this));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Get total debt balance & fee for an asset
|
||||
* @param token token address of the debt.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param rateMode Borrow rate mode (Stable = 1, Variable = 2)
|
||||
*/
|
||||
function getPaybackBalance(address token, uint rateMode) internal view returns (uint) {
|
||||
(, uint stableDebt, uint variableDebt, , , , , , ) = aaveData.getUserReserveData(token, address(this));
|
||||
return rateMode == 1 ? stableDebt : variableDebt;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Get total collateral balance for an asset
|
||||
* @param token token address of the collateral.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
*/
|
||||
function getCollateralBalance(address token) internal view returns (uint bal) {
|
||||
(bal, , , , , , , ,) = aaveData.getUserReserveData(token, address(this));
|
||||
}
|
||||
}
|
|
@ -1,47 +0,0 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
interface AaveInterface {
|
||||
function deposit(address _asset, uint256 _amount, address _onBehalfOf, uint16 _referralCode) external;
|
||||
function withdraw(address _asset, uint256 _amount, address _to) external;
|
||||
function borrow(
|
||||
address _asset,
|
||||
uint256 _amount,
|
||||
uint256 _interestRateMode,
|
||||
uint16 _referralCode,
|
||||
address _onBehalfOf
|
||||
) external;
|
||||
function repay(address _asset, uint256 _amount, uint256 _rateMode, address _onBehalfOf) external;
|
||||
function setUserUseReserveAsCollateral(address _asset, bool _useAsCollateral) external;
|
||||
function swapBorrowRateMode(address _asset, uint256 _rateMode) external;
|
||||
}
|
||||
|
||||
interface AaveLendingPoolProviderInterface {
|
||||
function getLendingPool() external view returns (address);
|
||||
}
|
||||
|
||||
interface AaveDataProviderInterface {
|
||||
function getReserveTokensAddresses(address _asset) external view returns (
|
||||
address aTokenAddress,
|
||||
address stableDebtTokenAddress,
|
||||
address variableDebtTokenAddress
|
||||
);
|
||||
function getUserReserveData(address _asset, address _user) external view returns (
|
||||
uint256 currentATokenBalance,
|
||||
uint256 currentStableDebt,
|
||||
uint256 currentVariableDebt,
|
||||
uint256 principalStableDebt,
|
||||
uint256 scaledVariableDebt,
|
||||
uint256 stableBorrowRate,
|
||||
uint256 liquidityRate,
|
||||
uint40 stableRateLastUpdated,
|
||||
bool usageAsCollateralEnabled
|
||||
);
|
||||
}
|
||||
|
||||
interface AaveAddressProviderRegistryInterface {
|
||||
function getAddressesProvidersList() external view returns (address[] memory);
|
||||
}
|
||||
|
||||
interface ATokenInterface {
|
||||
function balanceOf(address _user) external view returns(uint256);
|
||||
}
|
|
@ -1,205 +0,0 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
import { TokenInterface } from "../../../common/interfaces.sol";
|
||||
import { Stores } from "../../../common/stores.sol";
|
||||
import { Helpers } from "./helpers.sol";
|
||||
import { Events } from "./events.sol";
|
||||
import { AaveInterface } from "./interface.sol";
|
||||
|
||||
abstract contract AaveResolver is Events, Helpers {
|
||||
/**
|
||||
* @dev Deposit ETH/ERC20_Token.
|
||||
* @param token token address to deposit.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param amt token amount to deposit.
|
||||
* @param getId Get token amount at this ID from `InstaMemory` Contract.
|
||||
* @param setId Set token amount at this ID in `InstaMemory` Contract.
|
||||
*/
|
||||
function deposit(
|
||||
address token,
|
||||
uint256 amt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
) external payable returns (string memory _eventName, bytes memory _eventParam) {
|
||||
uint _amt = getUint(getId, amt);
|
||||
|
||||
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
|
||||
|
||||
bool isEth = token == ethAddr;
|
||||
address _token = isEth ? wethAddr : token;
|
||||
|
||||
TokenInterface tokenContract = TokenInterface(_token);
|
||||
|
||||
if (isEth) {
|
||||
_amt = _amt == uint(-1) ? address(this).balance : _amt;
|
||||
convertEthToWeth(isEth, tokenContract, _amt);
|
||||
} else {
|
||||
_amt = _amt == uint(-1) ? tokenContract.balanceOf(address(this)) : _amt;
|
||||
}
|
||||
|
||||
tokenContract.approve(address(aave), _amt);
|
||||
|
||||
aave.deposit(_token, _amt, address(this), referralCode);
|
||||
|
||||
if (!getIsColl(_token)) {
|
||||
aave.setUserUseReserveAsCollateral(_token, true);
|
||||
}
|
||||
|
||||
setUint(setId, _amt);
|
||||
|
||||
_eventName = "LogDeposit(address,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(token, _amt, getId, setId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Withdraw ETH/ERC20_Token.
|
||||
* @param token token address to withdraw.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param amt token amount to withdraw.
|
||||
* @param getId Get token amount at this ID from `InstaMemory` Contract.
|
||||
* @param setId Set token amount at this ID in `InstaMemory` Contract.
|
||||
*/
|
||||
function withdraw(
|
||||
address token,
|
||||
uint256 amt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
) external payable returns (string memory _eventName, bytes memory _eventParam) {
|
||||
uint _amt = getUint(getId, amt);
|
||||
|
||||
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
|
||||
bool isEth = token == ethAddr;
|
||||
address _token = isEth ? wethAddr : token;
|
||||
|
||||
TokenInterface tokenContract = TokenInterface(_token);
|
||||
|
||||
uint initialBal = tokenContract.balanceOf(address(this));
|
||||
aave.withdraw(_token, _amt, address(this));
|
||||
uint finalBal = tokenContract.balanceOf(address(this));
|
||||
|
||||
_amt = sub(finalBal, initialBal);
|
||||
|
||||
convertWethToEth(isEth, tokenContract, _amt);
|
||||
|
||||
setUint(setId, _amt);
|
||||
|
||||
_eventName = "LogWithdraw(address,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(token, _amt, getId, setId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Borrow ETH/ERC20_Token.
|
||||
* @param token token address to borrow.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param amt token amount to borrow.
|
||||
* @param rateMode type of borrow debt.(For Stable: 1, Variable: 2)
|
||||
* @param getId Get token amount at this ID from `InstaMemory` Contract.
|
||||
* @param setId Set token amount at this ID in `InstaMemory` Contract.
|
||||
*/
|
||||
function borrow(
|
||||
address token,
|
||||
uint256 amt,
|
||||
uint256 rateMode,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
) external payable returns (string memory _eventName, bytes memory _eventParam) {
|
||||
uint _amt = getUint(getId, amt);
|
||||
|
||||
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
|
||||
|
||||
bool isEth = token == ethAddr;
|
||||
address _token = isEth ? wethAddr : token;
|
||||
|
||||
aave.borrow(_token, _amt, rateMode, referralCode, address(this));
|
||||
convertWethToEth(isEth, TokenInterface(_token), _amt);
|
||||
|
||||
setUint(setId, _amt);
|
||||
|
||||
_eventName = "LogBorrow(address,uint256,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(token, _amt, rateMode, getId, setId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Payback borrowed ETH/ERC20_Token.
|
||||
* @param token token address to payback.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param amt token amount to payback.
|
||||
* @param rateMode type of borrow debt.(For Stable: 1, Variable: 2)
|
||||
* @param getId Get token amount at this ID from `InstaMemory` Contract.
|
||||
* @param setId Set token amount at this ID in `InstaMemory` Contract.
|
||||
*/
|
||||
function payback(
|
||||
address token,
|
||||
uint256 amt,
|
||||
uint256 rateMode,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
) external payable returns (string memory _eventName, bytes memory _eventParam) {
|
||||
uint _amt = getUint(getId, amt);
|
||||
|
||||
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
|
||||
|
||||
bool isEth = token == ethAddr;
|
||||
address _token = isEth ? wethAddr : token;
|
||||
|
||||
TokenInterface tokenContract = TokenInterface(_token);
|
||||
|
||||
_amt = _amt == uint(-1) ? getPaybackBalance(_token, rateMode) : _amt;
|
||||
|
||||
if (isEth) convertEthToWeth(isEth, tokenContract, _amt);
|
||||
|
||||
tokenContract.approve(address(aave), _amt);
|
||||
|
||||
aave.repay(_token, _amt, rateMode, address(this));
|
||||
|
||||
setUint(setId, _amt);
|
||||
|
||||
_eventName = "LogPayback(address,uint256,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(token, _amt, rateMode, getId, setId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Enable collateral
|
||||
* @param tokens Array of tokens to enable collateral
|
||||
*/
|
||||
function enableCollateral(
|
||||
address[] calldata tokens
|
||||
) external payable returns (string memory _eventName, bytes memory _eventParam) {
|
||||
uint _length = tokens.length;
|
||||
require(_length > 0, "0-tokens-not-allowed");
|
||||
|
||||
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
|
||||
|
||||
for (uint i = 0; i < _length; i++) {
|
||||
address token = tokens[i];
|
||||
if (getCollateralBalance(token) > 0 && !getIsColl(token)) {
|
||||
aave.setUserUseReserveAsCollateral(token, true);
|
||||
}
|
||||
}
|
||||
|
||||
_eventName = "LogEnableCollateral(address[])";
|
||||
_eventParam = abi.encode(tokens);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Swap borrow rate mode
|
||||
* @notice Swaps user borrow rate mode between variable and stable
|
||||
* @param token The address of the token to swap borrow rate.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param rateMode Desired borrow rate mode. (Stable = 1, Variable = 2)
|
||||
*/
|
||||
function swapBorrowRateMode(
|
||||
address token,
|
||||
uint rateMode
|
||||
) external payable returns (string memory _eventName, bytes memory _eventParam) {
|
||||
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
|
||||
|
||||
uint currentRateMode = rateMode == 1 ? 2 : 1;
|
||||
|
||||
if (getPaybackBalance(token, currentRateMode) > 0) {
|
||||
aave.swapBorrowRateMode(token, rateMode);
|
||||
}
|
||||
|
||||
_eventName = "LogSwapRateMode(address,uint256)";
|
||||
_eventParam = abi.encode(token, rateMode);
|
||||
}
|
||||
}
|
||||
|
||||
contract ConnectV2AaveV2AMM is AaveResolver {
|
||||
string constant public name = "AaveV2-AMM-v1";
|
||||
}
|
|
@ -1,62 +0,0 @@
|
|||
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
|
||||
);
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
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);
|
||||
}
|
|
@ -1,435 +0,0 @@
|
|||
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";
|
||||
}
|
11
contracts/polygon/connectors/paraswap/events.sol
Normal file
11
contracts/polygon/connectors/paraswap/events.sol
Normal file
|
@ -0,0 +1,11 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
contract Events {
|
||||
event LogSwap(
|
||||
address buyToken,
|
||||
address sellToken,
|
||||
uint256 buyAmt,
|
||||
uint256 sellAmt,
|
||||
uint256 setId
|
||||
);
|
||||
}
|
55
contracts/polygon/connectors/paraswap/helpers.sol
Normal file
55
contracts/polygon/connectors/paraswap/helpers.sol
Normal file
|
@ -0,0 +1,55 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
import { DSMath } from "../../common/math.sol";
|
||||
import { Basic } from "../../common/basic.sol";
|
||||
import { TokenInterface } from "../../common/interfaces.sol";
|
||||
|
||||
abstract contract Helpers is DSMath, Basic {
|
||||
|
||||
struct SwapData {
|
||||
TokenInterface sellToken;
|
||||
TokenInterface buyToken;
|
||||
uint256 _sellAmt;
|
||||
uint256 _buyAmt;
|
||||
uint256 unitAmt;
|
||||
bytes callData;
|
||||
}
|
||||
|
||||
address internal constant paraswap = 0x90249ed4d69D70E709fFCd8beE2c5A566f65dADE;
|
||||
|
||||
function _swapHelper(SwapData memory swapData, uint256 wmaticAmt) internal returns (uint256 buyAmt) {
|
||||
TokenInterface buyToken = swapData.buyToken;
|
||||
(uint256 _buyDec, uint256 _sellDec) = getTokensDec(buyToken, swapData.sellToken);
|
||||
uint256 _sellAmt18 = convertTo18(_sellDec, swapData._sellAmt);
|
||||
uint256 _slippageAmt = convert18ToDec(_buyDec, wmul(swapData.unitAmt, _sellAmt18));
|
||||
|
||||
uint256 initalBal = getTokenBal(buyToken);
|
||||
|
||||
(bool success, ) = paraswap.call{value: wmaticAmt}(swapData.callData);
|
||||
if (!success) revert("paraswap-failed");
|
||||
|
||||
uint256 finalBal = getTokenBal(buyToken);
|
||||
|
||||
buyAmt = sub(finalBal, initalBal);
|
||||
|
||||
require(_slippageAmt <= buyAmt, "Too much slippage");
|
||||
}
|
||||
|
||||
function _swap(SwapData memory swapData, uint256 setId) internal returns (SwapData memory) {
|
||||
TokenInterface _sellAddr = swapData.sellToken;
|
||||
|
||||
uint256 maticAmt;
|
||||
|
||||
if (address(_sellAddr) == maticAddr) {
|
||||
maticAmt = swapData._sellAmt;
|
||||
} else {
|
||||
TokenInterface(_sellAddr).approve(paraswap, swapData._sellAmt);
|
||||
}
|
||||
|
||||
swapData._buyAmt = _swapHelper(swapData, maticAmt);
|
||||
|
||||
setUint(setId, swapData._buyAmt);
|
||||
|
||||
return swapData;
|
||||
}
|
||||
}
|
36
contracts/polygon/connectors/paraswap/main.sol
Normal file
36
contracts/polygon/connectors/paraswap/main.sol
Normal file
|
@ -0,0 +1,36 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
import { TokenInterface } from "../../common/interfaces.sol";
|
||||
import { Stores } from "../../common/stores.sol";
|
||||
import { Helpers } from "./helpers.sol";
|
||||
|
||||
abstract contract ParaswapResolver is Helpers {
|
||||
function swap(
|
||||
address buyAddr,
|
||||
address sellAddr,
|
||||
uint256 sellAmt,
|
||||
uint256 unitAmt,
|
||||
bytes calldata callData,
|
||||
uint256 setId
|
||||
) external payable returns (string memory _eventName, bytes memory _eventParam) {
|
||||
Helpers.SwapData memory swapData = Helpers.SwapData({
|
||||
buyToken: TokenInterface(buyAddr),
|
||||
sellToken: TokenInterface(sellAddr),
|
||||
unitAmt: unitAmt,
|
||||
callData: callData,
|
||||
_sellAmt: sellAmt,
|
||||
_buyAmt: 0
|
||||
});
|
||||
|
||||
swapData = _swap(swapData, setId);
|
||||
|
||||
_eventName = "LogSwap(address,address,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(
|
||||
address(swapData.buyToken),
|
||||
address(swapData.sellToken),
|
||||
swapData._buyAmt,
|
||||
swapData._sellAmt,
|
||||
setId
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user