mirror of
https://github.com/Instadapp/yield-contract.git
synced 2024-07-29 21:47:29 +00:00
Merge branch 'logics'
This commit is contained in:
commit
6ebc191d93
102
contracts/logics/exchangeRate/daiLogic.sol
Normal file
102
contracts/logics/exchangeRate/daiLogic.sol
Normal file
|
@ -0,0 +1,102 @@
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.6.8;
|
||||||
|
|
||||||
|
import { DSMath } from "../../libs/safeMath.sol";
|
||||||
|
|
||||||
|
interface CTokenInterface {
|
||||||
|
function borrowBalanceCurrent(address account) external returns (uint256);
|
||||||
|
function exchangeRateCurrent() external returns (uint256);
|
||||||
|
|
||||||
|
function balanceOf(address owner) external view returns (uint256);
|
||||||
|
|
||||||
|
function underlying() external view returns (address);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TokenInterface {
|
||||||
|
function balanceOf(address owner) external view returns (uint256);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface CompTroller {
|
||||||
|
function getAllMarkets() external view returns (address[] memory);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ICurve {
|
||||||
|
function get_virtual_price() external view returns (uint256 out);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
interface PriceFeedInterface {
|
||||||
|
function getPrices(address[] memory tokens) external view returns (uint256[] memory pricesInETH);
|
||||||
|
function getPrice(address token) external view returns (uint256 priceInETH);
|
||||||
|
function getEthPrice() external view returns (uint256 ethPriceUSD);
|
||||||
|
}
|
||||||
|
|
||||||
|
contract DaiRateLogic is DSMath {
|
||||||
|
address public immutable poolToken;
|
||||||
|
address public immutable dsa;
|
||||||
|
|
||||||
|
address public constant ethAddr = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
|
||||||
|
address public constant daiAddr = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
|
||||||
|
|
||||||
|
address public constant PriceFeedAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88);
|
||||||
|
|
||||||
|
address public constant compTrollerAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88);
|
||||||
|
address public constant cethAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88);
|
||||||
|
|
||||||
|
address public constant curve3poolAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88);
|
||||||
|
address public constant curve3poolTokenAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88);
|
||||||
|
|
||||||
|
function getCompoundNetAssetsInEth(address _dsa) private returns (uint256 _netBal) {
|
||||||
|
uint totalSupplyInETH;
|
||||||
|
uint totalBorrowInETH;
|
||||||
|
address[] memory allMarkets = CompTroller(compTrollerAddr).getAllMarkets();
|
||||||
|
PriceFeedInterface priceFeedContract = PriceFeedInterface(PriceFeedAddr);
|
||||||
|
|
||||||
|
for (uint i = 0; i < allMarkets.length; i++) {
|
||||||
|
CTokenInterface ctoken = CTokenInterface(allMarkets[i]);
|
||||||
|
uint tokenPriceInETH = address(ctoken) == cethAddr ? 10 ** 18 : priceFeedContract.getPrice(ctoken.underlying());
|
||||||
|
uint supply = wmul(ctoken.balanceOf(_dsa), ctoken.exchangeRateCurrent());
|
||||||
|
uint supplyInETH = wmul(supply, tokenPriceInETH);
|
||||||
|
|
||||||
|
uint borrow = ctoken.borrowBalanceCurrent(_dsa);
|
||||||
|
uint borrowInETH = wmul(borrow, tokenPriceInETH);
|
||||||
|
|
||||||
|
totalSupplyInETH += add(totalSupplyInETH, supplyInETH);
|
||||||
|
totalBorrowInETH = add(totalBorrowInETH, borrowInETH);
|
||||||
|
}
|
||||||
|
_netBal = sub(totalSupplyInETH, totalBorrowInETH);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCurveNetAssetsInEth(address _dsa) private view returns (uint256 _netBal) {
|
||||||
|
PriceFeedInterface priceFeedContract = PriceFeedInterface(PriceFeedAddr);
|
||||||
|
uint ethPriceUSD = priceFeedContract.getEthPrice();
|
||||||
|
uint virtualPrice = ICurve(curve3poolAddr).get_virtual_price();
|
||||||
|
uint curveTokenBal = TokenInterface(curve3poolTokenAddr).balanceOf(_dsa);
|
||||||
|
uint amtInUSD = wmul(curveTokenBal, virtualPrice);
|
||||||
|
uint amtInETH = wdiv(amtInUSD, ethPriceUSD);
|
||||||
|
_netBal = add(_netBal, amtInETH);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getNetDsaAssetsInEth(address _dsa) private returns (uint256 _netBal) {
|
||||||
|
_netBal += getCompoundNetAssetsInEth(_dsa);
|
||||||
|
_netBal += getCurveNetAssetsInEth(_dsa);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTotalToken() public returns (uint256 _daiBal) {
|
||||||
|
address _dsa = 0x0000000000000000000000000000000000000000;
|
||||||
|
PriceFeedInterface priceFeedContract = PriceFeedInterface(PriceFeedAddr);
|
||||||
|
uint daiPriceInETH = priceFeedContract.getPrice(daiAddr);
|
||||||
|
|
||||||
|
TokenInterface daiToken = TokenInterface(daiAddr);
|
||||||
|
_daiBal = daiToken.balanceOf(poolToken);
|
||||||
|
_daiBal += daiToken.balanceOf(_dsa);
|
||||||
|
|
||||||
|
uint balInEth = getNetDsaAssetsInEth(_dsa);
|
||||||
|
_daiBal = wdiv(balInEth, daiPriceInETH);
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor (address daiPool, address _dsa) public {
|
||||||
|
poolToken = address(daiPool);
|
||||||
|
dsa = _dsa;
|
||||||
|
}
|
||||||
|
}
|
135
contracts/logics/exchangeRate/ethLogic.sol
Normal file
135
contracts/logics/exchangeRate/ethLogic.sol
Normal file
|
@ -0,0 +1,135 @@
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.6.8;
|
||||||
|
|
||||||
|
import { DSMath } from "../../libs/safeMath.sol";
|
||||||
|
|
||||||
|
interface CTokenInterface {
|
||||||
|
function borrowBalanceCurrent(address account) external returns (uint256);
|
||||||
|
function exchangeRateCurrent() external returns (uint256);
|
||||||
|
|
||||||
|
function balanceOf(address owner) external view returns (uint256);
|
||||||
|
|
||||||
|
function underlying() external view returns (address);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface CompTroller {
|
||||||
|
function getAllMarkets() external view returns (address[] memory);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ICurve {
|
||||||
|
function get_virtual_price() external view returns (uint256 out);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TokenInterface {
|
||||||
|
function balanceOf(address owner) external view returns (uint256);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface PriceFeedInterface {
|
||||||
|
function getPrices(address[] memory tokens) external view returns (uint256[] memory pricesInETH);
|
||||||
|
function getPrice(address token) external view returns (uint256 priceInETH);
|
||||||
|
function getEthPrice() external view returns (uint256 ethPriceUSD);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ManagerLike {
|
||||||
|
function ilks(uint) external view returns (bytes32);
|
||||||
|
function owns(uint) external view returns (address);
|
||||||
|
function urns(uint) external view returns (address);
|
||||||
|
function vat() external view returns (address);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface VatLike {
|
||||||
|
function ilks(bytes32) external view returns (uint, uint, uint, uint, uint);
|
||||||
|
function dai(address) external view returns (uint);
|
||||||
|
function urns(bytes32, address) external view returns (uint, uint);
|
||||||
|
function gem(bytes32, address) external view returns (uint);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
contract EthRateLogic is DSMath {
|
||||||
|
address public immutable poolToken;
|
||||||
|
address public immutable dsa;
|
||||||
|
|
||||||
|
uint public immutable vaultId;
|
||||||
|
address public immutable vaultUrn;
|
||||||
|
|
||||||
|
address public constant ethAddr = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
|
||||||
|
address public constant daiAddr = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
|
||||||
|
|
||||||
|
address public constant PriceFeedAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88);
|
||||||
|
|
||||||
|
address public constant compTrollerAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88);
|
||||||
|
address public constant cethAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88);
|
||||||
|
|
||||||
|
address public constant curve3poolAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88);
|
||||||
|
address public constant curve3poolTokenAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88);
|
||||||
|
|
||||||
|
address public constant managerAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88);
|
||||||
|
bytes32 public constant ethIlk = bytes32(0);
|
||||||
|
|
||||||
|
|
||||||
|
function getMakerNetAssetsInEth() private view returns (uint256 _netBal) {
|
||||||
|
ManagerLike managerContract = ManagerLike(managerAddr);
|
||||||
|
VatLike vatContract = VatLike(managerContract.vat());
|
||||||
|
uint daiPriceInETH = PriceFeedInterface(PriceFeedAddr).getPrice(daiAddr);
|
||||||
|
|
||||||
|
(uint coll, uint art) = vatContract.urns(ethIlk, vaultUrn);
|
||||||
|
(,uint rate,,,) = vatContract.ilks(ethIlk);
|
||||||
|
uint debt = rmul(art,rate);
|
||||||
|
uint debtInEth = wmul(debt, daiPriceInETH);
|
||||||
|
return sub(coll, debtInEth);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCompoundNetAssetsInEth(address _dsa) private returns (uint256 _netBal) {
|
||||||
|
uint totalSupplyInETH;
|
||||||
|
uint totalBorrowInETH;
|
||||||
|
address[] memory allMarkets = CompTroller(compTrollerAddr).getAllMarkets();
|
||||||
|
PriceFeedInterface priceFeedContract = PriceFeedInterface(PriceFeedAddr);
|
||||||
|
|
||||||
|
for (uint i = 0; i < allMarkets.length; i++) {
|
||||||
|
CTokenInterface ctoken = CTokenInterface(allMarkets[i]);
|
||||||
|
uint tokenPriceInETH = address(ctoken) == cethAddr ? 10 ** 18 : priceFeedContract.getPrice(ctoken.underlying());
|
||||||
|
uint supply = wmul(ctoken.balanceOf(_dsa), ctoken.exchangeRateCurrent());
|
||||||
|
uint supplyInETH = wmul(supply, tokenPriceInETH);
|
||||||
|
|
||||||
|
uint borrow = ctoken.borrowBalanceCurrent(_dsa);
|
||||||
|
uint borrowInETH = wmul(borrow, tokenPriceInETH);
|
||||||
|
|
||||||
|
totalSupplyInETH += add(totalSupplyInETH, supplyInETH);
|
||||||
|
totalBorrowInETH = add(totalBorrowInETH, borrowInETH);
|
||||||
|
}
|
||||||
|
_netBal = sub(totalSupplyInETH, totalBorrowInETH);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCurveNetAssetsInEth(address _dsa) private view returns (uint256 _netBal) {
|
||||||
|
PriceFeedInterface priceFeedContract = PriceFeedInterface(PriceFeedAddr);
|
||||||
|
uint ethPriceUSD = priceFeedContract.getEthPrice();
|
||||||
|
uint virtualPrice = ICurve(curve3poolAddr).get_virtual_price();
|
||||||
|
uint curveTokenBal = TokenInterface(curve3poolTokenAddr).balanceOf(_dsa);
|
||||||
|
uint amtInUSD = wmul(curveTokenBal, virtualPrice);
|
||||||
|
uint amtInETH = wdiv(amtInUSD, ethPriceUSD);
|
||||||
|
_netBal = add(_netBal, amtInETH);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getNetDsaAssets(address _dsa) private returns (uint256 _netBal) {
|
||||||
|
_netBal = _dsa.balance;
|
||||||
|
_netBal += getCompoundNetAssetsInEth(_dsa);
|
||||||
|
_netBal += getMakerNetAssetsInEth();
|
||||||
|
_netBal += getCurveNetAssetsInEth(_dsa);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTotalToken() public returns (uint256) {
|
||||||
|
address _dsa = 0x0000000000000000000000000000000000000000;
|
||||||
|
uint256 bal = poolToken.balance;
|
||||||
|
bal += getNetDsaAssets(_dsa);
|
||||||
|
return bal;
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor (address ethPool, address _dsa, uint _vaultId) public {
|
||||||
|
poolToken = address(ethPool);
|
||||||
|
vaultId = _vaultId;
|
||||||
|
dsa = _dsa;
|
||||||
|
vaultUrn = ManagerLike(managerAddr).urns(_vaultId);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
147
contracts/logics/exchangeRate/priceFeed.sol
Normal file
147
contracts/logics/exchangeRate/priceFeed.sol
Normal file
|
@ -0,0 +1,147 @@
|
||||||
|
pragma solidity ^0.6.0;
|
||||||
|
pragma experimental ABIEncoderV2;
|
||||||
|
|
||||||
|
interface ChainLinkInterface {
|
||||||
|
function latestAnswer() external view returns (int256);
|
||||||
|
function decimals() external view returns (uint256);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface IndexInterface {
|
||||||
|
function master() external view returns (address);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TokenInterface {
|
||||||
|
function decimals() external view returns (uint);
|
||||||
|
}
|
||||||
|
|
||||||
|
import { DSMath } from "../../libs/safeMath.sol";
|
||||||
|
|
||||||
|
|
||||||
|
contract Basic is DSMath {
|
||||||
|
address public constant instaIndex = 0x2971AdFa57b20E5a416aE5a708A8655A9c74f723;
|
||||||
|
address public constant ethAddr = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
|
||||||
|
|
||||||
|
modifier isChief {
|
||||||
|
require(
|
||||||
|
IndexInterface(instaIndex).master() == msg.sender, "not-Chief");
|
||||||
|
_;
|
||||||
|
}
|
||||||
|
|
||||||
|
event LogAddChainLinkMapping(
|
||||||
|
address tokenSymbol,
|
||||||
|
address chainlinkFeed
|
||||||
|
);
|
||||||
|
|
||||||
|
event LogRemoveChainLinkMapping(
|
||||||
|
address tokenSymbol,
|
||||||
|
address chainlinkFeed
|
||||||
|
);
|
||||||
|
|
||||||
|
struct FeedData {
|
||||||
|
address feedAddress;
|
||||||
|
uint multiplier;
|
||||||
|
}
|
||||||
|
|
||||||
|
mapping (address => FeedData) public chainLinkMapping;
|
||||||
|
|
||||||
|
|
||||||
|
function convertPrice(uint price, uint multiplier) internal pure returns (uint) {
|
||||||
|
return price * (10 ** multiplier);
|
||||||
|
}
|
||||||
|
|
||||||
|
function _addChainLinkMapping(
|
||||||
|
address token,
|
||||||
|
address chainlinkFeed
|
||||||
|
) internal {
|
||||||
|
require(token != address(0), "token-not-vaild");
|
||||||
|
require(chainlinkFeed != address(0), "chainlinkFeed-not-vaild");
|
||||||
|
require(chainLinkMapping[token].feedAddress == address(0), "chainlinkFeed-already-added");
|
||||||
|
uint tokenDec = token == ethAddr ? 18 : TokenInterface(token).decimals();
|
||||||
|
uint feedDec = ChainLinkInterface(chainlinkFeed).decimals();
|
||||||
|
|
||||||
|
chainLinkMapping[token].feedAddress = chainlinkFeed;
|
||||||
|
chainLinkMapping[token].multiplier = sub(36, add(tokenDec, feedDec));
|
||||||
|
emit LogAddChainLinkMapping(token, chainlinkFeed);
|
||||||
|
}
|
||||||
|
|
||||||
|
function _removeChainLinkMapping(address token) internal {
|
||||||
|
require(token != address(0), "token-not-vaild");
|
||||||
|
require(chainLinkMapping[token].feedAddress != address(0), "chainlinkFeed-not-added-yet");
|
||||||
|
|
||||||
|
emit LogRemoveChainLinkMapping(token, chainLinkMapping[token].feedAddress);
|
||||||
|
delete chainLinkMapping[token];
|
||||||
|
}
|
||||||
|
|
||||||
|
function addChainLinkMapping(
|
||||||
|
address[] memory tokens,
|
||||||
|
address[] memory chainlinkFeeds
|
||||||
|
) public isChief {
|
||||||
|
require(tokens.length == chainlinkFeeds.length, "lenght-not-same");
|
||||||
|
for (uint i = 0; i < tokens.length; i++) {
|
||||||
|
_addChainLinkMapping(tokens[i], chainlinkFeeds[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeChainLinkMapping(address[] memory tokens) public isChief {
|
||||||
|
for (uint i = 0; i < tokens.length; i++) {
|
||||||
|
_removeChainLinkMapping(tokens[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
contract Resolver is Basic {
|
||||||
|
function getPrices(address[] memory tokens)
|
||||||
|
public
|
||||||
|
view
|
||||||
|
returns (
|
||||||
|
uint[] memory tokensPriceInETH
|
||||||
|
) {
|
||||||
|
tokensPriceInETH = new uint[](tokens.length);
|
||||||
|
for (uint i = 0; i < tokens.length; i++) {
|
||||||
|
if (tokens[i] != ethAddr) {
|
||||||
|
FeedData memory feedData = chainLinkMapping[tokens[i]];
|
||||||
|
ChainLinkInterface feedContract = ChainLinkInterface(feedData.feedAddress);
|
||||||
|
require(address(feedContract) != address(0), "price-not-found");
|
||||||
|
tokensPriceInETH[i] = convertPrice(uint(feedContract.latestAnswer()), feedData.multiplier);
|
||||||
|
} else {
|
||||||
|
tokensPriceInETH[i] = 10 ** 18;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPrice(address token)
|
||||||
|
public
|
||||||
|
view
|
||||||
|
returns (
|
||||||
|
uint tokenPriceInETH
|
||||||
|
) {
|
||||||
|
if (token != ethAddr) {
|
||||||
|
FeedData memory tokenFeedData = chainLinkMapping[token];
|
||||||
|
ChainLinkInterface tokenFeed = ChainLinkInterface(tokenFeedData.feedAddress);
|
||||||
|
require(address(tokenFeed) != address(0), "price-not-found");
|
||||||
|
tokenPriceInETH = convertPrice(uint(tokenFeed.latestAnswer()), tokenFeedData.multiplier);
|
||||||
|
} else {
|
||||||
|
tokenPriceInETH = 10 ** 18;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getEthPrice()
|
||||||
|
public
|
||||||
|
view
|
||||||
|
returns (
|
||||||
|
uint ethPriceInUsd
|
||||||
|
) {
|
||||||
|
FeedData memory ethFeedData = chainLinkMapping[ethAddr];
|
||||||
|
ChainLinkInterface ethFeed = ChainLinkInterface(ethFeedData.feedAddress);
|
||||||
|
ethPriceInUsd = convertPrice(uint(ethFeed.latestAnswer()), ethFeedData.multiplier);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
contract ChainLinkPriceFeed is Resolver {
|
||||||
|
constructor (address[] memory tokens, address[] memory chainlinkFeeds) public {
|
||||||
|
require(tokens.length == chainlinkFeeds.length, "Lenght-not-same");
|
||||||
|
for (uint i = 0; i < tokens.length; i++) {
|
||||||
|
_addChainLinkMapping(tokens[i], chainlinkFeeds[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
144
contracts/logics/exchangeRate/usdcLogic.sol
Normal file
144
contracts/logics/exchangeRate/usdcLogic.sol
Normal file
|
@ -0,0 +1,144 @@
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.6.8;
|
||||||
|
|
||||||
|
import { DSMath } from "../../libs/safeMath.sol";
|
||||||
|
|
||||||
|
interface CTokenInterface {
|
||||||
|
function borrowBalanceCurrent(address account) external returns (uint256);
|
||||||
|
function exchangeRateCurrent() external returns (uint256);
|
||||||
|
|
||||||
|
function balanceOf(address owner) external view returns (uint256);
|
||||||
|
|
||||||
|
function underlying() external view returns (address);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface CompTroller {
|
||||||
|
function getAllMarkets() external view returns (address[] memory);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ICurve {
|
||||||
|
function get_virtual_price() external view returns (uint256 out);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TokenInterface {
|
||||||
|
function balanceOf(address owner) external view returns (uint256);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface PriceFeedInterface {
|
||||||
|
function getPrices(address[] memory tokens) external view returns (uint256[] memory pricesInETH);
|
||||||
|
function getPrice(address token) external view returns (uint256 priceInETH);
|
||||||
|
function getEthPrice() external view returns (uint256 ethPriceUSD);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ManagerLike {
|
||||||
|
function ilks(uint) external view returns (bytes32);
|
||||||
|
function owns(uint) external view returns (address);
|
||||||
|
function urns(uint) external view returns (address);
|
||||||
|
function vat() external view returns (address);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface VatLike {
|
||||||
|
function ilks(bytes32) external view returns (uint, uint, uint, uint, uint);
|
||||||
|
function dai(address) external view returns (uint);
|
||||||
|
function urns(bytes32, address) external view returns (uint, uint);
|
||||||
|
function gem(bytes32, address) external view returns (uint);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
contract UsdcRateLogic is DSMath {
|
||||||
|
address public immutable poolToken;
|
||||||
|
address public immutable dsa;
|
||||||
|
|
||||||
|
uint public immutable vaultId;
|
||||||
|
address public immutable vaultUrn;
|
||||||
|
|
||||||
|
address public constant ethAddr = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
|
||||||
|
address public constant daiAddr = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
|
||||||
|
address public constant usdcAddr = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
|
||||||
|
|
||||||
|
address public constant PriceFeedAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88);
|
||||||
|
|
||||||
|
address public constant compTrollerAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88);
|
||||||
|
address public constant cethAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88);
|
||||||
|
|
||||||
|
address public constant curve3poolAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88);
|
||||||
|
address public constant curve3poolTokenAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88);
|
||||||
|
|
||||||
|
address public constant managerAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88);
|
||||||
|
bytes32 public constant usdcAIlk = bytes32(0);
|
||||||
|
|
||||||
|
|
||||||
|
function getMakerNetAssetsInEth() private view returns (uint256 _netBal) {
|
||||||
|
ManagerLike managerContract = ManagerLike(managerAddr);
|
||||||
|
VatLike vatContract = VatLike(managerContract.vat());
|
||||||
|
uint daiPriceInETH = PriceFeedInterface(PriceFeedAddr).getPrice(daiAddr);
|
||||||
|
uint usdcPriceInETH = PriceFeedInterface(PriceFeedAddr).getPrice(usdcAddr);
|
||||||
|
|
||||||
|
(uint coll, uint art) = vatContract.urns(usdcAIlk, vaultUrn);
|
||||||
|
(,uint rate,,,) = vatContract.ilks(usdcAIlk);
|
||||||
|
uint debt = rmul(art, rate);
|
||||||
|
|
||||||
|
uint debtInEth = wmul(debt, daiPriceInETH);
|
||||||
|
uint collInEth = wmul(coll, usdcPriceInETH);
|
||||||
|
return sub(collInEth, debtInEth);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCompoundNetAssetsInEth(address _dsa) private returns (uint256 _netBalInEth) {
|
||||||
|
uint totalSupplyInETH;
|
||||||
|
uint totalBorrowInETH;
|
||||||
|
address[] memory allMarkets = CompTroller(compTrollerAddr).getAllMarkets();
|
||||||
|
PriceFeedInterface priceFeedContract = PriceFeedInterface(PriceFeedAddr);
|
||||||
|
|
||||||
|
for (uint i = 0; i < allMarkets.length; i++) {
|
||||||
|
CTokenInterface ctoken = CTokenInterface(allMarkets[i]);
|
||||||
|
uint tokenPriceInETH = address(ctoken) == cethAddr ? 10 ** 18 : priceFeedContract.getPrice(ctoken.underlying());
|
||||||
|
uint supply = wmul(ctoken.balanceOf(_dsa), ctoken.exchangeRateCurrent());
|
||||||
|
uint supplyInETH = wmul(supply, tokenPriceInETH);
|
||||||
|
|
||||||
|
uint borrow = ctoken.borrowBalanceCurrent(_dsa);
|
||||||
|
uint borrowInETH = wmul(borrow, tokenPriceInETH);
|
||||||
|
|
||||||
|
totalSupplyInETH += add(totalSupplyInETH, supplyInETH);
|
||||||
|
totalBorrowInETH = add(totalBorrowInETH, borrowInETH);
|
||||||
|
}
|
||||||
|
_netBalInEth = sub(totalSupplyInETH, totalBorrowInETH);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCurveNetAssetsInEth(address _dsa) private view returns (uint256 _netBalInEth) {
|
||||||
|
PriceFeedInterface priceFeedContract = PriceFeedInterface(PriceFeedAddr);
|
||||||
|
uint ethPriceUSD = priceFeedContract.getEthPrice();
|
||||||
|
uint virtualPrice = ICurve(curve3poolAddr).get_virtual_price();
|
||||||
|
uint curveTokenBal = TokenInterface(curve3poolTokenAddr).balanceOf(_dsa);
|
||||||
|
uint amtInUSD = wmul(curveTokenBal, virtualPrice);
|
||||||
|
uint amtInETH = wdiv(amtInUSD, ethPriceUSD);
|
||||||
|
_netBalInEth = add(_netBalInEth, amtInETH);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getNetDsaAssetsInEth(address _dsa) private returns (uint256 _netBalInEth) {
|
||||||
|
_netBalInEth += getCompoundNetAssetsInEth(_dsa);
|
||||||
|
_netBalInEth += getMakerNetAssetsInEth();
|
||||||
|
_netBalInEth += getCurveNetAssetsInEth(_dsa);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTotalToken() public returns (uint256 usdcBal) {
|
||||||
|
address _dsa = 0x0000000000000000000000000000000000000000;
|
||||||
|
PriceFeedInterface priceFeedContract = PriceFeedInterface(PriceFeedAddr);
|
||||||
|
uint usdcPriceInETH = priceFeedContract.getPrice(usdcAddr);
|
||||||
|
|
||||||
|
TokenInterface usdcToken = TokenInterface(usdcAddr);
|
||||||
|
usdcBal = usdcToken.balanceOf(_dsa);
|
||||||
|
usdcBal += TokenInterface(usdcAddr).balanceOf(poolToken);
|
||||||
|
|
||||||
|
uint balInEth = getNetDsaAssetsInEth(_dsa);
|
||||||
|
usdcBal += wdiv(balInEth, usdcPriceInETH);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
constructor (address usdcPool, address _dsa, uint _vaultId) public {
|
||||||
|
poolToken = address(usdcPool);
|
||||||
|
vaultId = _vaultId;
|
||||||
|
dsa = _dsa;
|
||||||
|
vaultUrn = ManagerLike(managerAddr).urns(_vaultId);
|
||||||
|
}
|
||||||
|
}
|
55
contracts/logics/settle/eth/basic.sol
Normal file
55
contracts/logics/settle/eth/basic.sol
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.6.8;
|
||||||
|
pragma experimental ABIEncoderV2;
|
||||||
|
|
||||||
|
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
|
||||||
|
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
||||||
|
|
||||||
|
import { DSMath } from "../../../libs/safeMath.sol";
|
||||||
|
|
||||||
|
interface DSAInterface {
|
||||||
|
function cast(address[] calldata _targets, bytes[] calldata _data, address _origin) external payable;
|
||||||
|
function isAuth(address) external returns(bool);
|
||||||
|
}
|
||||||
|
|
||||||
|
contract LogicOne {
|
||||||
|
|
||||||
|
using SafeERC20 for IERC20;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Return ethereum address
|
||||||
|
*/
|
||||||
|
function getEthAddr() internal pure returns (address) {
|
||||||
|
return 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; // ETH Address
|
||||||
|
}
|
||||||
|
|
||||||
|
function getOriginAddress() private pure returns(address) {
|
||||||
|
return 0xB7fA44c2E964B6EB24893f7082Ecc08c8d0c0F87; // DSA address
|
||||||
|
}
|
||||||
|
|
||||||
|
function deploy(address _dsa, address _token, uint amt) public {
|
||||||
|
require(DSAInterface(_dsa).isAuth(address(this)), "pool-not-auth-in-dsa");
|
||||||
|
if (_token == getEthAddr()) {
|
||||||
|
uint _bal = address(this).balance;
|
||||||
|
amt = amt > _bal ? _bal : amt;
|
||||||
|
payable(_dsa).transfer(amt);
|
||||||
|
} else {
|
||||||
|
IERC20 token = IERC20(_token);
|
||||||
|
uint _bal = token.balanceOf(address(this));
|
||||||
|
amt = amt > _bal ? _bal : amt;
|
||||||
|
token.safeTransfer(_dsa, amt);
|
||||||
|
}
|
||||||
|
// emit event?
|
||||||
|
}
|
||||||
|
|
||||||
|
// withdraw assets from DSA
|
||||||
|
function redeem(address _dsa, address _token, uint amt) public {
|
||||||
|
uint _bal = IERC20(_token).balanceOf(_dsa);
|
||||||
|
amt = amt > _bal ? _bal : amt;
|
||||||
|
address[] memory _targets = new address[](1);
|
||||||
|
_targets[0] = address(0); // Check9898 - address of basic connector
|
||||||
|
bytes[] memory _data = new bytes[](1);
|
||||||
|
_data[0] = abi.encodeWithSignature("withdraw(address,uint256,address,uint256,uint256)", _token, amt, address(this), uint(0), uint(0));
|
||||||
|
DSAInterface(_dsa).cast(_targets, _data, getOriginAddress());
|
||||||
|
}
|
||||||
|
}
|
97
contracts/logics/settle/eth/crvMining.sol
Normal file
97
contracts/logics/settle/eth/crvMining.sol
Normal file
|
@ -0,0 +1,97 @@
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.6.8;
|
||||||
|
pragma experimental ABIEncoderV2;
|
||||||
|
|
||||||
|
import { DSMath } from "../../../libs/safeMath.sol";
|
||||||
|
|
||||||
|
interface DSAInterface {
|
||||||
|
function cast(address[] calldata _targets, bytes[] calldata _data, address _origin) external payable;
|
||||||
|
}
|
||||||
|
|
||||||
|
contract LogicOne {
|
||||||
|
|
||||||
|
function getEthAddress() private pure returns(address) {
|
||||||
|
return 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCrvAddress() private pure returns(address) {
|
||||||
|
return 0xD533a949740bb3306d119CC777fa900bA034cd52;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getOriginAddress() private pure returns(address) {
|
||||||
|
return 0xB7fA44c2E964B6EB24893f7082Ecc08c8d0c0F87;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getDsaAddress() private pure returns(address) {
|
||||||
|
return address(0); // DSA address
|
||||||
|
}
|
||||||
|
|
||||||
|
function getGuageAddress() private pure returns(address) {
|
||||||
|
return 0xAf615b36Db171fD5A369A0060b9bCB88fFf0190d;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getGuageName() private pure returns(string memory) {
|
||||||
|
return "guage-3";
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCurveConnectAddress() private pure returns(address) {
|
||||||
|
return 0x1568a9D336A7aC051DCC4bdcc4A0B09299DE5Daf;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCurveGuageConnectAddress() private pure returns(address) {
|
||||||
|
return 0xAf615b36Db171fD5A369A0060b9bCB88fFf0190d;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getUniswapConnectAddress() private pure returns(address) {
|
||||||
|
return 0x62EbfF47B2Ba3e47796efaE7C51676762dC961c0;
|
||||||
|
}
|
||||||
|
|
||||||
|
function mineCrv(address token, uint amt, uint unitAmt) external {
|
||||||
|
address[] memory _targets = new address[](2);
|
||||||
|
bytes[] memory _data = new bytes[](2);
|
||||||
|
_targets[1] = getCurveConnectAddress();
|
||||||
|
_data[1] = abi.encodeWithSignature("deposit(address,uint256,uint256,uint256,uint256)", token, amt, unitAmt, uint(0), uint(0));
|
||||||
|
_targets[2] = getCurveGuageConnectAddress();
|
||||||
|
_data[2] = abi.encodeWithSignature("deposit(string,uint256,uint256,uint256)", getGuageName(), uint(-1), uint(0), uint(0));
|
||||||
|
DSAInterface(getDsaAddress()).cast(_targets, _data, getOriginAddress());
|
||||||
|
}
|
||||||
|
|
||||||
|
function redeemCrv(address token, uint amt, uint unitAmt) external {
|
||||||
|
address[] memory _targets;
|
||||||
|
bytes[] memory _data;
|
||||||
|
if (amt == uint(-1)) {
|
||||||
|
_targets = new address[](2);
|
||||||
|
_data = new bytes[](2);
|
||||||
|
} else {
|
||||||
|
_targets = new address[](3);
|
||||||
|
_data = new bytes[](3);
|
||||||
|
}
|
||||||
|
_targets[0] = getCurveGuageConnectAddress();
|
||||||
|
_data[0] = abi.encodeWithSignature("withdraw(string,uint256,uint256,uint256,uint256,uint256)", getGuageName(), uint(-1), uint(0), uint(0), uint(0), uint(0));
|
||||||
|
_targets[1] = getCurveConnectAddress();
|
||||||
|
_data[1] = abi.encodeWithSignature("withdraw(address,uint256,uint256,uint256,uint256)", token, amt, unitAmt, uint(0), uint(0));
|
||||||
|
if (amt != uint(-1)) {
|
||||||
|
_targets[2] = getCurveGuageConnectAddress();
|
||||||
|
_data[2] = abi.encodeWithSignature("deposit(string,uint256,uint256,uint256)", getGuageName(), uint(-1), uint(0), uint(0));
|
||||||
|
}
|
||||||
|
DSAInterface(getDsaAddress()).cast(_targets, _data, getOriginAddress());
|
||||||
|
}
|
||||||
|
|
||||||
|
function claimCrv() external {
|
||||||
|
address[] memory _target = new address[](1);
|
||||||
|
bytes[] memory _data = new bytes[](1);
|
||||||
|
_target[0] = getCurveGuageConnectAddress();
|
||||||
|
_data[0] = abi.encodeWithSignature("claimReward(string,uint256,uint256)", getGuageName(), 0, 0);
|
||||||
|
DSAInterface(getDsaAddress()).cast(_target, _data, getOriginAddress());
|
||||||
|
}
|
||||||
|
|
||||||
|
function claimCrvAndSwap(uint amt, uint unitAmt) external {
|
||||||
|
address crv = getCrvAddress();
|
||||||
|
address eth = getEthAddress();
|
||||||
|
address[] memory _target = new address[](1);
|
||||||
|
bytes[] memory _data = new bytes[](1);
|
||||||
|
_target[0] = getUniswapConnectAddress();
|
||||||
|
_data[0] = abi.encodeWithSignature("sell(address,address,unit256,unit256,unit256,unit256)", eth, crv, amt, unitAmt, 0, 0);
|
||||||
|
DSAInterface(getDsaAddress()).cast(_target, _data, getOriginAddress());
|
||||||
|
}
|
||||||
|
}
|
16
contracts/logics/settle/eth/exchangeRate.sol
Normal file
16
contracts/logics/settle/eth/exchangeRate.sol
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.6.8;
|
||||||
|
|
||||||
|
import { DSMath } from "../../../libs/safeMath.sol";
|
||||||
|
|
||||||
|
interface PoolInterface {
|
||||||
|
function setExchangeRate() external;
|
||||||
|
}
|
||||||
|
|
||||||
|
contract LogicOne {
|
||||||
|
|
||||||
|
function setExchangeRate() public payable {
|
||||||
|
PoolInterface(address(this)).setExchangeRate();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
88
contracts/logics/settle/eth/maker.sol
Normal file
88
contracts/logics/settle/eth/maker.sol
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.6.8;
|
||||||
|
pragma experimental ABIEncoderV2;
|
||||||
|
|
||||||
|
import { DSMath } from "../../../libs/safeMath.sol";
|
||||||
|
|
||||||
|
interface VaultDataInterface {
|
||||||
|
|
||||||
|
struct VaultData {
|
||||||
|
uint id;
|
||||||
|
address owner;
|
||||||
|
string colType;
|
||||||
|
uint collateral;
|
||||||
|
uint art;
|
||||||
|
uint debt;
|
||||||
|
uint liquidatedCol;
|
||||||
|
uint borrowRate;
|
||||||
|
uint colPrice;
|
||||||
|
uint liquidationRatio;
|
||||||
|
address vaultAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getVaultById(uint id) external view returns (VaultData memory);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
interface DSAInterface {
|
||||||
|
function cast(address[] calldata _targets, bytes[] calldata _datas, address _origin) external payable;
|
||||||
|
}
|
||||||
|
|
||||||
|
contract LogicOne is DSMath {
|
||||||
|
|
||||||
|
function getOriginAddress() private pure returns(address) {
|
||||||
|
return 0xB7fA44c2E964B6EB24893f7082Ecc08c8d0c0F87; // DSA address
|
||||||
|
}
|
||||||
|
|
||||||
|
function getMcdAddresses() public pure returns (address) {
|
||||||
|
return 0xF23196DF1C440345DE07feFbe556a5eF0dcD29F0;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getInstaMakerResolver() public pure returns (address) {
|
||||||
|
return 0x0A7008B38E7015F8C36A49eEbc32513ECA8801E5;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getMakerConnectAddress() public pure returns (address) {
|
||||||
|
return 0x6c4E4D4aB22cAB08b8498a3A232D92609e8b2d62;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getDsaAddress() private pure returns(address) {
|
||||||
|
return address(0); // DSA address
|
||||||
|
}
|
||||||
|
|
||||||
|
function vaultId() private pure returns(uint) {
|
||||||
|
return 0; // vault ID
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkMakerVault() private view {
|
||||||
|
VaultDataInterface.VaultData memory vaultData = VaultDataInterface(getInstaMakerResolver()).getVaultById(vaultId());
|
||||||
|
uint col = vaultData.collateral;
|
||||||
|
uint debt = vaultData.debt;
|
||||||
|
uint price = vaultData.colPrice / 10 ** 9; // making 18 decimal
|
||||||
|
// uint liquidation = vaultData.liqInk / 10 ** 9; // making 18 decimal
|
||||||
|
uint currentRatio = wdiv(wmul(col, price), debt);
|
||||||
|
require(200 * 10 ** 18 < currentRatio, "position-risky"); // ratio should be less than 50% (should we keep it 60%?)
|
||||||
|
}
|
||||||
|
|
||||||
|
function depositAndBorrow(uint depositAmt, uint borrowAmt) public {
|
||||||
|
address[] memory _targets = new address[](2);
|
||||||
|
bytes[] memory _data = new bytes[](2);
|
||||||
|
_targets[0] = getMakerConnectAddress();
|
||||||
|
_data[0] = abi.encodeWithSignature("deposit(uint256,uint256,uint256,uint256)", vaultId(), depositAmt, uint(0), uint(0));
|
||||||
|
_targets[1] = getMakerConnectAddress();
|
||||||
|
_data[1] = abi.encodeWithSignature("borrow(uint256,uint256,uint256,uint256)", vaultId(), borrowAmt, uint(0), uint(0));
|
||||||
|
DSAInterface(getDsaAddress()).cast(_targets, _data, getOriginAddress());
|
||||||
|
checkMakerVault();
|
||||||
|
}
|
||||||
|
|
||||||
|
function paybackAndWithdraw(uint withdrawAmt, uint paybackAmt) public {
|
||||||
|
address[] memory _targets = new address[](2);
|
||||||
|
bytes[] memory _data = new bytes[](2);
|
||||||
|
_targets[0] = getMakerConnectAddress();
|
||||||
|
_data[0] = abi.encodeWithSignature("payback(uint256,uint256,uint256,uint256)", vaultId(), paybackAmt, uint(0), uint(0));
|
||||||
|
_targets[1] = getMakerConnectAddress();
|
||||||
|
_data[1] = abi.encodeWithSignature("withdraw(uint256,uint256,uint256,uint256)", vaultId(), withdrawAmt, uint(0), uint(0));
|
||||||
|
DSAInterface(getDsaAddress()).cast(_targets, _data, getOriginAddress());
|
||||||
|
checkMakerVault();
|
||||||
|
}
|
||||||
|
}
|
132
contracts/logics/settle/eth/maxComp.sol
Normal file
132
contracts/logics/settle/eth/maxComp.sol
Normal file
|
@ -0,0 +1,132 @@
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.6.8;
|
||||||
|
pragma experimental ABIEncoderV2;
|
||||||
|
|
||||||
|
import { DSMath } from "../../../libs/safeMath.sol";
|
||||||
|
|
||||||
|
interface CTokenInterface {
|
||||||
|
function borrowBalanceCurrent(address account) external returns (uint256);
|
||||||
|
function exchangeRateCurrent() external returns (uint256);
|
||||||
|
|
||||||
|
function balanceOf(address owner) external view returns (uint256);
|
||||||
|
|
||||||
|
function underlying() external view returns (address);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface DSAInterface {
|
||||||
|
function cast(address[] calldata _targets, bytes[] calldata _datas, address _origin) external payable;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface CompTroller {
|
||||||
|
function getAllMarkets() external view returns (address[] memory);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface OracleComp {
|
||||||
|
function getUnderlyingPrice(address) external view returns (uint);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface InstaMapping {
|
||||||
|
function cTokenMapping(address) external view returns (address);
|
||||||
|
}
|
||||||
|
|
||||||
|
contract LogicOne is DSMath {
|
||||||
|
|
||||||
|
struct CastData {
|
||||||
|
address[] dsaTargets;
|
||||||
|
bytes[] dsaData;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getOriginAddress() private pure returns(address) {
|
||||||
|
return 0xB7fA44c2E964B6EB24893f7082Ecc08c8d0c0F87; // DSA address
|
||||||
|
}
|
||||||
|
|
||||||
|
function getEthAddress() private pure returns(address) {
|
||||||
|
return 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; // DSA address
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCompAddress() private pure returns(address) {
|
||||||
|
return 0xc00e94Cb662C3520282E6f5717214004A7f26888; // DSA address
|
||||||
|
}
|
||||||
|
|
||||||
|
function getComptrollerAddress() private pure returns (address) {
|
||||||
|
return 0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getDaiAddress() private pure returns(address) {
|
||||||
|
return 0x6B175474E89094C44Da98b954EedeAC495271d0F; // DAI address
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCdaiAddress() private pure returns(address) {
|
||||||
|
return 0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643; // CDAI address
|
||||||
|
}
|
||||||
|
|
||||||
|
function getDsaAddress() private pure returns(address) {
|
||||||
|
return address(0); // DSA address
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCompoundConnectAddress() private pure returns(address) {
|
||||||
|
return 0x07F81230d73a78f63F0c2A3403AD281b067d28F8;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getFlashloanConnectAddress() private pure returns(address) {
|
||||||
|
return 0xaA3EA0b22802d68DA73D5f4D3f9F1C7C238fE03A;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCompConnectAddress() private pure returns(address) {
|
||||||
|
return 0xB4a04F1C194bEed64FCE27843B5b3079339cdaD4;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getUniswapConnectAddress() private pure returns(address) {
|
||||||
|
return 0x62EbfF47B2Ba3e47796efaE7C51676762dC961c0;
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkCompoundAssets() private {
|
||||||
|
address[] memory allMarkets = CompTroller(getComptrollerAddress()).getAllMarkets();
|
||||||
|
uint supply;
|
||||||
|
uint borrow;
|
||||||
|
for (uint i = 0; i < allMarkets.length; i++) {
|
||||||
|
CTokenInterface ctoken = CTokenInterface(allMarkets[i]);
|
||||||
|
if (allMarkets[i] == getCdaiAddress()) {
|
||||||
|
supply = wmul(ctoken.balanceOf(getDsaAddress()), ctoken.exchangeRateCurrent());
|
||||||
|
}
|
||||||
|
borrow = ctoken.borrowBalanceCurrent(getDsaAddress());
|
||||||
|
|
||||||
|
if (allMarkets[i] != getCdaiAddress()) {
|
||||||
|
require(borrow == 0, "assets");
|
||||||
|
} else {
|
||||||
|
require(wdiv(borrow, supply) < 745 * 10 ** 15, "position-risky"); // DAI ratio - should be less than 74.5%
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function maxComp(uint flashAmt, uint route, address[] calldata _targets, bytes[] calldata _data) external {
|
||||||
|
address compoundConnect = getCompoundConnectAddress();
|
||||||
|
address flashloanConnect = getFlashloanConnectAddress();
|
||||||
|
for (uint i = 0; i < _targets.length; i++) {
|
||||||
|
require(_targets[i] == compoundConnect || _targets[i] == flashloanConnect, "not-authorised-connector");
|
||||||
|
}
|
||||||
|
bytes memory _dataEncode = abi.encode(_targets, _data);
|
||||||
|
address[] memory _targetFlash = new address[](1);
|
||||||
|
bytes[] memory _dataFlash = new bytes[](1);
|
||||||
|
_targetFlash[0] = flashloanConnect;
|
||||||
|
_dataFlash[0] = abi.encodeWithSignature("flashBorrowAndCast(address,uint256,uint256,bytes)", getDaiAddress(), flashAmt, route, _dataEncode);
|
||||||
|
DSAInterface(getDsaAddress()).cast(_targetFlash, _dataFlash, getOriginAddress());
|
||||||
|
checkCompoundAssets();
|
||||||
|
}
|
||||||
|
|
||||||
|
function claimComp(address[] calldata tokens) external {
|
||||||
|
address[] memory _target = new address[](1);
|
||||||
|
bytes[] memory _data = new bytes[](1);
|
||||||
|
_target[0] = getCompConnectAddress();
|
||||||
|
_data[0] = abi.encodeWithSignature("ClaimCompTwo(address[],uint256)", tokens, 0);
|
||||||
|
DSAInterface(getDsaAddress()).cast(_target, _data, getOriginAddress());
|
||||||
|
}
|
||||||
|
|
||||||
|
function swapComp(uint amt, uint unitAmt) external {
|
||||||
|
address[] memory _target = new address[](1);
|
||||||
|
bytes[] memory _data = new bytes[](1);
|
||||||
|
_target[0] = getUniswapConnectAddress();
|
||||||
|
_data[0] = abi.encodeWithSignature("sell(address,address,unit256,unit256,unit256,unit256)", getEthAddress(), getCompAddress(), amt, unitAmt, 0, 0);
|
||||||
|
DSAInterface(getDsaAddress()).cast(_target, _data, getOriginAddress());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user