Skip to content

Instantly share code, notes, and snippets.

@sponnet
Created March 30, 2016 09:56
Show Gist options
  • Save sponnet/3645a4519f3452d45c333187c4d4c58d to your computer and use it in GitHub Desktop.
Save sponnet/3645a4519f3452d45c333187c4d4c58d to your computer and use it in GitHub Desktop.
web3js filter example ( query a contract )

run a testnet geth client

geth --testnet --rpc

let it sync and then run the example above

node web3filterexample.js

It should return something like

received answer [ { address: '0xe0b7d3450cc66c3e693a38591a967f87c8d08bba',
    topics: [ '0xb96a5204da93e5d7ddd5b0c2616fd5f76322b9c383c5010b94fdc3df11b7be52' ],
    data: '0x0000000000000000000000003903a55e0802f011077bb33382822937d94efb7f0000000000000000000000000000000000000000000000000000000000000000',
    blockNumber: 405904,
    logIndex: 0,
    blockHash: '0x197cfc09de6b00c1d1e4078403b0915836df87121417cdfe5c4d239b776aebc1',
    transactionHash: '0xb456a4f903c12de9dfaf4a2105ffb2dca6e526aaf62f7d4d5b75b837463e6135',
    transactionIndex: 0 } ]
transaction details of 0xb456a4f903c12de9dfaf4a2105ffb2dca6e526aaf62f7d4d5b75b837463e6135 { hash: '0xb456a4f903c12de9dfaf4a2105ffb2dca6e526aaf62f7d4d5b75b837463e6135',
  nonce: 1048713,
  blockHash: '0x197cfc09de6b00c1d1e4078403b0915836df87121417cdfe5c4d239b776aebc1',
  blockNumber: 405904,
  transactionIndex: 0,
  from: '0x3903a55e0802f011077bb33382822937d94efb7f',
  to: null,
  value: { [String: '0'] s: 1, e: 0, c: [ 0 ] },
  gas: 3141592,
  gasPrice: { [String: '50000000000'] s: 1, e: 10, c: [ 50000000000 ] },
  input: '0x60606040527
var Web3 = require('web3');
var host = "http://localhost:8545";
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider(host));
// create a filter monitoring a contract address
var filter = web3.eth.filter({
fromBlock: 0,
toBlock: 'latest',
address: '0xe0b7d3450cc66c3e693a38591a967f87c8d08bba'
});
// query this filter
filter.get(function(error, result) {
if (error) {
console.log('error', error);
}
// result received
console.log('received answer', result);
// now show transaction details of this result
web3.eth.getTransaction(result[0].transactionHash, function(err, txresult) {
console.log('transaction details of', result[0].transactionHash, txresult);
});
});
@medlahbib
Copy link

Thank you man :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment