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) {
|
2020-05-15 15:30:12 +03:00
|
|
|
console.error('fetchPrices', e.message)
|
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')
|
|
|
|
}
|
2020-05-08 20:29:31 +03:00
|
|
|
|
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
|
|
|
}
|
2020-03-13 20:51:06 +03:00
|
|
|
|
|
|
|
if (json.percentile_97) {
|
|
|
|
this.gasPrices.fast = parseInt(json.percentile_90) + 1 / delimiter
|
|
|
|
}
|
2020-05-08 20:29:31 +03:00
|
|
|
// console.log('gas price fetch', this.gasPrices)
|
2019-11-23 11:18:54 +03:00
|
|
|
} else {
|
|
|
|
throw Error('Fetch gasPrice failed')
|
|
|
|
}
|
|
|
|
setTimeout(() => this.fetchGasPrice({ oracleIndex }), 15000)
|
|
|
|
} catch (e) {
|
2020-05-15 15:30:12 +03:00
|
|
|
console.log('fetchGasPrice', e.message)
|
2019-11-23 11:18:54 +03:00
|
|
|
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
|