From 2d2115ab439bbc77022bde60e98b95a7bd9841c2 Mon Sep 17 00:00:00 2001 From: Alexey Date: Thu, 19 Sep 2019 18:21:57 +0300 Subject: [PATCH] dai price --- abis/ETHDAIOracle.json | 17 +++++++++++++++++ mixerABI.json => abis/mixerABI.json | 0 config.js | 3 ++- index.js | 6 ++++-- utils.js | 18 +++++++++++++++--- 5 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 abis/ETHDAIOracle.json rename mixerABI.json => abis/mixerABI.json (100%) diff --git a/abis/ETHDAIOracle.json b/abis/ETHDAIOracle.json new file mode 100644 index 0000000..bf8ff2b --- /dev/null +++ b/abis/ETHDAIOracle.json @@ -0,0 +1,17 @@ +[ + { + "constant": true, + "inputs": [], + "name": "getMedianPrice", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + } +] \ No newline at end of file diff --git a/mixerABI.json b/abis/mixerABI.json similarity index 100% rename from mixerABI.json rename to abis/mixerABI.json diff --git a/config.js b/config.js index 0864fc9..3ffdb09 100644 --- a/config.js +++ b/config.js @@ -6,5 +6,6 @@ module.exports = { privateKey: process.env.PRIVATE_KEY, mixerAddress: process.env.MIXER_ADDRESS, 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' } \ No newline at end of file diff --git a/index.js b/index.js index eeb8ff6..cf0f3b3 100644 --- a/index.js +++ b/index.js @@ -21,16 +21,17 @@ app.use(function(req, res, next) { }) 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 account = web3.eth.accounts.privateKeyToAccount('0x' + privateKey) web3.eth.accounts.wallet.add('0x' + privateKey) 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 gasPrices = { fast: defaultGasPrice } +const ethPriceInDai = toWei('200') app.get('/', function (req, res) { // just for testing purposes @@ -87,5 +88,6 @@ app.listen(8000) if (Number(netId) === 1) { fetchGasPrice({ gasPrices }) + fetchDAIprice({ ethPriceInDai, web3 }) console.log('Gas price oracle started.') } diff --git a/utils.js b/utils.js index ea6e865..b98f643 100644 --- a/utils.js +++ b/utils.js @@ -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 }