Skip to content

Commit

Permalink
decode receipts logs on contract events
Browse files Browse the repository at this point in the history
  • Loading branch information
frozeman committed Feb 28, 2017
1 parent b1cce88 commit c800df8
Show file tree
Hide file tree
Showing 12 changed files with 248 additions and 112 deletions.
2 changes: 1 addition & 1 deletion docs/web3-eth-contract.rst
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ The **callback** will return the 32 bytes transaction hash.

- ``"transactionHash"`` returns ``String``: is fired right after the transaction is send and a transaction hash is available.
- ``"receipt"`` returns ``Object``: is fired when the transaction receipt is available.
- ``"confirmation"`` returns ``Number``, ``Object``: is fired for every confirmation up to the 12th confirmation. Receives the confirmation number as the first and the receipt as the second argument. Fired from confirmation 0 on, which is the block where its minded.
- ``"confirmation"`` returns ``Number``, ``Object``: is fired for every confirmation up to the 24th confirmation. Receives the confirmation number as the first and the receipt as the second argument. Fired from confirmation 0 on, which is the block where its minded.
- ``"error"`` returns ``Error``: is fired if an error occurs during sending. If a out of gas error, the second parameter is the receipt.


Expand Down
43 changes: 19 additions & 24 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,43 +33,38 @@ var packages = [{
src: './packages/web3-core-requestManager/src/index.js'
},{
fileName: 'web3-providers-ipc',
expose: 'IpcProvider',
expose: 'Web3IpcProvider',
src: './packages/web3-providers-ipc/src/index.js'
},{
fileName: 'web3-providers-http',
expose: 'HttpProvider',
expose: 'Web3HttpProvider',
src: './packages/web3-providers-http/src/index.js',
ignore: ['xmlhttprequest']
},{
fileName: 'web3-providers-ws',
expose: 'WsProvider',
expose: 'Web3WsProvider',
src: './packages/web3-providers-ws/src/index.js'
},{
fileName: 'web3-eth',
expose: 'Eth',
src: './packages/web3-eth/src/index.js'
},{
fileName: 'web3-personal',
expose: 'Personal',
src: './packages/web3-personal/src/index.js'
},{
fileName: 'web3-shh',
expose: 'Shh',
src: './packages/web3-shh/src/index.js'
},{
fileName: 'web3-bzz',
expose: 'Bzz',
src: './packages/web3-bzz/src/index.js'
},{
fileName: 'web3-eth-iban',
expose: 'Iban',
src: './packages/web3-eth-iban/src/index.js'
}];
// ,{
// fileName: 'web3-eth',
// expose: 'Eth',
// src: './packages/web3-eth/src/index.js'
// },{
// fileName: 'web3-personal',
// expose: 'Personal',
// src: './packages/web3-personal/src/index.js'
// },{
// fileName: 'web3-shh',
// expose: 'Shh',
// src: './packages/web3-shh/src/index.js'
// },{
// fileName: 'web3-bzz',
// expose: 'Bzz',
// src: './packages/web3-bzz/src/index.js'
// },{
// fileName: 'web3-eth-iban',
// expose: 'Iban',
// src: './packages/web3-eth-iban/src/index.js'
// }];

var browserifyOptions = {
debug: true,
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-core-helpers/src/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ var outputLogFormatter = function(log) {
typeof log.logIndex === 'string') {
var shaId = utils.sha3(log.blockHash.replace('0x','') + log.transactionHash.replace('0x','') + log.logIndex.replace('0x',''));
log.id = 'log_'+ shaId.replace('0x','').substr(0,8);
} else {
} else if(!log.id) {
log.id = null;
}

Expand Down
25 changes: 18 additions & 7 deletions packages/web3-core-method/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var utils = require('web3-utils');
var promiEvent = require('web3-core-promiEvent');

var TIMEOUTBLOCK = 50;
var CONFIRMATIONBLOCKS = 12;
var CONFIRMATIONBLOCKS = 24;

var Method = function Method(options) {

Expand Down Expand Up @@ -158,7 +158,7 @@ Method.prototype.attachToObject = function (obj) {
}
};

Method.prototype._confirmTransaction = function (defer, result, payload) {
Method.prototype._confirmTransaction = function (defer, result, payload, extraFormatters) {
var method = this,
promiseResolved = false,
canUnsubscribe = true,
Expand All @@ -183,13 +183,19 @@ Method.prototype._confirmTransaction = function (defer, result, payload) {
promiseResolved = true;
utils._fireError(receiptError, defer.eventEmitter, defer.reject);
})
// if CONFIRMATION listener exists check for confirmations
// if CONFIRMATION listener exists check for confirmations, by setting canUnsubscribe = false
.then(function(receipt) {

if (!receipt) {
throw new Error('Receipt is null');
throw new Error('Receipt is "null"');
}

// apply extra formatters
if (extraFormatters && extraFormatters.receiptFormatter) {
receipt = extraFormatters.receiptFormatter(receipt);
}

// check if confirmation listener exists
if (defer.eventEmitter.listeners('confirmation').length > 0) {

defer.eventEmitter.emit('confirmation', confirmationCount, receipt);
Expand Down Expand Up @@ -252,7 +258,10 @@ Method.prototype._confirmTransaction = function (defer, result, payload) {
defer.resolve(receipt);

} else {
utils._fireError([new Error('Transaction ran out of gas. Please provide more gas.'), receipt], defer.eventEmitter, defer.reject);
if(receipt) {
receipt = JSON.stringify(receipt, null, 2);
}
utils._fireError(new Error("Transaction ran out of gas. Please provide more gas:\n"+ receipt), defer.eventEmitter, defer.reject);
}

if (canUnsubscribe) {
Expand Down Expand Up @@ -285,12 +294,14 @@ Method.prototype._confirmTransaction = function (defer, result, payload) {

Method.prototype.buildCall = function() {
var method = this,
isSendTx = (method.call === 'eth_sendTransaction' || method.call === 'eth_sendRawTransaction');
call = (_.isString(method.call)) ? method.call.toLowerCase() : Method.call,
isSendTx = (call === 'eth_sendtransaction' || call === 'eth_sendrawtransaction');



// actual send function
var send = function () {
var extraFromatters = this;
var defer = promiEvent(!isSendTx),
payload = method.toPayload(Array.prototype.slice.call(arguments));

Expand Down Expand Up @@ -326,7 +337,7 @@ Method.prototype.buildCall = function() {
defer.eventEmitter.emit('transactionHash', result);


method._confirmTransaction(defer, result, payload);
method._confirmTransaction(defer, result, payload, extraFromatters);


}
Expand Down
23 changes: 21 additions & 2 deletions packages/web3-eth-contract/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,6 @@ Contract.prototype.deploy = function(options, callback){

};


/**
* Gets the event signature and outputformatters
*
Expand Down Expand Up @@ -771,7 +770,27 @@ Contract.prototype._executeMethod = function _executeMethod(){
return utils._fireError(new Error('Can not send value to non-payable contract method or constructor'), defer.eventEmitter, defer.reject, args.callback);
}

return this._parent._eth.sendTransaction(args.options, args.callback);

// make sure receipt logs are decoded
var extraFormatters = {
receiptFormatter: function (receipt) {
if (_.isArray(receipt.logs)) {

// decode logs
receipt.events = _.map(receipt.logs, function(log) {
return _this._parent._decodeEventABI.call({
name: 'ALLEVENTS',
jsonInterface: _this._parent.options.jsonInterface
}, log);
});

delete receipt.logs;
}
return receipt;
}
};

return this._parent._eth.sendTransaction.apply(extraFormatters, [args.options, args.callback]);

}

Expand Down
9 changes: 0 additions & 9 deletions packages/web3-providers-http/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,5 @@ HttpProvider.prototype.send = function (payload, callback) {
}
};

/**
* Synchronously tries to make Http request
*
* @method isConnected
* @return {Boolean} returns true if request haven't failed. Otherwise false
*/
HttpProvider.prototype.isConnected = function() {
return this.connected;
};

module.exports = HttpProvider;
19 changes: 6 additions & 13 deletions packages/web3-providers-ipc/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,21 +138,14 @@ IpcProvider.prototype.reconnect = function() {
this.connection.connect({path: this.path});
};

/**
Check if the current connection is still valid.
@method isConnected
*/
IpcProvider.prototype.isConnected = function() {
var _this = this;

// try reconnect, when connection is gone
if(!_this.connection.writable)
_this.connection.connect({path: _this.path});

return !!this.connection.writable;
};
/**
Sends the request
@method send
@param {Object} payload example: {id: 1, jsonrpc: '2.0', 'method': 'eth_someMethod', params: []}
@param {Function} callback the callback to call
*/
IpcProvider.prototype.send = function (payload, callback) {
// try reconnect, when connection is gone
if(!this.connection.writable)
Expand Down
33 changes: 16 additions & 17 deletions packages/web3-providers-ws/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,23 +190,6 @@ WebsocketProvider.prototype._timeout = function() {
}
};

// TODO add reconnect


/**
Check if the current connection is still valid.
@method isConnected
*/
WebsocketProvider.prototype.isConnected = function() {
// try reconnect, when connection is gone
// if(!_this.connection.writable)
// _this.connection.connect({path: _this.path});

console.log(this.connection);

// return !!this.connection.writable;
};

WebsocketProvider.prototype.send = function (payload, callback) {
// try reconnect, when connection is gone
Expand Down Expand Up @@ -239,6 +222,14 @@ WebsocketProvider.prototype.on = function (type, callback) {
this.connection.onopen = callback;
break;

case 'end':
this.connection.onclose = callback;
break;

case 'error':
this.connection.onerror = callback;
break;

// default:
// this.connection.on(type, callback);
// break;
Expand Down Expand Up @@ -291,6 +282,14 @@ WebsocketProvider.prototype.removeAllListeners = function (type) {
this.connection.onopen = null;
break;

case 'end':
this.connection.onclose = null;
break;

case 'error':
this.connection.onerror = null;
break;

default:
// this.connection.removeAllListeners(type);
break;
Expand Down
8 changes: 1 addition & 7 deletions packages/web3-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ var sha3 = function (value) {
* @return {Object} the emitter
*/
var _fireError = function (error, emitter, reject, callback) {
var additionalData;

if (isArray(error)) {
error = error[0];
additionalData = error[1];
}

if (isFunction(callback)) {
callback(error);
Expand All @@ -73,7 +67,7 @@ var _fireError = function (error, emitter, reject, callback) {
}

if(emitter && isFunction(emitter.emit)) {
emitter.emit('error', error, additionalData);
emitter.emit('error', error);
emitter.removeAllListeners();
}

Expand Down
Loading

0 comments on commit c800df8

Please sign in to comment.