2019-11-23 11:18:54 +03:00
|
|
|
const fetch = require('node-fetch')
|
2019-12-25 20:09:21 +03:00
|
|
|
const Web3 = require('web3')
|
2019-12-23 19:38:44 +03:00
|
|
|
const { gasOracleUrls, defaultGasPrice, oracleRpcUrl, oracleAddress } = require('../config')
|
|
|
|
const { getArgsForOracle } = require('./utils')
|
2019-12-12 13:58:58 +03:00
|
|
|
const { redisClient } = require('./redis')
|
2019-12-23 19:38:44 +03:00
|
|
|
const priceOracleABI = require('../abis/PriceOracle.abi.json')
|
2019-11-23 11:18:54 +03:00
|
|
|
|
|
|
|
class Fetcher {
|
|
|
|
constructor(web3) {
|
|
|
|
this.web3 = web3
|
2019-12-23 19:38:44 +03:00
|
|
|
this.oracleWeb3 = new Web3(oracleRpcUrl)
|
|
|
|
this.oracle = new this.oracleWeb3.eth.Contract(priceOracleABI, oracleAddress)
|
2019-11-23 11:18:54 +03:00
|
|
|
this.ethPrices = {
|
2019-12-23 19:38:44 +03:00
|
|
|
dai: '6700000000000000', // 0.0067
|
|
|
|
cdai: '157380000000000',
|
|
|
|
cusdc: '164630000000000',
|
|
|
|
usdc: '7878580000000000',
|
|
|
|
usdt: '7864940000000000'
|
2019-11-23 11:18:54 +03:00
|
|
|
}
|
2019-12-23 19:38:44 +03:00
|
|
|
this.tokenAddresses
|
|
|
|
this.oneUintAmount
|
|
|
|
this.currencyLookup
|
2019-11-23 11:18:54 +03:00
|
|
|
this.gasPrices = {
|
|
|
|
fast: defaultGasPrice
|
|
|
|
}
|
2019-12-23 19:38:44 +03:00
|
|
|
|
2019-12-23 22:40:08 +03:00
|
|
|
const { tokenAddresses, oneUintAmount, currencyLookup } = getArgsForOracle()
|
2019-12-23 19:38:44 +03:00
|
|
|
this.tokenAddresses = tokenAddresses
|
|
|
|
this.oneUintAmount = oneUintAmount
|
|
|
|
this.currencyLookup = currencyLookup
|
2019-11-23 11:18:54 +03:00
|
|
|
}
|
|
|
|
async fetchPrices() {
|
|
|
|
try {
|
2019-12-23 22:40:08 +03:00
|
|
|
let prices = await this.oracle.methods
|
|
|
|
.getPricesInETH(this.tokenAddresses, this.oneUintAmount)
|
|
|
|
.call()
|
2019-12-23 19:38:44 +03:00
|
|
|
this.ethPrices = prices.reduce((acc, price, i) => {
|
|
|
|
acc[this.currencyLookup[this.tokenAddresses[i]]] = price
|
2019-11-23 11:18:54 +03:00
|
|
|
return acc
|
|
|
|
}, {})
|
|
|
|
setTimeout(() => this.fetchPrices(), 1000 * 30)
|
|
|
|
} catch(e) {
|
2019-12-23 19:38:44 +03:00
|
|
|
console.error('fetchPrices', e)
|
2019-11-23 11:18:54 +03:00
|
|
|
setTimeout(() => this.fetchPrices(), 1000 * 30)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async fetchGasPrice({ oracleIndex = 0 } = {}) {
|
|
|
|
oracleIndex = (oracleIndex + 1) % gasOracleUrls.length
|
2019-12-05 15:51:01 +03:00
|
|
|
const url = gasOracleUrls[oracleIndex]
|
|
|
|
const delimiter = url === 'https://ethgasstation.info/json/ethgasAPI.json' ? 10 : 1
|
2019-11-23 11:18:54 +03:00
|
|
|
try {
|
2019-12-05 15:51:01 +03:00
|
|
|
const response = await fetch(url)
|
2019-11-23 11:18:54 +03:00
|
|
|
if (response.status === 200) {
|
|
|
|
const json = await response.json()
|
2019-12-05 15:51:01 +03:00
|
|
|
if (Number(json.fast) === 0) {
|
|
|
|
throw new Error('Fetch gasPrice failed')
|
|
|
|
}
|
2019-11-23 11:18:54 +03:00
|
|
|
|
|
|
|
if (json.slow) {
|
2019-12-05 15:51:01 +03:00
|
|
|
this.gasPrices.low = Number(json.slow) / delimiter
|
2019-11-23 11:18:54 +03:00
|
|
|
}
|
|
|
|
if (json.safeLow) {
|
2019-12-05 15:51:01 +03:00
|
|
|
this.gasPrices.low = Number(json.safeLow) / delimiter
|
2019-11-23 11:18:54 +03:00
|
|
|
}
|
|
|
|
if (json.standard) {
|
2019-12-05 15:51:01 +03:00
|
|
|
this.gasPrices.standard = Number(json.standard) / delimiter
|
2019-11-23 11:18:54 +03:00
|
|
|
}
|
|
|
|
if (json.fast) {
|
2019-12-05 15:51:01 +03:00
|
|
|
this.gasPrices.fast = Number(json.fast) / delimiter
|
2019-11-23 11:18:54 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw Error('Fetch gasPrice failed')
|
|
|
|
}
|
|
|
|
setTimeout(() => this.fetchGasPrice({ oracleIndex }), 15000)
|
|
|
|
} catch (e) {
|
|
|
|
setTimeout(() => this.fetchGasPrice({ oracleIndex }), 15000)
|
|
|
|
}
|
|
|
|
}
|
2019-12-03 16:26:16 +03:00
|
|
|
async fetchNonce() {
|
2019-12-03 21:03:14 +03:00
|
|
|
try {
|
2019-12-12 13:58:58 +03:00
|
|
|
const nonce = await this.web3.eth.getTransactionCount(this.web3.eth.defaultAccount)
|
|
|
|
await redisClient.set('nonce', nonce)
|
|
|
|
console.log(`Current nonce: ${nonce}`)
|
2019-12-03 21:03:14 +03:00
|
|
|
} catch(e) {
|
|
|
|
console.error('fetchNonce failed', e.message)
|
|
|
|
setTimeout(this.fetchNonce, 3000)
|
|
|
|
}
|
2019-12-03 16:26:16 +03:00
|
|
|
}
|
2019-11-23 11:18:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Fetcher
|