event example

This commit is contained in:
Marek Kotewicz 2015-01-29 15:05:43 +01:00
parent c8ee08c24b
commit df17c33898
8 changed files with 162 additions and 15 deletions

50
dist/ethereum.js vendored
View File

@ -453,7 +453,7 @@ module.exports = {
var web3 = require('./web3'); var web3 = require('./web3');
var abi = require('./abi'); var abi = require('./abi');
var eventImplementation = require('./event'); var eventImpl = require('./event');
var addFunctionRelatedPropertiesToContract = function (contract) { var addFunctionRelatedPropertiesToContract = function (contract) {
@ -558,11 +558,19 @@ var addEventsToContract = function (contract, desc, address) {
var impl = function () { var impl = function () {
var params = Array.prototype.slice.call(arguments); var params = Array.prototype.slice.call(arguments);
var signature = abi.methodSignature(e.name); var signature = abi.methodSignature(e.name);
var eventImpl = eventImplementation(address, signature); var event = eventImpl(address, signature);
var o = eventImpl.apply(null, params); var o = event.apply(null, params);
return web3.eth.watch(o); return web3.eth.watch(o);
}; };
impl.address = address;
Object.defineProperty(impl, 'topics', {
get: function() {
return [abi.methodSignature(e.name)];
}
});
// TODO: rename these methods, cause they are used not only for methods // TODO: rename these methods, cause they are used not only for methods
var displayName = abi.methodDisplayName(e.name); var displayName = abi.methodDisplayName(e.name);
var typeName = abi.methodTypeName(e.name); var typeName = abi.methodTypeName(e.name);
@ -628,8 +636,27 @@ module.exports = contract;
},{"./abi":1,"./event":3,"./web3":8}],3:[function(require,module,exports){ },{"./abi":1,"./event":3,"./web3":8}],3:[function(require,module,exports){
/*
This file is part of ethereum.js.
var abi = require('./abi'); ethereum.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.
ethereum.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 ethereum.js. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file event.js
* @authors:
* Marek Kotewicz <marek@ethdev.com>
* @date 2014
*/
var implementationOfEvent = function (address, signature) { var implementationOfEvent = function (address, signature) {
@ -645,7 +672,7 @@ var implementationOfEvent = function (address, signature) {
module.exports = implementationOfEvent; module.exports = implementationOfEvent;
},{"./abi":1}],4:[function(require,module,exports){ },{}],4:[function(require,module,exports){
/* /*
This file is part of ethereum.js. This file is part of ethereum.js.
@ -679,6 +706,19 @@ var Filter = function(options, impl) {
this.impl = impl; this.impl = impl;
this.callbacks = []; this.callbacks = [];
if (typeof options !== "string") {
// evaluate lazy properties
options = {
to: options.to,
topics: options.topics,
earliest: options.earliest,
latest: options.latest,
max: options.max,
skip: options.skip,
address: options.address
};
}
this.id = impl.newFilter(options); this.id = impl.newFilter(options);
web3.provider.startPolling({call: impl.changed, args: [this.id]}, this.id, this.trigger.bind(this)); web3.provider.startPolling({call: impl.changed, args: [this.id]}, this.id, this.trigger.bind(this));
}; };

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -17,7 +17,7 @@
var originalBalance = web3.toDecimal(balance); var originalBalance = web3.toDecimal(balance);
document.getElementById('original').innerText = 'original balance: ' + originalBalance + ' watching...'; document.getElementById('original').innerText = 'original balance: ' + originalBalance + ' watching...';
web3.eth.watch({altered: coinbase}).changed(function() { web3.eth.watch({address: coinbase}).changed(function() {
balance = web3.eth.balanceAt(coinbase) balance = web3.eth.balanceAt(coinbase)
var currentBalance = web3.toDecimal(balance); var currentBalance = web3.toDecimal(balance);
document.getElementById("current").innerText = 'current: ' + currentBalance; document.getElementById("current").innerText = 'current: ' + currentBalance;

67
example/event.html Normal file
View File

@ -0,0 +1,67 @@
<!doctype>
<html>
<head>
<script type="text/javascript" src="js/bignumber.js/bignumber.min.js"></script>
<script type="text/javascript" src="../dist/ethereum.js"></script>
<script type="text/javascript">
var web3 = require('web3');
web3.setProvider(new web3.providers.HttpSyncProvider('http://localhost:8080'));
var desc = [{
"type":"event",
"inputs": [{"name":"a","type":"uint256","indexed":true},{"name":"b","type":"hash256","indexed":false}],
"name":"Event"
}, {
"type":"event",
"inputs": [{"name":"a","type":"uint256","indexed":true},{"name":"b","type":"hash256","indexed":false}],
"name":"Event2"
}, {
"type":"function",
"inputs": [{"name":"a","type":"uint256"}],
"name":"foo",
"outputs": []
}];
var address = '0x01';
var contract = web3.eth.contract(address, desc);
function test1() {
web3.eth.watch(contract).changed(function (res) {
});
};
function test2() {
web3.eth.watch(contract.Event).changed(function (res) {
});
};
function test3() {
contract.Event().changed(function (res) {
});
};
// not valid
// function test4() {
// web3.eth.watch([contract.Event, contract.Event2]).changed(function (res) {
// });
// };
</script>
</head>
<body>
<div>
<button type="button" onClick="test1();">test1</button>
</div>
<div>
<button type="button" onClick="test2();">test2</button>
</div>
<div>
<button type="button" onClick="test3();">test3</button>
</div>
</body>
</html>

View File

@ -22,7 +22,7 @@
var web3 = require('./web3'); var web3 = require('./web3');
var abi = require('./abi'); var abi = require('./abi');
var eventImplementation = require('./event'); var eventImpl = require('./event');
var addFunctionRelatedPropertiesToContract = function (contract) { var addFunctionRelatedPropertiesToContract = function (contract) {
@ -127,11 +127,19 @@ var addEventsToContract = function (contract, desc, address) {
var impl = function () { var impl = function () {
var params = Array.prototype.slice.call(arguments); var params = Array.prototype.slice.call(arguments);
var signature = abi.methodSignature(e.name); var signature = abi.methodSignature(e.name);
var eventImpl = eventImplementation(address, signature); var event = eventImpl(address, signature);
var o = eventImpl.apply(null, params); var o = event.apply(null, params);
return web3.eth.watch(o); return web3.eth.watch(o);
}; };
impl.address = address;
Object.defineProperty(impl, 'topics', {
get: function() {
return [abi.methodSignature(e.name)];
}
});
// TODO: rename these methods, cause they are used not only for methods // TODO: rename these methods, cause they are used not only for methods
var displayName = abi.methodDisplayName(e.name); var displayName = abi.methodDisplayName(e.name);
var typeName = abi.methodTypeName(e.name); var typeName = abi.methodTypeName(e.name);

View File

@ -1,5 +1,24 @@
/*
This file is part of ethereum.js.
var abi = require('./abi'); ethereum.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.
ethereum.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 ethereum.js. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file event.js
* @authors:
* Marek Kotewicz <marek@ethdev.com>
* @date 2014
*/
var implementationOfEvent = function (address, signature) { var implementationOfEvent = function (address, signature) {

View File

@ -31,6 +31,19 @@ var Filter = function(options, impl) {
this.impl = impl; this.impl = impl;
this.callbacks = []; this.callbacks = [];
if (typeof options !== "string") {
// evaluate lazy properties
options = {
to: options.to,
topics: options.topics,
earliest: options.earliest,
latest: options.latest,
max: options.max,
skip: options.skip,
address: options.address
};
}
this.id = impl.newFilter(options); this.id = impl.newFilter(options);
web3.provider.startPolling({call: impl.changed, args: [this.id]}, this.id, this.trigger.bind(this)); web3.provider.startPolling({call: impl.changed, args: [this.id]}, this.id, this.trigger.bind(this));
}; };