@backt/protocol
Version:
Backt smart contracts implementation
215 lines (172 loc) • 7.23 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
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 _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _bluebird = require('bluebird');
var _bluebird2 = _interopRequireDefault(_bluebird);
var _contracts = require('./contracts');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var AdminAPI = function () {
(0, _createClass3.default)(AdminAPI, [{
key: 'changeDaemonAccount',
/**
* Deposit is called by a party wishing to join a new CFD.
*
* See joinerFee() for details of required fees in addition to the initial
* collateral. These fees are added on to the passed amount.
*
* @return Promise resolving to success with tx details or reject depending
* on the outcome.
*/
value: function changeDaemonAccount(newDaemonAddr) {
var _this = this;
return this.feeds.setDaemonAccount(newDaemonAddr, { from: this.config.ownerAccountAddr }).then(function () {
_this.config.daemonAccountAddr = newDaemonAddr;
console.log('Daemon updated on chain.\n');
console.log('Update daemonAccountAddr in your config.' + _this.config.network + '.json file now.');
});
}
/**
* Change the owner account across all the contracts (or Regsitry only
* if {registryOnly: true} - in case of a new deployment with a new owner).
*
* @param newOwnerAddr New account address
* @param options.registryOnly If true only update in the Registry owner
*
* @return Promise resolving to success with tx details or reject depending
* on the outcome.
*/
}, {
key: 'changeOwnerAccount',
value: function changeOwnerAccount(newOwnerAddr) {
var _this2 = this;
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { registryOnly: false };
var registryOnly = options.registryOnly === true;
var ownableContracts = registryOnly ? ['registry'] : ['feeds', 'registry', 'cfdRegistry', 'cfdFactory'];
return _bluebird2.default.all(ownableContracts.map(function (contractKey) {
return _this2[contractKey].transferOwnership(newOwnerAddr, { from: _this2.config.ownerAccountAddr });
})).then(function () {
_this2.config.ownerAccountAddr = newOwnerAddr;
console.log('Owner updated on chain to ' + newOwnerAddr + '.\n');
console.log('Update ownerAccountAddr in your config.' + _this2.config.network + '.json file now.');
});
}
/**
* NOTE: use AdminAPI.newInstance to create a new instance rather then this
* constructor.
*
* Construct an API instance setting config and web3. initialise() must be
* called after this to setup the contract handler. newInstance() does both
* these steps so is the preferred way to get an instance of this class.
*
* @param config Configuration object with all properties as per config.json.template
* @param web3 Initiated web3 instance for the network to work with.
*/
}], [{
key: 'newInstance',
/**
* Create a new instance of this class setting up contract handles and
* validating the config addresses point to actual deployed contracts.
*
* @param config Configuration object with all properties as per
* config.json.template
* @param web3 Initiated and connected web3 instance
*
* @return Constructed and initialised instance of this class
*/
value: function () {
var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(config, web3) {
var api;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
if (!(web3.isConnected() !== true)) {
_context.next = 2;
break;
}
return _context.abrupt('return', _bluebird2.default.reject(new Error('web3 is not connected - check the endpoint')));
case 2:
api = new AdminAPI(config, web3);
_context.next = 5;
return api.initialise();
case 5:
return _context.abrupt('return', api);
case 6:
case 'end':
return _context.stop();
}
}
}, _callee, this);
}));
function newInstance(_x2, _x3) {
return _ref.apply(this, arguments);
}
return newInstance;
}()
}]);
function AdminAPI(config, web3) {
(0, _classCallCheck3.default)(this, AdminAPI);
this.config = config;
this.web3 = web3;
this.web3.eth.getCodeAsync = _bluebird2.default.promisify(this.web3.eth.getCode);
}
/**
* NOTE: use newInstance() to create new instances rather then call this
* routine.
*
* Sets up contract handles and validiting the config addresses point to
* actual deployed contracts. Seperate to the constructor as it needs to make
* asynchronous calls.
*
* @return api instance
*/
(0, _createClass3.default)(AdminAPI, [{
key: 'initialise',
value: function () {
var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2() {
return _regenerator2.default.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 2;
return (0, _contracts.cfdFactoryInstanceDeployed)(this.config, this.web3);
case 2:
this.cfdFactory = _context2.sent;
_context2.next = 5;
return (0, _contracts.cfdRegistryInstanceDeployed)(this.config, this.web3);
case 5:
this.cfdRegistry = _context2.sent;
_context2.next = 8;
return (0, _contracts.feedsInstanceDeployed)(this.config, this.web3);
case 8:
this.feeds = _context2.sent;
_context2.next = 11;
return (0, _contracts.registryInstanceDeployed)(this.config, this.web3);
case 11:
this.registry = _context2.sent;
return _context2.abrupt('return', this);
case 13:
case 'end':
return _context2.stop();
}
}
}, _callee2, this);
}));
function initialise() {
return _ref2.apply(this, arguments);
}
return initialise;
}()
}]);
return AdminAPI;
}();
exports.default = AdminAPI;