web3.js/example/event_inc.html

79 lines
2.5 KiB
HTML
Raw Normal View History

2015-02-02 02:52:56 -08:00
<!doctype>
<html>
<head>
2015-04-06 19:39:47 -07:00
<script type="text/javascript" src="../dist/web3.js"></script>
2015-02-02 02:52:56 -08:00
<script type="text/javascript">
2015-04-06 19:39:47 -07:00
var web3 = require('web3');
2015-04-20 04:52:40 -07:00
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
2015-02-02 02:52:56 -08:00
2015-04-20 04:52:40 -07:00
/*var source = "" + */
/*"contract Contract { " +*/
/*" event Incremented(bool indexed odd, uint x); " +*/
/*" function Contract() { " +*/
/*" x = 69; " +*/
/*" } " +*/
/*" function inc() { " +*/
/*" ++x; " +*/
/*" Incremented(x % 2 == 1, x); " +*/
/*" } " +*/
/*" uint x; " +*/
/*"}";*/
var source = "5b60456000600050819055505b608c8060196000396000f3006000357c010000000000000000000000000000000000000000000000000000000090048063371303c014602e57005b6034603a565b60006000f35b6000600081815054600101919050819055506001600260006000505406147f6e61ef44ac2747ff8b84d353a908eb8bd5c3fb118334d57698c5cfc7041196ad600060006000505481526020016000a25b56";
2015-02-02 02:52:56 -08:00
var desc = [{
2015-04-20 04:52:40 -07:00
"constant" : false,
"inputs" : [],
"name" : "inc",
"outputs" : [],
"type" : "function"
2015-02-02 02:52:56 -08:00
}, {
2015-04-20 04:52:40 -07:00
"anonymous" : false,
"inputs" : [{
"indexed" : true,
"name" : "odd",
"type" : "bool"
}, {
"indexed" : false,
"name" : "x",
"type" : "uint256"
}],
"name" : "Incremented",
"type" : "event"
2015-02-02 02:52:56 -08:00
}];
var address;
var contract;
2015-04-20 04:52:40 -07:00
var update = function (err, x) {
2015-02-02 02:52:56 -08:00
document.getElementById('result').innerText = JSON.stringify(x);
};
var createContract = function () {
2015-04-20 04:52:40 -07:00
// let's assume that we have a private key to coinbase ;)
web3.eth.defaultAccount = web3.eth.coinbase;
2015-04-20 04:52:40 -07:00
var Contract = web3.eth.contract(desc);
2015-04-20 05:38:08 -07:00
contract = new Contract({data: source});
2015-04-20 04:52:40 -07:00
contract.Incremented({odd: true}).watch(update);
2015-02-02 02:52:56 -08:00
};
var callContract = function () {
contract.inc();
2015-02-02 02:52:56 -08:00
};
</script>
</head>
<body>
<div>
<button type="button" onClick="createContract();">create contract</button>
</div>
<div>
<button type="button" onClick="callContract();">test1</button>
</div>
<div id="result">
</div>
</body>
</html>