event example
This commit is contained in:
parent
c8ee08c24b
commit
df17c33898
50
dist/ethereum.js
vendored
50
dist/ethereum.js
vendored
@ -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));
|
||||||
};
|
};
|
||||||
|
8
dist/ethereum.js.map
vendored
8
dist/ethereum.js.map
vendored
File diff suppressed because one or more lines are too long
2
dist/ethereum.min.js
vendored
2
dist/ethereum.min.js
vendored
File diff suppressed because one or more lines are too long
@ -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
67
example/event.html
Normal 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>
|
@ -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);
|
||||||
|
21
lib/event.js
21
lib/event.js
@ -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) {
|
||||||
|
|
||||||
|
@ -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));
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user