-
Notifications
You must be signed in to change notification settings - Fork 0
/
oracle.js
60 lines (52 loc) · 2.16 KB
/
oracle.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
const OracleContract = require('./build/contracts/BambooPriceOracle.json')
const contract = require('truffle-contract');
const fetchUrl = require('fetch').fetchUrl;
const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
// Truffle abstraction to interact with our
// deployed contract
const oracle = contract(OracleContract);
oracle.setProvider(web3.currentProvider);
fetchUrl('https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD&sign=true', etherPrice);
function etherPrice (error, meta, body) {
if (error) { return console.error(error); }
let result = JSON.parse(body.toString());
let etherPrice = Number(result.USD);
console.log(`Ether price is: ${etherPrice.toFixed(2)}`);
// Dirty hack for web3@1.0.0 support for localhost testrpc
// see https://github.com/trufflesuite/truffle-contract/issues/56#issuecomment-331084530
if (typeof oracle.currentProvider.sendAsync !== "function") {
oracle.currentProvider.sendAsync = function() {
return oracle.currentProvider.send.apply(
oracle.currentProvider, arguments
);
};
}
// Get accounts from web3
web3.eth.getAccounts((error, accounts) => {
if (error) { return console.error(error); }
oracle.deployed()
.then(oracleInstance => {
console.log('Watching for events');
// Watch event and respond to event
// With a callback function
oracleInstance.CallbackGetEtherPrice()
.watch((error, event) => {
if (error) { return console.error(error); }
function setEtherPrice (error, meta, body) {
if (error) { return console.error(error); }
let result = JSON.parse(body.toString());
let etherPrice = Number(result.USD);
console.log(`Ether price is: ${etherPrice}`);
return oracleInstance.setEtherPrice(etherPrice.toFixed(2) * 100, {from: accounts[0]});
}
// Fetch data
// and update it into the contract
fetchUrl('https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD&sign=true', setEtherPrice);
});
})
.catch(error => {
if (error) {return console.error(error);}
});
});
}