dai price
This commit is contained in:
parent
53828dca12
commit
2d2115ab43
17
abis/ETHDAIOracle.json
Normal file
17
abis/ETHDAIOracle.json
Normal file
@ -0,0 +1,17 @@
|
||||
[
|
||||
{
|
||||
"constant": true,
|
||||
"inputs": [],
|
||||
"name": "getMedianPrice",
|
||||
"outputs": [
|
||||
{
|
||||
"internalType": "uint256",
|
||||
"name": "",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"payable": false,
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
}
|
||||
]
|
@ -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'
|
||||
}
|
6
index.js
6
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.')
|
||||
}
|
||||
|
18
utils.js
18
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 }
|
||||
|
Loading…
Reference in New Issue
Block a user