1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| var Web3 = require('web3'); var web3 = new Web3(Web3.givenProvider || "ws://localhost:8546"); web3.eth.defaultAccount = 'bbb';
var abi = [{"constant":true,"inputs":[{"name":"a","type":"uint256"},{"name":"b","type":"uint256"}],"name":"count","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"a","type":"uint256"},{"name":"b","type":"uint256"}],"name":"add","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"view","type":"function","constant":true,"name":"owner","outputs":[{"name":"","type":"address"}]},{"anonymous":false,"inputs":[{"indexed":false,"name":"owner","type":"address"},{"indexed":false,"name":"transactionSender","type":"address"},{"indexed":false,"name":"a","type":"uint256"},{"indexed":false,"name":"b","type":"uint256"},{"indexed":false,"name":"result","type":"uint256"}],"name":"getCount","type":"function","constant":true,"outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view"},{"inputs":[{"name":"a","type":"uint256"},{"name":"b","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function","constant":false,"name":"realAdd","outputs":[{"name":"","type":"uint256"}]},{"anonymous":false,"inputs":[{"indexed":false,"name":"owner","type":"address"},{"indexed":false,"name":"transactionSender","type":"address"},{"indexed":false,"name":"a","type":"uint256"},{"indexed":false,"name":"b","type":"uint256"},{"indexed":false,"name":"result","type":"uint256"}],"name":"EventSendCoin","type":"constructor","payable":false,"stateMutability":"nonpayable"},{"anonymous":false,"inputs":[{"indexed":false,"name":"owner","type":"address"},{"indexed":false,"name":"transactionSender","type":"address"},{"indexed":false,"name":"a","type":"uint256"},{"indexed":false,"name":"b","type":"uint256"},{"indexed":false,"name":"result","type":"uint256"}],"name":"addEvent","type":"event"}] var address = '"0x46a0efeda982381e8a6d75b9b3d25cabcd6b16e9"';
var code = '0x6060604052341561000f57600080fd5b336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555061038a8061005e6000396000f30060606040526004361061006d576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806306661abd14610072578063771602f71461009b5780638da5cb5b146100db578063a87d942c14610130578063e752be4e14610159575b600080fd5b341561007d57600080fd5b610085610199565b6040518082815260200191505060405180910390f35b34156100a657600080fd5b6100c5600480803590602001909190803590602001909190505061019f565b6040518082815260200191505060405180910390f35b34156100e657600080fd5b6100ee61027d565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561013b57600080fd5b6101436102a2565b6040518082815260200191505060405180910390f35b341561016457600080fd5b6101836004808035906020019091908035906020019091905050610351565b6040518082815260200191505060405180910390f35b60015481565b6000806001600081548092919060010191905055506101be8484610351565b90506000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167fbcc141744793f7e93083ceb1e4a61664d4a6428dadf6db4d2e8b1a162d0582b233868685604051808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200184815260200183815260200182815260200194505050505060405180910390a28091505092915050565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167fd54b813e36aaac7612686fa27ef81917785024960e408194ccaeb6b17926cecc33600154604051808373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018281526020019250505060405180910390a2600154905090565b60008183019050929150505600a165627a7a72305820d297e7e10c1b3d3f05cad089467287d5315e1957fe323040bcdea71f4eed65ad0029'; var abi = [{"constant":true,"inputs":[],"name":"count","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"a","type":"uint256"},{"name":"b","type":"uint256"}],"name":"add","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"getCount","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"a","type":"uint256"},{"name":"b","type":"uint256"}],"name":"realAdd","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":false,"name":"transactionSender","type":"address"},{"indexed":false,"name":"a","type":"uint256"},{"indexed":false,"name":"b","type":"uint256"},{"indexed":false,"name":"result","type":"uint256"}],"name":"AddEvent","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":false,"name":"transactionSender","type":"address"},{"indexed":false,"name":"result","type":"uint256"}],"name":"CountEvent","type":"event"}];
var address = '0x980e5451d2bd9c524e33f543af81b14e787488ef';
var myContract = new web3.eth.Contract(abi, address, { from: 'bbb', gasPrice: '1' }); myContract.options.from = 'bbb';
myContract.methods.add(1, 2).call({ from: web3.eth.defaultAccount}, function(error, result){console.log(error);console.log(result)}) myContract.methods.add(1, 2).send({ from: web3.eth.defaultAccount}, function(error, result){console.log(error);console.log(result)}) myContract.methods.add(1, 2).call({}, function(error, result){console.log(error);console.log(result)}) myContract.methods.owner().call({ from: 'bbb'}, function(error, result){console.log(error);console.log(result)}) myContract.methods.getCount().call({}, function(error, result){console.log(error);console.log(result)})
myContract.events.AddEvent({ filter: {from: web3.eth.defaultAccount}, fromBlock: 0, toBlock: 'latest' }, function(error, e){ if(!error) { console.log('the error is: ' + error) } if(!e) { console.log('the result is: ' + e.returnValues.result) } }).on('data', function(e){ console.log('the result is: ' + e.returnValues.result) }).on('changed', function(e){ console.log('inside the changed callback: ' + e); }).on('error', console.error);
myContract.methods.add(1, 2).send({ from: web3.eth.defaultAccount}, function(error, result){console.log(result)}).on('transactionHash', function(hash){ console.log('on transactionHash: ' + hash); }).on('confirmation', function(confirmationNumber, receipt){ }).on('receipt', function(receipt){ console.log('receipt: ' + JSON.stringify(receipt)); console.log('result is:' + receipt.events.AddEvent.returnValues.result) }).on('error', console.error);
myContract.getPastEvents('AddEvent', { fromBlock: 0, toBlock: 'latest' }, function(error, events){ console.log('events are: ' + events); }) .then(function(events){ console.log("inside then: " + events) });
|