2019-12-25 12:09:21 -05:00
|
|
|
const Web3 = require('web3')
|
2020-07-16 09:33:35 -04:00
|
|
|
const { defaultGasPrice, oracleRpcUrl, oracleAddress } = require('../config')
|
2019-12-23 11:38:44 -05:00
|
|
|
const { getArgsForOracle } = require('./utils')
|
2019-12-12 05:58:58 -05:00
|
|
|
const { redisClient } = require('./redis')
|
2019-12-23 11:38:44 -05:00
|
|
|
const priceOracleABI = require('../abis/PriceOracle.abi.json')
|
2019-11-23 03:18:54 -05:00
|
|
|
|
|
|
|
class Fetcher {
|
|
|
|
constructor(web3) {
|
|
|
|
this.web3 = web3
|
2019-12-23 11:38:44 -05:00
|
|
|
this.oracleWeb3 = new Web3(oracleRpcUrl)
|
|
|
|
this.oracle = new this.oracleWeb3.eth.Contract(priceOracleABI, oracleAddress)
|
2019-11-23 03:18:54 -05:00
|
|
|
this.ethPrices = {
|
2019-12-23 11:38:44 -05:00
|
|
|
dai: '6700000000000000', // 0.0067
|
|
|
|
cdai: '157380000000000',
|
|
|
|
cusdc: '164630000000000',
|
|
|
|
usdc: '7878580000000000',
|
|
|
|
usdt: '7864940000000000'
|
2019-11-23 03:18:54 -05:00
|
|
|
}
|
2019-12-23 11:38:44 -05:00
|
|
|
this.tokenAddresses
|
|
|
|
this.oneUintAmount
|
|
|
|
this.currencyLookup
|
2019-11-23 03:18:54 -05:00
|
|
|
this.gasPrices = {
|
|
|
|
fast: defaultGasPrice
|
|
|
|
}
|
2019-12-23 11:38:44 -05:00
|
|
|
|
2019-12-23 14:40:08 -05:00
|
|
|
const { tokenAddresses, oneUintAmount, currencyLookup } = getArgsForOracle()
|
2019-12-23 11:38:44 -05:00
|
|
|
this.tokenAddresses = tokenAddresses
|
|
|
|
this.oneUintAmount = oneUintAmount
|
|
|
|
this.currencyLookup = currencyLookup
|
2019-11-23 03:18:54 -05:00
|
|
|
}
|
|
|
|
async fetchPrices() {
|
|
|
|
try {
|
2020-08-04 03:39:56 -04:00
|
|
|
let prices = await this.oracle.methods.getPricesInETH(this.tokenAddresses, this.oneUintAmount).call()
|
2019-12-23 11:38:44 -05:00
|
|
|
this.ethPrices = prices.reduce((acc, price, i) => {
|
|
|
|
acc[this.currencyLookup[this.tokenAddresses[i]]] = price
|
2019-11-23 03:18:54 -05:00
|
|
|
return acc
|
|
|
|
}, {})
|
|
|
|
setTimeout(() => this.fetchPrices(), 1000 * 30)
|
2020-07-16 09:33:35 -04:00
|
|
|
} catch (e) {
|
2020-05-15 08:30:12 -04:00
|
|
|
console.error('fetchPrices', e.message)
|
2019-11-23 03:18:54 -05:00
|
|
|
setTimeout(() => this.fetchPrices(), 1000 * 30)
|
|
|
|
}
|
|
|
|
}
|
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}`)
|
2020-07-16 09:33:35 -04:00
|
|
|
} catch (e) {
|
2019-12-03 13:03:14 -05:00
|
|
|
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
|