-
Notifications
You must be signed in to change notification settings - Fork 1
/
eventOracle.js
57 lines (49 loc) · 2.2 KB
/
eventOracle.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
const Web3 = require('web3');
const HDWalletProvider = require('@truffle/hdwallet-provider');
require('dotenv').config();
const signProvider = new HDWalletProvider(process.env.SEED, `wss://goerli.infura.io/ws/v3/433dbe41c8664d74a0a191d9e655f643`);
const web3Sign = new Web3(signProvider);
const web3Listen = new Web3('wss://goerli.infura.io/ws/v3/433dbe41c8664d74a0a191d9e655f643');
let { abi } = require('./artifacts/contracts/OracleV2.sol/OracleV2.json');
let { deployer, address } = require('./Details2.json');
MyContractSign = new web3Sign.eth.Contract(abi, address);
MyContractListen = new web3Listen.eth.Contract(abi, address);
AccountAddress = deployer;
ContractAddress = address;
// a delay functon
const timer = ms => new Promise(res => setTimeout(res, ms))
eventOracleV2();
async function eventOracleV1() {
MyContractListen.events.PriceDataRequest()
.on("data", async (result) => {
console.log("Event recived")
let data1 = await axios.get(" https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=BTC,USD,EUR")
// let usd= JSON.stringify(data1.data.USD);
await MyContractSign.methods.updatePriceData(usd)
.send({ from: AccountAddress, gasLimit: "927000" })
console.log("Updated on chain oracle contract")
})
}
async function eventOracleV2() {
var options = {
reconnect: {
auto: true,
delay: 5000, // ms
maxAttempts: 5,
onTimeout: false
},
address: ContractAddress,
topics: []
};
var subscription = web3Listen.eth.subscribe('logs', options, function(error, result){
if (!error) console.log('got result');
else console.log(error);
}).on("data", async function(log){
console.log("Event recived")
let data1 = await axios.get(" https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=BTC,USD,EUR")
// let usd= JSON.stringify(data1.data.USD);
await MyContractSign.methods.updatePriceData(usd)
.send({ from: AccountAddress, gasLimit: "927000" })
console.log("Updated on chain oracle contract")
})
}