-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClient.js
44 lines (38 loc) · 1.73 KB
/
Client.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
const common = require('./utils/common.js')
const SLEEP_INTERVAL = process.env.SLEEP_INTERVAL || 2000
const PRIVATE_KEY_FILE_NAME = process.env.PRIVATE_KEY_FILE || './caller/caller_private_key'
const CallerJSON = require('./caller/build/contracts/CallerContract.json')
const OracleJSON = require('./oracle/build/contracts/EthPriceOracle.json')
async function getCallerContract (web3js) {
const networkId = await web3js.eth.net.getId()
return new web3js.eth.Contract(CallerJSON.abi, CallerJSON.networks[networkId].address)
}
async function filterEvents (callerContract) {
callerContract.events.PriceUpdatedEvent({ filter: { } }, async (err, event) => {
if (err) console.error('Error on event', err)
console.log('* New PriceUpdated event. ethPrice: ' + event.returnValues.ethPrice)
})
callerContract.events.ReceivedNewRequestIdEvent({ filter: { } }, async (err, event) => {
if (err) console.error('Error on event', err)
})
}
async function init () {
const { ownerAddress, web3js, client } = common.loadAccount(PRIVATE_KEY_FILE_NAME)
const callerContract = await getCallerContract(web3js)
filterEvents(callerContract)
return { callerContract, ownerAddress, client, web3js }
}
(async () => {
const { callerContract, ownerAddress, client, web3js } = await init()
process.on( 'SIGINT', () => {
console.log('Calling client.disconnect()')
client.disconnect();
process.exit( );
})
const networkId = await web3js.eth.net.getId()
const oracleAddress = OracleJSON.networks[networkId].address
await callerContract.methods.setOracleInstanceAddress(oracleAddress).send({ from: ownerAddress })
setInterval( async () => {
await callerContract.methods.updateEthPrice().send({ from: ownerAddress })
}, SLEEP_INTERVAL);
})()