2019-12-25 20:09:21 +03:00
|
|
|
const Web3 = require('web3')
|
2020-07-16 16:33:35 +03:00
|
|
|
const { defaultGasPrice, oracleRpcUrl, oracleAddress } = require('../config')
|
2019-12-23 19:38:44 +03:00
|
|
|
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 {
|
2020-08-04 10:39:56 +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)
|
2020-07-16 16:33:35 +03:00
|
|
|
} 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)
|
|
|
|
}
|
|
|
|
}
|
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}`)
|
2020-07-16 16:33:35 +03:00
|
|
|
} catch (e) {
|
2019-12-03 21:03:14 +03:00
|
|
|
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
|