aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/src/util/gas_price_estimator.ts
diff options
context:
space:
mode:
authorHsuan Lee <hsuan@cobinhood.com>2019-01-19 18:42:04 +0800
committerHsuan Lee <hsuan@cobinhood.com>2019-01-19 18:42:04 +0800
commit7ae38906926dc09bc10670c361af0d2bf0050426 (patch)
tree5fb10ae366b987db09e4ddb4bc3ba0f75404ad08 /packages/instant/src/util/gas_price_estimator.ts
parentb5fd3c72a08aaa6957917d74c333387a16edf66b (diff)
downloaddexon-sol-tools-7ae38906926dc09bc10670c361af0d2bf0050426.tar
dexon-sol-tools-7ae38906926dc09bc10670c361af0d2bf0050426.tar.gz
dexon-sol-tools-7ae38906926dc09bc10670c361af0d2bf0050426.tar.bz2
dexon-sol-tools-7ae38906926dc09bc10670c361af0d2bf0050426.tar.lz
dexon-sol-tools-7ae38906926dc09bc10670c361af0d2bf0050426.tar.xz
dexon-sol-tools-7ae38906926dc09bc10670c361af0d2bf0050426.tar.zst
dexon-sol-tools-7ae38906926dc09bc10670c361af0d2bf0050426.zip
Update dependency packages
Diffstat (limited to 'packages/instant/src/util/gas_price_estimator.ts')
-rw-r--r--packages/instant/src/util/gas_price_estimator.ts65
1 files changed, 0 insertions, 65 deletions
diff --git a/packages/instant/src/util/gas_price_estimator.ts b/packages/instant/src/util/gas_price_estimator.ts
deleted file mode 100644
index 9792b60ba..000000000
--- a/packages/instant/src/util/gas_price_estimator.ts
+++ /dev/null
@@ -1,65 +0,0 @@
-import { BigNumber, fetchAsync } from '@0x/utils';
-
-import {
- DEFAULT_ESTIMATED_TRANSACTION_TIME_MS,
- DEFAULT_GAS_PRICE,
- ETH_GAS_STATION_API_BASE_URL,
- GWEI_IN_WEI,
-} from '../constants';
-
-import { errorReporter } from './error_reporter';
-
-interface EthGasStationResult {
- average: number;
- fastestWait: number;
- fastWait: number;
- fast: number;
- safeLowWait: number;
- blockNum: number;
- avgWait: number;
- block_time: number;
- speed: number;
- fastest: number;
- safeLow: number;
-}
-
-interface GasInfo {
- gasPriceInWei: BigNumber;
- estimatedTimeMs: number;
-}
-
-const fetchFastAmountInWeiAsync = async (): Promise<GasInfo> => {
- const res = await fetchAsync(`${ETH_GAS_STATION_API_BASE_URL}/json/ethgasAPI.json`);
- const gasInfo = (await res.json()) as EthGasStationResult;
- // Eth Gas Station result is gwei * 10
- const gasPriceInGwei = new BigNumber(gasInfo.fast / 10);
- // Time is in minutes
- const estimatedTimeMs = gasInfo.fastWait * 60 * 1000; // Minutes to MS
- return { gasPriceInWei: gasPriceInGwei.multipliedBy(GWEI_IN_WEI), estimatedTimeMs };
-};
-
-export class GasPriceEstimator {
- private _lastFetched?: GasInfo;
- public async getGasInfoAsync(): Promise<GasInfo> {
- let fetchedAmount: GasInfo | undefined;
- try {
- fetchedAmount = await fetchFastAmountInWeiAsync();
- } catch (e) {
- fetchedAmount = undefined;
- errorReporter.report(e);
- }
-
- if (fetchedAmount) {
- this._lastFetched = fetchedAmount;
- }
-
- return (
- fetchedAmount ||
- this._lastFetched || {
- gasPriceInWei: DEFAULT_GAS_PRICE,
- estimatedTimeMs: DEFAULT_ESTIMATED_TRANSACTION_TIME_MS,
- }
- );
- }
-}
-export const gasPriceEstimator = new GasPriceEstimator();