UNPKG

@backt/protocol

Version:
342 lines (262 loc) 10.9 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.deployAll = undefined; var _regenerator = require('babel-runtime/regenerator'); var _regenerator2 = _interopRequireDefault(_regenerator); var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator'); var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2); var _contracts = require('./contracts'); var _utils = require('./utils'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Deploy and configure Registry. * @param web3 Connected Web3 instance * @param config Config instance (see config.<env>.json) * @param logFn Log progress with this function * @return registry Registry truffle-contract instance * @return updatedConfig Config instance with updated registryAddr */ var deployRegistry = function () { var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(web3, config, logFn) { var Registry, registry, updatedConfig; return _regenerator2.default.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: web3.eth.defaultAccount = config.ownerAccountAddr; Registry = (0, _contracts.registryInstance)(web3.currentProvider, config); logFn('Deploying Registry ...'); _context.next = 5; return Registry.new(); case 5: registry = _context.sent; logFn('Registry: ' + registry.address); logFn('Calling registry.setFees ...'); _context.next = 10; return registry.setFees(config.feesAccountAddr); case 10: logFn('done\n'); updatedConfig = Object.assign({}, config, { registryAddr: registry.address }); return _context.abrupt('return', { registry: registry, updatedConfig: updatedConfig }); case 13: case 'end': return _context.stop(); } } }, _callee, undefined); })); return function deployRegistry(_x, _x2, _x3) { return _ref.apply(this, arguments); }; }(); /** * Deploy and configure Feeds. * @param web3 Connected Web3 instance * @param config Config instance (see config.<env>.json) * @param logFn Log progress with this function * @return feeds Feeds truffle-contract instance * @return updatedConfig Config instance with updated feedsContractAddr */ var deployFeeds = function () { var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(web3, config, logFn) { var Feeds, feeds, updatedConfig; return _regenerator2.default.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: web3.eth.defaultAccount = config.ownerAccountAddr; Feeds = (0, _contracts.feedsInstance)(web3.currentProvider, config); logFn('Deploying Feeds ...'); _context2.next = 5; return Feeds.new(); case 5: feeds = _context2.sent; logFn('Feeds: ' + feeds.address); logFn('Calling feeds.setDaemonAccount ...'); _context2.next = 10; return feeds.setDaemonAccount(config.daemonAccountAddr); case 10: logFn('done\n'); updatedConfig = Object.assign({}, config, { feedsContractAddr: feeds.address }); return _context2.abrupt('return', { feeds: feeds, updatedConfig: updatedConfig }); case 13: case 'end': return _context2.stop(); } } }, _callee2, undefined); })); return function deployFeeds(_x4, _x5, _x6) { return _ref2.apply(this, arguments); }; }(); /** * Deploy and configure CFD and related contracts. * @param web3 Connected Web3 instance * @param config Config instance (see config.<env>.json) * @param logFn Log progress with this function * @return cfd ContractForDifference truffle-contract instance * @return cfdFactory ContractForDifferenceFactory truffle-contract instance * @return cfdRegistry ContractForDifferenceRegistry truffle-contract instance * @return updatedConfig Config instance with addresses of newly deployed contracts added */ var deployCFD = function () { var _ref3 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3(web3, config, logFn) { var registryAddr, ForwardFactory, CFD, CFDFactory, CFDRegistry, Feeds, ff, cfd, cfdRegistry, feeds, cfdFactory, Registry, registry, updatedConfig; return _regenerator2.default.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: registryAddr = config.registryAddr; web3.eth.defaultAccount = config.ownerAccountAddr; ForwardFactory = (0, _contracts.forwardFactoryInstance)(web3.currentProvider, config); CFD = (0, _contracts.cfdInstance)(web3.currentProvider, config); CFDFactory = (0, _contracts.cfdFactoryInstance)(web3.currentProvider, config); CFDRegistry = (0, _contracts.cfdRegistryInstance)(web3.currentProvider, config); Feeds = (0, _contracts.feedsInstance)(web3.currentProvider, config); logFn('Deploying ForwardFactory ...'); _context3.next = 10; return ForwardFactory.new(); case 10: ff = _context3.sent; logFn('ForwardFactory: ' + ff.address); logFn('Deploying ContractForDifference ...'); _context3.next = 15; return CFD.new({ gas: 7000000 }); case 15: cfd = _context3.sent; logFn('ContractForDifference: ' + cfd.address); logFn('Deploying ContractForDifferenceRegistry ...'); _context3.next = 20; return CFDRegistry.new(); case 20: cfdRegistry = _context3.sent; logFn('ContractForDifferenceRegistry: ' + cfdRegistry.address); _context3.next = 24; return Feeds.at(config.feedsContractAddr); case 24: feeds = _context3.sent; logFn('Deploying ContractForDifferenceFactory ...'); _context3.next = 28; return CFDFactory.new(registryAddr, cfd.address, ff.address, feeds.address, { gas: 3000000 }); case 28: cfdFactory = _context3.sent; logFn('ContractForDifferenceFactory: ' + cfdFactory.address); logFn('Calling cfdRegistry.setFactory ...'); _context3.next = 33; return cfdRegistry.setFactory(cfdFactory.address); case 33: logFn('done'); Registry = (0, _contracts.registryInstance)(web3.currentProvider, config); _context3.next = 37; return Registry.at(registryAddr); case 37: registry = _context3.sent; logFn('Setting up CFD Factory and Registry ...'); _context3.next = 41; return Promise.all([cfdFactory.setCFDRegistry(cfdRegistry.address), cfdRegistry.setFactory(cfdFactory.address), registry.setCFDFactoryLatest(cfdFactory.address)]); case 41: logFn('done\n'); updatedConfig = Object.assign({}, config, { cfdFactoryContractAddr: cfdFactory.address, cfdRegistryContractAddr: cfdRegistry.address }); return _context3.abrupt('return', { cfd: cfd, cfdFactory: cfdFactory, cfdRegistry: cfdRegistry, updatedConfig: updatedConfig }); case 44: case 'end': return _context3.stop(); } } }, _callee3, undefined); })); return function deployCFD(_x7, _x8, _x9) { return _ref3.apply(this, arguments); }; }(); var deployAll = function () { var _ref4 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4(web3, initialConfig) { var firstTime = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; var logProgress = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false; var log, config, registry, _ref5, _registryInstance, updatedConfig, deployFeedsResult, deployCFDResult; return _regenerator2.default.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: log = function log(logMsg) { if (logProgress === true) console.log(logMsg); }; config = initialConfig; registry = void 0; if (!(firstTime === true)) { _context4.next = 13; break; } _context4.next = 6; return deployRegistry(web3, config, log); case 6: _ref5 = _context4.sent; _registryInstance = _ref5.registry; updatedConfig = _ref5.updatedConfig; config = updatedConfig; registry = _registryInstance; _context4.next = 18; break; case 13: if ((0, _utils.isEthereumAddress)(config.registryAddr)) { _context4.next = 15; break; } throw new Error('Deploy firstTime = false however registryAddr is NOT set in config ...'); case 15: _context4.next = 17; return (0, _contracts.registryInstanceDeployed)(config, web3); case 17: registry = _context4.sent; case 18: _context4.next = 20; return deployFeeds(web3, config, log); case 20: deployFeedsResult = _context4.sent; config = deployFeedsResult.updatedConfig; _context4.next = 24; return deployCFD(web3, config, log); case 24: deployCFDResult = _context4.sent; config = deployCFDResult.updatedConfig; return _context4.abrupt('return', { cfd: deployCFDResult.cfd, cfdFactory: deployCFDResult.cfdFactory, cfdRegistry: deployCFDResult.cfdRegistry, feeds: deployFeedsResult.feeds, registry: registry, updatedConfig: config }); case 27: case 'end': return _context4.stop(); } } }, _callee4, undefined); })); return function deployAll(_x12, _x13) { return _ref4.apply(this, arguments); }; }(); exports.deployAll = deployAll;