web3.js/example/event_inc.html

89 lines
2.8 KiB
HTML
Raw Normal View History

2015-02-02 10:52:56 +00:00
<!doctype>
<html>
<head>
2015-04-07 02:39:47 +00:00
<script type="text/javascript" src="../dist/web3.js"></script>
2015-02-02 10:52:56 +00:00
<script type="text/javascript">
2015-10-21 04:02:00 +00:00
var Web3 = require('web3');
var web3 = new Web3();
2015-04-20 11:52:40 +00:00
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
2015-02-02 10:52:56 +00:00
2015-05-13 10:05:38 +00:00
var source = "" +
"contract Contract { " +
" event Incremented(bool indexed odd, uint x); " +
" function Contract() { " +
2015-05-13 18:05:23 +00:00
" x = 70; " +
2015-05-13 10:05:38 +00:00
" } " +
" function inc() { " +
" ++x; " +
" Incremented(x % 2 == 1, x); " +
" } " +
" uint x; " +
"}";
2015-02-02 10:52:56 +00:00
2015-05-21 10:02:15 +00:00
var compiled = web3.eth.compile.solidity(source);
var code = compiled.Contract.code;
var abi = compiled.Contract.info.abiDefinition;
2015-02-02 10:52:56 +00:00
var address;
var contract;
2015-06-10 10:06:37 +00:00
var inc;
2015-02-02 10:52:56 +00:00
2015-04-20 11:52:40 +00:00
var update = function (err, x) {
2015-04-28 15:01:37 +00:00
document.getElementById('result').textContent = JSON.stringify(x, null, 2);
2015-02-02 10:52:56 +00:00
};
var createContract = function () {
2015-04-20 11:52:40 +00:00
// let's assume that we have a private key to coinbase ;)
web3.eth.defaultAccount = web3.eth.coinbase;
2015-05-13 18:05:23 +00:00
document.getElementById('create').style.visibility = 'hidden';
document.getElementById('status').innerText = "transaction sent, waiting for confirmation";
2015-07-07 09:51:13 +00:00
web3.eth.contract(abi).new({data: code}, function (err, c) {
if (err) {
2015-07-07 09:58:42 +00:00
console.error(err);
2015-07-07 09:51:13 +00:00
return;
2015-05-13 18:05:23 +00:00
2015-07-20 14:44:25 +00:00
// callback fires twice, we only want the second call when the contract is deployed
2015-08-06 15:58:57 +00:00
} else if(c.address){
2015-07-20 14:44:25 +00:00
contract = c;
console.log('address: ' + contract.address);
document.getElementById('status').innerText = 'Mined!';
document.getElementById('call').style.visibility = 'visible';
2015-07-07 09:51:13 +00:00
2015-07-20 14:44:25 +00:00
inc = contract.Incremented({odd: true}, update);
}
2015-07-07 09:51:13 +00:00
});
2015-02-02 10:52:56 +00:00
};
2015-05-13 18:05:23 +00:00
var counter = 0;
2015-02-02 10:52:56 +00:00
var callContract = function () {
2015-05-13 18:05:23 +00:00
counter++;
var all = 70 + counter;
document.getElementById('count').innerText = 'Transaction sent ' + counter + ' times. ' +
'Expected x value is: ' + (all - (all % 2 ? 0 : 1)) + ' ' +
'Waiting for the blocks to be mined...';
contract.inc();
2015-02-02 10:52:56 +00:00
};
</script>
</head>
<body>
2015-05-13 18:05:23 +00:00
<div id="status"></div>
2015-02-02 10:52:56 +00:00
<div>
2015-05-13 18:05:23 +00:00
<button id="create" type="button" onClick="createContract();">create contract</button>
2015-02-02 10:52:56 +00:00
</div>
<div>
2015-05-13 18:05:23 +00:00
<button id="call" style="visibility: hidden;" type="button" onClick="callContract();">test1</button>
2015-02-02 10:52:56 +00:00
</div>
2015-05-13 18:05:23 +00:00
<div id='count'></div>
2015-02-02 10:52:56 +00:00
<div id="result">
</div>
</body>
</html>