diff options
author | fragosti <francesco.agosti93@gmail.com> | 2018-11-01 03:18:03 +0800 |
---|---|---|
committer | fragosti <francesco.agosti93@gmail.com> | 2018-11-01 03:18:03 +0800 |
commit | 28ded5d02dc97072ac9713568bd599daf2670fbc (patch) | |
tree | 46ca071cb155fc4e7e3c6cb5d9b17bd06ce4c743 /packages/instant/src/util | |
parent | 17f024056a56ae1b67a3a674f24460172ae84d27 (diff) | |
parent | 5d6fde356a19658ae91114df68fa507d93ff8049 (diff) | |
download | dexon-sol-tools-28ded5d02dc97072ac9713568bd599daf2670fbc.tar dexon-sol-tools-28ded5d02dc97072ac9713568bd599daf2670fbc.tar.gz dexon-sol-tools-28ded5d02dc97072ac9713568bd599daf2670fbc.tar.bz2 dexon-sol-tools-28ded5d02dc97072ac9713568bd599daf2670fbc.tar.lz dexon-sol-tools-28ded5d02dc97072ac9713568bd599daf2670fbc.tar.xz dexon-sol-tools-28ded5d02dc97072ac9713568bd599daf2670fbc.tar.zst dexon-sol-tools-28ded5d02dc97072ac9713568bd599daf2670fbc.zip |
fix: address PR comments
Diffstat (limited to 'packages/instant/src/util')
-rw-r--r-- | packages/instant/src/util/coinbase_api.ts | 7 | ||||
-rw-r--r-- | packages/instant/src/util/gas_price_estimator.ts | 44 |
2 files changed, 48 insertions, 3 deletions
diff --git a/packages/instant/src/util/coinbase_api.ts b/packages/instant/src/util/coinbase_api.ts index 080421f98..faac8d82d 100644 --- a/packages/instant/src/util/coinbase_api.ts +++ b/packages/instant/src/util/coinbase_api.ts @@ -1,9 +1,10 @@ -import { BigNumber } from '@0x/utils'; +import { BigNumber, fetchAsync } from '@0x/utils'; + +import { COINBASE_API_BASE_URL } from '../constants'; -const baseEndpoint = 'https://api.coinbase.com/v2'; export const coinbaseApi = { getEthUsdPrice: async (): Promise<BigNumber> => { - const res = await fetch(`${baseEndpoint}/prices/ETH-USD/buy`); + const res = await fetchAsync(`${COINBASE_API_BASE_URL}/prices/ETH-USD/buy`); const resJson = await res.json(); return new BigNumber(resJson.data.amount); }, 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..336c4a3fa --- /dev/null +++ b/packages/instant/src/util/gas_price_estimator.ts @@ -0,0 +1,44 @@ +import { BigNumber, fetchAsync } from '@0x/utils'; + +import { DEFAULT_GAS_PRICE, ETH_GAS_STATION_API_BASE_URL, GWEI_IN_WEI } from '../constants'; + +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; +} + +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(GWEI_IN_WEI); +}; + +export class GasPriceEstimator { + private _lastFetched?: BigNumber; + public async getFastAmountInWeiAsync(): Promise<BigNumber> { + let fetchedAmount: BigNumber | undefined; + try { + fetchedAmount = await fetchFastAmountInWeiAsync(); + } catch { + fetchedAmount = undefined; + } + + if (fetchedAmount) { + this._lastFetched = fetchedAmount; + } + + return fetchedAmount || this._lastFetched || DEFAULT_GAS_PRICE; + } +} +export const gasPriceEstimator = new GasPriceEstimator(); |