diff options
Diffstat (limited to 'packages/instant/src')
-rw-r--r-- | packages/instant/src/components/zero_ex_instant_provider.tsx | 13 | ||||
-rw-r--r-- | packages/instant/src/constants.ts | 1 | ||||
-rw-r--r-- | packages/instant/src/util/buy_quote_fetcher.ts | 18 | ||||
-rw-r--r-- | packages/instant/src/util/hearbeats.ts | 18 |
4 files changed, 45 insertions, 5 deletions
diff --git a/packages/instant/src/components/zero_ex_instant_provider.tsx b/packages/instant/src/components/zero_ex_instant_provider.tsx index 1805a11af..24ae0c4b6 100644 --- a/packages/instant/src/components/zero_ex_instant_provider.tsx +++ b/packages/instant/src/components/zero_ex_instant_provider.tsx @@ -5,7 +5,7 @@ import * as _ from 'lodash'; import * as React from 'react'; import { Provider as ReduxProvider } from 'react-redux'; -import { ACCOUNT_UPDATE_INTERVAL_TIME_MS } from '../constants'; +import { ACCOUNT_UPDATE_INTERVAL_TIME_MS, BUY_QUOTE_UPDATE_INTERVAL_TIME_MS } from '../constants'; import { SelectedAssetThemeProvider } from '../containers/selected_asset_theme_provider'; import { asyncData } from '../redux/async_data'; import { DEFAULT_STATE, DefaultState, State } from '../redux/reducer'; @@ -15,7 +15,7 @@ import { AffiliateInfo, AssetMetaData, Network, OrderSource } from '../types'; import { assetUtils } from '../util/asset'; import { errorFlasher } from '../util/error_flasher'; import { gasPriceEstimator } from '../util/gas_price_estimator'; -import { generateAccountHeartbeater, Heartbeater } from '../util/hearbeats'; +import { generateAccountHeartbeater, Heartbeater, generateBuyQuoteHeartbeater } from '../util/hearbeats'; import { providerStateFactory } from '../util/provider_state_factory'; fonts.include(); @@ -40,6 +40,8 @@ export interface ZeroExInstantProviderOptionalProps { export class ZeroExInstantProvider extends React.Component<ZeroExInstantProviderProps> { private readonly _store: Store; private _accountUpdateHeartbeat?: Heartbeater; + private _buyQuoteHeartbeat?: Heartbeater; + // TODO(fragosti): Write tests for this beast once we inject a provider. private static _mergeDefaultStateWithProps( props: ZeroExInstantProviderProps, @@ -101,6 +103,10 @@ export class ZeroExInstantProvider extends React.Component<ZeroExInstantProvider this._accountUpdateHeartbeat = generateAccountHeartbeater(this._store); this._accountUpdateHeartbeat.start(ACCOUNT_UPDATE_INTERVAL_TIME_MS); + // TODO assign and stop on unmount + this._buyQuoteHeartbeat = generateBuyQuoteHeartbeater(this._store); + this._buyQuoteHeartbeat.start(BUY_QUOTE_UPDATE_INTERVAL_TIME_MS); + // warm up the gas price estimator cache just in case we can't // grab the gas price estimate when submitting the transaction // tslint:disable-next-line:no-floating-promises @@ -112,6 +118,9 @@ export class ZeroExInstantProvider extends React.Component<ZeroExInstantProvider if (this._accountUpdateHeartbeat) { this._accountUpdateHeartbeat.stop(); } + if (this._buyQuoteHeartbeat) { + this._buyQuoteHeartbeat.stop(); + } } public render(): React.ReactNode { return ( diff --git a/packages/instant/src/constants.ts b/packages/instant/src/constants.ts index d0a86c780..dfbd0ccdf 100644 --- a/packages/instant/src/constants.ts +++ b/packages/instant/src/constants.ts @@ -11,6 +11,7 @@ export const GWEI_IN_WEI = new BigNumber(1000000000); export const ONE_SECOND_MS = 1000; export const ONE_MINUTE_MS = ONE_SECOND_MS * 60; export const ACCOUNT_UPDATE_INTERVAL_TIME_MS = ONE_SECOND_MS * 15; +export const BUY_QUOTE_UPDATE_INTERVAL_TIME_MS = ONE_SECOND_MS * 15; export const DEFAULT_GAS_PRICE = GWEI_IN_WEI.mul(6); export const DEFAULT_ESTIMATED_TRANSACTION_TIME_MS = ONE_MINUTE_MS * 2; export const ETH_GAS_STATION_API_BASE_URL = 'https://ethgasstation.info'; diff --git a/packages/instant/src/util/buy_quote_fetcher.ts b/packages/instant/src/util/buy_quote_fetcher.ts index c3e65cbc3..cc8aac0fa 100644 --- a/packages/instant/src/util/buy_quote_fetcher.ts +++ b/packages/instant/src/util/buy_quote_fetcher.ts @@ -1,5 +1,6 @@ // TODO: rename file and export object import { AssetBuyer, AssetBuyerError, BuyQuote } from '@0x/asset-buyer'; +import { AssetProxyId } from '@0x/types'; import { BigNumber } from '@0x/utils'; import { Web3Wrapper } from '@0x/web3-wrapper'; import * as _ from 'lodash'; @@ -7,6 +8,7 @@ import { Dispatch } from 'redux'; import { oc } from 'ts-optchain'; import { Action, actions } from '../redux/actions'; +import { State } from '../redux/reducer'; import { AffiliateInfo, ERC20Asset } from '../types'; import { assetUtils } from '../util/asset'; @@ -53,3 +55,19 @@ export const updateBuyQuoteOrFlashErrorAsync = async ( // invalidate the last buy quote. dispatch(actions.updateLatestBuyQuote(newBuyQuote)); }; + +export const updateBuyQuoteOrFlashErrorAsyncForState = async (state: State, dispatch: Dispatch<Action>) => { + const { selectedAsset, selectedAssetAmount, affiliateInfo } = state; + const assetBuyer = state.providerState.assetBuyer; + + if (selectedAsset && selectedAssetAmount && selectedAsset.metaData.assetProxyId === AssetProxyId.ERC20) { + // TODO: maybe dont do in the case of an error showing + updateBuyQuoteOrFlashErrorAsync( + assetBuyer, + selectedAsset as ERC20Asset, // TODO: better way to do this? + selectedAssetAmount, + dispatch, + affiliateInfo, + ); + } +}; diff --git a/packages/instant/src/util/hearbeats.ts b/packages/instant/src/util/hearbeats.ts index 443fd13ec..ecd7c5068 100644 --- a/packages/instant/src/util/hearbeats.ts +++ b/packages/instant/src/util/hearbeats.ts @@ -1,9 +1,14 @@ // TODO: rename file import * as _ from 'lodash'; +import { Dispatch } from 'redux'; -import { asyncData } from './../redux/async_data'; -import { Store } from './../redux/store'; +import { Action } from '../redux/actions'; +import { asyncData } from '../redux/async_data'; +import { State } from '../redux/reducer'; +import { Store } from '../redux/store'; + +import { updateBuyQuoteOrFlashErrorAsyncForState } from './buy_quote_fetcher'; type HeartbeatableFunction = () => Promise<void>; export class Heartbeater { @@ -39,7 +44,7 @@ export class Heartbeater { this._pendingRequest = true; try { - this._performingFunctionAsync(); + await this._performingFunctionAsync(); } finally { this._pendingRequest = false; } @@ -51,3 +56,10 @@ export const generateAccountHeartbeater = (store: Store): Heartbeater => { await asyncData.fetchAccountInfoAndDispatchToStore(store, { setLoading: false }); }); }; + +export const generateBuyQuoteHeartbeater = (store: Store): Heartbeater => { + return new Heartbeater(async () => { + await updateBuyQuoteOrFlashErrorAsyncForState(store.getState(), store.dispatch); + return Promise.resolve(); + }); +}; |