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
|
|
|
|
2017-05-09 12:01:44 +02:00
|
|
|
import { ERRORS, validatePositiveNumber } from '@parity/shared/util/validation';
|
|
|
|
import { DEFAULT_GAS, DEFAULT_GASPRICE, MAX_GAS_ESTIMATION } from '@parity/shared/util/constants';
|
2016-12-09 13:44:35 +01:00
|
|
|
|
2017-02-03 20:01:09 +01:00
|
|
|
const CONDITIONS = {
|
|
|
|
NONE: 'none',
|
|
|
|
BLOCK: 'blockNumber',
|
|
|
|
TIME: 'timestamp'
|
|
|
|
};
|
|
|
|
|
2016-12-09 13:44:35 +01:00
|
|
|
export default class GasPriceEditor {
|
2017-02-03 20:01:09 +01:00
|
|
|
@observable blockNumber = 0;
|
|
|
|
@observable condition = {};
|
|
|
|
@observable conditionBlockError = null;
|
|
|
|
@observable conditionType = CONDITIONS.NONE;
|
2016-12-09 13:44:35 +01:00
|
|
|
@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
|
|
|
|
2017-02-03 20:01:09 +01:00
|
|
|
constructor (api, { gas, gasLimit, gasPrice, condition = null }) {
|
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
|
|
|
|
2017-02-03 20:01:09 +01:00
|
|
|
if (condition) {
|
|
|
|
if (condition.block) {
|
|
|
|
this.condition = { block: condition.block.toFixed(0) };
|
|
|
|
this.conditionType = CONDITIONS.BLOCK;
|
|
|
|
} else if (condition.time) {
|
|
|
|
this.condition = { time: condition.time };
|
|
|
|
this.conditionType = CONDITIONS.TIME;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-31 23:36:24 +02:00
|
|
|
@computed get conditionValue () {
|
|
|
|
switch (this.conditionType) {
|
|
|
|
case CONDITIONS.BLOCK:
|
|
|
|
return { block: new BigNumber(this.condition.block || 0) };
|
|
|
|
|
|
|
|
case CONDITIONS.TIME:
|
|
|
|
return { time: this.condition.time };
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-03 20:01:09 +01:00
|
|
|
@action setConditionType = (conditionType = CONDITIONS.NONE) => {
|
|
|
|
transaction(() => {
|
|
|
|
this.conditionBlockError = null;
|
|
|
|
this.conditionType = conditionType;
|
|
|
|
|
|
|
|
switch (conditionType) {
|
|
|
|
case CONDITIONS.BLOCK:
|
2017-02-16 13:47:51 +01:00
|
|
|
this.setConditionBlockNumber(this.blockNumber || 1);
|
2017-02-03 20:01:09 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CONDITIONS.TIME:
|
2017-02-16 13:47:51 +01:00
|
|
|
this.setConditionDateTime(new Date());
|
2017-02-03 20:01:09 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CONDITIONS.NONE:
|
|
|
|
default:
|
|
|
|
this.condition = {};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@action setConditionBlockNumber = (block) => {
|
|
|
|
transaction(() => {
|
|
|
|
this.conditionBlockError = validatePositiveNumber(block).numberError;
|
|
|
|
this.condition = Object.assign({}, this.condition, { block });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-16 13:47:51 +01:00
|
|
|
@action setConditionDateTime = (_time) => {
|
|
|
|
const time = new Date(_time);
|
|
|
|
|
|
|
|
time.setMilliseconds(0); // ignored by/not passed to Parity
|
|
|
|
time.setSeconds(0); // current time selector doesn't allow seconds
|
|
|
|
|
2017-02-03 20:01:09 +01:00
|
|
|
this.condition = Object.assign({}, this.condition, { time });
|
|
|
|
}
|
|
|
|
|
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(() => ({
|
2017-02-07 19:55:58 +01:00
|
|
|
bucketBounds: [],
|
2017-02-01 16:18:22 +01:00
|
|
|
counts: []
|
|
|
|
})),
|
2017-02-03 20:01:09 +01:00
|
|
|
this._api.eth.gasPrice(),
|
|
|
|
this._api.eth.blockNumber()
|
2016-12-09 13:44:35 +01:00
|
|
|
])
|
2017-02-03 20:01:09 +01:00
|
|
|
.then(([histogram, _price, blockNumber]) => {
|
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;
|
2017-02-03 20:01:09 +01:00
|
|
|
this.blockNumber = blockNumber.toNumber();
|
2016-12-09 13:44:35 +01:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.warn('getDefaults', error);
|
|
|
|
});
|
|
|
|
}
|
2016-12-11 17:43:51 +01:00
|
|
|
|
|
|
|
overrideTransaction = (transaction) => {
|
2017-02-03 20:01:09 +01:00
|
|
|
if (this.errorGas || this.errorPrice || this.conditionBlockError) {
|
2016-12-11 17:43:51 +01:00
|
|
|
return transaction;
|
|
|
|
}
|
|
|
|
|
2017-02-03 20:01:09 +01:00
|
|
|
const override = {
|
|
|
|
condition: this.condition,
|
2016-12-11 17:43:51 +01:00
|
|
|
gas: new BigNumber(this.gas || DEFAULT_GAS),
|
|
|
|
gasPrice: new BigNumber(this.price || DEFAULT_GASPRICE)
|
2017-02-03 20:01:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const result = Object.assign({}, transaction, override);
|
|
|
|
|
|
|
|
switch (this.conditionType) {
|
|
|
|
case CONDITIONS.BLOCK:
|
|
|
|
case CONDITIONS.TIME:
|
2017-03-31 23:36:24 +02:00
|
|
|
result.condition = this.conditionValue;
|
2017-02-03 20:01:09 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CONDITIONS.NONE:
|
|
|
|
default:
|
|
|
|
delete result.condition;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2016-12-11 17:43:51 +01:00
|
|
|
}
|
2016-12-09 13:44:35 +01:00
|
|
|
}
|
2017-02-03 20:01:09 +01:00
|
|
|
|
|
|
|
export {
|
|
|
|
CONDITIONS
|
|
|
|
};
|