UNPKG

graceful-unwinding-widget

Version:

graceful-unwinding-widget React component

325 lines (260 loc) 10.3 kB
'use strict'; exports.__esModule = true; exports.default = undefined; var _regenerator = require('babel-runtime/regenerator'); var _regenerator2 = _interopRequireDefault(_regenerator); var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator'); var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _ethers = require('ethers'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var CdpManagerContractName = 'CDP_MANAGER'; var ProxyActionsContractName = 'PROXY_ACTIONS'; var ProxyRegistryContractName = 'PROXY_REGISTRY'; var UnwindContractName = 'Unwind'; var WrappedCdpManager = function () { function WrappedCdpManager(network, provider, unwindContractAddress) { (0, _classCallCheck3.default)(this, WrappedCdpManager); this.contracts = {}; this.network = network; this.provider = provider; this.address = unwindContractAddress; } WrappedCdpManager.prototype.isOwnCdp = function () { var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(id) { var contract, owner, proxy; return _regenerator2.default.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: contract = this.getContract(CdpManagerContractName); _context.next = 3; return contract.owns(id); case 3: owner = _context.sent; _context.next = 6; return this.getProxy(); case 6: proxy = _context.sent.address; return _context.abrupt('return', owner === proxy); case 8: case 'end': return _context.stop(); } } }, _callee, this); })); function isOwnCdp(_x) { return _ref.apply(this, arguments); } return isOwnCdp; }(); WrappedCdpManager.prototype.distributorCanManageCdp = function () { var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(id) { return _regenerator2.default.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: return _context2.abrupt('return', this.canManageCdp(id, this.address)); case 1: case 'end': return _context2.stop(); } } }, _callee2, this); })); function distributorCanManageCdp(_x2) { return _ref2.apply(this, arguments); } return distributorCanManageCdp; }(); WrappedCdpManager.prototype.canManageCdp = function () { var _ref3 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3(id, address) { var owner, contract; return _regenerator2.default.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: _context3.next = 2; return this.getProxy(); case 2: owner = _context3.sent.address; contract = this.getContract(CdpManagerContractName); return _context3.abrupt('return', contract.cdpCan(owner, id, address)); case 5: case 'end': return _context3.stop(); } } }, _callee3, this); })); function canManageCdp(_x3, _x4) { return _ref3.apply(this, arguments); } return canManageCdp; }(); WrappedCdpManager.prototype.allowDistributorManageCdp = function () { var _ref4 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4(id) { var allow = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1; return _regenerator2.default.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: return _context4.abrupt('return', this.allowManageCdp(id, this.address, allow)); case 1: case 'end': return _context4.stop(); } } }, _callee4, this); })); function allowDistributorManageCdp(_x5) { return _ref4.apply(this, arguments); } return allowDistributorManageCdp; }(); WrappedCdpManager.prototype.allowManageCdp = function () { var _ref5 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee5(id, address) { var allow = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1; var managerAddress, contract, data; return _regenerator2.default.wrap(function _callee5$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: managerAddress = WrappedCdpManager.getContractAddress(CdpManagerContractName, this.network); contract = this.getContract(ProxyActionsContractName); data = contract.interface.functions.cdpAllow.encode([managerAddress, id, address, allow]); _context5.next = 5; return this.getProxy(); case 5: _context5.t0 = contract.address; _context5.t1 = data; return _context5.abrupt('return', _context5.sent.execute(_context5.t0, _context5.t1)); case 8: case 'end': return _context5.stop(); } } }, _callee5, this); })); function allowManageCdp(_x7, _x8) { return _ref5.apply(this, arguments); } return allowManageCdp; }(); WrappedCdpManager.getContractAddress = function getContractAddress(name, network) { var addresses = require('./contracts/' + network + '.json'); return addresses[name]; }; WrappedCdpManager.prototype.getContract = function getContract(name) { if (!this.contracts[name]) { var abi = require('./contracts/maker-dao/abis/' + name + '.json'); this.contracts[name] = new _ethers.Contract(WrappedCdpManager.getContractAddress(name, this.network), abi, this.provider.getSigner()); } return this.contracts[name]; }; WrappedCdpManager.prototype.getProxy = function () { var _ref6 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee6() { var contract, abi; return _regenerator2.default.wrap(function _callee6$(_context6) { while (1) { switch (_context6.prev = _context6.next) { case 0: if (this.proxy) { _context6.next = 14; break; } contract = this.getContract(ProxyRegistryContractName); abi = require('./contracts/maker-dao/abis/DsProxy.json'); _context6.t0 = _ethers.Contract; _context6.t1 = contract; _context6.next = 7; return this.provider.listAccounts(); case 7: _context6.t2 = _context6.sent[0]; _context6.next = 10; return _context6.t1.proxies.call(_context6.t1, _context6.t2); case 10: _context6.t3 = _context6.sent; _context6.t4 = abi; _context6.t5 = this.provider.getSigner(); this.proxy = new _context6.t0(_context6.t3, _context6.t4, _context6.t5); case 14: return _context6.abrupt('return', this.proxy); case 15: case 'end': return _context6.stop(); } } }, _callee6, this); })); function getProxy() { return _ref6.apply(this, arguments); } return getProxy; }(); WrappedCdpManager.isNetworkSupported = function () { var _ref7 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee7(provider, address) { var code; return _regenerator2.default.wrap(function _callee7$(_context7) { while (1) { switch (_context7.prev = _context7.next) { case 0: _context7.next = 2; return provider.getCode(address); case 2: code = _context7.sent; return _context7.abrupt('return', code != null && code.length > 3); case 4: case 'end': return _context7.stop(); } } }, _callee7, this); })); function isNetworkSupported(_x10, _x11) { return _ref7.apply(this, arguments); } return isNetworkSupported; }(); WrappedCdpManager.prototype.getUnwindContract = function getUnwindContract(address) { if (!this.contracts[address]) { var _require = require('./contracts/' + UnwindContractName + '.json'), abi = _require.abi; this.contracts[address] = new _ethers.Contract(address, abi, this.provider.getSigner()); } return this.contracts[address]; }; WrappedCdpManager.prototype.getFeeInfo = function () { var _ref8 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee8() { var unwind, feeInfo; return _regenerator2.default.wrap(function _callee8$(_context8) { while (1) { switch (_context8.prev = _context8.next) { case 0: unwind = this.getUnwindContract(this.address); _context8.next = 3; return unwind.payments(); case 3: feeInfo = _context8.sent; return _context8.abrupt('return', { period: +feeInfo._period, fee: _ethers.ethers.utils.formatEther(feeInfo._flat ? feeInfo._fee : feeInfo._fee.mul(100)), isFlat: feeInfo._flat }); case 5: case 'end': return _context8.stop(); } } }, _callee8, this); })); function getFeeInfo() { return _ref8.apply(this, arguments); } return getFeeInfo; }(); return WrappedCdpManager; }(); exports.default = WrappedCdpManager; module.exports = exports['default'];