@backt/protocol
Version:
Backt smart contracts implementation
184 lines (150 loc) • 5.97 kB
JavaScript
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 EVENTAPI = function () {
(0, _createClass3.default)(EVENTAPI, [{
key: 'getAllTransactionsEvent',
/**
* Get all the transactions events for each contract passed in parameter
* @param contracts A list of contracts
* @param onSuccessCallback Callback receives list of events
* @param onErrorCallback Callback receives error details on error
*/
value: function getAllTransactionsEvent(contracts, onSuccessCallback, onErrorCallback) {
var self = this;
var transactions = [];
var nbContracts = contracts.length;
var nbContractDone = 0;
// For each contract
contracts.forEach(function (contract) {
// Get all the events
var events = self.cfd.at(contract.cfd.address).allEvents({ fromBlock: 0, toBlock: 'latest' });
events.get(function (err, events) {
// If we have at least one event, push them into the transactions array
if (err == null && events != null && events !== undefined && events.length > 0) {
events.forEach(function (e) {
transactions.push(e);
});
}
nbContractDone += 1;
// Check if we are done with all the contracts
if (nbContractDone >= nbContracts) {
// Sort array by block number
transactions.sort(function (a, b) {
return b.blockNumber - a.blockNumber;
});
onSuccessCallback(transactions);
}
});
});
}
/**
* NOTE: use EVENTAPI.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
* validiting 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 EVENTAPI(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(_x, _x2) {
return _ref.apply(this, arguments);
}
return newInstance;
}()
}]);
function EVENTAPI(config, web3) {
(0, _classCallCheck3.default)(this, EVENTAPI);
this.config = config;
this.web3 = web3;
this.web3.eth.getCodeAsync = _bluebird2.default.promisify(this.web3.eth.getCode);
this.web3.eth.getBlockAsync = _bluebird2.default.promisify(this.web3.eth.getBlock);
}
/**
* 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)(EVENTAPI, [{
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:
this.cfd = (0, _contracts.cfdInstance)(this.web3.currentProvider, this.config.ownerAccountAddr);
return _context2.abrupt('return', this);
case 2:
case 'end':
return _context2.stop();
}
}
}, _callee2, this);
}));
function initialise() {
return _ref2.apply(this, arguments);
}
return initialise;
}()
}]);
return EVENTAPI;
}();
exports.default = EVENTAPI;
;