-
Notifications
You must be signed in to change notification settings - Fork 7
/
oracle.js
60 lines (51 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
var fetch = require('fetch')
var OracleContract = require('./build/contracts/CMCOracle.json')
var contract = require('@truffle/contract');
var HDWalletProvider = require('@truffle/hdwallet-provider');
var dotenv = require('dotenv');
dotenv.config();
var provider = new HDWalletProvider({
mnemonic: process.env.MNEMONIC,
providerOrUrl: process.env.INFURA_PROJECT_ID ? `https://ropsten.infura.io/v3/${process.env.INFURA_PROJECT_ID}` : 'http://localhost:8545'
});
const oracleContractAddress = process.env.ORACLE_ADDRESS;
var Web3 = require('web3');
var web3 = new Web3(provider);
var web3Wss = new Web3(new Web3.providers.WebsocketProvider(`wss://ropsten.infura.io/ws/v3/${process.env.INFURA_PROJECT_ID}`));
// Truffle abstraction to interact with our
// deployed contract
var oracleContract = contract(OracleContract);
oracleContract.setProvider(provider);
var oracleContractWss = contract(OracleContract);
oracleContractWss.setProvider(web3Wss.currentProvider);
// Get accounts from web3
web3.eth.getAccounts((err, accounts) => {
// console.log(err, accounts)
oracleContract.at(oracleContractAddress)
.then(async (oracleInstance) => {
let btccap = await oracleInstance.getBTCCap({ from: accounts[0] })
await oracleInstance.setBTCCap(100, { from: accounts[0] });
console.log(btccap)
oracleContractWss.at(oracleContractAddress).then(async (oracleInstanceWss) => {
let allEventsFn = oracleInstanceWss.allEvents({ fromBlock: "latest" });
allEventsFn.on("data", (data) => {
console.log('got data', data);
fetch.fetchUrl('https://cryptoviz.adityakarnam.me/coinsData', async (err, m, b) => {
if (err) throw Error(err);
const resultJSON = JSON.parse(b.toString())
const btcMarketCap = parseInt(resultJSON.coinsData[Math.floor(Math.random() * 4)].volume);
console.log(btcMarketCap)
// Send data back contract on-chain
try {
await oracleInstance.setBTCCap(btcMarketCap, { from: accounts[0] });
} catch(contractSetError) {
console.error(contractSetError);
}
});
});
});
})
.catch((err) => {
console.log(err)
})
});