From 18ceba47104404293923030a565596a2a9e9a9db Mon Sep 17 00:00:00 2001 From: Guillaume Felley Date: Mon, 29 Mar 2021 11:57:54 +0200 Subject: [PATCH 1/9] Start with Maker connector --- contracts/connectors/reflexer/events.sol | 26 ++ contracts/connectors/reflexer/helpers.sol | 124 +++++ contracts/connectors/reflexer/interface.sol | 61 +++ contracts/connectors/reflexer/main.sol | 484 ++++++++++++++++++++ 4 files changed, 695 insertions(+) create mode 100644 contracts/connectors/reflexer/events.sol create mode 100644 contracts/connectors/reflexer/helpers.sol create mode 100644 contracts/connectors/reflexer/interface.sol create mode 100644 contracts/connectors/reflexer/main.sol diff --git a/contracts/connectors/reflexer/events.sol b/contracts/connectors/reflexer/events.sol new file mode 100644 index 00000000..702a70f3 --- /dev/null +++ b/contracts/connectors/reflexer/events.sol @@ -0,0 +1,26 @@ +pragma solidity ^0.7.0; + +contract Events { + event LogOpen(uint256 indexed vault, bytes32 indexed ilk); + event LogClose(uint256 indexed vault, bytes32 indexed ilk); + event LogTransfer(uint256 indexed vault, bytes32 indexed ilk, address newOwner); + event LogDeposit(uint256 indexed vault, bytes32 indexed ilk, uint256 tokenAmt, uint256 getId, uint256 setId); + event LogWithdraw(uint256 indexed vault, bytes32 indexed ilk, uint256 tokenAmt, uint256 getId, uint256 setId); + event LogBorrow(uint256 indexed vault, bytes32 indexed ilk, uint256 tokenAmt, uint256 getId, uint256 setId); + event LogPayback(uint256 indexed vault, bytes32 indexed ilk, uint256 tokenAmt, uint256 getId, uint256 setId); + event LogWithdrawLiquidated(uint256 indexed vault, bytes32 indexed ilk, uint256 tokenAmt, uint256 getId, uint256 setId); + event LogExitDai(uint256 indexed vault, bytes32 indexed ilk, uint256 tokenAmt, uint256 getId, uint256 setId); + event LogDepositDai(uint256 tokenAmt, uint256 getId, uint256 setId); + event LogWithdrawDai(uint256 tokenAmt, uint256 getId, uint256 setId); + + event LogDepositAndBorrow( + uint256 indexed vault, + bytes32 indexed ilk, + uint256 depositAmt, + uint256 borrowAmt, + uint256 getIdDeposit, + uint256 getIdBorrow, + uint256 setIdDeposit, + uint256 setIdBorrow + ); +} \ No newline at end of file diff --git a/contracts/connectors/reflexer/helpers.sol b/contracts/connectors/reflexer/helpers.sol new file mode 100644 index 00000000..a6c2b506 --- /dev/null +++ b/contracts/connectors/reflexer/helpers.sol @@ -0,0 +1,124 @@ +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, DaiJoinInterface, PotLike, VatLike, JugLike } from "./interface.sol"; + +abstract contract Helpers is DSMath, Basic { + /** + * @dev Manager Interface + */ + ManagerLike internal constant managerContract = ManagerLike(0x5ef30b9986345249bc32d8928B7ee64DE9435E39); + + /** + * @dev DAI Join + */ + DaiJoinInterface internal constant daiJoinContract = DaiJoinInterface(0x9759A6Ac90977b93B58547b4A71c78317f391A28); + + /** + * @dev Pot + */ + PotLike internal constant potContract = PotLike(0x197E90f9FAD81970bA7976f33CbD77088E5D7cf7); + + /** + * @dev Maker MCD Jug Address. + */ + JugLike internal constant mcdJug = JugLike(0x19c0976f590D67707E62397C87829d896Dc0f1F1); + + /** + * @dev Return Close Vault Address. + */ + address internal constant giveAddr = 0x4dD58550eb15190a5B3DfAE28BB14EeC181fC267; + + /** + * @dev Get Vault's ilk. + */ + function getVaultData(uint vault) internal view returns (bytes32 ilk, address urn) { + ilk = managerContract.ilks(vault); + urn = managerContract.urns(vault); + } + + /** + * @dev Gem Join address is ETH type collateral. + */ + function isEth(address tknAddr) internal pure returns (bool) { + return tknAddr == ethAddr ? true : false; + } + + /** + * @dev Get Vault Debt Amount. + */ + function _getVaultDebt( + address vat, + bytes32 ilk, + address urn + ) internal view returns (uint wad) { + (, uint rate,,,) = VatLike(vat).ilks(ilk); + (, uint art) = VatLike(vat).urns(ilk, urn); + uint dai = VatLike(vat).dai(urn); + + uint rad = sub(mul(art, rate), dai); + wad = rad / RAY; + + wad = mul(wad, RAY) < rad ? wad + 1 : wad; + } + + /** + * @dev Get Borrow Amount. + */ + function _getBorrowAmt( + address vat, + address urn, + bytes32 ilk, + uint amt + ) internal returns (int dart) + { + uint rate = mcdJug.drip(ilk); + uint dai = VatLike(vat).dai(urn); + if (dai < mul(amt, RAY)) { + dart = toInt(sub(mul(amt, RAY), dai) / rate); + dart = mul(uint(dart), rate) < mul(amt, RAY) ? dart + 1 : dart; + } + } + + /** + * @dev Get Payback Amount. + */ + function _getWipeAmt( + address vat, + uint amt, + address urn, + bytes32 ilk + ) internal view returns (int dart) + { + (, uint rate,,,) = VatLike(vat).ilks(ilk); + (, uint art) = VatLike(vat).urns(ilk, urn); + dart = toInt(amt / rate); + dart = uint(dart) <= art ? - dart : - toInt(art); + } + + /** + * @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 vault ID. If `vault` is 0, get last opened vault. + */ + function getVault(uint vault) internal view returns (uint _vault) { + if (vault == 0) { + require(managerContract.count(address(this)) > 0, "no-vault-opened"); + _vault = managerContract.last(address(this)); + } else { + _vault = vault; + } + } + +} \ 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..50aa6cd1 --- /dev/null +++ b/contracts/connectors/reflexer/interface.sol @@ -0,0 +1,61 @@ +pragma solidity ^0.7.0; + +import { TokenInterface } from "../../common/interfaces.sol"; + +interface ManagerLike { + function cdpCan(address, uint, address) external view returns (uint); + function ilks(uint) external view returns (bytes32); + function last(address) external view returns (uint); + function count(address) external view returns (uint); + function owns(uint) external view returns (address); + function urns(uint) external view returns (address); + function vat() external view returns (address); + function open(bytes32, address) external returns (uint); + function give(uint, address) external; + function frob(uint, int, int) external; + function flux(uint, address, uint) external; + function move(uint, address, uint) external; +} + +interface VatLike { + function can(address, address) external view returns (uint); + 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 frob( + bytes32, + address, + address, + address, + int, + int + ) external; + function hope(address) external; + function move(address, address, uint) external; + function gem(bytes32, address) external view returns (uint); +} + +interface TokenJoinInterface { + function dec() external returns (uint); + function gem() external returns (TokenInterface); + function join(address, uint) external payable; + function exit(address, uint) external; +} + +interface DaiJoinInterface { + function vat() external returns (VatLike); + function dai() external returns (TokenInterface); + function join(address, uint) external payable; + function exit(address, uint) external; +} + +interface JugLike { + function drip(bytes32) external returns (uint); +} + +interface PotLike { + function pie(address) external view returns (uint); + function drip() external returns (uint); + function join(uint) external; + function exit(uint) external; +} diff --git a/contracts/connectors/reflexer/main.sol b/contracts/connectors/reflexer/main.sol new file mode 100644 index 00000000..139438d3 --- /dev/null +++ b/contracts/connectors/reflexer/main.sol @@ -0,0 +1,484 @@ +pragma solidity ^0.7.0; + +import { TokenInterface } from "../../common/interfaces.sol"; +import { Helpers } from "./helpers.sol"; +import { Events } from "./events.sol"; +import { VatLike, TokenJoinInterface } from "./interface.sol"; + +abstract contract MakerResolver is Helpers, Events { + /** + * @dev Open Vault + * @param colType Type of Collateral.(eg: 'ETH-A') + */ + function open(string calldata colType) external payable returns (string memory _eventName, bytes memory _eventParam) { + bytes32 ilk = stringToBytes32(colType); + require(instaMapping.gemJoinMapping(ilk) != address(0), "wrong-col-type"); + uint256 vault = managerContract.open(ilk, address(this)); + + _eventName = "LogOpen(uint256,bytes32)"; + _eventParam = abi.encode(vault, ilk); + } + + /** + * @dev Close Vault + * @param vault Vault ID to close. + */ + function close(uint256 vault) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _vault = getVault(vault); + (bytes32 ilk, address urn) = getVaultData(_vault); + (uint ink, uint art) = VatLike(managerContract.vat()).urns(ilk, urn); + + require(ink == 0 && art == 0, "vault-has-assets"); + require(managerContract.owns(_vault) == address(this), "not-owner"); + + managerContract.give(_vault, giveAddr); + + _eventName = "LogClose(uint256,bytes32)"; + _eventParam = abi.encode(_vault, ilk); + } + + /** + * @dev Deposit ETH/ERC20_Token Collateral. + * @param vault Vault 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 vault, + uint256 amt, + uint256 getId, + uint256 setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amt); + uint _vault = getVault(vault); + (bytes32 ilk, address urn) = getVaultData(_vault); + + address colAddr = instaMapping.gemJoinMapping(ilk); + TokenJoinInterface tokenJoinContract = TokenJoinInterface(colAddr); + TokenInterface tokenContract = tokenJoinContract.gem(); + + 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); + + VatLike(managerContract.vat()).frob( + ilk, + urn, + address(this), + address(this), + toInt(convertTo18(tokenJoinContract.dec(), _amt)), + 0 + ); + + setUint(setId, _amt); + + _eventName = "LogDeposit(uint256,bytes32,uint256,uint256,uint256)"; + _eventParam = abi.encode(_vault, ilk, _amt, getId, setId); + } + + /** + * @dev Withdraw ETH/ERC20_Token Collateral. + * @param vault Vault 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 vault, + uint256 amt, + uint256 getId, + uint256 setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amt); + uint _vault = getVault(vault); + (bytes32 ilk, address urn) = getVaultData(_vault); + + address colAddr = instaMapping.gemJoinMapping(ilk); + TokenJoinInterface tokenJoinContract = TokenJoinInterface(colAddr); + + uint _amt18; + if (_amt == uint(-1)) { + (_amt18,) = VatLike(managerContract.vat()).urns(ilk, urn); + _amt = convert18ToDec(tokenJoinContract.dec(), _amt18); + } else { + _amt18 = convertTo18(tokenJoinContract.dec(), _amt); + } + + managerContract.frob( + _vault, + -toInt(_amt18), + 0 + ); + + managerContract.flux( + _vault, + address(this), + _amt18 + ); + + TokenInterface tokenContract = tokenJoinContract.gem(); + + 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(_vault, ilk, _amt, getId, setId); + } + + /** + * @dev Borrow DAI. + * @param vault Vault 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 vault, + uint256 amt, + uint256 getId, + uint256 setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amt); + uint _vault = getVault(vault); + (bytes32 ilk, address urn) = getVaultData(_vault); + + VatLike vatContract = VatLike(managerContract.vat()); + + managerContract.frob( + _vault, + 0, + _getBorrowAmt( + address(vatContract), + urn, + ilk, + _amt + ) + ); + + managerContract.move( + _vault, + address(this), + toRad(_amt) + ); + + if (vatContract.can(address(this), address(daiJoinContract)) == 0) { + vatContract.hope(address(daiJoinContract)); + } + + daiJoinContract.exit(address(this), _amt); + + setUint(setId, _amt); + + _eventName = "LogBorrow(uint256,bytes32,uint256,uint256,uint256)"; + _eventParam = abi.encode(_vault, ilk, _amt, getId, setId); + } + + /** + * @dev Payback borrowed DAI. + * @param vault Vault 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 vault, + uint256 amt, + uint256 getId, + uint256 setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amt); + uint _vault = getVault(vault); + (bytes32 ilk, address urn) = getVaultData(_vault); + + address vat = managerContract.vat(); + + uint _maxDebt = _getVaultDebt(vat, ilk, urn); + + _amt = _amt == uint(-1) ? _maxDebt : _amt; + + require(_maxDebt >= _amt, "paying-excess-debt"); + + daiJoinContract.dai().approve(address(daiJoinContract), _amt); + daiJoinContract.join(urn, _amt); + + managerContract.frob( + _vault, + 0, + _getWipeAmt( + vat, + VatLike(vat).dai(urn), + urn, + ilk + ) + ); + + setUint(setId, _amt); + + _eventName = "LogPayback(uint256,bytes32,uint256,uint256,uint256)"; + _eventParam = abi.encode(_vault, ilk, _amt, getId, setId); + } + + /** + * @dev Withdraw leftover ETH/ERC20_Token after Liquidation. + * @param vault Vault 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 vault, + uint256 amt, + uint256 getId, + uint256 setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amt); + (bytes32 ilk, address urn) = getVaultData(vault); + + address colAddr = instaMapping.gemJoinMapping(ilk); + TokenJoinInterface tokenJoinContract = TokenJoinInterface(colAddr); + + uint _amt18; + if (_amt == uint(-1)) { + _amt18 = VatLike(managerContract.vat()).gem(ilk, urn); + _amt = convert18ToDec(tokenJoinContract.dec(), _amt18); + } else { + _amt18 = convertTo18(tokenJoinContract.dec(), _amt); + } + + managerContract.flux( + vault, + address(this), + _amt18 + ); + + TokenInterface tokenContract = tokenJoinContract.gem(); + 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(vault, ilk, _amt, getId, setId); + } + + struct MakerData { + uint _vault; + address colAddr; + TokenJoinInterface tokenJoinContract; + VatLike vatContract; + TokenInterface tokenContract; + } + /** + * @dev Deposit ETH/ERC20_Token Collateral and Borrow DAI. + * @param vault Vault 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 vault, + uint256 depositAmt, + uint256 borrowAmt, + uint256 getIdDeposit, + uint256 getIdBorrow, + uint256 setIdDeposit, + uint256 setIdBorrow + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + MakerData memory makerData; + uint _amtDeposit = getUint(getIdDeposit, depositAmt); + uint _amtBorrow = getUint(getIdBorrow, borrowAmt); + + makerData._vault = getVault(vault); + (bytes32 ilk, address urn) = getVaultData(makerData._vault); + + makerData.colAddr = instaMapping.gemJoinMapping(ilk); + makerData.tokenJoinContract = TokenJoinInterface(makerData.colAddr); + makerData.vatContract = VatLike(managerContract.vat()); + makerData.tokenContract = makerData.tokenJoinContract.gem(); + + if (isEth(address(makerData.tokenContract))) { + _amtDeposit = _amtDeposit == uint(-1) ? address(this).balance : _amtDeposit; + makerData.tokenContract.deposit{value: _amtDeposit}(); + } else { + _amtDeposit = _amtDeposit == uint(-1) ? makerData.tokenContract.balanceOf(address(this)) : _amtDeposit; + } + + makerData.tokenContract.approve(address(makerData.colAddr), _amtDeposit); + makerData.tokenJoinContract.join(urn, _amtDeposit); + + managerContract.frob( + makerData._vault, + toInt(convertTo18(makerData.tokenJoinContract.dec(), _amtDeposit)), + _getBorrowAmt( + address(makerData.vatContract), + urn, + ilk, + _amtBorrow + ) + ); + + managerContract.move( + makerData._vault, + address(this), + toRad(_amtBorrow) + ); + + if (makerData.vatContract.can(address(this), address(daiJoinContract)) == 0) { + makerData.vatContract.hope(address(daiJoinContract)); + } + + daiJoinContract.exit(address(this), _amtBorrow); + + setUint(setIdDeposit, _amtDeposit); + setUint(setIdBorrow, _amtBorrow); + + _eventName = "LogDepositAndBorrow(uint256,bytes32,uint256,uint256,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode( + makerData._vault, + ilk, + _amtDeposit, + _amtBorrow, + getIdDeposit, + getIdBorrow, + setIdDeposit, + setIdBorrow + ); + } + + /** + * @dev Exit DAI from urn. + * @param vault Vault 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 exitDai( + uint256 vault, + uint256 amt, + uint256 getId, + uint256 setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amt); + uint _vault = getVault(vault); + (bytes32 ilk, address urn) = getVaultData(_vault); + + VatLike vatContract = VatLike(managerContract.vat()); + if(_amt == uint(-1)) { + _amt = vatContract.dai(urn); + _amt = _amt / 10 ** 27; + } + + managerContract.move( + _vault, + address(this), + toRad(_amt) + ); + + if (vatContract.can(address(this), address(daiJoinContract)) == 0) { + vatContract.hope(address(daiJoinContract)); + } + + daiJoinContract.exit(address(this), _amt); + + setUint(setId, _amt); + + _eventName = "LogExitDai(uint256,bytes32,uint256,uint256,uint256)"; + _eventParam = abi.encode(_vault, ilk, _amt, getId, setId); + } + + /** + * @dev Deposit DAI in DSR. + * @param amt DAI 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 depositDai( + uint256 amt, + uint256 getId, + uint256 setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amt); + + _amt = _amt == uint(-1) ? + daiJoinContract.dai().balanceOf(address(this)) : + _amt; + + VatLike vat = daiJoinContract.vat(); + uint chi = potContract.drip(); + + daiJoinContract.dai().approve(address(daiJoinContract), _amt); + daiJoinContract.join(address(this), _amt); + if (vat.can(address(this), address(potContract)) == 0) { + vat.hope(address(potContract)); + } + + potContract.join(mul(_amt, RAY) / chi); + setUint(setId, _amt); + + _eventName = "LogDepositDai(uint256,uint256,uint256)"; + _eventParam = abi.encode(_amt, getId, setId); + } + + /** + * @dev Withdraw DAI from DSR. + * @param amt DAI 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 withdrawDai( + uint256 amt, + uint256 getId, + uint256 setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amt); + + VatLike vat = daiJoinContract.vat(); + + uint chi = potContract.drip(); + uint pie; + if (_amt == uint(-1)) { + pie = potContract.pie(address(this)); + _amt = mul(chi, pie) / RAY; + } else { + pie = mul(_amt, RAY) / chi; + } + + potContract.exit(pie); + + uint bal = vat.dai(address(this)); + if (vat.can(address(this), address(daiJoinContract)) == 0) { + vat.hope(address(daiJoinContract)); + } + daiJoinContract.exit( + address(this), + bal >= mul(_amt, RAY) ? _amt : bal / RAY + ); + + setUint(setId, _amt); + + _eventName = "LogWithdrawDai(uint256,uint256,uint256)"; + _eventParam = abi.encode(_amt, getId, setId); + } +} + +contract ConnectV2Maker is MakerResolver { + string public constant name = "MakerDao-v1"; +} From 14892e6e9c699b89f95f40b26829be383ae399f8 Mon Sep 17 00:00:00 2001 From: Guillaume Felley Date: Mon, 29 Mar 2021 12:34:18 +0200 Subject: [PATCH 2/9] Rename all exteranl interfaces --- contracts/connectors/reflexer/helpers.sol | 41 +++--- contracts/connectors/reflexer/interface.sol | 61 ++++---- contracts/connectors/reflexer/main.sol | 149 +++++--------------- 3 files changed, 83 insertions(+), 168 deletions(-) diff --git a/contracts/connectors/reflexer/helpers.sol b/contracts/connectors/reflexer/helpers.sol index a6c2b506..2cb478e2 100644 --- a/contracts/connectors/reflexer/helpers.sol +++ b/contracts/connectors/reflexer/helpers.sol @@ -3,7 +3,7 @@ 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, DaiJoinInterface, PotLike, VatLike, JugLike } from "./interface.sol"; +import { ManagerLike, CoinJoinInterface, SafeEngineLike, TaxCollectorLike } from "./interface.sol"; abstract contract Helpers is DSMath, Basic { /** @@ -14,17 +14,12 @@ abstract contract Helpers is DSMath, Basic { /** * @dev DAI Join */ - DaiJoinInterface internal constant daiJoinContract = DaiJoinInterface(0x9759A6Ac90977b93B58547b4A71c78317f391A28); - - /** - * @dev Pot - */ - PotLike internal constant potContract = PotLike(0x197E90f9FAD81970bA7976f33CbD77088E5D7cf7); + CoinJoinInterface internal constant daiJoinContract = CoinJoinInterface(0x9759A6Ac90977b93B58547b4A71c78317f391A28); /** * @dev Maker MCD Jug Address. */ - JugLike internal constant mcdJug = JugLike(0x19c0976f590D67707E62397C87829d896Dc0f1F1); + TaxCollectorLike internal constant mcdJug = TaxCollectorLike(0x19c0976f590D67707E62397C87829d896Dc0f1F1); /** * @dev Return Close Vault Address. @@ -35,8 +30,8 @@ abstract contract Helpers is DSMath, Basic { * @dev Get Vault's ilk. */ function getVaultData(uint vault) internal view returns (bytes32 ilk, address urn) { - ilk = managerContract.ilks(vault); - urn = managerContract.urns(vault); + ilk = managerContract.collateralTypes(vault); + urn = managerContract.safes(vault); } /** @@ -54,11 +49,11 @@ abstract contract Helpers is DSMath, Basic { bytes32 ilk, address urn ) internal view returns (uint wad) { - (, uint rate,,,) = VatLike(vat).ilks(ilk); - (, uint art) = VatLike(vat).urns(ilk, urn); - uint dai = VatLike(vat).dai(urn); + (, uint rate,,,) = SafeEngineLike(vat).collateralTypes(ilk); + (, uint art) = SafeEngineLike(vat).safes(ilk, urn); + uint coin = SafeEngineLike(vat).coin(urn); - uint rad = sub(mul(art, rate), dai); + uint rad = sub(mul(art, rate), coin); wad = rad / RAY; wad = mul(wad, RAY) < rad ? wad + 1 : wad; @@ -74,10 +69,10 @@ abstract contract Helpers is DSMath, Basic { uint amt ) internal returns (int dart) { - uint rate = mcdJug.drip(ilk); - uint dai = VatLike(vat).dai(urn); - if (dai < mul(amt, RAY)) { - dart = toInt(sub(mul(amt, RAY), dai) / rate); + uint rate = mcdJug.taxSingle(ilk); + uint coin = SafeEngineLike(vat).coin(urn); + if (coin < mul(amt, RAY)) { + dart = toInt(sub(mul(amt, RAY), coin) / rate); dart = mul(uint(dart), rate) < mul(amt, RAY) ? dart + 1 : dart; } } @@ -92,8 +87,8 @@ abstract contract Helpers is DSMath, Basic { bytes32 ilk ) internal view returns (int dart) { - (, uint rate,,,) = VatLike(vat).ilks(ilk); - (, uint art) = VatLike(vat).urns(ilk, urn); + (, uint rate,,,) = SafeEngineLike(vat).collateralTypes(ilk); + (, uint art) = SafeEngineLike(vat).safes(ilk, urn); dart = toInt(amt / rate); dart = uint(dart) <= art ? - dart : - toInt(art); } @@ -110,12 +105,12 @@ abstract contract Helpers is DSMath, Basic { } /** - * @dev Get vault ID. If `vault` is 0, get last opened vault. + * @dev Get vault ID. If `vault` is 0, get lastSAFEID opened vault. */ function getVault(uint vault) internal view returns (uint _vault) { if (vault == 0) { - require(managerContract.count(address(this)) > 0, "no-vault-opened"); - _vault = managerContract.last(address(this)); + require(managerContract.safeCount(address(this)) > 0, "no-vault-opened"); + _vault = managerContract.lastSAFEID(address(this)); } else { _vault = vault; } diff --git a/contracts/connectors/reflexer/interface.sol b/contracts/connectors/reflexer/interface.sol index 50aa6cd1..983734d7 100644 --- a/contracts/connectors/reflexer/interface.sol +++ b/contracts/connectors/reflexer/interface.sol @@ -3,26 +3,26 @@ pragma solidity ^0.7.0; import { TokenInterface } from "../../common/interfaces.sol"; interface ManagerLike { - function cdpCan(address, uint, address) external view returns (uint); - function ilks(uint) external view returns (bytes32); - function last(address) external view returns (uint); - function count(address) external view returns (uint); - function owns(uint) external view returns (address); - function urns(uint) external view returns (address); - function vat() external view returns (address); - function open(bytes32, address) external returns (uint); - function give(uint, address) external; - function frob(uint, int, int) external; - function flux(uint, address, uint) external; - function move(uint, address, uint) external; + 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 VatLike { +interface SafeEngineLike { function can(address, address) external view returns (uint); - 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 frob( + function collateralTypes(bytes32) external view returns (uint, uint, uint, uint, uint); + function coin(address) external view returns (uint); + function safes(bytes32, address) external view returns (uint, uint); + function modifySAFECollateralization( bytes32, address, address, @@ -30,32 +30,25 @@ interface VatLike { int, int ) external; - function hope(address) external; - function move(address, address, uint) external; - function gem(bytes32, address) external view returns (uint); + function approveSAFEModification(address) external; + function transferInternalCoins(address, address, uint) external; + function tokenCollateral(bytes32, address) external view returns (uint); } interface TokenJoinInterface { - function dec() external returns (uint); - function gem() external returns (TokenInterface); + function decimals() external returns (uint); + function collateral() external returns (TokenInterface); function join(address, uint) external payable; function exit(address, uint) external; } -interface DaiJoinInterface { - function vat() external returns (VatLike); - function dai() external returns (TokenInterface); +interface CoinJoinInterface { + function safeEngine() external returns (SafeEngineLike); + function coin() external returns (TokenInterface); function join(address, uint) external payable; function exit(address, uint) external; } -interface JugLike { - function drip(bytes32) external returns (uint); -} - -interface PotLike { - function pie(address) external view returns (uint); - function drip() external returns (uint); - function join(uint) external; - function exit(uint) external; +interface TaxCollectorLike { + function taxSingle(bytes32) external returns (uint); } diff --git a/contracts/connectors/reflexer/main.sol b/contracts/connectors/reflexer/main.sol index 139438d3..c28fba79 100644 --- a/contracts/connectors/reflexer/main.sol +++ b/contracts/connectors/reflexer/main.sol @@ -3,7 +3,7 @@ pragma solidity ^0.7.0; import { TokenInterface } from "../../common/interfaces.sol"; import { Helpers } from "./helpers.sol"; import { Events } from "./events.sol"; -import { VatLike, TokenJoinInterface } from "./interface.sol"; +import { SafeEngineLike, TokenJoinInterface } from "./interface.sol"; abstract contract MakerResolver is Helpers, Events { /** @@ -13,7 +13,7 @@ abstract contract MakerResolver is Helpers, Events { function open(string calldata colType) external payable returns (string memory _eventName, bytes memory _eventParam) { bytes32 ilk = stringToBytes32(colType); require(instaMapping.gemJoinMapping(ilk) != address(0), "wrong-col-type"); - uint256 vault = managerContract.open(ilk, address(this)); + uint256 vault = managerContract.openSAFE(ilk, address(this)); _eventName = "LogOpen(uint256,bytes32)"; _eventParam = abi.encode(vault, ilk); @@ -26,12 +26,12 @@ abstract contract MakerResolver is Helpers, Events { function close(uint256 vault) external payable returns (string memory _eventName, bytes memory _eventParam) { uint _vault = getVault(vault); (bytes32 ilk, address urn) = getVaultData(_vault); - (uint ink, uint art) = VatLike(managerContract.vat()).urns(ilk, urn); + (uint ink, uint art) = SafeEngineLike(managerContract.safeEngine()).safes(ilk, urn); require(ink == 0 && art == 0, "vault-has-assets"); - require(managerContract.owns(_vault) == address(this), "not-owner"); + require(managerContract.ownsSAFE(_vault) == address(this), "not-owner"); - managerContract.give(_vault, giveAddr); + managerContract.transferSAFEOwnership(_vault, giveAddr); _eventName = "LogClose(uint256,bytes32)"; _eventParam = abi.encode(_vault, ilk); @@ -56,7 +56,7 @@ abstract contract MakerResolver is Helpers, Events { address colAddr = instaMapping.gemJoinMapping(ilk); TokenJoinInterface tokenJoinContract = TokenJoinInterface(colAddr); - TokenInterface tokenContract = tokenJoinContract.gem(); + TokenInterface tokenContract = tokenJoinContract.collateral(); if (isEth(address(tokenContract))) { _amt = _amt == uint(-1) ? address(this).balance : _amt; @@ -68,12 +68,12 @@ abstract contract MakerResolver is Helpers, Events { tokenContract.approve(address(colAddr), _amt); tokenJoinContract.join(address(this), _amt); - VatLike(managerContract.vat()).frob( + SafeEngineLike(managerContract.safeEngine()).modifySAFECollateralization( ilk, urn, address(this), address(this), - toInt(convertTo18(tokenJoinContract.dec(), _amt)), + toInt(convertTo18(tokenJoinContract.decimals(), _amt)), 0 ); @@ -105,25 +105,25 @@ abstract contract MakerResolver is Helpers, Events { uint _amt18; if (_amt == uint(-1)) { - (_amt18,) = VatLike(managerContract.vat()).urns(ilk, urn); - _amt = convert18ToDec(tokenJoinContract.dec(), _amt18); + (_amt18,) = SafeEngineLike(managerContract.safeEngine()).safes(ilk, urn); + _amt = convert18ToDec(tokenJoinContract.decimals(), _amt18); } else { - _amt18 = convertTo18(tokenJoinContract.dec(), _amt); + _amt18 = convertTo18(tokenJoinContract.decimals(), _amt); } - managerContract.frob( + managerContract.modifySAFECollateralization( _vault, -toInt(_amt18), 0 ); - managerContract.flux( + managerContract.transferCollateral( _vault, address(this), _amt18 ); - TokenInterface tokenContract = tokenJoinContract.gem(); + TokenInterface tokenContract = tokenJoinContract.collateral(); if (isEth(address(tokenContract))) { tokenJoinContract.exit(address(this), _amt); @@ -155,9 +155,9 @@ abstract contract MakerResolver is Helpers, Events { uint _vault = getVault(vault); (bytes32 ilk, address urn) = getVaultData(_vault); - VatLike vatContract = VatLike(managerContract.vat()); + SafeEngineLike vatContract = SafeEngineLike(managerContract.safeEngine()); - managerContract.frob( + managerContract.modifySAFECollateralization( _vault, 0, _getBorrowAmt( @@ -168,14 +168,14 @@ abstract contract MakerResolver is Helpers, Events { ) ); - managerContract.move( + managerContract.transferInternalCoins( _vault, address(this), toRad(_amt) ); if (vatContract.can(address(this), address(daiJoinContract)) == 0) { - vatContract.hope(address(daiJoinContract)); + vatContract.approveSAFEModification(address(daiJoinContract)); } daiJoinContract.exit(address(this), _amt); @@ -203,7 +203,7 @@ abstract contract MakerResolver is Helpers, Events { uint _vault = getVault(vault); (bytes32 ilk, address urn) = getVaultData(_vault); - address vat = managerContract.vat(); + address vat = managerContract.safeEngine(); uint _maxDebt = _getVaultDebt(vat, ilk, urn); @@ -211,15 +211,15 @@ abstract contract MakerResolver is Helpers, Events { require(_maxDebt >= _amt, "paying-excess-debt"); - daiJoinContract.dai().approve(address(daiJoinContract), _amt); + daiJoinContract.coin().approve(address(daiJoinContract), _amt); daiJoinContract.join(urn, _amt); - managerContract.frob( + managerContract.modifySAFECollateralization( _vault, 0, _getWipeAmt( vat, - VatLike(vat).dai(urn), + SafeEngineLike(vat).coin(urn), urn, ilk ) @@ -252,19 +252,19 @@ abstract contract MakerResolver is Helpers, Events { uint _amt18; if (_amt == uint(-1)) { - _amt18 = VatLike(managerContract.vat()).gem(ilk, urn); - _amt = convert18ToDec(tokenJoinContract.dec(), _amt18); + _amt18 = SafeEngineLike(managerContract.safeEngine()).tokenCollateral(ilk, urn); + _amt = convert18ToDec(tokenJoinContract.decimals(), _amt18); } else { - _amt18 = convertTo18(tokenJoinContract.dec(), _amt); + _amt18 = convertTo18(tokenJoinContract.decimals(), _amt); } - managerContract.flux( + managerContract.transferCollateral( vault, address(this), _amt18 ); - TokenInterface tokenContract = tokenJoinContract.gem(); + TokenInterface tokenContract = tokenJoinContract.collateral(); tokenJoinContract.exit(address(this), _amt); if (isEth(address(tokenContract))) { tokenContract.withdraw(_amt); @@ -280,7 +280,7 @@ abstract contract MakerResolver is Helpers, Events { uint _vault; address colAddr; TokenJoinInterface tokenJoinContract; - VatLike vatContract; + SafeEngineLike vatContract; TokenInterface tokenContract; } /** @@ -311,8 +311,8 @@ abstract contract MakerResolver is Helpers, Events { makerData.colAddr = instaMapping.gemJoinMapping(ilk); makerData.tokenJoinContract = TokenJoinInterface(makerData.colAddr); - makerData.vatContract = VatLike(managerContract.vat()); - makerData.tokenContract = makerData.tokenJoinContract.gem(); + makerData.vatContract = SafeEngineLike(managerContract.safeEngine()); + makerData.tokenContract = makerData.tokenJoinContract.collateral(); if (isEth(address(makerData.tokenContract))) { _amtDeposit = _amtDeposit == uint(-1) ? address(this).balance : _amtDeposit; @@ -324,9 +324,9 @@ abstract contract MakerResolver is Helpers, Events { makerData.tokenContract.approve(address(makerData.colAddr), _amtDeposit); makerData.tokenJoinContract.join(urn, _amtDeposit); - managerContract.frob( + managerContract.modifySAFECollateralization( makerData._vault, - toInt(convertTo18(makerData.tokenJoinContract.dec(), _amtDeposit)), + toInt(convertTo18(makerData.tokenJoinContract.decimals(), _amtDeposit)), _getBorrowAmt( address(makerData.vatContract), urn, @@ -335,14 +335,14 @@ abstract contract MakerResolver is Helpers, Events { ) ); - managerContract.move( + managerContract.transferInternalCoins( makerData._vault, address(this), toRad(_amtBorrow) ); if (makerData.vatContract.can(address(this), address(daiJoinContract)) == 0) { - makerData.vatContract.hope(address(daiJoinContract)); + makerData.vatContract.approveSAFEModification(address(daiJoinContract)); } daiJoinContract.exit(address(this), _amtBorrow); @@ -380,20 +380,20 @@ abstract contract MakerResolver is Helpers, Events { uint _vault = getVault(vault); (bytes32 ilk, address urn) = getVaultData(_vault); - VatLike vatContract = VatLike(managerContract.vat()); + SafeEngineLike vatContract = SafeEngineLike(managerContract.safeEngine()); if(_amt == uint(-1)) { - _amt = vatContract.dai(urn); + _amt = vatContract.coin(urn); _amt = _amt / 10 ** 27; } - managerContract.move( + managerContract.transferInternalCoins( _vault, address(this), toRad(_amt) ); if (vatContract.can(address(this), address(daiJoinContract)) == 0) { - vatContract.hope(address(daiJoinContract)); + vatContract.approveSAFEModification(address(daiJoinContract)); } daiJoinContract.exit(address(this), _amt); @@ -404,79 +404,6 @@ abstract contract MakerResolver is Helpers, Events { _eventParam = abi.encode(_vault, ilk, _amt, getId, setId); } - /** - * @dev Deposit DAI in DSR. - * @param amt DAI 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 depositDai( - uint256 amt, - uint256 getId, - uint256 setId - ) external payable returns (string memory _eventName, bytes memory _eventParam) { - uint _amt = getUint(getId, amt); - - _amt = _amt == uint(-1) ? - daiJoinContract.dai().balanceOf(address(this)) : - _amt; - - VatLike vat = daiJoinContract.vat(); - uint chi = potContract.drip(); - - daiJoinContract.dai().approve(address(daiJoinContract), _amt); - daiJoinContract.join(address(this), _amt); - if (vat.can(address(this), address(potContract)) == 0) { - vat.hope(address(potContract)); - } - - potContract.join(mul(_amt, RAY) / chi); - setUint(setId, _amt); - - _eventName = "LogDepositDai(uint256,uint256,uint256)"; - _eventParam = abi.encode(_amt, getId, setId); - } - - /** - * @dev Withdraw DAI from DSR. - * @param amt DAI 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 withdrawDai( - uint256 amt, - uint256 getId, - uint256 setId - ) external payable returns (string memory _eventName, bytes memory _eventParam) { - uint _amt = getUint(getId, amt); - - VatLike vat = daiJoinContract.vat(); - - uint chi = potContract.drip(); - uint pie; - if (_amt == uint(-1)) { - pie = potContract.pie(address(this)); - _amt = mul(chi, pie) / RAY; - } else { - pie = mul(_amt, RAY) / chi; - } - - potContract.exit(pie); - - uint bal = vat.dai(address(this)); - if (vat.can(address(this), address(daiJoinContract)) == 0) { - vat.hope(address(daiJoinContract)); - } - daiJoinContract.exit( - address(this), - bal >= mul(_amt, RAY) ? _amt : bal / RAY - ); - - setUint(setId, _amt); - - _eventName = "LogWithdrawDai(uint256,uint256,uint256)"; - _eventParam = abi.encode(_amt, getId, setId); - } } contract ConnectV2Maker is MakerResolver { From 770fd97f45bae58405a39163a381b8c6a3775b1b Mon Sep 17 00:00:00 2001 From: Guillaume Felley Date: Mon, 29 Mar 2021 17:20:34 +0200 Subject: [PATCH 3/9] Internal function renaming --- contracts/connectors/reflexer/events.sol | 26 +-- contracts/connectors/reflexer/helpers.sol | 86 ++++---- contracts/connectors/reflexer/interface.sol | 11 +- contracts/connectors/reflexer/main.sol | 230 ++++++++++---------- 4 files changed, 181 insertions(+), 172 deletions(-) diff --git a/contracts/connectors/reflexer/events.sol b/contracts/connectors/reflexer/events.sol index 702a70f3..d5067dcd 100644 --- a/contracts/connectors/reflexer/events.sol +++ b/contracts/connectors/reflexer/events.sol @@ -1,21 +1,21 @@ pragma solidity ^0.7.0; contract Events { - event LogOpen(uint256 indexed vault, bytes32 indexed ilk); - event LogClose(uint256 indexed vault, bytes32 indexed ilk); - event LogTransfer(uint256 indexed vault, bytes32 indexed ilk, address newOwner); - event LogDeposit(uint256 indexed vault, bytes32 indexed ilk, uint256 tokenAmt, uint256 getId, uint256 setId); - event LogWithdraw(uint256 indexed vault, bytes32 indexed ilk, uint256 tokenAmt, uint256 getId, uint256 setId); - event LogBorrow(uint256 indexed vault, bytes32 indexed ilk, uint256 tokenAmt, uint256 getId, uint256 setId); - event LogPayback(uint256 indexed vault, bytes32 indexed ilk, uint256 tokenAmt, uint256 getId, uint256 setId); - event LogWithdrawLiquidated(uint256 indexed vault, bytes32 indexed ilk, uint256 tokenAmt, uint256 getId, uint256 setId); - event LogExitDai(uint256 indexed vault, bytes32 indexed ilk, uint256 tokenAmt, uint256 getId, uint256 setId); - event LogDepositDai(uint256 tokenAmt, uint256 getId, uint256 setId); - event LogWithdrawDai(uint256 tokenAmt, uint256 getId, uint256 setId); + 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 LogDeposit(uint256 tokenAmt, uint256 getId, uint256 setId); + event LogWithdraw(uint256 tokenAmt, uint256 getId, uint256 setId); event LogDepositAndBorrow( - uint256 indexed vault, - bytes32 indexed ilk, + uint256 indexed safe, + bytes32 indexed collateralType, uint256 depositAmt, uint256 borrowAmt, uint256 getIdDeposit, diff --git a/contracts/connectors/reflexer/helpers.sol b/contracts/connectors/reflexer/helpers.sol index 2cb478e2..278fe306 100644 --- a/contracts/connectors/reflexer/helpers.sol +++ b/contracts/connectors/reflexer/helpers.sol @@ -3,57 +3,57 @@ 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 } from "./interface.sol"; +import { ManagerLike, CoinJoinInterface, SafeEngineLike, TaxCollectorLike, TokenJoinInterface } from "./interface.sol"; abstract contract Helpers is DSMath, Basic { /** * @dev Manager Interface */ - ManagerLike internal constant managerContract = ManagerLike(0x5ef30b9986345249bc32d8928B7ee64DE9435E39); + ManagerLike internal constant managerContract = ManagerLike(0xEfe0B4cA532769a3AE758fD82E1426a03A94F185); /** - * @dev DAI Join + * @dev Coin Join */ - CoinJoinInterface internal constant daiJoinContract = CoinJoinInterface(0x9759A6Ac90977b93B58547b4A71c78317f391A28); + CoinJoinInterface internal constant coinJoinContract = CoinJoinInterface(0x0A5653CCa4DB1B6E265F47CAf6969e64f1CFdC45); /** - * @dev Maker MCD Jug Address. + * @dev Reflexer Tax collector Address. */ - TaxCollectorLike internal constant mcdJug = TaxCollectorLike(0x19c0976f590D67707E62397C87829d896Dc0f1F1); + TaxCollectorLike internal constant taxCollectorContract = TaxCollectorLike(0xcDB05aEda142a1B0D6044C09C64e4226c1a281EB); /** - * @dev Return Close Vault Address. + * @dev Return Close Safe Address. */ address internal constant giveAddr = 0x4dD58550eb15190a5B3DfAE28BB14EeC181fC267; /** - * @dev Get Vault's ilk. + * @dev Get Safe's collateral type. */ - function getVaultData(uint vault) internal view returns (bytes32 ilk, address urn) { - ilk = managerContract.collateralTypes(vault); - urn = managerContract.safes(vault); + function getSafeData(uint safe) internal view returns (bytes32 collateralType, address handler) { + collateralType = managerContract.collateralTypes(safe); + handler = managerContract.safes(safe); } /** - * @dev Gem Join address is ETH type collateral. + * @dev Collateral Join address is ETH type collateral. */ function isEth(address tknAddr) internal pure returns (bool) { return tknAddr == ethAddr ? true : false; } /** - * @dev Get Vault Debt Amount. + * @dev Get Safe Debt Amount. */ - function _getVaultDebt( - address vat, - bytes32 ilk, - address urn + function _getSafeDebt( + address safeEngine, + bytes32 collateralType, + address handler ) internal view returns (uint wad) { - (, uint rate,,,) = SafeEngineLike(vat).collateralTypes(ilk); - (, uint art) = SafeEngineLike(vat).safes(ilk, urn); - uint coin = SafeEngineLike(vat).coin(urn); + (, uint rate,,,) = SafeEngineLike(safeEngine).collateralTypes(collateralType); + (, uint debt) = SafeEngineLike(safeEngine).safes(collateralType, handler); + uint coin = SafeEngineLike(safeEngine).coin(handler); - uint rad = sub(mul(art, rate), coin); + uint rad = sub(mul(debt, rate), coin); wad = rad / RAY; wad = mul(wad, RAY) < rad ? wad + 1 : wad; @@ -63,17 +63,17 @@ abstract contract Helpers is DSMath, Basic { * @dev Get Borrow Amount. */ function _getBorrowAmt( - address vat, - address urn, - bytes32 ilk, + address safeEngine, + address handler, + bytes32 collateralType, uint amt - ) internal returns (int dart) + ) internal returns (int deltaDebt) { - uint rate = mcdJug.taxSingle(ilk); - uint coin = SafeEngineLike(vat).coin(urn); + uint rate = taxCollectorContract.taxSingle(collateralType); + uint coin = SafeEngineLike(safeEngine).coin(handler); if (coin < mul(amt, RAY)) { - dart = toInt(sub(mul(amt, RAY), coin) / rate); - dart = mul(uint(dart), rate) < mul(amt, RAY) ? dart + 1 : dart; + deltaDebt = toInt(sub(mul(amt, RAY), coin) / rate); + deltaDebt = mul(uint(deltaDebt), rate) < mul(amt, RAY) ? deltaDebt + 1 : deltaDebt; } } @@ -81,16 +81,16 @@ abstract contract Helpers is DSMath, Basic { * @dev Get Payback Amount. */ function _getWipeAmt( - address vat, + address safeEngine, uint amt, - address urn, - bytes32 ilk - ) internal view returns (int dart) + address handler, + bytes32 collateralType + ) internal view returns (int deltaDebt) { - (, uint rate,,,) = SafeEngineLike(vat).collateralTypes(ilk); - (, uint art) = SafeEngineLike(vat).safes(ilk, urn); - dart = toInt(amt / rate); - dart = uint(dart) <= art ? - dart : - toInt(art); + (, uint rate,,,) = SafeEngineLike(safeEngine).collateralTypes(collateralType); + (, uint debt) = SafeEngineLike(safeEngine).safes(collateralType, handler); + deltaDebt = toInt(amt / rate); + deltaDebt = uint(deltaDebt) <= debt ? - deltaDebt : - toInt(debt); } /** @@ -105,14 +105,14 @@ abstract contract Helpers is DSMath, Basic { } /** - * @dev Get vault ID. If `vault` is 0, get lastSAFEID opened vault. + * @dev Get safe ID. If `safe` is 0, get lastSAFEID opened safe. */ - function getVault(uint vault) internal view returns (uint _vault) { - if (vault == 0) { - require(managerContract.safeCount(address(this)) > 0, "no-vault-opened"); - _vault = managerContract.lastSAFEID(address(this)); + 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 { - _vault = vault; + _safe = safe; } } diff --git a/contracts/connectors/reflexer/interface.sol b/contracts/connectors/reflexer/interface.sol index 983734d7..250e4678 100644 --- a/contracts/connectors/reflexer/interface.sol +++ b/contracts/connectors/reflexer/interface.sol @@ -18,7 +18,7 @@ interface ManagerLike { } interface SafeEngineLike { - function can(address, address) external view returns (uint); + function safeRights(address, address) external view returns (uint); function collateralTypes(bytes32) external view returns (uint, uint, uint, uint, uint); function coin(address) external view returns (uint); function safes(bytes32, address) external view returns (uint, uint); @@ -38,6 +38,7 @@ interface SafeEngineLike { 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; } @@ -52,3 +53,11 @@ interface CoinJoinInterface { 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); +} diff --git a/contracts/connectors/reflexer/main.sol b/contracts/connectors/reflexer/main.sol index c28fba79..07d44411 100644 --- a/contracts/connectors/reflexer/main.sol +++ b/contracts/connectors/reflexer/main.sol @@ -5,56 +5,56 @@ import { Helpers } from "./helpers.sol"; import { Events } from "./events.sol"; import { SafeEngineLike, TokenJoinInterface } from "./interface.sol"; -abstract contract MakerResolver is Helpers, Events { +abstract contract GebResolver is Helpers, Events { /** - * @dev Open Vault + * @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 ilk = stringToBytes32(colType); - require(instaMapping.gemJoinMapping(ilk) != address(0), "wrong-col-type"); - uint256 vault = managerContract.openSAFE(ilk, address(this)); + bytes32 collateralType = stringToBytes32(colType); + require(instaMapping.gemJoinMapping(collateralType) != address(0), "wrong-col-type"); + uint256 safe = managerContract.openSAFE(collateralType, address(this)); _eventName = "LogOpen(uint256,bytes32)"; - _eventParam = abi.encode(vault, ilk); + _eventParam = abi.encode(safe, collateralType); } /** - * @dev Close Vault - * @param vault Vault ID to close. + * @dev Close Safe + * @param safe Safe ID to close. */ - function close(uint256 vault) external payable returns (string memory _eventName, bytes memory _eventParam) { - uint _vault = getVault(vault); - (bytes32 ilk, address urn) = getVaultData(_vault); - (uint ink, uint art) = SafeEngineLike(managerContract.safeEngine()).safes(ilk, urn); + 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(ink == 0 && art == 0, "vault-has-assets"); - require(managerContract.ownsSAFE(_vault) == address(this), "not-owner"); + require(collateral == 0 && debt == 0, "safe-has-assets"); + require(managerContract.ownsSAFE(_safe) == address(this), "not-owner"); - managerContract.transferSAFEOwnership(_vault, giveAddr); + managerContract.transferSAFEOwnership(_safe, giveAddr); _eventName = "LogClose(uint256,bytes32)"; - _eventParam = abi.encode(_vault, ilk); + _eventParam = abi.encode(_safe, collateralType); } /** * @dev Deposit ETH/ERC20_Token Collateral. - * @param vault Vault ID. + * @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 vault, + uint256 safe, uint256 amt, uint256 getId, uint256 setId ) external payable returns (string memory _eventName, bytes memory _eventParam) { uint _amt = getUint(getId, amt); - uint _vault = getVault(vault); - (bytes32 ilk, address urn) = getVaultData(_vault); + uint _safe = getSafe(safe); + (bytes32 collateralType, address handler) = getSafeData(_safe); - address colAddr = instaMapping.gemJoinMapping(ilk); + address colAddr = instaMapping.gemJoinMapping(collateralType); TokenJoinInterface tokenJoinContract = TokenJoinInterface(colAddr); TokenInterface tokenContract = tokenJoinContract.collateral(); @@ -69,8 +69,8 @@ abstract contract MakerResolver is Helpers, Events { tokenJoinContract.join(address(this), _amt); SafeEngineLike(managerContract.safeEngine()).modifySAFECollateralization( - ilk, - urn, + collateralType, + handler, address(this), address(this), toInt(convertTo18(tokenJoinContract.decimals(), _amt)), @@ -80,45 +80,45 @@ abstract contract MakerResolver is Helpers, Events { setUint(setId, _amt); _eventName = "LogDeposit(uint256,bytes32,uint256,uint256,uint256)"; - _eventParam = abi.encode(_vault, ilk, _amt, getId, setId); + _eventParam = abi.encode(_safe, collateralType, _amt, getId, setId); } /** * @dev Withdraw ETH/ERC20_Token Collateral. - * @param vault Vault ID. + * @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 vault, + uint256 safe, uint256 amt, uint256 getId, uint256 setId ) external payable returns (string memory _eventName, bytes memory _eventParam) { uint _amt = getUint(getId, amt); - uint _vault = getVault(vault); - (bytes32 ilk, address urn) = getVaultData(_vault); + uint _safe = getSafe(safe); + (bytes32 collateralType, address handler) = getSafeData(_safe); - address colAddr = instaMapping.gemJoinMapping(ilk); + address colAddr = instaMapping.gemJoinMapping(collateralType); TokenJoinInterface tokenJoinContract = TokenJoinInterface(colAddr); uint _amt18; if (_amt == uint(-1)) { - (_amt18,) = SafeEngineLike(managerContract.safeEngine()).safes(ilk, urn); + (_amt18,) = SafeEngineLike(managerContract.safeEngine()).safes(collateralType, handler); _amt = convert18ToDec(tokenJoinContract.decimals(), _amt18); } else { _amt18 = convertTo18(tokenJoinContract.decimals(), _amt); } managerContract.modifySAFECollateralization( - _vault, + _safe, -toInt(_amt18), 0 ); managerContract.transferCollateral( - _vault, + _safe, address(this), _amt18 ); @@ -135,131 +135,131 @@ abstract contract MakerResolver is Helpers, Events { setUint(setId, _amt); _eventName = "LogWithdraw(uint256,bytes32,uint256,uint256,uint256)"; - _eventParam = abi.encode(_vault, ilk, _amt, getId, setId); + _eventParam = abi.encode(_safe, collateralType, _amt, getId, setId); } /** - * @dev Borrow DAI. - * @param vault Vault ID. + * @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 vault, + uint256 safe, uint256 amt, uint256 getId, uint256 setId ) external payable returns (string memory _eventName, bytes memory _eventParam) { uint _amt = getUint(getId, amt); - uint _vault = getVault(vault); - (bytes32 ilk, address urn) = getVaultData(_vault); + uint _safe = getSafe(safe); + (bytes32 collateralType, address handler) = getSafeData(_safe); - SafeEngineLike vatContract = SafeEngineLike(managerContract.safeEngine()); + SafeEngineLike safeEngineContract = SafeEngineLike(managerContract.safeEngine()); managerContract.modifySAFECollateralization( - _vault, + _safe, 0, _getBorrowAmt( - address(vatContract), - urn, - ilk, + address(safeEngineContract), + handler, + collateralType, _amt ) ); managerContract.transferInternalCoins( - _vault, + _safe, address(this), toRad(_amt) ); - if (vatContract.can(address(this), address(daiJoinContract)) == 0) { - vatContract.approveSAFEModification(address(daiJoinContract)); + if (safeEngineContract.safeRights(address(this), address(coinJoinContract)) == 0) { + safeEngineContract.approveSAFEModification(address(coinJoinContract)); } - daiJoinContract.exit(address(this), _amt); + coinJoinContract.exit(address(this), _amt); setUint(setId, _amt); _eventName = "LogBorrow(uint256,bytes32,uint256,uint256,uint256)"; - _eventParam = abi.encode(_vault, ilk, _amt, getId, setId); + _eventParam = abi.encode(_safe, collateralType, _amt, getId, setId); } /** - * @dev Payback borrowed DAI. - * @param vault Vault ID. + * @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 vault, + uint256 safe, uint256 amt, uint256 getId, uint256 setId ) external payable returns (string memory _eventName, bytes memory _eventParam) { uint _amt = getUint(getId, amt); - uint _vault = getVault(vault); - (bytes32 ilk, address urn) = getVaultData(_vault); + uint _safe = getSafe(safe); + (bytes32 collateralType, address handler) = getSafeData(_safe); - address vat = managerContract.safeEngine(); + address safeEngine = managerContract.safeEngine(); - uint _maxDebt = _getVaultDebt(vat, ilk, urn); + uint _maxDebt = _getSafeDebt(safeEngine, collateralType, handler); _amt = _amt == uint(-1) ? _maxDebt : _amt; require(_maxDebt >= _amt, "paying-excess-debt"); - daiJoinContract.coin().approve(address(daiJoinContract), _amt); - daiJoinContract.join(urn, _amt); + coinJoinContract.coin().approve(address(coinJoinContract), _amt); + coinJoinContract.join(handler, _amt); managerContract.modifySAFECollateralization( - _vault, + _safe, 0, _getWipeAmt( - vat, - SafeEngineLike(vat).coin(urn), - urn, - ilk + safeEngine, + SafeEngineLike(safeEngine).coin(handler), + handler, + collateralType ) ); setUint(setId, _amt); _eventName = "LogPayback(uint256,bytes32,uint256,uint256,uint256)"; - _eventParam = abi.encode(_vault, ilk, _amt, getId, setId); + _eventParam = abi.encode(_safe, collateralType, _amt, getId, setId); } /** * @dev Withdraw leftover ETH/ERC20_Token after Liquidation. - * @param vault Vault ID. + * @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 vault, + uint256 safe, uint256 amt, uint256 getId, uint256 setId ) external payable returns (string memory _eventName, bytes memory _eventParam) { uint _amt = getUint(getId, amt); - (bytes32 ilk, address urn) = getVaultData(vault); + (bytes32 collateralType, address handler) = getSafeData(safe); - address colAddr = instaMapping.gemJoinMapping(ilk); + address colAddr = instaMapping.gemJoinMapping(collateralType); TokenJoinInterface tokenJoinContract = TokenJoinInterface(colAddr); uint _amt18; if (_amt == uint(-1)) { - _amt18 = SafeEngineLike(managerContract.safeEngine()).tokenCollateral(ilk, urn); + _amt18 = SafeEngineLike(managerContract.safeEngine()).tokenCollateral(collateralType, handler); _amt = convert18ToDec(tokenJoinContract.decimals(), _amt18); } else { _amt18 = convertTo18(tokenJoinContract.decimals(), _amt); } managerContract.transferCollateral( - vault, + safe, address(this), _amt18 ); @@ -273,19 +273,19 @@ abstract contract MakerResolver is Helpers, Events { setUint(setId, _amt); _eventName = "LogWithdrawLiquidated(uint256,bytes32,uint256,uint256,uint256)"; - _eventParam = abi.encode(vault, ilk, _amt, getId, setId); + _eventParam = abi.encode(safe, collateralType, _amt, getId, setId); } - struct MakerData { - uint _vault; + struct GebData { + uint _safe; address colAddr; TokenJoinInterface tokenJoinContract; - SafeEngineLike vatContract; + SafeEngineLike safeEngineContract; TokenInterface tokenContract; } /** - * @dev Deposit ETH/ERC20_Token Collateral and Borrow DAI. - * @param vault Vault ID. + * @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. @@ -294,7 +294,7 @@ abstract contract MakerResolver is Helpers, Events { * @param setIdBorrow Set borrow token amount at this ID in `InstaMemory` Contract. */ function depositAndBorrow( - uint256 vault, + uint256 safe, uint256 depositAmt, uint256 borrowAmt, uint256 getIdDeposit, @@ -302,58 +302,58 @@ abstract contract MakerResolver is Helpers, Events { uint256 setIdDeposit, uint256 setIdBorrow ) external payable returns (string memory _eventName, bytes memory _eventParam) { - MakerData memory makerData; + GebData memory gebData; uint _amtDeposit = getUint(getIdDeposit, depositAmt); uint _amtBorrow = getUint(getIdBorrow, borrowAmt); - makerData._vault = getVault(vault); - (bytes32 ilk, address urn) = getVaultData(makerData._vault); + gebData._safe = getSafe(safe); + (bytes32 collateralType, address handler) = getSafeData(gebData._safe); - makerData.colAddr = instaMapping.gemJoinMapping(ilk); - makerData.tokenJoinContract = TokenJoinInterface(makerData.colAddr); - makerData.vatContract = SafeEngineLike(managerContract.safeEngine()); - makerData.tokenContract = makerData.tokenJoinContract.collateral(); + gebData.colAddr = instaMapping.gemJoinMapping(collateralType); + gebData.tokenJoinContract = TokenJoinInterface(gebData.colAddr); + gebData.safeEngineContract = SafeEngineLike(managerContract.safeEngine()); + gebData.tokenContract = gebData.tokenJoinContract.collateral(); - if (isEth(address(makerData.tokenContract))) { + if (isEth(address(gebData.tokenContract))) { _amtDeposit = _amtDeposit == uint(-1) ? address(this).balance : _amtDeposit; - makerData.tokenContract.deposit{value: _amtDeposit}(); + gebData.tokenContract.deposit{value: _amtDeposit}(); } else { - _amtDeposit = _amtDeposit == uint(-1) ? makerData.tokenContract.balanceOf(address(this)) : _amtDeposit; + _amtDeposit = _amtDeposit == uint(-1) ? gebData.tokenContract.balanceOf(address(this)) : _amtDeposit; } - makerData.tokenContract.approve(address(makerData.colAddr), _amtDeposit); - makerData.tokenJoinContract.join(urn, _amtDeposit); + gebData.tokenContract.approve(address(gebData.colAddr), _amtDeposit); + gebData.tokenJoinContract.join(handler, _amtDeposit); managerContract.modifySAFECollateralization( - makerData._vault, - toInt(convertTo18(makerData.tokenJoinContract.decimals(), _amtDeposit)), + gebData._safe, + toInt(convertTo18(gebData.tokenJoinContract.decimals(), _amtDeposit)), _getBorrowAmt( - address(makerData.vatContract), - urn, - ilk, + address(gebData.safeEngineContract), + handler, + collateralType, _amtBorrow ) ); managerContract.transferInternalCoins( - makerData._vault, + gebData._safe, address(this), toRad(_amtBorrow) ); - if (makerData.vatContract.can(address(this), address(daiJoinContract)) == 0) { - makerData.vatContract.approveSAFEModification(address(daiJoinContract)); + if (gebData.safeEngineContract.safeRights(address(this), address(coinJoinContract)) == 0) { + gebData.safeEngineContract.approveSAFEModification(address(coinJoinContract)); } - daiJoinContract.exit(address(this), _amtBorrow); + coinJoinContract.exit(address(this), _amtBorrow); setUint(setIdDeposit, _amtDeposit); setUint(setIdBorrow, _amtBorrow); _eventName = "LogDepositAndBorrow(uint256,bytes32,uint256,uint256,uint256,uint256,uint256,uint256)"; _eventParam = abi.encode( - makerData._vault, - ilk, + gebData._safe, + collateralType, _amtDeposit, _amtBorrow, getIdDeposit, @@ -364,48 +364,48 @@ abstract contract MakerResolver is Helpers, Events { } /** - * @dev Exit DAI from urn. - * @param vault Vault ID. + * @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 exitDai( - uint256 vault, + 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 _vault = getVault(vault); - (bytes32 ilk, address urn) = getVaultData(_vault); + uint _safe = getSafe(safe); + (bytes32 collateralType, address handler) = getSafeData(_safe); - SafeEngineLike vatContract = SafeEngineLike(managerContract.safeEngine()); + SafeEngineLike safeEngineContract = SafeEngineLike(managerContract.safeEngine()); if(_amt == uint(-1)) { - _amt = vatContract.coin(urn); + _amt = safeEngineContract.coin(handler); _amt = _amt / 10 ** 27; } managerContract.transferInternalCoins( - _vault, + _safe, address(this), toRad(_amt) ); - if (vatContract.can(address(this), address(daiJoinContract)) == 0) { - vatContract.approveSAFEModification(address(daiJoinContract)); + if (safeEngineContract.safeRights(address(this), address(coinJoinContract)) == 0) { + safeEngineContract.approveSAFEModification(address(coinJoinContract)); } - daiJoinContract.exit(address(this), _amt); + coinJoinContract.exit(address(this), _amt); setUint(setId, _amt); - _eventName = "LogExitDai(uint256,bytes32,uint256,uint256,uint256)"; - _eventParam = abi.encode(_vault, ilk, _amt, getId, setId); + _eventName = "LogExit(uint256,bytes32,uint256,uint256,uint256)"; + _eventParam = abi.encode(_safe, collateralType, _amt, getId, setId); } } -contract ConnectV2Maker is MakerResolver { - string public constant name = "MakerDao-v1"; +contract ConnectV2Reflexer is GebResolver { + string public constant name = "Reflexer-v1"; } From b67be72a13c692bbfdcede6e3ce6092bd860508f Mon Sep 17 00:00:00 2001 From: Guillaume Felley Date: Tue, 30 Mar 2021 12:52:29 +0200 Subject: [PATCH 4/9] Add Reflexer mapping --- contracts/connectors/reflexer/helpers.sol | 21 +++++++- contracts/connectors/reflexer/interface.sol | 4 ++ contracts/connectors/reflexer/main.sol | 10 ++-- contracts/mapping/reflexer.sol | 57 +++++++++++++++++++++ 4 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 contracts/mapping/reflexer.sol diff --git a/contracts/connectors/reflexer/helpers.sol b/contracts/connectors/reflexer/helpers.sol index 278fe306..26c130b8 100644 --- a/contracts/connectors/reflexer/helpers.sol +++ b/contracts/connectors/reflexer/helpers.sol @@ -3,7 +3,14 @@ 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 } from "./interface.sol"; +import { + ManagerLike, + CoinJoinInterface, + SafeEngineLike, + TaxCollectorLike, + TokenJoinInterface, + GebMapping +} from "./interface.sol"; abstract contract Helpers is DSMath, Basic { /** @@ -26,6 +33,18 @@ abstract contract Helpers is DSMath, Basic { */ 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. */ diff --git a/contracts/connectors/reflexer/interface.sol b/contracts/connectors/reflexer/interface.sol index 250e4678..a26f1244 100644 --- a/contracts/connectors/reflexer/interface.sol +++ b/contracts/connectors/reflexer/interface.sol @@ -61,3 +61,7 @@ interface ConnectorsInterface { 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 index 07d44411..c23d0079 100644 --- a/contracts/connectors/reflexer/main.sol +++ b/contracts/connectors/reflexer/main.sol @@ -12,7 +12,7 @@ abstract contract GebResolver is Helpers, Events { */ function open(string calldata colType) external payable returns (string memory _eventName, bytes memory _eventParam) { bytes32 collateralType = stringToBytes32(colType); - require(instaMapping.gemJoinMapping(collateralType) != address(0), "wrong-col-type"); + require(getCollateralJoinAddress(collateralType) != address(0), "wrong-col-type"); uint256 safe = managerContract.openSAFE(collateralType, address(this)); _eventName = "LogOpen(uint256,bytes32)"; @@ -54,7 +54,7 @@ abstract contract GebResolver is Helpers, Events { uint _safe = getSafe(safe); (bytes32 collateralType, address handler) = getSafeData(_safe); - address colAddr = instaMapping.gemJoinMapping(collateralType); + address colAddr = getCollateralJoinAddress(collateralType); TokenJoinInterface tokenJoinContract = TokenJoinInterface(colAddr); TokenInterface tokenContract = tokenJoinContract.collateral(); @@ -100,7 +100,7 @@ abstract contract GebResolver is Helpers, Events { uint _safe = getSafe(safe); (bytes32 collateralType, address handler) = getSafeData(_safe); - address colAddr = instaMapping.gemJoinMapping(collateralType); + address colAddr = getCollateralJoinAddress(collateralType); TokenJoinInterface tokenJoinContract = TokenJoinInterface(colAddr); uint _amt18; @@ -247,7 +247,7 @@ abstract contract GebResolver is Helpers, Events { uint _amt = getUint(getId, amt); (bytes32 collateralType, address handler) = getSafeData(safe); - address colAddr = instaMapping.gemJoinMapping(collateralType); + address colAddr = getCollateralJoinAddress(collateralType); TokenJoinInterface tokenJoinContract = TokenJoinInterface(colAddr); uint _amt18; @@ -309,7 +309,7 @@ abstract contract GebResolver is Helpers, Events { gebData._safe = getSafe(safe); (bytes32 collateralType, address handler) = getSafeData(gebData._safe); - gebData.colAddr = instaMapping.gemJoinMapping(collateralType); + gebData.colAddr = getCollateralJoinAddress(collateralType); gebData.tokenJoinContract = TokenJoinInterface(gebData.colAddr); gebData.safeEngineContract = SafeEngineLike(managerContract.safeEngine()); gebData.tokenContract = gebData.tokenJoinContract.collateral(); 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 From b36812140928fcdb19c9587cac469745a7d09df7 Mon Sep 17 00:00:00 2001 From: Guillaume Felley Date: Tue, 30 Mar 2021 12:53:38 +0200 Subject: [PATCH 5/9] Fix bugs after testing --- contracts/connectors/reflexer/events.sol | 4 ++-- contracts/connectors/reflexer/helpers.sol | 4 ++-- contracts/connectors/reflexer/interface.sol | 4 ++-- contracts/connectors/reflexer/main.sol | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/contracts/connectors/reflexer/events.sol b/contracts/connectors/reflexer/events.sol index d5067dcd..d8981be9 100644 --- a/contracts/connectors/reflexer/events.sol +++ b/contracts/connectors/reflexer/events.sol @@ -10,8 +10,8 @@ contract Events { 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 LogDeposit(uint256 tokenAmt, uint256 getId, uint256 setId); - event LogWithdraw(uint256 tokenAmt, uint256 getId, uint256 setId); + event LogDepositCoin(uint256 tokenAmt, uint256 getId, uint256 setId); + event LogWithdrawCoin(uint256 tokenAmt, uint256 getId, uint256 setId); event LogDepositAndBorrow( uint256 indexed safe, diff --git a/contracts/connectors/reflexer/helpers.sol b/contracts/connectors/reflexer/helpers.sol index 26c130b8..abcba530 100644 --- a/contracts/connectors/reflexer/helpers.sol +++ b/contracts/connectors/reflexer/helpers.sol @@ -70,7 +70,7 @@ abstract contract Helpers is DSMath, Basic { ) internal view returns (uint wad) { (, uint rate,,,) = SafeEngineLike(safeEngine).collateralTypes(collateralType); (, uint debt) = SafeEngineLike(safeEngine).safes(collateralType, handler); - uint coin = SafeEngineLike(safeEngine).coin(handler); + uint coin = SafeEngineLike(safeEngine).coinBalance(handler); uint rad = sub(mul(debt, rate), coin); wad = rad / RAY; @@ -89,7 +89,7 @@ abstract contract Helpers is DSMath, Basic { ) internal returns (int deltaDebt) { uint rate = taxCollectorContract.taxSingle(collateralType); - uint coin = SafeEngineLike(safeEngine).coin(handler); + 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; diff --git a/contracts/connectors/reflexer/interface.sol b/contracts/connectors/reflexer/interface.sol index a26f1244..7cee1580 100644 --- a/contracts/connectors/reflexer/interface.sol +++ b/contracts/connectors/reflexer/interface.sol @@ -20,7 +20,7 @@ interface ManagerLike { interface SafeEngineLike { function safeRights(address, address) external view returns (uint); function collateralTypes(bytes32) external view returns (uint, uint, uint, uint, uint); - function coin(address) external view returns (uint); + function coinBalance(address) external view returns (uint); function safes(bytes32, address) external view returns (uint, uint); function modifySAFECollateralization( bytes32, @@ -45,7 +45,7 @@ interface TokenJoinInterface { interface CoinJoinInterface { function safeEngine() external returns (SafeEngineLike); - function coin() external returns (TokenInterface); + function systemCoin() external returns (TokenInterface); function join(address, uint) external payable; function exit(address, uint) external; } diff --git a/contracts/connectors/reflexer/main.sol b/contracts/connectors/reflexer/main.sol index c23d0079..f00dcb1a 100644 --- a/contracts/connectors/reflexer/main.sol +++ b/contracts/connectors/reflexer/main.sol @@ -211,7 +211,7 @@ abstract contract GebResolver is Helpers, Events { require(_maxDebt >= _amt, "paying-excess-debt"); - coinJoinContract.coin().approve(address(coinJoinContract), _amt); + coinJoinContract.systemCoin().approve(address(coinJoinContract), _amt); coinJoinContract.join(handler, _amt); managerContract.modifySAFECollateralization( @@ -219,7 +219,7 @@ abstract contract GebResolver is Helpers, Events { 0, _getWipeAmt( safeEngine, - SafeEngineLike(safeEngine).coin(handler), + SafeEngineLike(safeEngine).coinBalance(handler), handler, collateralType ) @@ -382,7 +382,7 @@ abstract contract GebResolver is Helpers, Events { SafeEngineLike safeEngineContract = SafeEngineLike(managerContract.safeEngine()); if(_amt == uint(-1)) { - _amt = safeEngineContract.coin(handler); + _amt = safeEngineContract.coinBalance(handler); _amt = _amt / 10 ** 27; } From da08f615b5eb97e29594fa6aa8f5286b415e43fa Mon Sep 17 00:00:00 2001 From: Guillaume Felley Date: Tue, 30 Mar 2021 13:06:09 +0200 Subject: [PATCH 6/9] Adjust event names --- contracts/connectors/reflexer/main.sol | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/connectors/reflexer/main.sol b/contracts/connectors/reflexer/main.sol index f00dcb1a..981dfcf7 100644 --- a/contracts/connectors/reflexer/main.sol +++ b/contracts/connectors/reflexer/main.sol @@ -79,7 +79,7 @@ abstract contract GebResolver is Helpers, Events { setUint(setId, _amt); - _eventName = "LogDeposit(uint256,bytes32,uint256,uint256,uint256)"; + _eventName = "LogDepositCoin(uint256,bytes32,uint256,uint256,uint256)"; _eventParam = abi.encode(_safe, collateralType, _amt, getId, setId); } @@ -134,7 +134,7 @@ abstract contract GebResolver is Helpers, Events { setUint(setId, _amt); - _eventName = "LogWithdraw(uint256,bytes32,uint256,uint256,uint256)"; + _eventName = "LogWithdrawCoin(uint256,bytes32,uint256,uint256,uint256)"; _eventParam = abi.encode(_safe, collateralType, _amt, getId, setId); } From aa4af5ae115a1f80e5fd22e0cac608aec18f61d3 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Tue, 30 Mar 2021 16:32:41 +0200 Subject: [PATCH 7/9] LogWithdraw fix Co-authored-by: Thrilok kumar --- contracts/connectors/reflexer/main.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/connectors/reflexer/main.sol b/contracts/connectors/reflexer/main.sol index 981dfcf7..397f79ae 100644 --- a/contracts/connectors/reflexer/main.sol +++ b/contracts/connectors/reflexer/main.sol @@ -134,7 +134,7 @@ abstract contract GebResolver is Helpers, Events { setUint(setId, _amt); - _eventName = "LogWithdrawCoin(uint256,bytes32,uint256,uint256,uint256)"; + _eventName = "LogWithdraw(uint256,bytes32,uint256,uint256,uint256)"; _eventParam = abi.encode(_safe, collateralType, _amt, getId, setId); } From 5cc5d848e0fc5aaa7c89df5c1c5cafc31265e7f0 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Tue, 30 Mar 2021 16:33:01 +0200 Subject: [PATCH 8/9] LogDeposit Co-authored-by: Thrilok kumar --- contracts/connectors/reflexer/main.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/connectors/reflexer/main.sol b/contracts/connectors/reflexer/main.sol index 397f79ae..f00dcb1a 100644 --- a/contracts/connectors/reflexer/main.sol +++ b/contracts/connectors/reflexer/main.sol @@ -79,7 +79,7 @@ abstract contract GebResolver is Helpers, Events { setUint(setId, _amt); - _eventName = "LogDepositCoin(uint256,bytes32,uint256,uint256,uint256)"; + _eventName = "LogDeposit(uint256,bytes32,uint256,uint256,uint256)"; _eventParam = abi.encode(_safe, collateralType, _amt, getId, setId); } From cc151ef773bf408fc9bcaf3850a952f651172532 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Tue, 30 Mar 2021 16:33:20 +0200 Subject: [PATCH 9/9] Remove ex-DSR event Co-authored-by: Thrilok kumar --- contracts/connectors/reflexer/events.sol | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/contracts/connectors/reflexer/events.sol b/contracts/connectors/reflexer/events.sol index d8981be9..6c04e376 100644 --- a/contracts/connectors/reflexer/events.sol +++ b/contracts/connectors/reflexer/events.sol @@ -10,9 +10,6 @@ contract Events { 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 LogDepositCoin(uint256 tokenAmt, uint256 getId, uint256 setId); - event LogWithdrawCoin(uint256 tokenAmt, uint256 getId, uint256 setId); - event LogDepositAndBorrow( uint256 indexed safe, bytes32 indexed collateralType, @@ -23,4 +20,4 @@ contract Events { uint256 setIdDeposit, uint256 setIdBorrow ); -} \ No newline at end of file +}