git ssb

0+

dangerousbeans / eth_watcher



Tree: 0eb95aa50649c50e649d4a15e0e8985752ea5db5

Files: 0eb95aa50649c50e649d4a15e0e8985752ea5db5 / index.js

2198 bytesRaw
1#!/usr/bin/env node
2var program = require('commander');
3var chalk = require('chalk');
4
5var fs = require('fs');
6var exec = require('child_process').exec;
7
8
9// Ethereum stuff
10var Web3 = require('web3')
11
12const TESTRPC_HOST = 'localhost'
13const TESTRPC_PORT = '8545'
14
15var address = ""
16program
17 .version('0.0.1')
18 .arguments('<address>')
19 .option('-e, --event <event>', 'The event name to watch for', '*')
20 .option('-a, --abi <abi>', 'Path to the ABI definition')
21 .option('-f, --from <from>', 'Block to watch from', 0)
22 .option('-t, --to <to>', 'Block to watch until', 'latest')
23 .option('-r, --rpc <rpc>', 'Address of RPC Ethereum Node', 'http://localhost:8545')
24 // .option('-c, --command <command>', 'Command to run with event JSON as input', 'jq')
25 .option('-s, --silent <silent>', 'Dont display non-json info on startup', false)
26 .action(function(address) {
27 watch_address = address
28 })
29 .parse(process.argv);
30
31if(!program.silent)
32{
33 console.log(chalk.bold.red( ` Connected to: ${program.rpc}`))
34 console.log(chalk.bold.cyan(`Watching address: ${watch_address}`))
35 console.log(chalk.bold.cyan(` From block: ${program.from}`))
36 console.log(chalk.bold.cyan(` To block: ${program.to}`))
37 console.log(chalk.bold.cyan(` for event: ${program.event}`))
38 console.log(chalk.bold.cyan(` with abi: ${program.abi}`))
39}
40
41var web3 = new Web3()
42web3.setProvider(new web3.providers.HttpProvider(program.rpc));
43
44var abi = JSON.parse(fs.readFileSync(program.abi, 'utf8'));
45var contract = web3.eth.contract(abi)
46var contract_instance = contract.at(watch_address)
47
48function handle_event(error, result){
49 // Should we respond to this event?
50 if(program.event == '*' || result.event == program.event)
51 {
52 // Output json to console
53 console.log(JSON.stringify(result))
54 }
55}
56
57// Safety check this action name exists
58if(program.event != "*" && contract_instance[program.event] == null)
59{
60 console.log(`${program.event} is not an Event defined in this ABI`)
61}
62else
63{
64 // Begin watching
65 var event_being_watched
66 event_being_watched = contract_instance.allEvents({}, {fromBlock: program.from, toBlock: program.to});
67
68 event_being_watched.watch(handle_event);
69}
70

Built with git-ssb-web