2020-11-02 10:51:49 +00:00
|
|
|
// SPDX-License-Identifier: UNLICENSED
|
|
|
|
pragma solidity 0.7.4;
|
|
|
|
|
2020-11-02 14:52:03 +00:00
|
|
|
import {MCD_MANAGER} from "../../constants/CMaker.sol";
|
2020-11-23 07:17:25 +00:00
|
|
|
import {INSTA_MAPPING} from "../../constants/CInstaDapp.sol";
|
|
|
|
import {
|
|
|
|
ITokenJoinInterface
|
|
|
|
} from "../../interfaces/dapps/Maker/ITokenJoinInterface.sol";
|
2020-11-02 14:52:03 +00:00
|
|
|
import {IMcdManager} from "../../interfaces/dapps/Maker/IMcdManager.sol";
|
2020-11-23 07:17:25 +00:00
|
|
|
import {InstaMapping} from "../../interfaces/InstaDapp/IInstaDapp.sol";
|
2020-11-02 14:52:03 +00:00
|
|
|
import {IVat} from "../../interfaces/dapps/Maker/IVat.sol";
|
2020-11-23 07:17:25 +00:00
|
|
|
import {RAY, add, sub, mul} from "../../vendor/DSMath.sol";
|
|
|
|
import {_stringToBytes32, _convertTo18} from "../../vendor/Convert.sol";
|
2020-11-02 10:51:49 +00:00
|
|
|
|
|
|
|
function _getMakerVaultDebt(uint256 _vaultId) view returns (uint256 wad) {
|
2020-11-02 14:52:03 +00:00
|
|
|
IMcdManager manager = IMcdManager(MCD_MANAGER);
|
2020-11-02 10:51:49 +00:00
|
|
|
|
2020-11-02 14:52:03 +00:00
|
|
|
(bytes32 ilk, address urn) = _getVaultData(manager, _vaultId);
|
|
|
|
IVat vat = IVat(manager.vat());
|
2020-11-02 10:51:49 +00:00
|
|
|
(, uint256 rate, , , ) = vat.ilks(ilk);
|
|
|
|
(, uint256 art) = vat.urns(ilk, urn);
|
|
|
|
uint256 dai = vat.dai(urn);
|
|
|
|
|
|
|
|
uint256 rad = sub(mul(art, rate), dai);
|
|
|
|
wad = rad / RAY;
|
|
|
|
|
|
|
|
wad = mul(wad, RAY) < rad ? wad + 1 : wad;
|
|
|
|
}
|
|
|
|
|
2020-11-11 11:55:36 +00:00
|
|
|
function _getMakerRawVaultDebt(uint256 _vaultId) view returns (uint256 tab) {
|
|
|
|
IMcdManager manager = IMcdManager(MCD_MANAGER);
|
|
|
|
|
|
|
|
(bytes32 ilk, address urn) = _getVaultData(manager, _vaultId);
|
|
|
|
IVat vat = IVat(manager.vat());
|
|
|
|
(, uint256 rate, , , ) = vat.ilks(ilk);
|
|
|
|
(, uint256 art) = vat.urns(ilk, urn);
|
|
|
|
|
|
|
|
uint256 rad = mul(art, rate);
|
|
|
|
|
|
|
|
tab = rad / RAY;
|
|
|
|
tab = mul(tab, RAY) < rad ? tab + 1 : tab;
|
|
|
|
}
|
|
|
|
|
2020-11-02 10:51:49 +00:00
|
|
|
function _getMakerVaultCollateralBalance(uint256 _vaultId)
|
|
|
|
view
|
|
|
|
returns (uint256)
|
|
|
|
{
|
2020-11-02 14:52:03 +00:00
|
|
|
IMcdManager manager = IMcdManager(MCD_MANAGER);
|
2020-11-02 10:51:49 +00:00
|
|
|
|
2020-11-02 14:52:03 +00:00
|
|
|
IVat vat = IVat(manager.vat());
|
|
|
|
(bytes32 ilk, address urn) = _getVaultData(manager, _vaultId);
|
2020-11-02 10:51:49 +00:00
|
|
|
(uint256 ink, ) = vat.urns(ilk, urn);
|
|
|
|
|
|
|
|
return ink;
|
|
|
|
}
|
|
|
|
|
2020-11-23 09:56:20 +00:00
|
|
|
function _vaultWillBeSafe(
|
2020-11-23 07:17:25 +00:00
|
|
|
uint256 _vaultId,
|
|
|
|
uint256 _amtToBorrow,
|
|
|
|
uint256 _colToDeposit
|
|
|
|
) view returns (bool) {
|
2020-11-23 09:56:20 +00:00
|
|
|
require(_vaultId != 0, "_vaultWillBeSafe: invalid vault id.");
|
2020-11-23 07:17:25 +00:00
|
|
|
|
|
|
|
IMcdManager manager = IMcdManager(MCD_MANAGER);
|
|
|
|
|
|
|
|
(bytes32 ilk, address urn) = _getVaultData(manager, _vaultId);
|
|
|
|
|
|
|
|
ITokenJoinInterface tokenJoinContract =
|
|
|
|
ITokenJoinInterface(InstaMapping(INSTA_MAPPING).gemJoinMapping(ilk));
|
|
|
|
|
|
|
|
IVat vat = IVat(manager.vat());
|
|
|
|
(, uint256 rate, uint256 spot, , ) = vat.ilks(ilk);
|
|
|
|
(uint256 ink, uint256 art) = vat.urns(ilk, urn);
|
|
|
|
uint256 dai = vat.dai(urn);
|
|
|
|
|
|
|
|
uint256 dink = _convertTo18(tokenJoinContract.dec(), _colToDeposit);
|
|
|
|
uint256 dart = _getBorrowAmt(_amtToBorrow, dai, rate);
|
|
|
|
|
|
|
|
ink = add(ink, dink);
|
|
|
|
art = add(art, dart);
|
|
|
|
|
|
|
|
uint256 tab = mul(rate, art);
|
|
|
|
|
|
|
|
return tab <= mul(ink, spot);
|
|
|
|
}
|
|
|
|
|
2020-11-23 09:56:20 +00:00
|
|
|
function _newVaultWillBeSafe(
|
2020-11-23 07:17:25 +00:00
|
|
|
string memory _colType,
|
|
|
|
uint256 _amtToBorrow,
|
|
|
|
uint256 _colToDeposit
|
|
|
|
) view returns (bool) {
|
|
|
|
IMcdManager manager = IMcdManager(MCD_MANAGER);
|
|
|
|
IVat vat = IVat(manager.vat());
|
|
|
|
|
|
|
|
bytes32 ilk = _stringToBytes32(_colType);
|
|
|
|
|
|
|
|
(, uint256 rate, uint256 spot, , ) = vat.ilks(ilk);
|
|
|
|
|
|
|
|
ITokenJoinInterface tokenJoinContract =
|
|
|
|
ITokenJoinInterface(InstaMapping(INSTA_MAPPING).gemJoinMapping(ilk));
|
|
|
|
|
|
|
|
uint256 ink = _convertTo18(tokenJoinContract.dec(), _colToDeposit);
|
|
|
|
uint256 art = _getBorrowAmt(_amtToBorrow, 0, rate);
|
|
|
|
|
|
|
|
uint256 tab = mul(rate, art);
|
|
|
|
|
|
|
|
return tab <= mul(ink, spot);
|
|
|
|
}
|
|
|
|
|
2020-11-02 14:52:03 +00:00
|
|
|
function _getVaultData(IMcdManager manager, uint256 vault)
|
2020-11-02 10:51:49 +00:00
|
|
|
view
|
|
|
|
returns (bytes32 ilk, address urn)
|
|
|
|
{
|
2020-11-02 14:52:03 +00:00
|
|
|
ilk = manager.ilks(vault);
|
|
|
|
urn = manager.urns(vault);
|
2020-11-02 10:51:49 +00:00
|
|
|
}
|
2020-11-23 07:17:25 +00:00
|
|
|
|
|
|
|
function _getBorrowAmt(
|
|
|
|
uint256 _amt,
|
|
|
|
uint256 _dai,
|
|
|
|
uint256 _rate
|
|
|
|
) pure returns (uint256 dart) {
|
|
|
|
dart = sub(mul(_amt, RAY), _dai) / _rate;
|
|
|
|
dart = mul(dart, _rate) < mul(_amt, RAY) ? dart + 1 : dart;
|
|
|
|
}
|
2020-11-24 09:50:56 +00:00
|
|
|
|
2020-11-24 16:49:27 +00:00
|
|
|
function _isVaultOwner(uint256 _vaultId, address _owner) view returns (bool) {
|
|
|
|
if (_vaultId == 0) return false;
|
|
|
|
|
|
|
|
try IMcdManager(MCD_MANAGER).owns(_vaultId) returns (address owner) {
|
|
|
|
return _owner == owner;
|
|
|
|
} catch Error(string memory error) {
|
|
|
|
revert(string(abi.encodePacked("FMaker._isVaultOwner:", error)));
|
|
|
|
} catch {
|
|
|
|
revert("FMaker._isVaultOwner:undefined");
|
|
|
|
}
|
2020-11-24 09:50:56 +00:00
|
|
|
}
|