Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1237 from 0xProject/feature/instant/buy-quote-hea…
Browse files Browse the repository at this point in the history
…rtbeat

[instant] Heartbeats for account info and buy quotes
  • Loading branch information
Steve Klebanoff committed Nov 10, 2018
2 parents 2c585bf + fd83ca2 commit 26cbe7a
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 13 deletions.
35 changes: 31 additions & 4 deletions packages/instant/src/components/zero_ex_instant_provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import * as _ from 'lodash';
import * as React from 'react';
import { Provider as ReduxProvider } from 'react-redux';

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';
import { store, Store } from '../redux/store';
import { fonts } from '../style/fonts';
import { AffiliateInfo, AssetMetaData, Network, OrderSource } from '../types';
import { AccountState, 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 { Heartbeater } from '../util/heartbeater';
import { generateAccountHeartbeater, generateBuyQuoteHeartbeater } from '../util/heartbeater_factory';
import { providerStateFactory } from '../util/provider_state_factory';

fonts.include();
Expand All @@ -37,6 +40,9 @@ 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,
Expand Down Expand Up @@ -92,17 +98,38 @@ export class ZeroExInstantProvider extends React.Component<ZeroExInstantProvider
// tslint:disable-next-line:no-floating-promises
asyncData.fetchAvailableAssetDatasAndDispatchToStore(this._store);
}

if (state.providerState.account.state !== AccountState.None) {
this._accountUpdateHeartbeat = generateAccountHeartbeater({
store: this._store,
shouldPerformImmediatelyOnStart: true,
});
this._accountUpdateHeartbeat.start(ACCOUNT_UPDATE_INTERVAL_TIME_MS);
}

this._buyQuoteHeartbeat = generateBuyQuoteHeartbeater({
store: this._store,
shouldPerformImmediatelyOnStart: false,
});
this._buyQuoteHeartbeat.start(BUY_QUOTE_UPDATE_INTERVAL_TIME_MS);
// tslint:disable-next-line:no-floating-promises
asyncData.fetchAccountInfoAndDispatchToStore(this._store);
// tslint:disable-next-line:no-floating-promises
asyncData.fetchCurrentBuyQuoteAndDispatchToStore(this._store);
asyncData.fetchCurrentBuyQuoteAndDispatchToStore({ store: this._store, shouldSetPending: true });

// 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
gasPriceEstimator.getGasInfoAsync();
// tslint:disable-next-line:no-floating-promises
this._flashErrorIfWrongNetwork();
}
public componentWillUnmount(): void {
if (this._accountUpdateHeartbeat) {
this._accountUpdateHeartbeat.stop();
}
if (this._buyQuoteHeartbeat) {
this._buyQuoteHeartbeat.stop();
}
}
public render(): React.ReactNode {
return (
<ReduxProvider store={this._store}>
Expand Down
2 changes: 2 additions & 0 deletions packages/instant/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export const WEB_3_WRAPPER_TRANSACTION_FAILED_ERROR_MSG_PREFIX = 'Transaction fa
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 * 5;
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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const mapStateToProps = (state: State, _ownProps: SelectedERC20AssetAmountInputP

const debouncedUpdateBuyQuoteAsync = _.debounce(buyQuoteUpdater.updateBuyQuoteAsync.bind(buyQuoteUpdater), 200, {
trailing: true,
});
}) as typeof buyQuoteUpdater.updateBuyQuoteAsync;

const mapDispatchToProps = (
dispatch: Dispatch<Action>,
Expand All @@ -87,7 +87,7 @@ const mapDispatchToProps = (
// even if it's debounced, give them the illusion it's loading
dispatch(actions.setQuoteRequestStatePending());
// tslint:disable-next-line:no-floating-promises
debouncedUpdateBuyQuoteAsync(assetBuyer, dispatch, asset, value, affiliateInfo);
debouncedUpdateBuyQuoteAsync(assetBuyer, dispatch, asset, value, true, affiliateInfo);
}
},
});
Expand Down
14 changes: 9 additions & 5 deletions packages/instant/src/redux/async_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AssetProxyId } from '@0x/types';
import * as _ from 'lodash';

import { BIG_NUMBER_ZERO } from '../constants';
import { AccountState, ERC20Asset } from '../types';
import { AccountState, ERC20Asset, OrderProcessState } from '../types';
import { assetUtils } from '../util/asset';
import { buyQuoteUpdater } from '../util/buy_quote_updater';
import { coinbaseApi } from '../util/coinbase_api';
Expand Down Expand Up @@ -36,10 +36,11 @@ export const asyncData = {
store.dispatch(actions.setAvailableAssets([]));
}
},
fetchAccountInfoAndDispatchToStore: async (store: Store) => {
fetchAccountInfoAndDispatchToStore: async (options: { store: Store; shouldSetToLoading: boolean }) => {
const { store, shouldSetToLoading } = options;
const { providerState } = store.getState();
const web3Wrapper = providerState.web3Wrapper;
if (providerState.account.state !== AccountState.Loading) {
if (shouldSetToLoading && providerState.account.state !== AccountState.Loading) {
store.dispatch(actions.setAccountStateLoading());
}
let availableAddresses: string[];
Expand Down Expand Up @@ -74,19 +75,22 @@ export const asyncData = {
return;
}
},
fetchCurrentBuyQuoteAndDispatchToStore: async (store: Store) => {
const { providerState, selectedAsset, selectedAssetAmount, affiliateInfo } = store.getState();
fetchCurrentBuyQuoteAndDispatchToStore: async (options: { store: Store; shouldSetPending: boolean }) => {
const { store, shouldSetPending } = options;
const { buyOrderState, providerState, selectedAsset, selectedAssetAmount, affiliateInfo } = store.getState();
const assetBuyer = providerState.assetBuyer;
if (
!_.isUndefined(selectedAssetAmount) &&
!_.isUndefined(selectedAsset) &&
buyOrderState.processState === OrderProcessState.None &&
selectedAsset.metaData.assetProxyId === AssetProxyId.ERC20
) {
await buyQuoteUpdater.updateBuyQuoteAsync(
assetBuyer,
store.dispatch,
selectedAsset as ERC20Asset,
selectedAssetAmount,
shouldSetPending,
affiliateInfo,
);
}
Expand Down
7 changes: 5 additions & 2 deletions packages/instant/src/util/buy_quote_updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ export const buyQuoteUpdater = {
dispatch: Dispatch<Action>,
asset: ERC20Asset,
assetAmount: BigNumber,
setPending = true,
affiliateInfo?: AffiliateInfo,
): Promise<void> => {
// get a new buy quote.
const baseUnitValue = Web3Wrapper.toBaseUnitAmount(assetAmount, asset.metaData.decimals);
// mark quote as pending
dispatch(actions.setQuoteRequestStatePending());
if (setPending) {
// mark quote as pending
dispatch(actions.setQuoteRequestStatePending());
}
const feePercentage = oc(affiliateInfo).feePercentage();
let newBuyQuote: BuyQuote | undefined;
try {
Expand Down
35 changes: 35 additions & 0 deletions packages/instant/src/util/heartbeater.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { intervalUtils } from '@0x/utils';
import * as _ from 'lodash';

type HeartbeatableFunction = () => Promise<void>;
export class Heartbeater {
private _intervalId?: NodeJS.Timer;
private readonly _performImmediatelyOnStart: boolean;
private readonly _performFunction: HeartbeatableFunction;

public constructor(performingFunctionAsync: HeartbeatableFunction, performImmediatelyOnStart: boolean) {
this._performFunction = performingFunctionAsync;
this._performImmediatelyOnStart = performImmediatelyOnStart;
}

public start(intervalTimeMs: number): void {
if (!_.isUndefined(this._intervalId)) {
throw new Error('Heartbeat is running, please stop before restarting');
}

if (this._performImmediatelyOnStart) {
// tslint:disable-next-line:no-floating-promises
this._performFunction();
}

// tslint:disable-next-line:no-unbound-method
this._intervalId = intervalUtils.setAsyncExcludingInterval(this._performFunction, intervalTimeMs, _.noop);
}

public stop(): void {
if (this._intervalId) {
intervalUtils.clearInterval(this._intervalId);
}
this._intervalId = undefined;
}
}
22 changes: 22 additions & 0 deletions packages/instant/src/util/heartbeater_factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { asyncData } from '../redux/async_data';
import { Store } from '../redux/store';

import { Heartbeater } from './heartbeater';

export interface HeartbeatFactoryOptions {
store: Store;
shouldPerformImmediatelyOnStart: boolean;
}
export const generateAccountHeartbeater = (options: HeartbeatFactoryOptions): Heartbeater => {
const { store, shouldPerformImmediatelyOnStart } = options;
return new Heartbeater(async () => {
await asyncData.fetchAccountInfoAndDispatchToStore({ store, shouldSetToLoading: false });
}, shouldPerformImmediatelyOnStart);
};

export const generateBuyQuoteHeartbeater = (options: HeartbeatFactoryOptions): Heartbeater => {
const { store, shouldPerformImmediatelyOnStart } = options;
return new Heartbeater(async () => {
await asyncData.fetchCurrentBuyQuoteAndDispatchToStore({ store, shouldSetPending: false });
}, shouldPerformImmediatelyOnStart);
};

0 comments on commit 26cbe7a

Please sign in to comment.