@colony/colony-js-contract-client
Version:
Method-like interface for Smart Contracts
171 lines (125 loc) • 4.93 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray');
var _slicedToArray3 = _interopRequireDefault(_slicedToArray2);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _colonyJsUtils = require('@colony/colony-js-utils');
var _ContractClient = require('./ContractClient');
var _ContractClient2 = _interopRequireDefault(_ContractClient);
var _paramConversion = require('../modules/paramConversion');
var _paramValidation = require('../modules/paramValidation');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var ContractEvent = function () {
// A store for the event handlers that got wrapped for type validation.
// The respective contract's ContractClient instance.
function ContractEvent(_ref) {
var eventName = _ref.eventName,
client = _ref.client,
argsDef = _ref.argsDef;
(0, _classCallCheck3.default)(this, ContractEvent);
this.eventName = eventName;
this.client = client;
this.argsDef = argsDef;
if (!this.interface) throw new Error('No such event "' + eventName + '" in loaded ABI');
this._wrappedHandlers = new Map();
this.assertValid = (0, _colonyJsUtils.makeAssert)('Validation failed for event ' + eventName);
}
/**
* In order to be able to parse and validate event parameters as expected,
* we use this static method to wrap event handlers before executing them.
*/
// Throwing custom assertion messages.
// The type definitions of the event's arguments.
// The event's name as defined in the contract.
(0, _createClass3.default)(ContractEvent, [{
key: 'parseLogs',
/**
* Given an array of logs, filter matching topics and parse event data from them.
*/
value: function parseLogs(logs) {
var _this = this;
return logs.filter(function (_ref2) {
var _ref2$topics = (0, _slicedToArray3.default)(_ref2.topics, 1),
topic = _ref2$topics[0];
return _this.interface.topics.includes(topic);
}).map(function (log) {
return _this.parseLog(log);
});
}
/**
* Given a log, parse its event data.
*/
}, {
key: 'parseLog',
value: function parseLog(_ref3) {
var topics = _ref3.topics,
data = _ref3.data;
if (!this.interface.topics.includes(topics[0])) throw new Error('Cannot parse unknown topic');
return this.parse(this.interface.parse(topics, data));
}
}, {
key: 'parse',
value: function parse(args) {
return this.constructor.parse(args, this.argsDef, this.assertValid);
}
/**
* Given the contract this instance has been instantiated on, `addListener`
* adds a new event handler to the respective event that gets called once the
* event has been emitted by the contract.
*/
}, {
key: 'addListener',
value: function addListener(handlerFunction) {
if (this._wrappedHandlers.get(handlerFunction)) {
return;
}
var wrappedHandlerFunction = this.constructor.wrapHandlerFunction(handlerFunction, this.argsDef, this.assertValid);
this.client.contract.addListener(this.eventName, wrappedHandlerFunction);
this._wrappedHandlers.set(handlerFunction, wrappedHandlerFunction);
}
/**
* If an event handler has been added for this event previously,
* `removeListener` removes it.
*/
}, {
key: 'removeListener',
value: function removeListener(handlerFunction) {
var wrappedHandlerFunction = this._wrappedHandlers.get(handlerFunction);
if (wrappedHandlerFunction) {
this.client.contract.removeListener(this.eventName, wrappedHandlerFunction);
this._wrappedHandlers.delete(handlerFunction);
}
}
}, {
key: 'interface',
get: function get() {
return this.client.contract.interface.events[this.eventName];
}
}], [{
key: 'wrapHandlerFunction',
value: function wrapHandlerFunction(handlerFunction, argsDef, assertValid) {
var _this2 = this;
return function (_ref4) {
var args = _ref4.args;
var parsedArgs = _this2.parse(args, argsDef, assertValid);
handlerFunction(parsedArgs);
};
}
}, {
key: 'parse',
value: function parse(args, argsDef, assertValid) {
var parsedArgs = (0, _paramConversion.convertOutputValues)(args, argsDef);
(0, _paramValidation.validateParams)(parsedArgs, argsDef, assertValid);
return parsedArgs;
}
}]);
return ContractEvent;
}();
/* eslint-disable import/no-cycle */
exports.default = ContractEvent;
//# sourceMappingURL=ContractEvent.js.map