aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant
diff options
context:
space:
mode:
authorSteve Klebanoff <steve.klebanoff@gmail.com>2018-10-30 06:19:48 +0800
committerSteve Klebanoff <steve.klebanoff@gmail.com>2018-10-30 06:21:30 +0800
commit8ab8c279989114662d6dce4b5b998ad8fd88fc1a (patch)
tree1197a450703b938abf68f74c1b679b3c61ccf86e /packages/instant
parent48ff13e3e22bf9f71bc1a367f86aaa0ae89989ae (diff)
downloaddexon-sol-tools-8ab8c279989114662d6dce4b5b998ad8fd88fc1a.tar.gz
dexon-sol-tools-8ab8c279989114662d6dce4b5b998ad8fd88fc1a.tar.zst
dexon-sol-tools-8ab8c279989114662d6dce4b5b998ad8fd88fc1a.zip
gas price estimator
Diffstat (limited to 'packages/instant')
-rw-r--r--packages/instant/src/components/buy_button.tsx5
-rw-r--r--packages/instant/src/components/zero_ex_instant.tsx2
-rw-r--r--packages/instant/src/util/gas_price_estimator.ts45
3 files changed, 50 insertions, 2 deletions
diff --git a/packages/instant/src/components/buy_button.tsx b/packages/instant/src/components/buy_button.tsx
index e65e62e47..58575b1b3 100644
--- a/packages/instant/src/components/buy_button.tsx
+++ b/packages/instant/src/components/buy_button.tsx
@@ -7,6 +7,7 @@ import { ColorOption } from '../style/theme';
import { ZeroExInstantError } from '../types';
import { getBestAddress } from '../util/address';
import { balanceUtil } from '../util/balance';
+import { gasPriceEstimator } from '../util/gas_price_estimator';
import { util } from '../util/util';
import { web3Wrapper } from '../util/web3_wrapper';
@@ -56,9 +57,9 @@ export class BuyButton extends React.Component<BuyButtonProps> {
}
let txHash: string | undefined;
+ const gasPrice = await gasPriceEstimator.getFastAmountInWeiAsync();
try {
- const gasPrice = DEFAULT_GAS_PRICE;
- txHash = await assetBuyer.executeBuyQuoteAsync(buyQuote, { gasPrice, takerAddress });
+ txHash = await assetBuyer.executeBuyQuoteAsync(buyQuote, { takerAddress, gasPrice });
} catch (e) {
if (e instanceof Error) {
if (e.message === AssetBuyerError.SignatureRequestDenied) {
diff --git a/packages/instant/src/components/zero_ex_instant.tsx b/packages/instant/src/components/zero_ex_instant.tsx
index 19a2d6b9b..6a5846963 100644
--- a/packages/instant/src/components/zero_ex_instant.tsx
+++ b/packages/instant/src/components/zero_ex_instant.tsx
@@ -13,6 +13,7 @@ import { AssetMetaData, Network } from '../types';
import { assetUtils } from '../util/asset';
import { BigNumberInput } from '../util/big_number_input';
import { errorFlasher } from '../util/error_flasher';
+import { gasPriceEstimator } from '../util/gas_price_estimator';
import { getProvider } from '../util/provider';
import { web3Wrapper } from '../util/web3_wrapper';
@@ -78,6 +79,7 @@ export class ZeroExInstant extends React.Component<ZeroExInstantProps> {
public componentDidMount(): void {
// tslint:disable-next-line:no-floating-promises
asyncData.fetchAndDispatchToStore(this._store);
+ gasPriceEstimator.getFastAmountInWeiAsync();
// tslint:disable-next-line:no-floating-promises
this._flashErrorIfWrongNetwork();
}
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<BigNumber> {
+ 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();