mirror of
https://github.com/Instadapp/Gelato-automations.git
synced 2024-07-29 22:28:07 +00:00
1102 lines
31 KiB
Solidity
1102 lines
31 KiB
Solidity
pragma solidity ^0.6.0;
|
|
|
|
interface TokenInterface {
|
|
function approve(address, uint256) external;
|
|
|
|
function transfer(address, uint256) external;
|
|
|
|
function transferFrom(
|
|
address,
|
|
address,
|
|
uint256
|
|
) external;
|
|
|
|
function deposit() external payable;
|
|
|
|
function withdraw(uint256) external;
|
|
|
|
function balanceOf(address) external view returns (uint256);
|
|
}
|
|
|
|
interface ManagerLike {
|
|
function cdpCan(
|
|
address,
|
|
uint256,
|
|
address
|
|
) external view returns (uint256);
|
|
|
|
function ilks(uint256) external view returns (bytes32);
|
|
|
|
function last(address) external view returns (uint256);
|
|
|
|
function count(address) external view returns (uint256);
|
|
|
|
function owns(uint256) external view returns (address);
|
|
|
|
function urns(uint256) external view returns (address);
|
|
|
|
function vat() external view returns (address);
|
|
|
|
function open(bytes32, address) external returns (uint256);
|
|
|
|
function give(uint256, address) external;
|
|
|
|
function frob(
|
|
uint256,
|
|
int256,
|
|
int256
|
|
) external;
|
|
|
|
function flux(
|
|
uint256,
|
|
address,
|
|
uint256
|
|
) external;
|
|
|
|
function move(
|
|
uint256,
|
|
address,
|
|
uint256
|
|
) external;
|
|
}
|
|
|
|
interface VatLike {
|
|
function can(address, address) external view returns (uint256);
|
|
|
|
function ilks(bytes32)
|
|
external
|
|
view
|
|
returns (
|
|
uint256,
|
|
uint256,
|
|
uint256,
|
|
uint256,
|
|
uint256
|
|
);
|
|
|
|
function dai(address) external view returns (uint256);
|
|
|
|
function urns(bytes32, address) external view returns (uint256, uint256);
|
|
|
|
function frob(
|
|
bytes32,
|
|
address,
|
|
address,
|
|
address,
|
|
int256,
|
|
int256
|
|
) external;
|
|
|
|
function hope(address) external;
|
|
|
|
function move(
|
|
address,
|
|
address,
|
|
uint256
|
|
) external;
|
|
|
|
function gem(bytes32, address) external view returns (uint256);
|
|
}
|
|
|
|
interface TokenJoinInterface {
|
|
function dec() external returns (uint256);
|
|
|
|
function gem() external returns (TokenInterface);
|
|
|
|
function join(address, uint256) external payable;
|
|
|
|
function exit(address, uint256) external;
|
|
}
|
|
|
|
interface DaiJoinInterface {
|
|
function vat() external returns (VatLike);
|
|
|
|
function dai() external returns (TokenInterface);
|
|
|
|
function join(address, uint256) external payable;
|
|
|
|
function exit(address, uint256) external;
|
|
}
|
|
|
|
interface JugLike {
|
|
function drip(bytes32) external returns (uint256);
|
|
}
|
|
|
|
interface PotLike {
|
|
function pie(address) external view returns (uint256);
|
|
|
|
function drip() external returns (uint256);
|
|
|
|
function join(uint256) external;
|
|
|
|
function exit(uint256) external;
|
|
}
|
|
|
|
interface MemoryInterface {
|
|
function getUint(uint256 _id) external returns (uint256 _num);
|
|
|
|
function setUint(uint256 _id, uint256 _val) external;
|
|
}
|
|
|
|
interface InstaMapping {
|
|
function gemJoinMapping(bytes32) external view returns (address);
|
|
}
|
|
|
|
interface EventInterface {
|
|
function emitEvent(
|
|
uint256 _connectorType,
|
|
uint256 _connectorID,
|
|
bytes32 _eventCode,
|
|
bytes calldata _eventData
|
|
) external;
|
|
}
|
|
|
|
interface AccountInterface {
|
|
function isAuth(address _user) external view returns (bool);
|
|
}
|
|
|
|
contract DSMath {
|
|
uint256 constant RAY = 10**27;
|
|
uint256 constant WAD = 10**18;
|
|
|
|
function add(uint256 x, uint256 y) internal pure returns (uint256 z) {
|
|
require((z = x + y) >= x, "math-not-safe");
|
|
}
|
|
|
|
function sub(uint256 x, uint256 y) internal pure returns (uint256 z) {
|
|
require((z = x - y) <= x, "sub-overflow");
|
|
}
|
|
|
|
function mul(uint256 x, uint256 y) internal pure returns (uint256 z) {
|
|
require(y == 0 || (z = x * y) / y == x, "math-not-safe");
|
|
}
|
|
|
|
function wmul(uint256 x, uint256 y) internal pure returns (uint256 z) {
|
|
z = add(mul(x, y), WAD / 2) / WAD;
|
|
}
|
|
|
|
function wdiv(uint256 x, uint256 y) internal pure returns (uint256 z) {
|
|
z = add(mul(x, WAD), y / 2) / y;
|
|
}
|
|
|
|
function rdiv(uint256 x, uint256 y) internal pure returns (uint256 z) {
|
|
z = add(mul(x, RAY), y / 2) / y;
|
|
}
|
|
|
|
function rmul(uint256 x, uint256 y) internal pure returns (uint256 z) {
|
|
z = add(mul(x, y), RAY / 2) / RAY;
|
|
}
|
|
|
|
function toInt(uint256 x) internal pure returns (int256 y) {
|
|
y = int256(x);
|
|
require(y >= 0, "int-overflow");
|
|
}
|
|
|
|
function toRad(uint256 wad) internal pure returns (uint256 rad) {
|
|
rad = mul(wad, 10**27);
|
|
}
|
|
|
|
function convertTo18(uint256 _dec, uint256 _amt)
|
|
internal
|
|
pure
|
|
returns (uint256 amt)
|
|
{
|
|
amt = mul(_amt, 10**(18 - _dec));
|
|
}
|
|
|
|
function convert18ToDec(uint256 _dec, uint256 _amt)
|
|
internal
|
|
pure
|
|
returns (uint256 amt)
|
|
{
|
|
amt = (_amt / 10**(18 - _dec));
|
|
}
|
|
}
|
|
|
|
contract Helpers is DSMath {
|
|
/**
|
|
* @dev Return ETH Address.
|
|
*/
|
|
function getAddressETH() internal pure returns (address) {
|
|
return 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
|
|
}
|
|
|
|
/**
|
|
* @dev Return WETH Address.
|
|
*/
|
|
function getAddressWETH() internal pure returns (address) {
|
|
return 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
|
|
}
|
|
|
|
/**
|
|
* @dev Return InstAaMemory Address.
|
|
*/
|
|
function getMemoryAddr() internal pure returns (address) {
|
|
return 0x8a5419CfC711B2343c17a6ABf4B2bAFaBb06957F;
|
|
}
|
|
|
|
/**
|
|
* @dev Return InstaEvent Address.
|
|
*/
|
|
function getEventAddr() internal pure returns (address) {
|
|
return 0x2af7ea6Cb911035f3eb1ED895Cb6692C39ecbA97;
|
|
}
|
|
|
|
/**
|
|
* @dev Get Uint value from InstaMemory Contract.
|
|
*/
|
|
function getUint(uint256 getId, uint256 val)
|
|
internal
|
|
returns (uint256 returnVal)
|
|
{
|
|
returnVal = getId == 0
|
|
? val
|
|
: MemoryInterface(getMemoryAddr()).getUint(getId);
|
|
}
|
|
|
|
/**
|
|
* @dev Set Uint value in InstaMemory Contract.
|
|
*/
|
|
function setUint(uint256 setId, uint256 val) internal {
|
|
if (setId != 0) MemoryInterface(getMemoryAddr()).setUint(setId, val);
|
|
}
|
|
|
|
/**
|
|
* @dev Connector Details
|
|
*/
|
|
function connectorID() public pure returns (uint256 _type, uint256 _id) {
|
|
(_type, _id) = (1, 40);
|
|
}
|
|
}
|
|
|
|
contract MakerMCDAddresses is Helpers {
|
|
/**
|
|
* @dev Return Maker MCD Manager Address.
|
|
*/
|
|
function getMcdManager() internal pure returns (address) {
|
|
return 0x5ef30b9986345249bc32d8928B7ee64DE9435E39;
|
|
}
|
|
|
|
/**
|
|
* @dev Return Maker MCD DAI Address.
|
|
*/
|
|
function getMcdDai() internal pure returns (address) {
|
|
return 0x6B175474E89094C44Da98b954EedeAC495271d0F;
|
|
}
|
|
|
|
/**
|
|
* @dev Return Maker MCD DAI_Join Address.
|
|
*/
|
|
function getMcdDaiJoin() internal pure returns (address) {
|
|
return 0x9759A6Ac90977b93B58547b4A71c78317f391A28;
|
|
}
|
|
|
|
/**
|
|
* @dev Return Maker MCD Jug Address.
|
|
*/
|
|
function getMcdJug() internal pure returns (address) {
|
|
return 0x19c0976f590D67707E62397C87829d896Dc0f1F1;
|
|
}
|
|
|
|
/**
|
|
* @dev Return Maker MCD Pot Address.
|
|
*/
|
|
function getMcdPot() internal pure returns (address) {
|
|
return 0x197E90f9FAD81970bA7976f33CbD77088E5D7cf7;
|
|
}
|
|
}
|
|
|
|
contract MakerHelpers is MakerMCDAddresses {
|
|
/**
|
|
* @dev Return InstaMapping Address.
|
|
*/
|
|
function getMappingAddr() internal pure returns (address) {
|
|
return 0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88;
|
|
}
|
|
|
|
/**
|
|
* @dev Return Close Vault Address.
|
|
*/
|
|
function getGiveAddress() internal pure returns (address) {
|
|
return 0x4dD58550eb15190a5B3DfAE28BB14EeC181fC267;
|
|
}
|
|
|
|
/**
|
|
* @dev Get Vault's ilk.
|
|
*/
|
|
function getVaultData(ManagerLike managerContract, uint256 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 == getAddressWETH() ? true : false;
|
|
}
|
|
|
|
/**
|
|
* @dev Get Vault Debt Amount.
|
|
*/
|
|
function _getVaultDebt(
|
|
address vat,
|
|
bytes32 ilk,
|
|
address urn
|
|
) internal view returns (uint256 wad) {
|
|
(, uint256 rate, , , ) = VatLike(vat).ilks(ilk);
|
|
(, uint256 art) = VatLike(vat).urns(ilk, urn);
|
|
uint256 dai = VatLike(vat).dai(urn);
|
|
|
|
uint256 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,
|
|
uint256 amt
|
|
) internal returns (int256 dart) {
|
|
address jug = getMcdJug();
|
|
uint256 rate = JugLike(jug).drip(ilk);
|
|
uint256 dai = VatLike(vat).dai(urn);
|
|
if (dai < mul(amt, RAY)) {
|
|
dart = toInt(sub(mul(amt, RAY), dai) / rate);
|
|
dart = mul(uint256(dart), rate) < mul(amt, RAY) ? dart + 1 : dart;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @dev Get Payback Amount.
|
|
*/
|
|
function _getWipeAmt(
|
|
address vat,
|
|
uint256 amt,
|
|
address urn,
|
|
bytes32 ilk
|
|
) internal view returns (int256 dart) {
|
|
(, uint256 rate, , , ) = VatLike(vat).ilks(ilk);
|
|
(, uint256 art) = VatLike(vat).urns(ilk, urn);
|
|
dart = toInt(amt / rate);
|
|
dart = uint256(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(ManagerLike managerContract, uint256 vault)
|
|
internal
|
|
view
|
|
returns (uint256 _vault)
|
|
{
|
|
if (vault == 0) {
|
|
require(
|
|
managerContract.count(address(this)) > 0,
|
|
"no-vault-opened"
|
|
);
|
|
_vault = managerContract.last(address(this));
|
|
} else {
|
|
_vault = vault;
|
|
}
|
|
}
|
|
}
|
|
|
|
contract EventHelper is MakerHelpers {
|
|
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
|
|
);
|
|
|
|
function emitLogDeposit(
|
|
uint256 vault,
|
|
bytes32 ilk,
|
|
uint256 tokenAmt,
|
|
uint256 getId,
|
|
uint256 setId
|
|
) internal {
|
|
emit LogDeposit(vault, ilk, tokenAmt, getId, setId);
|
|
bytes32 _eventCode =
|
|
keccak256("LogDeposit(uint256,bytes32,uint256,uint256,uint256)");
|
|
bytes memory _eventParam =
|
|
abi.encode(vault, ilk, tokenAmt, getId, setId);
|
|
(uint256 _type, uint256 _id) = connectorID();
|
|
EventInterface(getEventAddr()).emitEvent(
|
|
_type,
|
|
_id,
|
|
_eventCode,
|
|
_eventParam
|
|
);
|
|
}
|
|
|
|
function emitLogBorrow(
|
|
uint256 vault,
|
|
bytes32 ilk,
|
|
uint256 tokenAmt,
|
|
uint256 getId,
|
|
uint256 setId
|
|
) internal {
|
|
emit LogBorrow(vault, ilk, tokenAmt, getId, setId);
|
|
bytes32 _eventCode =
|
|
keccak256("LogBorrow(uint256,bytes32,uint256,uint256,uint256)");
|
|
bytes memory _eventParam =
|
|
abi.encode(vault, ilk, tokenAmt, getId, setId);
|
|
(uint256 _type, uint256 _id) = connectorID();
|
|
EventInterface(getEventAddr()).emitEvent(
|
|
_type,
|
|
_id,
|
|
_eventCode,
|
|
_eventParam
|
|
);
|
|
}
|
|
}
|
|
|
|
contract BasicResolver is EventHelper {
|
|
/**
|
|
* @dev Open Vault
|
|
* @param colType Type of Collateral.(eg: 'ETH-A')
|
|
*/
|
|
function open(string calldata colType)
|
|
external
|
|
payable
|
|
returns (uint256 vault)
|
|
{
|
|
bytes32 ilk = stringToBytes32(colType);
|
|
require(
|
|
InstaMapping(getMappingAddr()).gemJoinMapping(ilk) != address(0),
|
|
"wrong-col-type"
|
|
);
|
|
vault = ManagerLike(getMcdManager()).open(ilk, address(this));
|
|
|
|
emit LogOpen(vault, ilk);
|
|
bytes32 _eventCode = keccak256("LogOpen(uint256,bytes32)");
|
|
bytes memory _eventParam = abi.encode(vault, ilk);
|
|
(uint256 _type, uint256 _id) = connectorID();
|
|
EventInterface(getEventAddr()).emitEvent(
|
|
_type,
|
|
_id,
|
|
_eventCode,
|
|
_eventParam
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @dev Close Vault
|
|
* @param vault Vault ID to close.
|
|
*/
|
|
function close(uint256 vault) external payable {
|
|
ManagerLike managerContract = ManagerLike(getMcdManager());
|
|
|
|
uint256 _vault = getVault(managerContract, vault);
|
|
(bytes32 ilk, address urn) = getVaultData(managerContract, _vault);
|
|
(uint256 ink, uint256 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, getGiveAddress());
|
|
|
|
emit LogClose(_vault, ilk);
|
|
bytes32 _eventCode = keccak256("LogClose(uint256,bytes32)");
|
|
bytes memory _eventParam = abi.encode(_vault, ilk);
|
|
(uint256 _type, uint256 _id) = connectorID();
|
|
EventInterface(getEventAddr()).emitEvent(
|
|
_type,
|
|
_id,
|
|
_eventCode,
|
|
_eventParam
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @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 {
|
|
ManagerLike managerContract = ManagerLike(getMcdManager());
|
|
|
|
uint256 _amt = getUint(getId, amt);
|
|
uint256 _vault = getVault(managerContract, vault);
|
|
(bytes32 ilk, address urn) = getVaultData(managerContract, _vault);
|
|
|
|
address colAddr = InstaMapping(getMappingAddr()).gemJoinMapping(ilk);
|
|
TokenJoinInterface tokenJoinContract = TokenJoinInterface(colAddr);
|
|
TokenInterface tokenContract = tokenJoinContract.gem();
|
|
|
|
if (isEth(address(tokenContract))) {
|
|
_amt = _amt == uint256(-1) ? address(this).balance : _amt;
|
|
tokenContract.deposit.value(_amt)();
|
|
} else {
|
|
_amt = _amt == uint256(-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);
|
|
|
|
emitLogDeposit(_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 {
|
|
ManagerLike managerContract = ManagerLike(getMcdManager());
|
|
|
|
uint256 _amt = getUint(getId, amt);
|
|
uint256 _vault = getVault(managerContract, vault);
|
|
(bytes32 ilk, address urn) = getVaultData(managerContract, _vault);
|
|
|
|
address colAddr = InstaMapping(getMappingAddr()).gemJoinMapping(ilk);
|
|
TokenJoinInterface tokenJoinContract = TokenJoinInterface(colAddr);
|
|
|
|
uint256 _amt18;
|
|
if (_amt == uint256(-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);
|
|
|
|
emit LogWithdraw(_vault, ilk, _amt, getId, setId);
|
|
bytes32 _eventCode =
|
|
keccak256("LogWithdraw(uint256,bytes32,uint256,uint256,uint256)");
|
|
bytes memory _eventParam = abi.encode(_vault, ilk, _amt, getId, setId);
|
|
(uint256 _type, uint256 _id) = connectorID();
|
|
EventInterface(getEventAddr()).emitEvent(
|
|
_type,
|
|
_id,
|
|
_eventCode,
|
|
_eventParam
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @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 {
|
|
ManagerLike managerContract = ManagerLike(getMcdManager());
|
|
|
|
uint256 _amt = getUint(getId, amt);
|
|
uint256 _vault = getVault(managerContract, vault);
|
|
(bytes32 ilk, address urn) = getVaultData(managerContract, _vault);
|
|
|
|
address daiJoin = getMcdDaiJoin();
|
|
|
|
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(daiJoin)) == 0) {
|
|
vatContract.hope(daiJoin);
|
|
}
|
|
|
|
DaiJoinInterface(daiJoin).exit(address(this), _amt);
|
|
|
|
setUint(setId, _amt);
|
|
|
|
emitLogBorrow(_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 {
|
|
ManagerLike managerContract = ManagerLike(getMcdManager());
|
|
uint256 _amt = getUint(getId, amt);
|
|
uint256 _vault = getVault(managerContract, vault);
|
|
(bytes32 ilk, address urn) = getVaultData(managerContract, _vault);
|
|
|
|
address vat = managerContract.vat();
|
|
|
|
uint256 _maxDebt = _getVaultDebt(vat, ilk, urn);
|
|
|
|
_amt = _amt == uint256(-1) ? _maxDebt : _amt;
|
|
|
|
require(_maxDebt >= _amt, "paying-excess-debt");
|
|
|
|
DaiJoinInterface daiJoinContract = DaiJoinInterface(getMcdDaiJoin());
|
|
daiJoinContract.dai().approve(getMcdDaiJoin(), _amt);
|
|
daiJoinContract.join(urn, _amt);
|
|
|
|
managerContract.frob(
|
|
_vault,
|
|
0,
|
|
_getWipeAmt(vat, VatLike(vat).dai(urn), urn, ilk)
|
|
);
|
|
|
|
setUint(setId, _amt);
|
|
|
|
emit LogPayback(_vault, ilk, _amt, getId, setId);
|
|
bytes32 _eventCode =
|
|
keccak256("LogPayback(uint256,bytes32,uint256,uint256,uint256)");
|
|
bytes memory _eventParam = abi.encode(_vault, ilk, _amt, getId, setId);
|
|
(uint256 _type, uint256 _id) = connectorID();
|
|
EventInterface(getEventAddr()).emitEvent(
|
|
_type,
|
|
_id,
|
|
_eventCode,
|
|
_eventParam
|
|
);
|
|
}
|
|
}
|
|
|
|
contract BasicExtraResolver is BasicResolver {
|
|
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
|
|
);
|
|
|
|
/**
|
|
* @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 {
|
|
ManagerLike managerContract = ManagerLike(getMcdManager());
|
|
|
|
uint256 _amt = getUint(getId, amt);
|
|
(bytes32 ilk, address urn) = getVaultData(managerContract, vault);
|
|
|
|
address colAddr = InstaMapping(getMappingAddr()).gemJoinMapping(ilk);
|
|
TokenJoinInterface tokenJoinContract = TokenJoinInterface(colAddr);
|
|
|
|
uint256 _amt18;
|
|
if (_amt == uint256(-1)) {
|
|
_amt18 = VatLike(managerContract.vat()).gem(ilk, urn);
|
|
_amt = convert18ToDec(tokenJoinContract.dec(), _amt);
|
|
} 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);
|
|
|
|
emit LogWithdrawLiquidated(vault, ilk, _amt, getId, setId);
|
|
bytes32 _eventCode =
|
|
keccak256(
|
|
"LogWithdrawLiquidated(uint256,bytes32,uint256,uint256,uint256)"
|
|
);
|
|
bytes memory _eventParam = abi.encode(vault, ilk, _amt, getId, setId);
|
|
(uint256 _type, uint256 _id) = connectorID();
|
|
EventInterface(getEventAddr()).emitEvent(
|
|
_type,
|
|
_id,
|
|
_eventCode,
|
|
_eventParam
|
|
);
|
|
}
|
|
|
|
struct MakerData {
|
|
uint256 _vault;
|
|
address colAddr;
|
|
address daiJoin;
|
|
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 {
|
|
ManagerLike managerContract = ManagerLike(getMcdManager());
|
|
MakerData memory makerData;
|
|
uint256 _amtDeposit = getUint(getIdDeposit, depositAmt);
|
|
uint256 _amtBorrow = getUint(getIdBorrow, borrowAmt);
|
|
|
|
makerData._vault = getVault(managerContract, vault);
|
|
(bytes32 ilk, address urn) =
|
|
getVaultData(managerContract, makerData._vault);
|
|
|
|
makerData.colAddr = InstaMapping(getMappingAddr()).gemJoinMapping(ilk);
|
|
makerData.tokenJoinContract = TokenJoinInterface(makerData.colAddr);
|
|
makerData.vatContract = VatLike(managerContract.vat());
|
|
makerData.daiJoin = getMcdDaiJoin();
|
|
|
|
makerData.tokenContract = makerData.tokenJoinContract.gem();
|
|
|
|
if (isEth(address(makerData.tokenContract))) {
|
|
_amtDeposit = _amtDeposit == uint256(-1)
|
|
? address(this).balance
|
|
: _amtDeposit;
|
|
makerData.tokenContract.deposit.value(_amtDeposit)();
|
|
} else {
|
|
_amtDeposit = _amtDeposit == uint256(-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(makerData.daiJoin)
|
|
) == 0
|
|
) {
|
|
makerData.vatContract.hope(makerData.daiJoin);
|
|
}
|
|
|
|
DaiJoinInterface(makerData.daiJoin).exit(address(this), _amtBorrow);
|
|
|
|
setUint(setIdDeposit, _amtDeposit);
|
|
setUint(setIdBorrow, _amtBorrow);
|
|
|
|
emitLogDeposit(
|
|
makerData._vault,
|
|
ilk,
|
|
_amtDeposit,
|
|
getIdDeposit,
|
|
setIdDeposit
|
|
);
|
|
|
|
emitLogBorrow(
|
|
makerData._vault,
|
|
ilk,
|
|
_amtBorrow,
|
|
getIdBorrow,
|
|
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 {
|
|
ManagerLike managerContract = ManagerLike(getMcdManager());
|
|
|
|
uint256 _amt = getUint(getId, amt);
|
|
uint256 _vault = getVault(managerContract, vault);
|
|
(bytes32 ilk, address urn) = getVaultData(managerContract, _vault);
|
|
|
|
address daiJoin = getMcdDaiJoin();
|
|
|
|
VatLike vatContract = VatLike(managerContract.vat());
|
|
if (_amt == uint256(-1)) {
|
|
_amt = vatContract.dai(urn);
|
|
_amt = _amt / 10**27;
|
|
}
|
|
|
|
managerContract.move(_vault, address(this), toRad(_amt));
|
|
|
|
if (vatContract.can(address(this), address(daiJoin)) == 0) {
|
|
vatContract.hope(daiJoin);
|
|
}
|
|
|
|
DaiJoinInterface(daiJoin).exit(address(this), _amt);
|
|
|
|
setUint(setId, _amt);
|
|
|
|
emit LogExitDai(_vault, ilk, _amt, getId, setId);
|
|
bytes32 _eventCode =
|
|
keccak256("LogExitDai(uint256,bytes32,uint256,uint256,uint256)");
|
|
bytes memory _eventParam = abi.encode(_vault, ilk, _amt, getId, setId);
|
|
(uint256 _type, uint256 _id) = connectorID();
|
|
EventInterface(getEventAddr()).emitEvent(
|
|
_type,
|
|
_id,
|
|
_eventCode,
|
|
_eventParam
|
|
);
|
|
}
|
|
}
|
|
|
|
contract DsrResolver is BasicExtraResolver {
|
|
event LogDepositDai(uint256 tokenAmt, uint256 getId, uint256 setId);
|
|
event LogWithdrawDai(uint256 tokenAmt, uint256 getId, uint256 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 {
|
|
uint256 _amt = getUint(getId, amt);
|
|
address pot = getMcdPot();
|
|
address daiJoin = getMcdDaiJoin();
|
|
DaiJoinInterface daiJoinContract = DaiJoinInterface(daiJoin);
|
|
|
|
_amt = _amt == uint256(-1)
|
|
? daiJoinContract.dai().balanceOf(address(this))
|
|
: _amt;
|
|
|
|
VatLike vat = daiJoinContract.vat();
|
|
PotLike potContract = PotLike(pot);
|
|
|
|
uint256 chi = potContract.drip();
|
|
|
|
daiJoinContract.dai().approve(daiJoin, _amt);
|
|
daiJoinContract.join(address(this), _amt);
|
|
if (vat.can(address(this), address(pot)) == 0) {
|
|
vat.hope(pot);
|
|
}
|
|
|
|
potContract.join(mul(_amt, RAY) / chi);
|
|
setUint(setId, _amt);
|
|
|
|
emit LogDepositDai(_amt, getId, setId);
|
|
bytes32 _eventCode =
|
|
keccak256("LogDepositDai(uint256,uint256,uint256)");
|
|
bytes memory _eventParam = abi.encode(_amt, getId, setId);
|
|
(uint256 _type, uint256 _id) = connectorID();
|
|
EventInterface(getEventAddr()).emitEvent(
|
|
_type,
|
|
_id,
|
|
_eventCode,
|
|
_eventParam
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @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 {
|
|
address daiJoin = getMcdDaiJoin();
|
|
|
|
uint256 _amt = getUint(getId, amt);
|
|
|
|
DaiJoinInterface daiJoinContract = DaiJoinInterface(daiJoin);
|
|
VatLike vat = daiJoinContract.vat();
|
|
PotLike potContract = PotLike(getMcdPot());
|
|
|
|
uint256 chi = potContract.drip();
|
|
uint256 pie;
|
|
if (_amt == uint256(-1)) {
|
|
pie = potContract.pie(address(this));
|
|
_amt = mul(chi, pie) / RAY;
|
|
} else {
|
|
pie = mul(_amt, RAY) / chi;
|
|
}
|
|
|
|
potContract.exit(pie);
|
|
|
|
uint256 bal = vat.dai(address(this));
|
|
if (vat.can(address(this), address(daiJoin)) == 0) {
|
|
vat.hope(daiJoin);
|
|
}
|
|
daiJoinContract.exit(
|
|
address(this),
|
|
bal >= mul(_amt, RAY) ? _amt : bal / RAY
|
|
);
|
|
|
|
setUint(setId, _amt);
|
|
|
|
emit LogWithdrawDai(_amt, getId, setId);
|
|
bytes32 _eventCode =
|
|
keccak256("LogWithdrawDai(uint256,uint256,uint256)");
|
|
bytes memory _eventParam = abi.encode(_amt, getId, setId);
|
|
(uint256 _type, uint256 _id) = connectorID();
|
|
EventInterface(getEventAddr()).emitEvent(
|
|
_type,
|
|
_id,
|
|
_eventCode,
|
|
_eventParam
|
|
);
|
|
}
|
|
}
|
|
|
|
contract ConnectMaker is DsrResolver {
|
|
string public constant name = "MakerDao-v1.3";
|
|
}
|