dai price

This commit is contained in:
Alexey 2019-09-19 18:21:57 +03:00
parent 53828dca12
commit 2d2115ab43
5 changed files with 38 additions and 6 deletions

View file

@ -1,6 +1,7 @@
const fetch = require('node-fetch')
const { isHexStrict } = require('web3-utils')
const { gasOracleUrls } = require('./config')
const { isHexStrict, hexToNumberString } = require('web3-utils')
const { gasOracleUrls, ethdaiAddress } = require('./config')
const oracleABI = require('./abis/ETHDAIOracle.json')
async function fetchGasPrice({ gasPrices, oracleIndex = 0 }) {
oracleIndex = (oracleIndex + 1) % gasOracleUrls.length
@ -30,6 +31,17 @@ async function fetchGasPrice({ gasPrices, oracleIndex = 0 }) {
}
}
async function fetchDAIprice({ ethPriceInDai, web3 }) {
try {
const ethDaiInstance = web3.eth.Contract(oracleABI, ethdaiAddress)
const price = await ethDaiInstance.methods.getMedianPrice().call()
ethPriceInDai = hexToNumberString(price)
setTimeout(() => fetchDAIprice({ ethPriceInDai, web3 }), 1000 * 30)
} catch(e) {
setTimeout(() => fetchDAIprice({ ethPriceInDai, web3 }), 1000 * 30)
}
}
function isValidProof(proof) {
// validator expects `websnarkUtils.toSolidityInput(proof)` output
@ -82,4 +94,4 @@ function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
module.exports = { fetchGasPrice, isValidProof, sleep }
module.exports = { fetchGasPrice, isValidProof, sleep, fetchDAIprice }