added getPastEvent docs

This commit is contained in:
Fabian Vogelsteller 2016-11-10 15:38:03 +01:00
parent f1658b1eda
commit cce1b3d68c
No known key found for this signature in database
GPG Key ID: E51EADA77F1A4124
4 changed files with 135 additions and 54 deletions

View File

@ -50,7 +50,7 @@ Example
.. code-block:: javascript
var myContract = new web3.eth.contract([...], '0x1234567890123456789012345678901234567891', {
var myContract = new web3.eth.contract([...], '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe', {
from: '0x1234567890123456789012345678901234567891' // default from address
gasPrice: '20000000000000' // default gas price in wei
});
@ -217,21 +217,21 @@ Parameters
----------
1. ``Object`` - **options**: The options used for deployemnt.
* ``Array``` - **arguments**: The arguments which get passed to the constructor on deployment.
* ``String`` - **data**: The byte code of the contract.
* ``String`` - **from**: The address transactions should be made from.
* ``String`` - **gasPrice**: The gas price in wei to use for transactions.
* ``Number`` - **gas**: The maximum gas provided for a transaction (gas limit).
2. ``Function`` - **callbackj**: This callback will be fired when the transaction receipt is available. If the contract couldn't be deployed, the first argument will be an error object.
* ``String`` - **data**: The byte code of the contract.
* ``Array``` - **arguments** (optional): The arguments which get passed to the constructor on deployment.
* ``String`` - **gasPrice** (optional): The gas price in wei to use for transactions.
* ``Number`` - **gas** (optional): The maximum gas provided for a transaction (gas limit).
2. ``Function`` - **callback** (optional): This callback will be fired when the transaction receipt is available. If the contract couldn't be deployed, the first argument will be an error object.
-------
Returns
-------
``PromiEvent``: A promise combined event emitter. Will be resolved when the transaction *receipt* is available. Additionally the following events are available:
* ``transactionHash``: is fired right after the transaction is send and a transaction hash is available.
* ``receipt``: is fired when the transaction receipt with the contract address is available.
* ``error``: is fired if an error occurs during deployment.
* ``transactionHash`` returns ``String``: is fired right after the transaction is send and a transaction hash is available.
* ``receipt`` returns ``String``: is fired when the transaction receipt with the contract address is available.
* ``error`` returns ``Error``: is fired if an error occurs during deployment.
-------
Example
@ -257,3 +257,77 @@ Example
------------------------------------------------------------------------------
getPastEvents
=====================
.. code-block:: javascript
myContract.getPastEvents(event[, options][, callback])
Gets past events for this contract.
----------
Parameters
----------
1. ``String`` - **event**: The name of the event in the contract, or ``"allEvents"`` to get all events.
1. ``Object`` - **options** (optional): The options used for deployment.
* ``Object`` - **filter** (optional): Let you filter events by indexed parameters, e.g. ``{filter: {myNumber: [12,13]}}`` means all events where "myNumber" is 12 or 13.
* ``Number`` - **fromBlock** (optional): The block number from which to get events on.
* ``Number`` - **toBlock** (optional): The block number until events to get (Defaults to ``"latest"``).
* ``Array`` - **topics** (optional): This allows to manually set the topics for the event filter. If given the filter property and event signature (topic[0]) will not be set automatically.
2. ``Function`` - **callback** (optional): This callback will be fired with an array of event logs as the second argument, or an error as the first argument.
-------
Returns
-------
``Promise`` returns ``Array``: An array with the past event ``Objects``, matching the given event name and filter.
Structure of a returned event ``Object`` in the ``Arrray``:
- ``Object`` - **returnValues**: The return values coming from the event, e.g. ``{myVar: 1, myVar2: '0x234...'}``.
- ``String`` - **event**: The event name.
- ``Number`` - **logIndex**: Integer of the event index position in the block.
- ``Number`` - **transactionIndex**: Integer of the transaction's index position, the event was created in.
- ``String`` 32 Bytes - **transactionHash**: Hash of the transaction this event was created in.
- ``String`` 32 Bytes - **blockHash**: Hash of the block where this event was created in. ``null`` when its still pending.
- ``Number`` - **blockNumber**: The block number where this log was created in. ``null`` when still pending.
- ``String`` - **address**: from which this event originated from.
-------
Example
-------
.. code-block:: javascript
myContract.getPastEvents('MyEvent', {
filter: {myIndexedParam: [20,23], myOtherIndexedParam: '0x123456789...'}, // Using an array means OR: e.g. 20 or 23
fromBlock: 0,
toBlock: 'latest'
})
.then(function(events){
console.log(events)
});
> [{
returnValues: {
myIndexedParam: 20,
myOtherIndexedParam: '0x123456789...',
myNonIndexParam: 'My String'
},
event: 'MyEvent',
logIndex: 0,
transactionIndex: 0,
transactionHash: '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385',
blockHash: '0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7',
blockNumber: 1234,
address: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'
},{
...
}]
------------------------------------------------------------------------------

View File

@ -86,8 +86,10 @@ var _fireError = function (error, emitter, reject, callback) {
if(isFunction(reject)) {
reject(error);
}
emitter.emit('error', error);
emitter.removeAllListeners();
if(emitter && isFunction(emitter.emit)) {
emitter.emit('error', error);
emitter.removeAllListeners();
}
}, 0);
return emitter;
};

View File

@ -22,7 +22,8 @@
var utils = require('../utils/utils');
var errors = require('./errors');
var eventifiedPromise = require('./eventifiedPromise.js');
var Promise = require("bluebird");
// var eventifiedPromise = require('./eventifiedPromise.js');
var Method = function (options) {
this.name = options.name;
@ -146,7 +147,11 @@ Method.prototype.attachToObject = function (obj) {
Method.prototype.buildCall = function() {
var method = this;
var send = function () {
var defer = eventifiedPromise(),
var resolve, reject,
promise = new Promise(function() {
resolve = arguments[0];
reject = arguments[1];
}),//eventifiedPromise(),
payload = method.toPayload(Array.prototype.slice.call(arguments));
@ -161,15 +166,15 @@ Method.prototype.buildCall = function() {
if(payload.callback) {
payload.callback(null, result);
}
defer.promise.emit('data', result);
defer.resolve(result);
defer.promise.removeAllListeners();
// defer.promise.emit('data', result);
resolve(result);
//defer.promise.removeAllListeners();
} else {
return utils._fireError(err, defer.promise, defer.reject, payload.callback);
return utils._fireError(err, null, reject, payload.callback);
}
});
return defer.promise;
return promise;
};
send.request = this.request.bind(this);
return send;

View File

@ -1325,50 +1325,50 @@ describe('contract', function () {
}]);
var contract = new web3.eth.contract(abi, address);
contract.getPastEvents('Changed',{filter: {from: address2}})
.on('data', function (result) {
contract.getPastEvents('Changed', {filter: {from: address2}})
.then(function (result) {
assert.deepEqual(result, [{
assert.deepEqual(result, [{
event: "Changed",
id: "log_9ff24cb4",
address: address,
topics: topic1,
blockNumber: 3,
transactionHash: '0x1234',
blockHash: '0x1345',
logIndex: 4,
transactionIndex: 0,
data: '0x0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000009',
returnValues: {
from: address,
amount: new BigNumber(0xa),
t1: new BigNumber(0x2),
t2: new BigNumber(0x9),
}
},
{
event: "Changed",
id: "log_9ff24cb4",
id: "log_29c93e15",
address: address,
topics: topic1,
blockNumber: 3,
transactionHash: '0x1234',
blockHash: '0x1345',
logIndex: 4,
topics: topic2,
blockNumber: 4,
transactionHash: '0x1235',
blockHash: '0x1346',
logIndex: 1,
transactionIndex: 0,
data: '0x0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000009',
data: '0x0000000000000000000000000000000000000000000000000000000000000004' +
'0000000000000000000000000000000000000000000000000000000000000005',
returnValues: {
from: address,
amount: new BigNumber(0xa),
t1: new BigNumber(0x2),
t2: new BigNumber(0x9),
amount: new BigNumber(0x3),
t1: new BigNumber(0x4),
t2: new BigNumber(0x5),
}
},
{
event: "Changed",
id: "log_29c93e15",
address: address,
topics: topic2,
blockNumber: 4,
transactionHash: '0x1235',
blockHash: '0x1346',
logIndex: 1,
transactionIndex: 0,
data: '0x0000000000000000000000000000000000000000000000000000000000000004' +
'0000000000000000000000000000000000000000000000000000000000000005',
returnValues: {
from: address,
amount: new BigNumber(0x3),
t1: new BigNumber(0x4),
t2: new BigNumber(0x5),
}
}]);
}]);
done();
});
done();
});
});