-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
161 lines (149 loc) · 3.74 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
import Web3 from "web3";
import Net from "net";
import request from "request";
const privateKey = "0xae6ae8e5ccbfb04590405997ee2d52d2b330726137b875053c36d94e974d162f";
const abi = [
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"name": "_adr",
"type": "address"
},
{
"indexed": false,
"name": "id",
"type": "uint256"
},
{
"indexed": false,
"name": "urlToQuery",
"type": "string"
},
{
"indexed": false,
"name": "attributeToFetch",
"type": "string"
}
],
"name": "NewRequest",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"name": "id",
"type": "uint256"
},
{
"indexed": false,
"name": "urlToQuery",
"type": "string"
},
{
"indexed": false,
"name": "attributeToFetch",
"type": "string"
},
{
"indexed": false,
"name": "agreedValue",
"type": "string"
}
],
"name": "UpdatedRequest",
"type": "event"
},
{
"constant": false,
"inputs": [
{
"name": "_urlToQuery",
"type": "string"
},
{
"name": "_attributeToFetch",
"type": "string"
}
],
"name": "createRequest",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_id",
"type": "uint256"
},
{
"name": "_valueRetrieved",
"type": "string"
}
],
"name": "updateRequest",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
];
const addressOracle = "0xb9A219631Aed55eBC3D998f17C3840B7eC39C0cc";
var server = Net.createServer().listen();
var web3;
var contract;
initWeb3();
initDefaultAccount();
initContract();
contract.events.NewRequest({})
.on('data', (event) => {
print("Id:" + event.returnValues.id + " " + "URL: " + event.returnValues.urlToQuery + " Address sender: " + event.returnValues._adr);
var ID = event.returnValues.id;
var _request = event.returnValues.urlToQuery;
request(_request, function(error, response, body) {
if(error){
print("ERROR: " + error);
}
else {
print('Status code '+ response.statusCode);
print("Response: " + body);
contract.methods.updateRequest(ID,body).send({from: web3.eth.defaultAccount, gas: 17000000000});
}
});
})
.on('error', console.error);
contract.events.UpdatedRequest({})
.on('data', (event) => {
print("Update request id = " + event.returnValues.id);
})
.on('error', console.error);
function initContract(){
print("Smart contract Oracle initialization");
try{
contract = new web3.eth.Contract(abi,addressOracle);
print("Address Oracle - " + addressOracle);
} catch(err){
print(err);
}
}
function initWeb3(){
print("Web3 initialization");
web3 = new Web3(new Web3.providers.WebsocketProvider('ws://localhost:8546'));
print("WebSocket - " + web3.currentProvider.url);
}
function initDefaultAccount(){
const account = web3.eth.accounts.privateKeyToAccount(privateKey);
web3.eth.accounts.wallet.add(account);
web3.eth.defaultAccount = account.address;
print("Default signing account - " + account.address);
}
function print(_str){
var data = new Date()
console.log('\x1b[36m%s\x1b[0m',"[" + data.toLocaleDateString() + " " + data.toLocaleTimeString() + "] ", _str);
}