-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (59 loc) · 2.56 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
const express = require('express');
const app = express();
const port = 4050;
const cors = require('cors');
const moment = require('moment');
const config = require('./config.json');
app.use(cors());
const title = 'FIN4OracleEngine';
const HDWalletProvider = require('@truffle/hdwallet-provider');
const Tx = require('ethereumjs-tx').Transaction;
const Web3 = require('web3');
const networkID = 0; // 4
const networkURL = 'http://localhost:7545'; // 'https://rinkeby.infura.io/v3/' + config.INFURA_API_KEY;
const provider = new HDWalletProvider(config.ORACLE_ACCOUNT.MNEMONIC, networkURL);
const web3 = new Web3(provider);
const accountAddress = web3.currentProvider.addresses[0];
const privateKey = Buffer.from(config.ORACLE_ACCOUNT.PRIVATE_KEY, 'hex');
const Fin4OracleHubABI = require('./Fin4OracleHub.json').abi;
const contract = new web3.eth.Contract(Fin4OracleHubABI);
const contractAddress = config.Fin4OracleHubAddress;
app.listen(port, () => console.log(title + ' listening on port ' + port));
app.get('/', (req, res) => res.send(title));
app.post('/sensor', (request, response) => {
console.log('Received sensor request: ', request.query);
// e.g. http://localhost:4050/sensor?id=PlantWateringSensor&data=ThePlantSaysThanksForWatering
let sensorID = request.query.id;
let timestamp = Math.round(moment().valueOf());
let data = request.query.data;
callFin4OracleHub(sensorID, timestamp, data, response);
});
let callFin4OracleHub = async function(sensorID, timestamp, data, response) {
console.log('Attempting to call Fin4OracleHub.receiveSensorSignal()', contractAddress, sensorID, timestamp, data);
let callData = contract.methods.receiveSensorSignal(sensorID, timestamp, data).encodeABI();
web3.eth.getGasPrice((e, gasPrice) => {
console.log('Got gas price: ' + gasPrice);
web3.eth.getTransactionCount(accountAddress).then(count => {
console.log('Got transaction count: ' + count);
const rawTransaction = {
from: accountAddress,
gas: web3.utils.toHex(100000 * 10), // is * 10 a good factor??
gasPrice: web3.utils.toHex(gasPrice * 2),
to: contractAddress,
value: '0x00',
chainId: web3.utils.toHex(networkID),
data: callData,
nonce: web3.utils.toHex(count)
};
console.log('Created rawTransaction');
var tx = new Tx(rawTransaction);
tx.sign(privateKey);
console.log('Transaction is signed');
web3.eth.sendSignedTransaction('0x' + tx.serialize().toString('hex')).on('receipt', receipt => {
let report = 'Called Fin4OracleHub.receiveSensorSignal()';
console.log(report);
response.send(report);
});
});
});
};