dsa-connectors/contracts/mainnet/connectors/reflexer/helpers.sol

137 lines
4.2 KiB
Solidity
Raw Normal View History

2021-03-29 09:57:54 +00:00
pragma solidity ^0.7.0;
import { DSMath } from "../../common/math.sol";
import { Basic } from "../../common/basic.sol";
import { TokenInterface } from "../../common/interfaces.sol";
2021-03-30 10:52:29 +00:00
import {
ManagerLike,
CoinJoinInterface,
SafeEngineLike,
TaxCollectorLike,
TokenJoinInterface,
GebMapping
} from "./interface.sol";
2021-03-29 09:57:54 +00:00
abstract contract Helpers is DSMath, Basic {
/**
* @dev Manager Interface
*/
2021-03-29 15:20:34 +00:00
ManagerLike internal constant managerContract = ManagerLike(0xEfe0B4cA532769a3AE758fD82E1426a03A94F185);
2021-03-29 09:57:54 +00:00
/**
2021-03-29 15:20:34 +00:00
* @dev Coin Join
2021-03-29 09:57:54 +00:00
*/
2021-03-29 15:20:34 +00:00
CoinJoinInterface internal constant coinJoinContract = CoinJoinInterface(0x0A5653CCa4DB1B6E265F47CAf6969e64f1CFdC45);
2021-03-29 09:57:54 +00:00
/**
2021-03-29 15:20:34 +00:00
* @dev Reflexer Tax collector Address.
2021-03-29 09:57:54 +00:00
*/
2021-03-29 15:20:34 +00:00
TaxCollectorLike internal constant taxCollectorContract = TaxCollectorLike(0xcDB05aEda142a1B0D6044C09C64e4226c1a281EB);
2021-03-29 09:57:54 +00:00
/**
2021-03-29 15:20:34 +00:00
* @dev Return Close Safe Address.
2021-03-29 09:57:54 +00:00
*/
address internal constant giveAddr = 0x4dD58550eb15190a5B3DfAE28BB14EeC181fC267;
2021-03-30 10:52:29 +00:00
/**
* @dev Return Reflexer mapping Address.
*/
function getGebMappingAddress() internal pure returns (address) {
2021-08-05 07:27:09 +00:00
return 0x573e5132693C046D1A9F75Bac683889164bA41b4;
2021-03-30 10:52:29 +00:00
}
function getCollateralJoinAddress(bytes32 collateralType) internal view returns (address) {
return GebMapping(getGebMappingAddress()).collateralJoinMapping(collateralType);
}
2021-03-29 09:57:54 +00:00
/**
2021-03-29 15:20:34 +00:00
* @dev Get Safe's collateral type.
2021-03-29 09:57:54 +00:00
*/
2021-03-29 15:20:34 +00:00
function getSafeData(uint safe) internal view returns (bytes32 collateralType, address handler) {
collateralType = managerContract.collateralTypes(safe);
handler = managerContract.safes(safe);
2021-03-29 09:57:54 +00:00
}
/**
2021-03-29 15:20:34 +00:00
* @dev Collateral Join address is ETH type collateral.
2021-03-29 09:57:54 +00:00
*/
function isEth(address tknAddr) internal pure returns (bool) {
2021-06-18 08:28:08 +00:00
return tknAddr == wethAddr ? true : false;
2021-03-29 09:57:54 +00:00
}
/**
2021-03-29 15:20:34 +00:00
* @dev Get Safe Debt Amount.
2021-03-29 09:57:54 +00:00
*/
2021-03-29 15:20:34 +00:00
function _getSafeDebt(
address safeEngine,
bytes32 collateralType,
address handler
2021-03-29 09:57:54 +00:00
) internal view returns (uint wad) {
2021-03-29 15:20:34 +00:00
(, uint rate,,,) = SafeEngineLike(safeEngine).collateralTypes(collateralType);
(, uint debt) = SafeEngineLike(safeEngine).safes(collateralType, handler);
2021-03-30 10:53:38 +00:00
uint coin = SafeEngineLike(safeEngine).coinBalance(handler);
2021-03-29 09:57:54 +00:00
2021-03-29 15:20:34 +00:00
uint rad = sub(mul(debt, rate), coin);
2021-03-29 09:57:54 +00:00
wad = rad / RAY;
wad = mul(wad, RAY) < rad ? wad + 1 : wad;
}
/**
* @dev Get Borrow Amount.
*/
function _getBorrowAmt(
2021-03-29 15:20:34 +00:00
address safeEngine,
address handler,
bytes32 collateralType,
2021-03-29 09:57:54 +00:00
uint amt
2021-03-29 15:20:34 +00:00
) internal returns (int deltaDebt)
2021-03-29 09:57:54 +00:00
{
2021-03-29 15:20:34 +00:00
uint rate = taxCollectorContract.taxSingle(collateralType);
2021-03-30 10:53:38 +00:00
uint coin = SafeEngineLike(safeEngine).coinBalance(handler);
2021-03-29 10:34:18 +00:00
if (coin < mul(amt, RAY)) {
2021-03-29 15:20:34 +00:00
deltaDebt = toInt(sub(mul(amt, RAY), coin) / rate);
deltaDebt = mul(uint(deltaDebt), rate) < mul(amt, RAY) ? deltaDebt + 1 : deltaDebt;
2021-03-29 09:57:54 +00:00
}
}
/**
* @dev Get Payback Amount.
*/
function _getWipeAmt(
2021-03-29 15:20:34 +00:00
address safeEngine,
2021-03-29 09:57:54 +00:00
uint amt,
2021-03-29 15:20:34 +00:00
address handler,
bytes32 collateralType
) internal view returns (int deltaDebt)
2021-03-29 09:57:54 +00:00
{
2021-03-29 15:20:34 +00:00
(, uint rate,,,) = SafeEngineLike(safeEngine).collateralTypes(collateralType);
(, uint debt) = SafeEngineLike(safeEngine).safes(collateralType, handler);
deltaDebt = toInt(amt / rate);
deltaDebt = uint(deltaDebt) <= debt ? - deltaDebt : - toInt(debt);
2021-03-29 09:57:54 +00:00
}
/**
* @dev Convert String to bytes32.
*/
function stringToBytes32(string memory str) internal pure returns (bytes32 result) {
require(bytes(str).length != 0, "string-empty");
// solium-disable-next-line security/no-inline-assembly
assembly {
result := mload(add(str, 32))
}
}
/**
2021-03-29 15:20:34 +00:00
* @dev Get safe ID. If `safe` is 0, get lastSAFEID opened safe.
2021-03-29 09:57:54 +00:00
*/
2021-03-29 15:20:34 +00:00
function getSafe(uint safe) internal view returns (uint _safe) {
if (safe == 0) {
require(managerContract.safeCount(address(this)) > 0, "no-safe-opened");
_safe = managerContract.lastSAFEID(address(this));
2021-03-29 09:57:54 +00:00
} else {
2021-03-29 15:20:34 +00:00
_safe = safe;
2021-03-29 09:57:54 +00:00
}
}
}