1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | 1 1 1 1 1 1 61 61 61 61 61 61 61 1 16 50 25 1 30 1 22 1 2 1 12 12 12 12 24 4 12 12 11 9 11 21 16 16 6 10 1 2 9 11 12 12 1 8 8 8 8 1 1 1 1 1 1 1 7 7 7 8 12 8 8 8 8 8 8 8 25 25 8 8 8 1 3 3 3 1 3 1 3 3 3 1 1 48 48 48 26 22 22 22 22 1 | /* This file is part of web3.js. web3.js is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. web3.js is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with web3.js. If not, see <http://www.gnu.org/licenses/>. */ /** * @file event.js * @author Marek Kotewicz <marek@ethdev.com> * @author Fabian Vogelsteller <fabian@frozeman.de> * @date 2016 */ var utils = require('../utils/utils'); var coder = require('../solidity/coder'); var formatters = require('./formatters'); var sha3 = require('../utils/sha3'); var Subscription = require('./subscription'); /** * This prototype should be used to create event filters */ var ContractEvent = function (requestManager, json, address, allEvents) { this._requestManager = requestManager; this._address = address; this._json = json; this._name = json.inputs ? utils.transformToFullName(json) : null; this._params = json.inputs; this._anonymous = json.anonymous; this._allEvents = !!allEvents; }; /** * Should be used to get filtered param types * * @method types * @param {Bool} decide if returned typed should be indexed * @param {Object} params the parameters of the event * @return {Array} array of types */ ContractEvent.prototype.types = function (indexed, params) { return params.filter(function (i) { return i.indexed === indexed; }).map(function (i) { return i.type; }); }; /** * Should be used to get event display name * * @method displayName * @param {String} name (optional) the events name * @return {String} event display name */ ContractEvent.prototype.displayName = function (name) { return utils.extractDisplayName(name || this._name); }; /** * Should be used to get event type name * * @method typeName * @return {String} event type name */ ContractEvent.prototype.typeName = function () { return utils.extractTypeName(this._name); }; /** * Should be used to get event signature * * @method signature * @return {String} event signature */ ContractEvent.prototype.signature = function () { return sha3(this._name); }; /** * Should be used to encode indexed params and options to one final object * * @method encode * @param {Object} options * @return {Object} everything combined together and encoded */ ContractEvent.prototype.encode = function (options) { options = options || {}; indexed = options.filter || {}; var result = {}; ['fromBlock', 'toBlock'].filter(function (f) { return options[f] !== undefined; }).forEach(function (f) { result[f] = formatters.inputBlockNumberFormatter(options[f]); }); result.topics = []; // single events if(!this._allEvents) { if (!this._anonymous) { result.topics.push('0x' + this.signature()); } var indexedTopics = this._params.filter(function (i) { return i.indexed === true; }).map(function (i) { var value = indexed[i.name]; if (value === undefined || value === null) { return null; } if (utils.isArray(value)) { return value.map(function (v) { return '0x' + coder.encodeParam(i.type, v); }); } return '0x' + coder.encodeParam(i.type, value); }); result.topics = result.topics.concat(indexedTopics); } result.address = this._address; return result; }; /** * Should be used to decode indexed params and options * * @method decode * @param {Object} data * @return {Object} result object with decoded indexed && not indexed params */ ContractEvent.prototype.decode = function (data) { var name = params = anonymous = null; data.data = data.data || ''; data.topics = data.topics || []; // all events if(this._allEvents) { var eventTopic = data.topics[0].slice(2); var match = this._json.filter(function (j) { return eventTopic === sha3(utils.transformToFullName(j)); })[0]; Iif (!match) { // cannot find matching event? console.warn('Can\'t find event for log'); return data; } name = utils.transformToFullName(match); params = match.inputs; anonymous = match.anonymous; // single event } else { name = this._name; params = this._params; anonymous = this._anonymous; } var argTopics = anonymous ? data.topics : data.topics.slice(1); var indexedData = argTopics.map(function (topics) { return topics.slice(2); }).join(""); var indexedParams = coder.decodeParams(this.types(true, params), indexedData); var notIndexedData = data.data.slice(2); var notIndexedParams = coder.decodeParams(this.types(false, params), notIndexedData); var result = formatters.outputLogFormatter(data); result.event = this.displayName(name); result.address = data.address; result.returnValues = params.reduce(function (acc, current) { acc[current.name] = current.indexed ? indexedParams.shift() : notIndexedParams.shift(); return acc; }, {}); delete result.data; delete result.topics; return result; }; /** * Get the arguments of the function call * * @method getArgs * @param {Object} options * @param {Function} callback * @return {Object} filter object */ ContractEvent.prototype.getArgs = function (options, callback) { Eif (utils.isFunction(arguments[arguments.length - 1])) { callback = arguments[arguments.length - 1]; if(arguments.length === 1) { options = null; } } return { options: this.encode(options), formatter: this.decode.bind(this), callback: callback }; }; /** * Should be used to create new filter object from event * * @method execute * @param {Object} options * @param {Function} callback * @return {Object} filter object */ ContractEvent.prototype.execute = function (options, callback) { var args = this.getArgs.apply(this, arguments); var subscription = new Subscription({ subscription: { params: 1, inputFormatter: [formatters.inputLogFormatter], outputFormatter: args.formatter }, subscribeMethod: 'eth_subscribe', unsubscribeMethod: 'eth_unsubscribe', requestManager: this._requestManager }); return subscription.subscribe.apply(subscription, ['logs', args.options, args.callback]); }; // TODO: put indexed args into the options object /** * Get past logs for this event * * @method getPastEvents * @param {Object} options * @param {Function} callback * @param {Contract} */ ContractEvent.prototype.getPastEvents = function(options, callback){ var args = this.getArgs.apply(this, arguments); if (utils.isFunction(callback)) { this._requestManager.sendAsync({ method: 'eth_getLogs', params: [args.options] }, function(error, logs){ if(!error) { args.callback(null, logs.map(args.formatter)); } else { args.callback(error); } }); } return this._requestManager.send({ method: 'eth_getLogs', params: [args.options] }).map(args.formatter); }; /** * Should be used to attach event to contract object * * @method attachToContract * @param {Contract} */ ContractEvent.prototype.attachToContract = function (contract) { var execute = this.execute.bind(this); // attach past logs execute.getPastEvents = this.getPastEvents.bind(this); // all events if(this._allEvents) { contract.allEvents = execute; // single event } else { var displayName = this.displayName(); Eif (!contract[displayName]) { contract[displayName] = execute; } contract[displayName][this.typeName()] = this.execute.bind(this, contract); } }; module.exports = ContractEvent; |