-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
196 lines (181 loc) · 5.59 KB
/
index.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env node
/*
Libraries
*/
var chalk = require('chalk');
var clear = require('clear');
var figlet = require('figlet');
var inquirer = require('inquirer');
var Web3 = require('web3');
/*
Variables
*/
var accountPassword;
var web3;
var contractAddress;
var abi;
var eventName;
var contract;
var event;
/********************************************************************
1. Print headline
*******************************************************************/
clear();
console.log(
chalk.blue(
figlet.textSync('Ethereum listener', {horizontalLayout: 'full'})
)
);
/********************************************************************
2. Ask for the address of the ethereum client and also for the password of the user
*******************************************************************/
getWeb3();
function getWeb3() {
getEthereumProvider(function () {
accountPassword = arguments[0].password;
web3 = new Web3(new Web3.providers.HttpProvider(arguments[0].ethereumProvider));
web3.eth.getBlock('latest', function (error, result) {
if (!error) {
web3.eth.defaultAccount = web3.eth.accounts[0];
unlockDefaultAccount();
}
else {
console.error("Wrong ethereum client address!");
getWeb3();
}
});
});
}
function getEthereumProvider(callback) {
var questions = [
{
name: 'ethereumProvider',
type: 'input',
message: 'Enter your ethereum client address:',
validate: function (value) {
if (value.length) {
return true;
} else {
return 'Please enter your username or e-mail address';
}
}
},
{
name: 'password',
type: 'password',
message: 'Enter your ethereum user password:',
validate: function (value) {
if (value.length) {
return true;
} else {
return 'Please enter your password';
}
}
}
];
inquirer.prompt(questions).then(callback);
}
function unlockDefaultAccount() {
web3.personal.unlockAccount(web3.eth.defaultAccount, accountPassword, 999, (function (error, result) {
if (!error) {
// all ok
// console.log(result);
getContract();
}
else {
console.error("Wrong ethereum password!");
getWeb3();
}
}));
}
/********************************************************************
3. Ask for the contract address, abi and for the name of the event that we want to subscribe to
*******************************************************************/
function getContract() {
getContractArguments(function () {
contractAddress = arguments[0].contractAddress;
abi = JSON.parse(arguments[0].contractAbi);
eventName = arguments[0].eventName;
web3.personal.unlockAccount(web3.eth.defaultAccount, accountPassword, 9999, eventListener);
});
}
function getContractArguments(callback) {
var questions = [
{
name: 'contractAddress',
type: 'input',
message: 'Enter the address of the contract that you want to listen to:',
validate: function (value) {
if (value.length) {
return true;
} else {
return 'Please enter the address of the contract that you want to listen to';
}
}
},
{
name: 'contractAbi',
type: 'editor',
message: 'Enter the ABI of the contract that you want to listen to:',
validate: function (value) {
if (value.length) {
return true;
} else {
return 'Please enter the ABI of the contract that you want to listen to:';
}
}
},
{
name: 'eventName',
type: 'input',
message: 'Enter the name of the event that you want to subscribe to:',
validate: function (value) {
if (value.length) {
return true;
} else {
return 'Please enter the ABI of the contract that you want to listen to:';
}
}
}
];
inquirer.prompt(questions).then(callback);
}
/********************************************************************
4. Listen to the event of the contract
*******************************************************************/
function eventListener(error, result) {
if (!error) {
contract = web3.eth.contract(abi).at(contractAddress);
event = contract[eventName]();
console.log(contract);
console.log(event);
event.watch(function (error, eventResult) {
if (error) {
console.error(error);
} else {
console.log(eventResult);
}
});
}
else {
console.error(error);
}
}
/**
* TODO Not used so far. At the moment the npm package can only be used as a CLI tool
*/
module.exports = {
subscribe: function () {
// watch for changes
monitoringEvent.watch(function (error, result) {
if (error) {
console.error(error);
} else {
console.log(result);
}
});
},
unsubscribe: function () {
monitoringEvent.stopWatching();
}
};