2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-12-09 13:44:35 +01:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import BigNumber from 'bignumber.js';
|
2016-12-09 15:43:24 +01:00
|
|
|
import { action, computed, observable, transaction } from 'mobx';
|
2016-12-09 13:44:35 +01:00
|
|
|
|
|
|
|
import { ERRORS, validatePositiveNumber } from '~/util/validation';
|
|
|
|
import { DEFAULT_GAS, DEFAULT_GASPRICE, MAX_GAS_ESTIMATION } from '~/util/constants';
|
|
|
|
|
|
|
|
export default class GasPriceEditor {
|
|
|
|
@observable errorEstimated = null;
|
|
|
|
@observable errorGas = null;
|
|
|
|
@observable errorPrice = null;
|
2016-12-09 15:43:24 +01:00
|
|
|
@observable errorTotal = null;
|
2016-12-09 13:44:35 +01:00
|
|
|
@observable estimated = DEFAULT_GAS;
|
2016-12-11 17:43:51 +01:00
|
|
|
@observable gas;
|
|
|
|
@observable gasLimit;
|
2016-12-09 13:44:35 +01:00
|
|
|
@observable histogram = null;
|
2016-12-11 17:43:51 +01:00
|
|
|
@observable isEditing = false;
|
|
|
|
@observable price;
|
|
|
|
@observable priceDefault;
|
2016-12-09 15:43:24 +01:00
|
|
|
@observable weiValue = '0';
|
2016-12-09 13:44:35 +01:00
|
|
|
|
2016-12-11 17:43:51 +01:00
|
|
|
constructor (api, { gas, gasLimit, gasPrice }) {
|
2016-12-09 13:44:35 +01:00
|
|
|
this._api = api;
|
2016-12-11 17:43:51 +01:00
|
|
|
|
|
|
|
this.gas = gas;
|
2016-12-09 13:44:35 +01:00
|
|
|
this.gasLimit = gasLimit;
|
2016-12-11 17:43:51 +01:00
|
|
|
this.price = gasPrice;
|
2016-12-09 13:44:35 +01:00
|
|
|
|
2016-12-11 17:43:51 +01:00
|
|
|
if (api) {
|
2016-12-09 13:44:35 +01:00
|
|
|
this.loadDefaults();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 15:43:24 +01:00
|
|
|
@computed get totalValue () {
|
|
|
|
try {
|
|
|
|
return new BigNumber(this.gas).mul(this.price).add(this.weiValue);
|
|
|
|
} catch (error) {
|
|
|
|
return new BigNumber(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 17:43:51 +01:00
|
|
|
@action setEditing = (isEditing) => {
|
|
|
|
this.isEditing = isEditing;
|
|
|
|
}
|
|
|
|
|
2016-12-09 15:43:24 +01:00
|
|
|
@action setErrorTotal = (errorTotal) => {
|
|
|
|
this.errorTotal = errorTotal;
|
|
|
|
}
|
|
|
|
|
2017-01-12 13:56:37 +01:00
|
|
|
@action setEstimatedError = (errorEstimated = ERRORS.gasException) => {
|
|
|
|
this.errorEstimated = errorEstimated;
|
|
|
|
}
|
|
|
|
|
2016-12-09 13:44:35 +01:00
|
|
|
@action setEstimated = (estimated) => {
|
|
|
|
transaction(() => {
|
|
|
|
const bn = new BigNumber(estimated);
|
|
|
|
|
|
|
|
this.estimated = estimated;
|
|
|
|
|
|
|
|
if (bn.gte(MAX_GAS_ESTIMATION)) {
|
2017-01-12 13:56:37 +01:00
|
|
|
this.setEstimatedError(ERRORS.gasException);
|
2016-12-09 13:44:35 +01:00
|
|
|
} else if (bn.gte(this.gasLimit)) {
|
2017-01-12 13:56:37 +01:00
|
|
|
this.setEstimatedError(ERRORS.gasBlockLimit);
|
2016-12-09 13:44:35 +01:00
|
|
|
} else {
|
2017-01-12 13:56:37 +01:00
|
|
|
this.setEstimatedError(null);
|
2016-12-09 13:44:35 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-09 15:43:24 +01:00
|
|
|
@action setEthValue = (weiValue) => {
|
|
|
|
this.weiValue = weiValue;
|
|
|
|
}
|
|
|
|
|
2016-12-09 13:44:35 +01:00
|
|
|
@action setGas = (gas) => {
|
|
|
|
transaction(() => {
|
|
|
|
const { numberError } = validatePositiveNumber(gas);
|
|
|
|
|
|
|
|
this.gas = gas;
|
|
|
|
|
|
|
|
if (numberError) {
|
|
|
|
this.errorGas = numberError;
|
|
|
|
} else {
|
2016-12-11 17:43:51 +01:00
|
|
|
const bn = new BigNumber(gas);
|
|
|
|
|
|
|
|
if (bn.gte(this.gasLimit)) {
|
|
|
|
this.errorGas = ERRORS.gasBlockLimit;
|
|
|
|
} else {
|
|
|
|
this.errorGas = null;
|
|
|
|
}
|
2016-12-09 13:44:35 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-11 17:43:51 +01:00
|
|
|
@action setGasLimit = (gasLimit) => {
|
|
|
|
this.gasLimit = gasLimit;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action setHistogram = (gasHistogram) => {
|
|
|
|
this.histogram = gasHistogram;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action setPrice = (price) => {
|
|
|
|
transaction(() => {
|
|
|
|
this.errorPrice = validatePositiveNumber(price).numberError;
|
|
|
|
this.price = price;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-09 13:44:35 +01:00
|
|
|
@action loadDefaults () {
|
|
|
|
Promise
|
|
|
|
.all([
|
2017-02-01 16:18:22 +01:00
|
|
|
// NOTE fetching histogram may fail if there is not enough data.
|
|
|
|
// We fallback to empty histogram.
|
|
|
|
this._api.parity.gasPriceHistogram().catch(() => ({
|
|
|
|
bucket_bounds: [],
|
|
|
|
counts: []
|
|
|
|
})),
|
2016-12-09 13:44:35 +01:00
|
|
|
this._api.eth.gasPrice()
|
|
|
|
])
|
2016-12-11 17:43:51 +01:00
|
|
|
.then(([histogram, _price]) => {
|
2016-12-09 13:44:35 +01:00
|
|
|
transaction(() => {
|
2016-12-11 17:43:51 +01:00
|
|
|
const price = _price.toFixed(0);
|
2016-12-09 13:44:35 +01:00
|
|
|
|
2016-12-11 17:43:51 +01:00
|
|
|
if (!this.price) {
|
|
|
|
this.setPrice(price);
|
|
|
|
}
|
|
|
|
this.setHistogram(histogram);
|
|
|
|
|
|
|
|
this.priceDefault = price;
|
2016-12-09 13:44:35 +01:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.warn('getDefaults', error);
|
|
|
|
});
|
|
|
|
}
|
2016-12-11 17:43:51 +01:00
|
|
|
|
|
|
|
overrideTransaction = (transaction) => {
|
|
|
|
if (this.errorGas || this.errorPrice) {
|
|
|
|
return transaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.assign({}, transaction, {
|
|
|
|
gas: new BigNumber(this.gas || DEFAULT_GAS),
|
|
|
|
gasPrice: new BigNumber(this.price || DEFAULT_GASPRICE)
|
|
|
|
});
|
|
|
|
}
|
2016-12-09 13:44:35 +01:00
|
|
|
}
|