Files: 0eb95aa50649c50e649d4a15e0e8985752ea5db5 / index.js
2198 bytesRaw
1 | |
2 | var program = require('commander'); |
3 | var chalk = require('chalk'); |
4 | |
5 | var fs = require('fs'); |
6 | var exec = require('child_process').exec; |
7 | |
8 | |
9 | // Ethereum stuff |
10 | var Web3 = require('web3') |
11 | |
12 | const TESTRPC_HOST = 'localhost' |
13 | const TESTRPC_PORT = '8545' |
14 | |
15 | var address = "" |
16 | program |
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 | |
31 | if(!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 | |
41 | var web3 = new Web3() |
42 | web3.setProvider(new web3.providers.HttpProvider(program.rpc)); |
43 | |
44 | var abi = JSON.parse(fs.readFileSync(program.abi, 'utf8')); |
45 | var contract = web3.eth.contract(abi) |
46 | var contract_instance = contract.at(watch_address) |
47 | |
48 | function 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 |
58 | if(program.event != "*" && contract_instance[program.event] == null) |
59 | { |
60 | console.log(`${program.event} is not an Event defined in this ABI`) |
61 | } |
62 | else |
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