tornado-relayer/src/priceWatcher.js

45 lines
1.4 KiB
JavaScript
Raw Normal View History

2020-10-05 14:22:52 +00:00
const Redis = require('ioredis')
2021-03-02 04:38:16 +00:00
const { redisUrl, offchainOracleAddress, oracleRpcUrl } = require('./config')
2020-10-05 14:22:52 +00:00
const { getArgsForOracle, setSafeInterval } = require('./utils')
const redis = new Redis(redisUrl)
const Web3 = require('web3')
2021-03-02 04:55:03 +00:00
const web3 = new Web3(
new Web3.providers.HttpProvider(oracleRpcUrl, {
timeout: 200000, // ms
}),
)
2020-10-05 14:22:52 +00:00
2021-02-26 09:52:15 +00:00
const offchainOracleABI = require('../abis/OffchainOracle.abi.json')
2021-03-02 04:38:16 +00:00
const offchainOracle = new web3.eth.Contract(offchainOracleABI, offchainOracleAddress)
2020-10-05 14:22:52 +00:00
const { tokenAddresses, oneUintAmount, currencyLookup } = getArgsForOracle()
2021-02-26 09:52:15 +00:00
const { toBN } = require('web3-utils')
2020-10-05 14:22:52 +00:00
async function main() {
2021-03-02 04:38:16 +00:00
try {
const ethPrices = {}
for (let i = 0; i < tokenAddresses.length; i++) {
try {
const price = await offchainOracle.methods
2021-06-19 20:03:18 +00:00
.getRate(tokenAddresses[i], '0x0000000000000000000000000000000000000000', false)
2021-03-02 04:38:16 +00:00
.call()
const numerator = toBN(oneUintAmount[i])
const denominator = toBN(10).pow(toBN(18)) // eth decimals
const priceFormatted = toBN(price).mul(numerator).div(denominator)
ethPrices[currencyLookup[tokenAddresses[i]]] = priceFormatted.toString()
} catch (e) {
console.error('cant get price of ', tokenAddresses[i])
}
2021-02-26 09:52:15 +00:00
}
2021-03-02 04:38:16 +00:00
await redis.hmset('prices', ethPrices)
console.log('Wrote following prices to redis', ethPrices)
} catch (e) {
console.error('priceWatcher error', e)
2021-02-26 09:52:15 +00:00
}
2020-10-05 14:22:52 +00:00
}
setSafeInterval(main, 30 * 1000)