2022-03-23 18:35:41 +03:00
|
|
|
const { offchainOracleAddress } = require('./config')
|
2022-04-08 13:29:37 +03:00
|
|
|
const {
|
|
|
|
getArgsForOracle,
|
|
|
|
setSafeInterval,
|
|
|
|
toChecksumAddress,
|
|
|
|
toBN,
|
|
|
|
RelayerError,
|
|
|
|
logRelayerError,
|
|
|
|
} = require('./utils')
|
2022-03-23 18:35:41 +03:00
|
|
|
const { redis } = require('./modules/redis')
|
|
|
|
const web3 = require('./modules/web3')()
|
2020-10-05 17:22:52 +03:00
|
|
|
|
2021-02-26 12:52:15 +03:00
|
|
|
const offchainOracleABI = require('../abis/OffchainOracle.abi.json')
|
|
|
|
|
2021-03-02 07:38:16 +03:00
|
|
|
const offchainOracle = new web3.eth.Contract(offchainOracleABI, offchainOracleAddress)
|
2020-10-05 17:22:52 +03:00
|
|
|
const { tokenAddresses, oneUintAmount, currencyLookup } = getArgsForOracle()
|
|
|
|
|
|
|
|
async function main() {
|
2021-03-02 07:38:16 +03:00
|
|
|
try {
|
|
|
|
const ethPrices = {}
|
|
|
|
for (let i = 0; i < tokenAddresses.length; i++) {
|
|
|
|
try {
|
2021-06-19 23:20:19 +03:00
|
|
|
const isWrap =
|
|
|
|
toChecksumAddress(tokenAddresses[i]) ===
|
|
|
|
toChecksumAddress('0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643')
|
2021-06-19 23:10:31 +03:00
|
|
|
|
2021-06-19 23:20:19 +03:00
|
|
|
const price = await offchainOracle.methods.getRateToEth(tokenAddresses[i], isWrap).call()
|
2021-03-02 07:38:16 +03:00
|
|
|
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 12:52:15 +03:00
|
|
|
}
|
2022-04-07 08:02:28 +03:00
|
|
|
if (!Object.values(ethPrices).length) {
|
|
|
|
throw new RelayerError('Can`t update prices', 1)
|
|
|
|
}
|
2021-03-02 07:38:16 +03:00
|
|
|
await redis.hmset('prices', ethPrices)
|
|
|
|
console.log('Wrote following prices to redis', ethPrices)
|
|
|
|
} catch (e) {
|
2022-04-08 13:29:37 +03:00
|
|
|
await logRelayerError(redis, e.message)
|
2021-03-02 07:38:16 +03:00
|
|
|
console.error('priceWatcher error', e)
|
2021-02-26 12:52:15 +03:00
|
|
|
}
|
2020-10-05 17:22:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
setSafeInterval(main, 30 * 1000)
|