-
Notifications
You must be signed in to change notification settings - Fork 67
/
index.js
117 lines (99 loc) · 4.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const path = require('path');
const axios = require('axios');
const Web3Implementation = require('../Web3Implementation');
const constants = require('../../constants');
const coinGeckoLink = 'https://api.coingecko.com/api/v3/simple/price?ids=origintrail&vs_currencies=eth';
const gasStationLink = 'https://ethgasstation.info/json/ethgasAPI.json';
class Ethereum extends Web3Implementation {
/**
* Initializing Ethereum blockchain connector
*/
constructor({ config, emitter, logger }, configuration) {
super({
config,
emitter,
logger,
contractPath: path.join(__dirname, 'abi'),
}, configuration);
this.logger.info(`[${this.getBlockchainId()}] Selected blockchain: Ethereum`);
}
async getRelativeTracPrice() {
const response = await axios.get(coinGeckoLink);
if (response) {
return response.data.origintrail.eth;
}
return undefined;
}
/**
* Returns gas price, throws error if not urgent and gas price higher than maximum allowed price
* @param urgent
* @returns {Promise<*|number>}
*/
async getGasPrice(urgent = false) {
const gasPrice = await this.calculateGasPrice();
if (gasPrice > this.config.max_allowed_gas_price && !urgent) {
throw new Error(`[${this.getBlockchainId()}] Gas price higher than maximum allowed price`);
} else {
return gasPrice;
}
}
async calculateGasPrice() {
if (process.env.NODE_ENV !== 'mainnet') {
this.logger.trace(`[${this.getBlockchainId()}] Using default gas price from `
+ `configuration: ${this.config.gas_price}`);
return this.config.gas_price;
}
const now = new Date().getTime();
if (this.config.gas_price_last_update_timestamp
+ constants.GAS_PRICE_VALIDITY_TIME_IN_MILLS > now) {
this.logger.trace(`[${this.getBlockchainId()}] Using gas price from configuration: `
+ `${this.config.gas_price}`);
return this.config.gas_price;
}
let gasStationGasPrice = await this.getGasStationGasPrice()
.catch((err) => { this.logger.warn(err); }) * constants.AVERAGE_GAS_PRICE_MULTIPLIER;
gasStationGasPrice = Math.round(gasStationGasPrice);
let web3GasPrice = await this.web3.eth.getGasPrice()
.catch((err) => { this.logger.warn(err); }) * constants.AVERAGE_GAS_PRICE_MULTIPLIER;
web3GasPrice = Math.round(web3GasPrice);
if (gasStationGasPrice && web3GasPrice) {
const gasPrice = (
gasStationGasPrice > web3GasPrice ? gasStationGasPrice : web3GasPrice);
this.saveNewGasPriceAndTime(gasPrice);
const service = gasStationGasPrice > web3GasPrice ? 'gas station' : 'web3';
this.logger.trace(`[${this.getBlockchainId()}] Using gas price from ${service}`
+ ` service: ${gasStationGasPrice}`);
return gasPrice;
} else if (gasStationGasPrice) {
this.saveNewGasPriceAndTime(gasStationGasPrice);
this.logger.trace(`[${this.getBlockchainId()}] Using gas price from gas station`
+ ` service: ${gasStationGasPrice}`);
return gasStationGasPrice;
} else if (web3GasPrice) {
this.saveNewGasPriceAndTime(web3GasPrice);
this.logger.trace(`[${this.getBlockchainId()}] Using gas price from web3`
+ ` service: ${web3GasPrice}`);
return web3GasPrice;
}
this.logger.trace(`[${this.getBlockchainId()}] Using gas price from configuration: `
+ `${this.config.gas_price}`);
return this.config.gas_price;
}
async getGasStationGasPrice() {
const response = await axios.get(gasStationLink)
.catch((err) => {
this.logger.warn(err);
return undefined;
});
if (response) {
// todo check if the multiplier has valid value
return response.data.average * 100000000;
}
return undefined;
}
saveNewGasPriceAndTime(gasPrice) {
this.config.gas_price = gasPrice;
this.config.gas_price_last_update_timestamp = new Date().getTime();
}
}
module.exports = Ethereum;