mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
Merge branch 'main' of https://github.com/InstaDApp/dsa-connectors-new into main
This commit is contained in:
commit
1dab4ecfc3
23
contracts/connectors/reflexer/events.sol
Normal file
23
contracts/connectors/reflexer/events.sol
Normal file
|
@ -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
|
||||||
|
);
|
||||||
|
}
|
138
contracts/connectors/reflexer/helpers.sol
Normal file
138
contracts/connectors/reflexer/helpers.sol
Normal file
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
67
contracts/connectors/reflexer/interface.sol
Normal file
67
contracts/connectors/reflexer/interface.sol
Normal file
|
@ -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);
|
||||||
|
}
|
411
contracts/connectors/reflexer/main.sol
Normal file
411
contracts/connectors/reflexer/main.sol
Normal file
|
@ -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";
|
||||||
|
}
|
56
contracts/mapping/reflexer.sol
Normal file
56
contracts/mapping/reflexer.sol
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
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 {
|
||||||
|
ConnectorsInterface public constant connectors = ConnectorsInterface(0x7D53E606308A2E0A1D396F30dc305cc7f8483436);
|
||||||
|
IndexInterface public constant instaIndex = IndexInterface(0x2971AdFa57b20E5a416aE5a708A8655A9c74f723);
|
||||||
|
uint public version = 1;
|
||||||
|
|
||||||
|
mapping (bytes32 => address) public collateralJoinMapping;
|
||||||
|
|
||||||
|
event LogAddCollateralJoinMapping(address[] collateralJoin);
|
||||||
|
|
||||||
|
modifier isChief {
|
||||||
|
require(connectors.chief(msg.sender) || instaIndex.master() == msg.sender, "not-a-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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -29,12 +29,7 @@ module.exports = {
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
networks: {
|
networks: {
|
||||||
mainnet: {
|
default: "hardhat",
|
||||||
url: process.env.ETH_NODE_URL,
|
|
||||||
chainId: 1,
|
|
||||||
timeout: 500000,
|
|
||||||
accounts: [`0x${PRIVATE_KEY}`]
|
|
||||||
},
|
|
||||||
kovan: {
|
kovan: {
|
||||||
url: `https://eth-kovan.alchemyapi.io/v2/${ALCHEMY_ID}`,
|
url: `https://eth-kovan.alchemyapi.io/v2/${ALCHEMY_ID}`,
|
||||||
accounts: [`0x${PRIVATE_KEY}`]
|
accounts: [`0x${PRIVATE_KEY}`]
|
||||||
|
|
21945
package-lock.json
generated
21945
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
17
package.json
17
package.json
|
@ -2,14 +2,11 @@
|
||||||
"name": "dsa-connectors",
|
"name": "dsa-connectors",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "truffle-config.js",
|
|
||||||
"directories": {},
|
"directories": {},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "truffle test",
|
"test": "hardhat test",
|
||||||
"coverage": "./node_modules/.bin/solidity-coverage",
|
"coverage": "./node_modules/.bin/solidity-coverage",
|
||||||
"solium": "solium -d contracts/",
|
"build-contracts": "sol-merger \"./contracts/connectors/mock.sol\" ./contracts/build"
|
||||||
"build-contracts": "sol-merger \"./contracts/connectors/mock.sol\" ./contracts/build",
|
|
||||||
"ganache": "ganache-cli"
|
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
@ -23,23 +20,16 @@
|
||||||
"homepage": "https://github.com/InstaDApp/dsa-connectors-new#readme",
|
"homepage": "https://github.com/InstaDApp/dsa-connectors-new#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@openzeppelin/contracts": "^3.4.0-solc-0.7",
|
"@openzeppelin/contracts": "^3.4.0-solc-0.7",
|
||||||
"@openzeppelin/upgrades": "^2.8.0",
|
|
||||||
"@truffle/artifactor": "^4.0.45",
|
|
||||||
"chalk": "^4.0.0",
|
"chalk": "^4.0.0",
|
||||||
"commander": "^7.1.0",
|
"commander": "^7.1.0",
|
||||||
"dotenv": "^7.0.0",
|
"dotenv": "^7.0.0",
|
||||||
"ethereumjs-abi": "^0.6.8",
|
"ethereumjs-abi": "^0.6.8",
|
||||||
"hardhat-docgen": "^1.1.0",
|
"hardhat-docgen": "^1.1.0",
|
||||||
"minimist": "^1.2.5",
|
"minimist": "^1.2.5",
|
||||||
"solc": "^0.7.0",
|
"solc": "^0.7.0"
|
||||||
"truffle-assertions": "^0.9.2",
|
|
||||||
"truffle-hdwallet-provider": "^1.0.17",
|
|
||||||
"truffle-plugin-verify": "^0.3.10",
|
|
||||||
"truffle-verify": "^1.0.8"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@nomiclabs/buidler": "^1.3.8",
|
"@nomiclabs/buidler": "^1.3.8",
|
||||||
"@nomiclabs/buidler-truffle5": "^1.3.4",
|
|
||||||
"@nomiclabs/buidler-web3": "^1.3.4",
|
"@nomiclabs/buidler-web3": "^1.3.4",
|
||||||
"@nomiclabs/hardhat-ethers": "^2.0.2",
|
"@nomiclabs/hardhat-ethers": "^2.0.2",
|
||||||
"@nomiclabs/hardhat-etherscan": "^2.1.1",
|
"@nomiclabs/hardhat-etherscan": "^2.1.1",
|
||||||
|
@ -48,7 +38,6 @@
|
||||||
"@studydefi/money-legos": "^2.3.7",
|
"@studydefi/money-legos": "^2.3.7",
|
||||||
"@tenderly/hardhat-tenderly": "^1.0.6",
|
"@tenderly/hardhat-tenderly": "^1.0.6",
|
||||||
"ethers": "^5.0.32",
|
"ethers": "^5.0.32",
|
||||||
"ganache-cli": "^6.10.0-beta.2",
|
|
||||||
"hardhat": "^2.0.8",
|
"hardhat": "^2.0.8",
|
||||||
"sol-merger": "^2.0.1",
|
"sol-merger": "^2.0.1",
|
||||||
"solidity-coverage": "0.5.11",
|
"solidity-coverage": "0.5.11",
|
||||||
|
|
|
@ -1,96 +0,0 @@
|
||||||
/**
|
|
||||||
* Use this file to configure your truffle project. It's seeded with some
|
|
||||||
* common settings for different networks and features like migrations,
|
|
||||||
* compilation and testing. Uncomment the ones you need or modify
|
|
||||||
* them to suit your project as necessary.
|
|
||||||
*
|
|
||||||
* More information about configuration can be found at:
|
|
||||||
*
|
|
||||||
* trufflesuite.com/docs/advanced/configuration
|
|
||||||
*
|
|
||||||
* To deploy via Infura you'll need a wallet provider (like @truffle/hdwallet-provider)
|
|
||||||
* to sign your transactions before they're sent to a remote public node. Infura accounts
|
|
||||||
* are available for free at: infura.io/register.
|
|
||||||
*
|
|
||||||
* You'll also need a mnemonic - the twelve word phrase the wallet uses to generate
|
|
||||||
* public/private key pairs. If you're publishing your code to GitHub make sure you load this
|
|
||||||
* phrase from a file you've .gitignored so it doesn't accidentally become public.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
// const HDWalletProvider = require('@truffle/hdwallet-provider');
|
|
||||||
// const infuraKey = "fj4jll3k.....";
|
|
||||||
//
|
|
||||||
// const fs = require('fs');
|
|
||||||
// const mnemonic = fs.readFileSync(".secret").toString().trim();
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
/**
|
|
||||||
* Networks define how you connect to your ethereum client and let you set the
|
|
||||||
* defaults web3 uses to send transactions. If you don't specify one truffle
|
|
||||||
* will spin up a development blockchain for you on port 9545 when you
|
|
||||||
* run `develop` or `test`. You can ask a truffle command to use a specific
|
|
||||||
* network from the command line, e.g
|
|
||||||
*
|
|
||||||
* $ truffle test --network <network-name>
|
|
||||||
*/
|
|
||||||
|
|
||||||
networks: {
|
|
||||||
// Useful for testing. The `development` name is special - truffle uses it by default
|
|
||||||
// if it's defined here and no other network is specified at the command line.
|
|
||||||
// You should run a client (like ganache-cli, geth or parity) in a separate terminal
|
|
||||||
// tab if you use this network and you must also set the `host`, `port` and `network_id`
|
|
||||||
// options below to some value.
|
|
||||||
//
|
|
||||||
// development: {
|
|
||||||
// host: "127.0.0.1", // Localhost (default: none)
|
|
||||||
// port: 8545, // Standard Ethereum port (default: none)
|
|
||||||
// network_id: "*", // Any network (default: none)
|
|
||||||
// },
|
|
||||||
// Another network with more advanced options...
|
|
||||||
// advanced: {
|
|
||||||
// port: 8777, // Custom port
|
|
||||||
// network_id: 1342, // Custom network
|
|
||||||
// gas: 8500000, // Gas sent with each transaction (default: ~6700000)
|
|
||||||
// gasPrice: 20000000000, // 20 gwei (in wei) (default: 100 gwei)
|
|
||||||
// from: <address>, // Account to send txs from (default: accounts[0])
|
|
||||||
// websockets: true // Enable EventEmitter interface for web3 (default: false)
|
|
||||||
// },
|
|
||||||
// Useful for deploying to a public network.
|
|
||||||
// NB: It's important to wrap the provider as a function.
|
|
||||||
// ropsten: {
|
|
||||||
// provider: () => new HDWalletProvider(mnemonic, `https://ropsten.infura.io/v3/YOUR-PROJECT-ID`),
|
|
||||||
// network_id: 3, // Ropsten's id
|
|
||||||
// gas: 5500000, // Ropsten has a lower block limit than mainnet
|
|
||||||
// confirmations: 2, // # of confs to wait between deployments. (default: 0)
|
|
||||||
// timeoutBlocks: 200, // # of blocks before a deployment times out (minimum/default: 50)
|
|
||||||
// skipDryRun: true // Skip dry run before migrations? (default: false for public nets )
|
|
||||||
// },
|
|
||||||
// Useful for private networks
|
|
||||||
// private: {
|
|
||||||
// provider: () => new HDWalletProvider(mnemonic, `https://network.io`),
|
|
||||||
// network_id: 2111, // This network is yours, in the cloud.
|
|
||||||
// production: true // Treats this network as if it was a public net. (default: false)
|
|
||||||
// }
|
|
||||||
},
|
|
||||||
|
|
||||||
// Set default mocha options here, use special reporters etc.
|
|
||||||
mocha: {
|
|
||||||
// timeout: 100000
|
|
||||||
},
|
|
||||||
|
|
||||||
// Configure your compilers
|
|
||||||
compilers: {
|
|
||||||
solc: {
|
|
||||||
// version: "0.5.1", // Fetch exact version from solc-bin (default: truffle's version)
|
|
||||||
// docker: true, // Use "0.5.1" you've installed locally with docker (default: false)
|
|
||||||
// settings: { // See the solidity docs for advice about optimization and evmVersion
|
|
||||||
// optimizer: {
|
|
||||||
// enabled: false,
|
|
||||||
// runs: 200
|
|
||||||
// },
|
|
||||||
// evmVersion: "byzantium"
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
Loading…
Reference in New Issue
Block a user