From 8ab8c279989114662d6dce4b5b998ad8fd88fc1a Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Mon, 29 Oct 2018 15:19:48 -0700 Subject: gas price estimator --- packages/instant/src/util/gas_price_estimator.ts | 45 ++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 packages/instant/src/util/gas_price_estimator.ts (limited to 'packages/instant/src/util/gas_price_estimator.ts') diff --git a/packages/instant/src/util/gas_price_estimator.ts b/packages/instant/src/util/gas_price_estimator.ts new file mode 100644 index 000000000..bd81ea05f --- /dev/null +++ b/packages/instant/src/util/gas_price_estimator.ts @@ -0,0 +1,45 @@ +import { BigNumber } from '@0x/utils'; + +// TODO: merge development and move to constants +const ENDPOINT_URL = 'https://ethgasstation.info/json/ethgasAPI.json'; +const DEFAULT_GAS_PRICE_WEI = new BigNumber(20000000000); + +interface GasStationResult { + average: number; + fastestWait: number; + fastWait: number; + fast: number; + safeLowWait: number; + blockNum: number; + avgWait: number; + block_time: number; + speed: number; + fastest: number; + safeLow: number; +} + +const fetchFastAmountInWei = async () => { + const res = await fetch(ENDPOINT_URL); + const gasInfo = (await res.json()) as GasStationResult; + const gasPriceInGwei = new BigNumber(gasInfo.fast / 10); + return gasPriceInGwei.mul(1000000000); +}; + +export class GasPriceEstimator { + private _lastFetched?: BigNumber; + public async getFastAmountInWeiAsync(): Promise { + let fetchedAmount: BigNumber | undefined; + try { + fetchedAmount = await fetchFastAmountInWei(); + } catch { + fetchedAmount = undefined; + } + + if (fetchedAmount) { + this._lastFetched = fetchedAmount; + } + + return fetchedAmount || this._lastFetched || DEFAULT_GAS_PRICE_WEI; + } +} +export const gasPriceEstimator = new GasPriceEstimator(); -- cgit From 9610ada4467472318db0de17f794737ada42836b Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Mon, 29 Oct 2018 15:23:16 -0700 Subject: Use constant that exists now --- packages/instant/src/util/gas_price_estimator.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'packages/instant/src/util/gas_price_estimator.ts') diff --git a/packages/instant/src/util/gas_price_estimator.ts b/packages/instant/src/util/gas_price_estimator.ts index bd81ea05f..9eccfe510 100644 --- a/packages/instant/src/util/gas_price_estimator.ts +++ b/packages/instant/src/util/gas_price_estimator.ts @@ -1,8 +1,6 @@ import { BigNumber } from '@0x/utils'; -// TODO: merge development and move to constants -const ENDPOINT_URL = 'https://ethgasstation.info/json/ethgasAPI.json'; -const DEFAULT_GAS_PRICE_WEI = new BigNumber(20000000000); +import { DEFAULT_GAS_PRICE } from '../constants'; interface GasStationResult { average: number; @@ -18,8 +16,9 @@ interface GasStationResult { safeLow: number; } +const endpointUrl = 'https://ethgasstation.info/json/ethgasAPI.json'; const fetchFastAmountInWei = async () => { - const res = await fetch(ENDPOINT_URL); + const res = await fetch(endpointUrl); const gasInfo = (await res.json()) as GasStationResult; const gasPriceInGwei = new BigNumber(gasInfo.fast / 10); return gasPriceInGwei.mul(1000000000); @@ -39,7 +38,7 @@ export class GasPriceEstimator { this._lastFetched = fetchedAmount; } - return fetchedAmount || this._lastFetched || DEFAULT_GAS_PRICE_WEI; + return fetchedAmount || this._lastFetched || DEFAULT_GAS_PRICE; } } export const gasPriceEstimator = new GasPriceEstimator(); -- cgit From 274e4b3bcd1633e8fe78a33528d63b7567355f9e Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Mon, 29 Oct 2018 16:53:18 -0700 Subject: Introduce constants for eth gas station and coinbase --- packages/instant/src/util/gas_price_estimator.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'packages/instant/src/util/gas_price_estimator.ts') diff --git a/packages/instant/src/util/gas_price_estimator.ts b/packages/instant/src/util/gas_price_estimator.ts index 9eccfe510..818bf6f92 100644 --- a/packages/instant/src/util/gas_price_estimator.ts +++ b/packages/instant/src/util/gas_price_estimator.ts @@ -1,8 +1,8 @@ import { BigNumber } from '@0x/utils'; -import { DEFAULT_GAS_PRICE } from '../constants'; +import { DEFAULT_GAS_PRICE, ETH_GAS_STATION_API_BASE_URL } from '../constants'; -interface GasStationResult { +interface EthGasStationResult { average: number; fastestWait: number; fastWait: number; @@ -16,10 +16,9 @@ interface GasStationResult { safeLow: number; } -const endpointUrl = 'https://ethgasstation.info/json/ethgasAPI.json'; const fetchFastAmountInWei = async () => { - const res = await fetch(endpointUrl); - const gasInfo = (await res.json()) as GasStationResult; + const res = await fetch(`${ETH_GAS_STATION_API_BASE_URL}/json/ethgasAPI.json`); + const gasInfo = (await res.json()) as EthGasStationResult; const gasPriceInGwei = new BigNumber(gasInfo.fast / 10); return gasPriceInGwei.mul(1000000000); }; -- cgit From 30454fe46746b8c1caf47cb35f39b46d0749a224 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Mon, 29 Oct 2018 17:11:02 -0700 Subject: async suffix and use polyfill fetch util --- packages/instant/src/util/gas_price_estimator.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'packages/instant/src/util/gas_price_estimator.ts') diff --git a/packages/instant/src/util/gas_price_estimator.ts b/packages/instant/src/util/gas_price_estimator.ts index 818bf6f92..33eeb932d 100644 --- a/packages/instant/src/util/gas_price_estimator.ts +++ b/packages/instant/src/util/gas_price_estimator.ts @@ -1,4 +1,4 @@ -import { BigNumber } from '@0x/utils'; +import { BigNumber, fetchAsync } from '@0x/utils'; import { DEFAULT_GAS_PRICE, ETH_GAS_STATION_API_BASE_URL } from '../constants'; @@ -16,8 +16,8 @@ interface EthGasStationResult { safeLow: number; } -const fetchFastAmountInWei = async () => { - const res = await fetch(`${ETH_GAS_STATION_API_BASE_URL}/json/ethgasAPI.json`); +const fetchFastAmountInWeiAsync = async () => { + const res = await fetchAsync(`${ETH_GAS_STATION_API_BASE_URL}/json/ethgasAPI.json`); const gasInfo = (await res.json()) as EthGasStationResult; const gasPriceInGwei = new BigNumber(gasInfo.fast / 10); return gasPriceInGwei.mul(1000000000); @@ -28,7 +28,7 @@ export class GasPriceEstimator { public async getFastAmountInWeiAsync(): Promise { let fetchedAmount: BigNumber | undefined; try { - fetchedAmount = await fetchFastAmountInWei(); + fetchedAmount = await fetchFastAmountInWeiAsync(); } catch { fetchedAmount = undefined; } -- cgit From 09ee7d84f70418e128a445bf3a11b813ac1c7d2e Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Mon, 29 Oct 2018 17:17:04 -0700 Subject: Add comment and use constant --- packages/instant/src/util/gas_price_estimator.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'packages/instant/src/util/gas_price_estimator.ts') diff --git a/packages/instant/src/util/gas_price_estimator.ts b/packages/instant/src/util/gas_price_estimator.ts index 33eeb932d..336c4a3fa 100644 --- a/packages/instant/src/util/gas_price_estimator.ts +++ b/packages/instant/src/util/gas_price_estimator.ts @@ -1,6 +1,6 @@ import { BigNumber, fetchAsync } from '@0x/utils'; -import { DEFAULT_GAS_PRICE, ETH_GAS_STATION_API_BASE_URL } from '../constants'; +import { DEFAULT_GAS_PRICE, ETH_GAS_STATION_API_BASE_URL, GWEI_IN_WEI } from '../constants'; interface EthGasStationResult { average: number; @@ -19,8 +19,9 @@ interface EthGasStationResult { const fetchFastAmountInWeiAsync = async () => { 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); - return gasPriceInGwei.mul(1000000000); + return gasPriceInGwei.mul(GWEI_IN_WEI); }; export class GasPriceEstimator { -- cgit