diff --git a/contracts/connectors/reflexer/events.sol b/contracts/connectors/reflexer/events.sol new file mode 100644 index 00000000..6c04e376 --- /dev/null +++ b/contracts/connectors/reflexer/events.sol @@ -0,0 +1,23 @@ +pragma solidity ^0.7.0; + +contract Events { + event LogOpen(uint256 indexed safe, bytes32 indexed collateralType); + event LogClose(uint256 indexed safe, bytes32 indexed collateralType); + event LogTransfer(uint256 indexed safe, bytes32 indexed collateralType, address newOwner); + event LogDeposit(uint256 indexed safe, bytes32 indexed collateralType, uint256 tokenAmt, uint256 getId, uint256 setId); + event LogWithdraw(uint256 indexed safe, bytes32 indexed collateralType, uint256 tokenAmt, uint256 getId, uint256 setId); + event LogBorrow(uint256 indexed safe, bytes32 indexed collateralType, uint256 tokenAmt, uint256 getId, uint256 setId); + event LogPayback(uint256 indexed safe, bytes32 indexed collateralType, uint256 tokenAmt, uint256 getId, uint256 setId); + event LogWithdrawLiquidated(uint256 indexed safe, bytes32 indexed collateralType, uint256 tokenAmt, uint256 getId, uint256 setId); + event LogExit(uint256 indexed safe, bytes32 indexed collateralType, uint256 tokenAmt, uint256 getId, uint256 setId); + event LogDepositAndBorrow( + uint256 indexed safe, + bytes32 indexed collateralType, + uint256 depositAmt, + uint256 borrowAmt, + uint256 getIdDeposit, + uint256 getIdBorrow, + uint256 setIdDeposit, + uint256 setIdBorrow + ); +} diff --git a/contracts/connectors/reflexer/helpers.sol b/contracts/connectors/reflexer/helpers.sol new file mode 100644 index 00000000..abcba530 --- /dev/null +++ b/contracts/connectors/reflexer/helpers.sol @@ -0,0 +1,138 @@ +pragma solidity ^0.7.0; + +import { DSMath } from "../../common/math.sol"; +import { Basic } from "../../common/basic.sol"; +import { TokenInterface } from "../../common/interfaces.sol"; +import { + ManagerLike, + CoinJoinInterface, + SafeEngineLike, + TaxCollectorLike, + TokenJoinInterface, + GebMapping +} from "./interface.sol"; + +abstract contract Helpers is DSMath, Basic { + /** + * @dev Manager Interface + */ + ManagerLike internal constant managerContract = ManagerLike(0xEfe0B4cA532769a3AE758fD82E1426a03A94F185); + + /** + * @dev Coin Join + */ + CoinJoinInterface internal constant coinJoinContract = CoinJoinInterface(0x0A5653CCa4DB1B6E265F47CAf6969e64f1CFdC45); + + /** + * @dev Reflexer Tax collector Address. + */ + TaxCollectorLike internal constant taxCollectorContract = TaxCollectorLike(0xcDB05aEda142a1B0D6044C09C64e4226c1a281EB); + + /** + * @dev Return Close Safe Address. + */ + address internal constant giveAddr = 0x4dD58550eb15190a5B3DfAE28BB14EeC181fC267; + + /** + * @dev Return Reflexer mapping Address. + */ + function getGebMappingAddress() internal pure returns (address) { + // TODO: Set the real deployed Reflexer mapping address + return 0x0000000000000000000000000000000000000000; + } + + function getCollateralJoinAddress(bytes32 collateralType) internal view returns (address) { + return GebMapping(getGebMappingAddress()).collateralJoinMapping(collateralType); + } + + /** + * @dev Get Safe's collateral type. + */ + function getSafeData(uint safe) internal view returns (bytes32 collateralType, address handler) { + collateralType = managerContract.collateralTypes(safe); + handler = managerContract.safes(safe); + } + + /** + * @dev Collateral Join address is ETH type collateral. + */ + function isEth(address tknAddr) internal pure returns (bool) { + return tknAddr == ethAddr ? true : false; + } + + /** + * @dev Get Safe Debt Amount. + */ + function _getSafeDebt( + address safeEngine, + bytes32 collateralType, + address handler + ) internal view returns (uint wad) { + (, uint rate,,,) = SafeEngineLike(safeEngine).collateralTypes(collateralType); + (, uint debt) = SafeEngineLike(safeEngine).safes(collateralType, handler); + uint coin = SafeEngineLike(safeEngine).coinBalance(handler); + + uint rad = sub(mul(debt, rate), coin); + wad = rad / RAY; + + wad = mul(wad, RAY) < rad ? wad + 1 : wad; + } + + /** + * @dev Get Borrow Amount. + */ + function _getBorrowAmt( + address safeEngine, + address handler, + bytes32 collateralType, + uint amt + ) internal returns (int deltaDebt) + { + uint rate = taxCollectorContract.taxSingle(collateralType); + uint coin = SafeEngineLike(safeEngine).coinBalance(handler); + if (coin < mul(amt, RAY)) { + deltaDebt = toInt(sub(mul(amt, RAY), coin) / rate); + deltaDebt = mul(uint(deltaDebt), rate) < mul(amt, RAY) ? deltaDebt + 1 : deltaDebt; + } + } + + /** + * @dev Get Payback Amount. + */ + function _getWipeAmt( + address safeEngine, + uint amt, + address handler, + bytes32 collateralType + ) internal view returns (int deltaDebt) + { + (, uint rate,,,) = SafeEngineLike(safeEngine).collateralTypes(collateralType); + (, uint debt) = SafeEngineLike(safeEngine).safes(collateralType, handler); + deltaDebt = toInt(amt / rate); + deltaDebt = uint(deltaDebt) <= debt ? - deltaDebt : - toInt(debt); + } + + /** + * @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)) + } + } + + /** + * @dev Get safe ID. If `safe` is 0, get lastSAFEID opened safe. + */ + 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)); + } else { + _safe = safe; + } + } + +} \ No newline at end of file diff --git a/contracts/connectors/reflexer/interface.sol b/contracts/connectors/reflexer/interface.sol new file mode 100644 index 00000000..7cee1580 --- /dev/null +++ b/contracts/connectors/reflexer/interface.sol @@ -0,0 +1,67 @@ +pragma solidity ^0.7.0; + +import { TokenInterface } from "../../common/interfaces.sol"; + +interface ManagerLike { + function safeCan(address, uint, address) external view returns (uint); + function collateralTypes(uint) external view returns (bytes32); + function lastSAFEID(address) external view returns (uint); + function safeCount(address) external view returns (uint); + function ownsSAFE(uint) external view returns (address); + function safes(uint) external view returns (address); + function safeEngine() external view returns (address); + function openSAFE(bytes32, address) external returns (uint); + function transferSAFEOwnership(uint, address) external; + function modifySAFECollateralization(uint, int, int) external; + function transferCollateral(uint, address, uint) external; + function transferInternalCoins(uint, address, uint) external; +} + +interface SafeEngineLike { + function safeRights(address, address) external view returns (uint); + function collateralTypes(bytes32) external view returns (uint, uint, uint, uint, uint); + function coinBalance(address) external view returns (uint); + function safes(bytes32, address) external view returns (uint, uint); + function modifySAFECollateralization( + bytes32, + address, + address, + address, + int, + int + ) external; + function approveSAFEModification(address) external; + function transferInternalCoins(address, address, uint) external; + function tokenCollateral(bytes32, address) external view returns (uint); +} + +interface TokenJoinInterface { + function decimals() external returns (uint); + function collateral() external returns (TokenInterface); + function collateralType() external returns (bytes32); + function join(address, uint) external payable; + function exit(address, uint) external; +} + +interface CoinJoinInterface { + function safeEngine() external returns (SafeEngineLike); + function systemCoin() external returns (TokenInterface); + function join(address, uint) external payable; + function exit(address, uint) external; +} + +interface TaxCollectorLike { + function taxSingle(bytes32) external returns (uint); +} + +interface ConnectorsInterface { + function chief(address) external view returns (bool); +} + +interface IndexInterface { + function master() external view returns (address); +} + +interface GebMapping { + function collateralJoinMapping(bytes32) external view returns(address); +} \ No newline at end of file diff --git a/contracts/connectors/reflexer/main.sol b/contracts/connectors/reflexer/main.sol new file mode 100644 index 00000000..f00dcb1a --- /dev/null +++ b/contracts/connectors/reflexer/main.sol @@ -0,0 +1,411 @@ +pragma solidity ^0.7.0; + +import { TokenInterface } from "../../common/interfaces.sol"; +import { Helpers } from "./helpers.sol"; +import { Events } from "./events.sol"; +import { SafeEngineLike, TokenJoinInterface } from "./interface.sol"; + +abstract contract GebResolver is Helpers, Events { + /** + * @dev Open Safe + * @param colType Type of Collateral.(eg: 'ETH-A') + */ + function open(string calldata colType) external payable returns (string memory _eventName, bytes memory _eventParam) { + bytes32 collateralType = stringToBytes32(colType); + require(getCollateralJoinAddress(collateralType) != address(0), "wrong-col-type"); + uint256 safe = managerContract.openSAFE(collateralType, address(this)); + + _eventName = "LogOpen(uint256,bytes32)"; + _eventParam = abi.encode(safe, collateralType); + } + + /** + * @dev Close Safe + * @param safe Safe ID to close. + */ + function close(uint256 safe) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _safe = getSafe(safe); + (bytes32 collateralType, address handler) = getSafeData(_safe); + (uint collateral, uint debt) = SafeEngineLike(managerContract.safeEngine()).safes(collateralType, handler); + + require(collateral == 0 && debt == 0, "safe-has-assets"); + require(managerContract.ownsSAFE(_safe) == address(this), "not-owner"); + + managerContract.transferSAFEOwnership(_safe, giveAddr); + + _eventName = "LogClose(uint256,bytes32)"; + _eventParam = abi.encode(_safe, collateralType); + } + + /** + * @dev Deposit ETH/ERC20_Token Collateral. + * @param safe Safe ID. + * @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( + uint256 safe, + uint256 amt, + uint256 getId, + uint256 setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amt); + uint _safe = getSafe(safe); + (bytes32 collateralType, address handler) = getSafeData(_safe); + + address colAddr = getCollateralJoinAddress(collateralType); + TokenJoinInterface tokenJoinContract = TokenJoinInterface(colAddr); + TokenInterface tokenContract = tokenJoinContract.collateral(); + + if (isEth(address(tokenContract))) { + _amt = _amt == uint(-1) ? address(this).balance : _amt; + tokenContract.deposit{value: _amt}(); + } else { + _amt = _amt == uint(-1) ? tokenContract.balanceOf(address(this)) : _amt; + } + + tokenContract.approve(address(colAddr), _amt); + tokenJoinContract.join(address(this), _amt); + + SafeEngineLike(managerContract.safeEngine()).modifySAFECollateralization( + collateralType, + handler, + address(this), + address(this), + toInt(convertTo18(tokenJoinContract.decimals(), _amt)), + 0 + ); + + setUint(setId, _amt); + + _eventName = "LogDeposit(uint256,bytes32,uint256,uint256,uint256)"; + _eventParam = abi.encode(_safe, collateralType, _amt, getId, setId); + } + + /** + * @dev Withdraw ETH/ERC20_Token Collateral. + * @param safe Safe ID. + * @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( + uint256 safe, + uint256 amt, + uint256 getId, + uint256 setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amt); + uint _safe = getSafe(safe); + (bytes32 collateralType, address handler) = getSafeData(_safe); + + address colAddr = getCollateralJoinAddress(collateralType); + TokenJoinInterface tokenJoinContract = TokenJoinInterface(colAddr); + + uint _amt18; + if (_amt == uint(-1)) { + (_amt18,) = SafeEngineLike(managerContract.safeEngine()).safes(collateralType, handler); + _amt = convert18ToDec(tokenJoinContract.decimals(), _amt18); + } else { + _amt18 = convertTo18(tokenJoinContract.decimals(), _amt); + } + + managerContract.modifySAFECollateralization( + _safe, + -toInt(_amt18), + 0 + ); + + managerContract.transferCollateral( + _safe, + address(this), + _amt18 + ); + + TokenInterface tokenContract = tokenJoinContract.collateral(); + + if (isEth(address(tokenContract))) { + tokenJoinContract.exit(address(this), _amt); + tokenContract.withdraw(_amt); + } else { + tokenJoinContract.exit(address(this), _amt); + } + + setUint(setId, _amt); + + _eventName = "LogWithdraw(uint256,bytes32,uint256,uint256,uint256)"; + _eventParam = abi.encode(_safe, collateralType, _amt, getId, setId); + } + + /** + * @dev Borrow Coin. + * @param safe Safe ID. + * @param amt token amount to borrow. + * @param getId Get token amount at this ID from `InstaMemory` Contract. + * @param setId Set token amount at this ID in `InstaMemory` Contract. + */ + function borrow( + uint256 safe, + uint256 amt, + uint256 getId, + uint256 setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amt); + uint _safe = getSafe(safe); + (bytes32 collateralType, address handler) = getSafeData(_safe); + + SafeEngineLike safeEngineContract = SafeEngineLike(managerContract.safeEngine()); + + managerContract.modifySAFECollateralization( + _safe, + 0, + _getBorrowAmt( + address(safeEngineContract), + handler, + collateralType, + _amt + ) + ); + + managerContract.transferInternalCoins( + _safe, + address(this), + toRad(_amt) + ); + + if (safeEngineContract.safeRights(address(this), address(coinJoinContract)) == 0) { + safeEngineContract.approveSAFEModification(address(coinJoinContract)); + } + + coinJoinContract.exit(address(this), _amt); + + setUint(setId, _amt); + + _eventName = "LogBorrow(uint256,bytes32,uint256,uint256,uint256)"; + _eventParam = abi.encode(_safe, collateralType, _amt, getId, setId); + } + + /** + * @dev Payback borrowed Coin. + * @param safe Safe ID. + * @param amt token amount to payback. + * @param getId Get token amount at this ID from `InstaMemory` Contract. + * @param setId Set token amount at this ID in `InstaMemory` Contract. + */ + function payback( + uint256 safe, + uint256 amt, + uint256 getId, + uint256 setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amt); + uint _safe = getSafe(safe); + (bytes32 collateralType, address handler) = getSafeData(_safe); + + address safeEngine = managerContract.safeEngine(); + + uint _maxDebt = _getSafeDebt(safeEngine, collateralType, handler); + + _amt = _amt == uint(-1) ? _maxDebt : _amt; + + require(_maxDebt >= _amt, "paying-excess-debt"); + + coinJoinContract.systemCoin().approve(address(coinJoinContract), _amt); + coinJoinContract.join(handler, _amt); + + managerContract.modifySAFECollateralization( + _safe, + 0, + _getWipeAmt( + safeEngine, + SafeEngineLike(safeEngine).coinBalance(handler), + handler, + collateralType + ) + ); + + setUint(setId, _amt); + + _eventName = "LogPayback(uint256,bytes32,uint256,uint256,uint256)"; + _eventParam = abi.encode(_safe, collateralType, _amt, getId, setId); + } + + /** + * @dev Withdraw leftover ETH/ERC20_Token after Liquidation. + * @param safe Safe ID. + * @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 withdrawLiquidated( + uint256 safe, + uint256 amt, + uint256 getId, + uint256 setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amt); + (bytes32 collateralType, address handler) = getSafeData(safe); + + address colAddr = getCollateralJoinAddress(collateralType); + TokenJoinInterface tokenJoinContract = TokenJoinInterface(colAddr); + + uint _amt18; + if (_amt == uint(-1)) { + _amt18 = SafeEngineLike(managerContract.safeEngine()).tokenCollateral(collateralType, handler); + _amt = convert18ToDec(tokenJoinContract.decimals(), _amt18); + } else { + _amt18 = convertTo18(tokenJoinContract.decimals(), _amt); + } + + managerContract.transferCollateral( + safe, + address(this), + _amt18 + ); + + TokenInterface tokenContract = tokenJoinContract.collateral(); + tokenJoinContract.exit(address(this), _amt); + if (isEth(address(tokenContract))) { + tokenContract.withdraw(_amt); + } + + setUint(setId, _amt); + + _eventName = "LogWithdrawLiquidated(uint256,bytes32,uint256,uint256,uint256)"; + _eventParam = abi.encode(safe, collateralType, _amt, getId, setId); + } + + struct GebData { + uint _safe; + address colAddr; + TokenJoinInterface tokenJoinContract; + SafeEngineLike safeEngineContract; + TokenInterface tokenContract; + } + /** + * @dev Deposit ETH/ERC20_Token Collateral and Borrow Coin. + * @param safe Safe ID. + * @param depositAmt token deposit amount to Withdraw. + * @param borrowAmt token borrow amount to Withdraw. + * @param getIdDeposit Get deposit token amount at this ID from `InstaMemory` Contract. + * @param getIdBorrow Get borrow token amount at this ID from `InstaMemory` Contract. + * @param setIdDeposit Set deposit token amount at this ID in `InstaMemory` Contract. + * @param setIdBorrow Set borrow token amount at this ID in `InstaMemory` Contract. + */ + function depositAndBorrow( + uint256 safe, + uint256 depositAmt, + uint256 borrowAmt, + uint256 getIdDeposit, + uint256 getIdBorrow, + uint256 setIdDeposit, + uint256 setIdBorrow + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + GebData memory gebData; + uint _amtDeposit = getUint(getIdDeposit, depositAmt); + uint _amtBorrow = getUint(getIdBorrow, borrowAmt); + + gebData._safe = getSafe(safe); + (bytes32 collateralType, address handler) = getSafeData(gebData._safe); + + gebData.colAddr = getCollateralJoinAddress(collateralType); + gebData.tokenJoinContract = TokenJoinInterface(gebData.colAddr); + gebData.safeEngineContract = SafeEngineLike(managerContract.safeEngine()); + gebData.tokenContract = gebData.tokenJoinContract.collateral(); + + if (isEth(address(gebData.tokenContract))) { + _amtDeposit = _amtDeposit == uint(-1) ? address(this).balance : _amtDeposit; + gebData.tokenContract.deposit{value: _amtDeposit}(); + } else { + _amtDeposit = _amtDeposit == uint(-1) ? gebData.tokenContract.balanceOf(address(this)) : _amtDeposit; + } + + gebData.tokenContract.approve(address(gebData.colAddr), _amtDeposit); + gebData.tokenJoinContract.join(handler, _amtDeposit); + + managerContract.modifySAFECollateralization( + gebData._safe, + toInt(convertTo18(gebData.tokenJoinContract.decimals(), _amtDeposit)), + _getBorrowAmt( + address(gebData.safeEngineContract), + handler, + collateralType, + _amtBorrow + ) + ); + + managerContract.transferInternalCoins( + gebData._safe, + address(this), + toRad(_amtBorrow) + ); + + if (gebData.safeEngineContract.safeRights(address(this), address(coinJoinContract)) == 0) { + gebData.safeEngineContract.approveSAFEModification(address(coinJoinContract)); + } + + coinJoinContract.exit(address(this), _amtBorrow); + + setUint(setIdDeposit, _amtDeposit); + setUint(setIdBorrow, _amtBorrow); + + _eventName = "LogDepositAndBorrow(uint256,bytes32,uint256,uint256,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode( + gebData._safe, + collateralType, + _amtDeposit, + _amtBorrow, + getIdDeposit, + getIdBorrow, + setIdDeposit, + setIdBorrow + ); + } + + /** + * @dev Exit Coin from handler. + * @param safe Safe ID. + * @param amt token amount to exit. + * @param getId Get token amount at this ID from `InstaMemory` Contract. + * @param setId Set token amount at this ID in `InstaMemory` Contract. + */ + function exit( + uint256 safe, + uint256 amt, + uint256 getId, + uint256 setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amt); + uint _safe = getSafe(safe); + (bytes32 collateralType, address handler) = getSafeData(_safe); + + SafeEngineLike safeEngineContract = SafeEngineLike(managerContract.safeEngine()); + if(_amt == uint(-1)) { + _amt = safeEngineContract.coinBalance(handler); + _amt = _amt / 10 ** 27; + } + + managerContract.transferInternalCoins( + _safe, + address(this), + toRad(_amt) + ); + + if (safeEngineContract.safeRights(address(this), address(coinJoinContract)) == 0) { + safeEngineContract.approveSAFEModification(address(coinJoinContract)); + } + + coinJoinContract.exit(address(this), _amt); + + setUint(setId, _amt); + + _eventName = "LogExit(uint256,bytes32,uint256,uint256,uint256)"; + _eventParam = abi.encode(_safe, collateralType, _amt, getId, setId); + } + +} + +contract ConnectV2Reflexer is GebResolver { + string public constant name = "Reflexer-v1"; +} diff --git a/contracts/mapping/reflexer.sol b/contracts/mapping/reflexer.sol new file mode 100644 index 00000000..c44e140c --- /dev/null +++ b/contracts/mapping/reflexer.sol @@ -0,0 +1,57 @@ +pragma solidity ^0.6.0; +pragma experimental ABIEncoderV2; + +interface CollateralJoinInterface { + function collateralType() external view returns (bytes32); +} + +interface ConnectorsInterface { + function chief(address) external view returns (bool); +} + +interface IndexInterface { + function master() external view returns (address); +} + + +contract Helpers { + address public constant connectors = 0xD6A602C01a023B98Ecfb29Df02FBA380d3B21E0c; + address public constant instaIndex = 0x2971AdFa57b20E5a416aE5a708A8655A9c74f723; + uint public version = 1; + + mapping (bytes32 => address) public collateralJoinMapping; + + event LogAddCollateralJoinMapping(address[] collateralJoin); + + modifier isChief { + require( + ConnectorsInterface(connectors).chief(msg.sender) || + IndexInterface(instaIndex).master() == msg.sender, "not-Chief"); + _; + } + + function addCollateralJoinMapping(address[] memory collateralJoins) public isChief { + _addCollateralJoinMapping(collateralJoins); + } + + function _addCollateralJoinMapping(address[] memory collateralJoins) internal { + require(collateralJoins.length > 0, "No-CollateralJoin-Address"); + for(uint i = 0; i < collateralJoins.length; i++) { + address collateralJoin = collateralJoins[i]; + bytes32 collateralType = CollateralJoinInterface(collateralJoin).collateralType(); + require(collateralJoinMapping[collateralType] == address(0), "CollateralJoin-Already-Added"); + collateralJoinMapping[collateralType] = collateralJoin; + } + emit LogAddCollateralJoinMapping(collateralJoins); + } + +} + +contract GebMapping is Helpers { + string constant public name = "Reflexer-Mapping-v1"; + constructor() public { + address[] memory collateralJoins = new address[](1); + collateralJoins[0] = 0x2D3cD7b81c93f188F3CB8aD87c8Acc73d6226e3A; // ETH-A Join contract address + _addCollateralJoinMapping(collateralJoins); + } +} \ No newline at end of file