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

17
abis/ETHDAIOracle.json Normal file
View File

@ -0,0 +1,17 @@
[
{
"constant": true,
"inputs": [],
"name": "getMedianPrice",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
]

View File

@ -6,5 +6,6 @@ module.exports = {
privateKey: process.env.PRIVATE_KEY, privateKey: process.env.PRIVATE_KEY,
mixerAddress: process.env.MIXER_ADDRESS, mixerAddress: process.env.MIXER_ADDRESS,
defaultGasPrice: 2, defaultGasPrice: 2,
gasOracleUrls: ['https://www.etherchain.org/api/gasPriceOracle', 'https://gasprice.poa.network/'] gasOracleUrls: ['https://www.etherchain.org/api/gasPriceOracle', 'https://gasprice.poa.network/'],
ethdaiAddress: '0x7Ef645705cb7D401C5CD91a395cfcc3Db3C93689'
} }

View File

@ -21,16 +21,17 @@ app.use(function(req, res, next) {
}) })
const { netId, rpcUrl, privateKey, mixerAddress, defaultGasPrice } = require('./config') const { netId, rpcUrl, privateKey, mixerAddress, defaultGasPrice } = require('./config')
const { fetchGasPrice, isValidProof } = require('./utils') const { fetchGasPrice, isValidProof, fetchDAIprice } = require('./utils')
const web3 = new Web3(rpcUrl, null, { transactionConfirmationBlocks: 1 }) const web3 = new Web3(rpcUrl, null, { transactionConfirmationBlocks: 1 })
const account = web3.eth.accounts.privateKeyToAccount('0x' + privateKey) const account = web3.eth.accounts.privateKeyToAccount('0x' + privateKey)
web3.eth.accounts.wallet.add('0x' + privateKey) web3.eth.accounts.wallet.add('0x' + privateKey)
web3.eth.defaultAccount = account.address web3.eth.defaultAccount = account.address
const mixerABI = require('./mixerABI.json') const mixerABI = require('./abis/mixerABI.json')
const mixer = new web3.eth.Contract(mixerABI, mixerAddress) const mixer = new web3.eth.Contract(mixerABI, mixerAddress)
const gasPrices = { fast: defaultGasPrice } const gasPrices = { fast: defaultGasPrice }
const ethPriceInDai = toWei('200')
app.get('/', function (req, res) { app.get('/', function (req, res) {
// just for testing purposes // just for testing purposes
@ -87,5 +88,6 @@ app.listen(8000)
if (Number(netId) === 1) { if (Number(netId) === 1) {
fetchGasPrice({ gasPrices }) fetchGasPrice({ gasPrices })
fetchDAIprice({ ethPriceInDai, web3 })
console.log('Gas price oracle started.') console.log('Gas price oracle started.')
} }

View File

@ -1,6 +1,7 @@
const fetch = require('node-fetch') const fetch = require('node-fetch')
const { isHexStrict } = require('web3-utils') const { isHexStrict, hexToNumberString } = require('web3-utils')
const { gasOracleUrls } = require('./config') const { gasOracleUrls, ethdaiAddress } = require('./config')
const oracleABI = require('./abis/ETHDAIOracle.json')
async function fetchGasPrice({ gasPrices, oracleIndex = 0 }) { async function fetchGasPrice({ gasPrices, oracleIndex = 0 }) {
oracleIndex = (oracleIndex + 1) % gasOracleUrls.length 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) { function isValidProof(proof) {
// validator expects `websnarkUtils.toSolidityInput(proof)` output // validator expects `websnarkUtils.toSolidityInput(proof)` output
@ -82,4 +94,4 @@ function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms)) return new Promise(resolve => setTimeout(resolve, ms))
} }
module.exports = { fetchGasPrice, isValidProof, sleep } module.exports = { fetchGasPrice, isValidProof, sleep, fetchDAIprice }