Commit 513dab30f5306edc1844234b880c34d8a6761bb1
working prototype
Joran committed on 5/20/2017, 1:54:58 AMParent: f8480d9008c9cbb0c9d002cba1eace79b1b67034
Files changed
ReadMe.md | changed |
index.js | changed |
package.json | changed |
example_abi.js | added |
potato.abi | added |
ReadMe.md | ||
---|---|---|
@@ -10,7 +10,7 @@ | ||
10 | 10 … | ### Run |
11 | 11 … | |
12 | 12 … | ``` |
13 | 13 … | ethwatcher "0x27dcf986bc1151b39ceadd53660e4af56b0d5f84" \ |
14 | - -event "NewPotato" \ | |
15 | - -abi ./potatoes.abi | |
14 … | + --event "NewPotato" \ | |
15 … | + --abi ./potatoes.abi | |
16 | 16 … | ``` |
index.js | |||
---|---|---|---|
@@ -1,13 +1,80 @@ | |||
1 | 1 … | #!/usr/bin/env node | |
2 | 2 … | var program = require('commander'); | |
3 | 3 … | var chalk = require('chalk'); | |
4 | 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 = "" | ||
5 | 16 … | program | |
6 | -.arguments('<contract_address>') | ||
7 | -.option('-e, --event <event>', 'The event name to watch for') | ||
8 | -.option('-a, --abi <abi>', 'Path to the ABI definition') | ||
9 | -.action(function(address, event, abi) { | ||
10 | - console.log(chalk.bold.cyan(`Watching address: ${address}`)) | ||
11 | - console.log(chalk.bold.cyan(` for event: ${event}`)) | ||
12 | -}) | ||
13 | -.parse(process.argv); | ||
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 … | + // console.log(chalk.bold.yellow(` output to: ${program.command}`)) | ||
41 … | +} | ||
42 … | + | ||
43 … | +var web3 = new Web3() | ||
44 … | +web3.setProvider(new web3.providers.HttpProvider(program.rpc)); | ||
45 … | + | ||
46 … | +var abi = JSON.parse(fs.readFileSync(program.abi, 'utf8')); | ||
47 … | +var contract = web3.eth.contract(abi) | ||
48 … | +var contract_instance = contract.at(watch_address) | ||
49 … | + | ||
50 … | +function handle_event(error, result){ | ||
51 … | + console.log(JSON.stringify(result)) | ||
52 … | + // var cmd = program.command + " " + JSON.stringify(result) | ||
53 … | + | ||
54 … | + // console.log(cmd) | ||
55 … | + // | ||
56 … | + // exec(cmd, function(error, stdout, stderr) { | ||
57 … | + // // command output is in stdout | ||
58 … | + // console.log(stdout) | ||
59 … | + // }); | ||
60 … | +} | ||
61 … | + | ||
62 … | +// Safety check this action name exists | ||
63 … | +if(program.event != "*" && contract_instance[program.event] == null) | ||
64 … | +{ | ||
65 … | + console.log(`${program.event} is not an Event defined in this ABI`) | ||
66 … | +} | ||
67 … | +else | ||
68 … | +{ | ||
69 … | + // Begin watching | ||
70 … | + var event_being_watched | ||
71 … | + if(program.event == "*") | ||
72 … | + { | ||
73 … | + event_being_watched = contract_instance.allEvents({}, {fromBlock: program.from, toBlock: program.to}); | ||
74 … | + } | ||
75 … | + else | ||
76 … | + { | ||
77 … | + event_being_watched = contract_instance[program.event]({}, {fromBlock: program.from, toBlock: program.to}); | ||
78 … | + } | ||
79 … | + event_being_watched.watch(handle_event); | ||
80 … | +} |
package.json | ||
---|---|---|
@@ -16,7 +16,8 @@ | ||
16 | 16 … | "ethwatcher": "./index.js" |
17 | 17 … | }, |
18 | 18 … | "dependencies": { |
19 | 19 … | "chalk": "^1.1.3", |
20 | - "commander": "^2.9.0" | |
20 … | + "commander": "^2.9.0", | |
21 … | + "web3": "^0.19.0" | |
21 | 22 … | } |
22 | 23 … | } |
example_abi.js | ||
---|---|---|
@@ -1,0 +1,1 @@ | ||
1 … | +[{"constant":false,"inputs":[{"name":"_etherAmount","type":"uint256"},{"name":"_description","type":"string"},{"name":"_tags","type":"string"}],"name":"newAction","outputs":[{"name":"actionID","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"actions","outputs":[{"name":"amount","type":"uint256"},{"name":"description","type":"string"},{"name":"tags","type":"string"},{"name":"votingDeadline","type":"uint256"},{"name":"done","type":"bool"},{"name":"actionPassed","type":"bool"},{"name":"numberOfVotes","type":"uint256"},{"name":"votingTally","type":"int256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"index","type":"uint256"}],"name":"getAction","outputs":[{"name":"","type":"uint256"},{"name":"","type":"string"},{"name":"","type":"uint256"},{"name":"","type":"bool"},{"name":"","type":"bool"},{"name":"","type":"uint256"},{"name":"","type":"int256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"actions_count","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"actionId","type":"uint256"},{"name":"in_favour","type":"bool"}],"name":"vote","outputs":[],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_from","type":"address"},{"indexed":true,"name":"_to","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"actionID","type":"uint256"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"description","type":"string"}],"name":"ActionAdded","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"actionID","type":"uint256"},{"indexed":false,"name":"in_favour","type":"bool"},{"indexed":false,"name":"citizen","type":"address"}],"name":"Voted","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_guardian","type":"address"},{"indexed":true,"name":"_creator","type":"address"}],"name":"NewGuardian","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_citizen","type":"address"},{"indexed":true,"name":"_guardian","type":"address"}],"name":"NewTrust","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_citizen","type":"address"},{"indexed":true,"name":"_guardian","type":"address"}],"name":"TrustLost","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_guardian","type":"address"},{"indexed":true,"name":"limit","type":"uint256"}],"name":"SafetyLimitChange","type":"event"}] |
potato.abi |
---|
Built with git-ssb-web