2019-11-23 03:18:54 -05:00
|
|
|
const CoinGecko = require('coingecko-api')
|
|
|
|
const fetch = require('node-fetch')
|
|
|
|
const { toWei } = require('web3-utils')
|
2019-12-02 07:49:24 -05:00
|
|
|
const { gasOracleUrls, defaultGasPrice } = require('../config')
|
|
|
|
const { getMainnetTokens } = require('./utils')
|
2019-12-12 05:58:58 -05:00
|
|
|
const { redisClient } = require('./redis')
|
2019-11-23 03:18:54 -05:00
|
|
|
|
|
|
|
class Fetcher {
|
|
|
|
constructor(web3) {
|
|
|
|
this.web3 = web3
|
|
|
|
this.ethPrices = {
|
|
|
|
dai: '6700000000000000' // 0.0067
|
|
|
|
}
|
|
|
|
this.gasPrices = {
|
|
|
|
fast: defaultGasPrice
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async fetchPrices() {
|
2019-12-02 07:49:24 -05:00
|
|
|
const { tokenAddresses, currencyLookup } = getMainnetTokens()
|
2019-11-23 03:18:54 -05:00
|
|
|
try {
|
|
|
|
const CoinGeckoClient = new CoinGecko()
|
|
|
|
const price = await CoinGeckoClient.simple.fetchTokenPrice({
|
2019-12-02 07:49:24 -05:00
|
|
|
contract_addresses: tokenAddresses,
|
2019-11-23 03:18:54 -05:00
|
|
|
vs_currencies: 'eth',
|
|
|
|
assetPlatform: 'ethereum'
|
|
|
|
})
|
|
|
|
this.ethPrices = Object.entries(price.data).reduce((acc, token) => {
|
|
|
|
if (token[1].eth) {
|
2019-12-02 07:49:24 -05:00
|
|
|
acc[currencyLookup[token[0]]] = toWei(token[1].eth.toString())
|
2019-11-23 03:18:54 -05:00
|
|
|
}
|
|
|
|
return acc
|
|
|
|
}, {})
|
|
|
|
setTimeout(() => this.fetchPrices(), 1000 * 30)
|
|
|
|
} catch(e) {
|
|
|
|
setTimeout(() => this.fetchPrices(), 1000 * 30)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async fetchGasPrice({ oracleIndex = 0 } = {}) {
|
|
|
|
oracleIndex = (oracleIndex + 1) % gasOracleUrls.length
|
2019-12-05 07:51:01 -05:00
|
|
|
const url = gasOracleUrls[oracleIndex]
|
|
|
|
const delimiter = url === 'https://ethgasstation.info/json/ethgasAPI.json' ? 10 : 1
|
2019-11-23 03:18:54 -05:00
|
|
|
try {
|
2019-12-05 07:51:01 -05:00
|
|
|
const response = await fetch(url)
|
2019-11-23 03:18:54 -05:00
|
|
|
if (response.status === 200) {
|
|
|
|
const json = await response.json()
|
2019-12-05 07:51:01 -05:00
|
|
|
if (Number(json.fast) === 0) {
|
|
|
|
throw new Error('Fetch gasPrice failed')
|
|
|
|
}
|
2019-11-23 03:18:54 -05:00
|
|
|
|
|
|
|
if (json.slow) {
|
2019-12-05 07:51:01 -05:00
|
|
|
this.gasPrices.low = Number(json.slow) / delimiter
|
2019-11-23 03:18:54 -05:00
|
|
|
}
|
|
|
|
if (json.safeLow) {
|
2019-12-05 07:51:01 -05:00
|
|
|
this.gasPrices.low = Number(json.safeLow) / delimiter
|
2019-11-23 03:18:54 -05:00
|
|
|
}
|
|
|
|
if (json.standard) {
|
2019-12-05 07:51:01 -05:00
|
|
|
this.gasPrices.standard = Number(json.standard) / delimiter
|
2019-11-23 03:18:54 -05:00
|
|
|
}
|
|
|
|
if (json.fast) {
|
2019-12-05 07:51:01 -05:00
|
|
|
this.gasPrices.fast = Number(json.fast) / delimiter
|
2019-11-23 03:18:54 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw Error('Fetch gasPrice failed')
|
|
|
|
}
|
|
|
|
setTimeout(() => this.fetchGasPrice({ oracleIndex }), 15000)
|
|
|
|
} catch (e) {
|
|
|
|
setTimeout(() => this.fetchGasPrice({ oracleIndex }), 15000)
|
|
|
|
}
|
|
|
|
}
|
2019-12-03 08:26:16 -05:00
|
|
|
async fetchNonce() {
|
2019-12-03 13:03:14 -05:00
|
|
|
try {
|
2019-12-12 05:58:58 -05: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 13:03:14 -05:00
|
|
|
} catch(e) {
|
|
|
|
console.error('fetchNonce failed', e.message)
|
|
|
|
setTimeout(this.fetchNonce, 3000)
|
|
|
|
}
|
2019-12-03 08:26:16 -05:00
|
|
|
}
|
2019-11-23 03:18:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Fetcher
|