Have heartbeat update not trigger errors

This commit is contained in:
Steve Klebanoff
2018-11-15 14:20:19 -08:00
parent 34d86647bf
commit 447b0f91f9
5 changed files with 39 additions and 25 deletions

View File

@@ -113,7 +113,8 @@ export class ZeroExInstantProvider extends React.Component<ZeroExInstantProvider
}); });
this._buyQuoteHeartbeat.start(BUY_QUOTE_UPDATE_INTERVAL_TIME_MS); this._buyQuoteHeartbeat.start(BUY_QUOTE_UPDATE_INTERVAL_TIME_MS);
// tslint:disable-next-line:no-floating-promises // tslint:disable-next-line:no-floating-promises
asyncData.fetchCurrentBuyQuoteAndDispatchToStore(state, dispatch, true); // Trigger first buyquote fetch
asyncData.fetchCurrentBuyQuoteAndDispatchToStore(state, dispatch, { updateSilently: false });
// warm up the gas price estimator cache just in case we can't // warm up the gas price estimator cache just in case we can't
// grab the gas price estimate when submitting the transaction // grab the gas price estimate when submitting the transaction
// tslint:disable-next-line:no-floating-promises // tslint:disable-next-line:no-floating-promises

View File

@@ -81,7 +81,14 @@ const mapDispatchToProps = (
// even if it's debounced, give them the illusion it's loading // even if it's debounced, give them the illusion it's loading
dispatch(actions.setQuoteRequestStatePending()); dispatch(actions.setQuoteRequestStatePending());
// tslint:disable-next-line:no-floating-promises // tslint:disable-next-line:no-floating-promises
debouncedUpdateBuyQuoteAsync(assetBuyer, dispatch, asset, value, true, affiliateInfo); debouncedUpdateBuyQuoteAsync(
assetBuyer,
dispatch,
asset,
value,
{ setPending: true, dispatchErrors: true },
affiliateInfo,
);
} }
}, },
}); });

View File

@@ -82,7 +82,7 @@ export const asyncData = {
fetchCurrentBuyQuoteAndDispatchToStore: async ( fetchCurrentBuyQuoteAndDispatchToStore: async (
state: State, state: State,
dispatch: Dispatch, dispatch: Dispatch,
shouldSetPending: boolean = false, options: { updateSilently: boolean },
) => { ) => {
const { buyOrderState, providerState, selectedAsset, selectedAssetUnitAmount, affiliateInfo } = state; const { buyOrderState, providerState, selectedAsset, selectedAssetUnitAmount, affiliateInfo } = state;
const assetBuyer = providerState.assetBuyer; const assetBuyer = providerState.assetBuyer;
@@ -97,7 +97,7 @@ export const asyncData = {
dispatch, dispatch,
selectedAsset as ERC20Asset, selectedAsset as ERC20Asset,
selectedAssetUnitAmount, selectedAssetUnitAmount,
shouldSetPending, { setPending: !options.updateSilently, dispatchErrors: !options.updateSilently },
affiliateInfo, affiliateInfo,
); );
} }

View File

@@ -16,12 +16,12 @@ export const buyQuoteUpdater = {
dispatch: Dispatch<Action>, dispatch: Dispatch<Action>,
asset: ERC20Asset, asset: ERC20Asset,
assetUnitAmount: BigNumber, assetUnitAmount: BigNumber,
setPending = true, options: { setPending: boolean; dispatchErrors: boolean },
affiliateInfo?: AffiliateInfo, affiliateInfo?: AffiliateInfo,
): Promise<void> => { ): Promise<void> => {
// get a new buy quote. // get a new buy quote.
const baseUnitValue = Web3Wrapper.toBaseUnitAmount(assetUnitAmount, asset.metaData.decimals); const baseUnitValue = Web3Wrapper.toBaseUnitAmount(assetUnitAmount, asset.metaData.decimals);
if (setPending) { if (options.setPending) {
// mark quote as pending // mark quote as pending
dispatch(actions.setQuoteRequestStatePending()); dispatch(actions.setQuoteRequestStatePending());
} }
@@ -30,25 +30,29 @@ export const buyQuoteUpdater = {
try { try {
newBuyQuote = await assetBuyer.getBuyQuoteAsync(asset.assetData, baseUnitValue, { feePercentage }); newBuyQuote = await assetBuyer.getBuyQuoteAsync(asset.assetData, baseUnitValue, { feePercentage });
} catch (error) { } catch (error) {
dispatch(actions.setQuoteRequestStateFailure()); if (options.dispatchErrors) {
let errorMessage; dispatch(actions.setQuoteRequestStateFailure());
if (error.message === AssetBuyerError.InsufficientAssetLiquidity) { let errorMessage;
const assetName = assetUtils.bestNameForAsset(asset, 'of this asset'); if (error.message === AssetBuyerError.InsufficientAssetLiquidity) {
errorMessage = `Not enough ${assetName} available`; const assetName = assetUtils.bestNameForAsset(asset, 'of this asset');
} else if (error.message === AssetBuyerError.InsufficientZrxLiquidity) { errorMessage = `Not enough ${assetName} available`;
errorMessage = 'Not enough ZRX available'; } else if (error.message === AssetBuyerError.InsufficientZrxLiquidity) {
} else if ( errorMessage = 'Not enough ZRX available';
error.message === AssetBuyerError.StandardRelayerApiError || } else if (
error.message.startsWith(AssetBuyerError.AssetUnavailable) error.message === AssetBuyerError.StandardRelayerApiError ||
) { error.message.startsWith(AssetBuyerError.AssetUnavailable)
const assetName = assetUtils.bestNameForAsset(asset, 'This asset'); ) {
errorMessage = `${assetName} is currently unavailable`; const assetName = assetUtils.bestNameForAsset(asset, 'This asset');
} errorMessage = `${assetName} is currently unavailable`;
if (!_.isUndefined(errorMessage)) { }
errorFlasher.flashNewErrorMessage(dispatch, errorMessage); if (!_.isUndefined(errorMessage)) {
} else { errorFlasher.flashNewErrorMessage(dispatch, errorMessage);
throw error; } else {
throw error;
}
} }
// TODO: report to error reporter on else
return; return;
} }
// We have a successful new buy quote // We have a successful new buy quote

View File

@@ -17,6 +17,8 @@ export const generateAccountHeartbeater = (options: HeartbeatFactoryOptions): He
export const generateBuyQuoteHeartbeater = (options: HeartbeatFactoryOptions): Heartbeater => { export const generateBuyQuoteHeartbeater = (options: HeartbeatFactoryOptions): Heartbeater => {
const { store, shouldPerformImmediatelyOnStart } = options; const { store, shouldPerformImmediatelyOnStart } = options;
return new Heartbeater(async () => { return new Heartbeater(async () => {
await asyncData.fetchCurrentBuyQuoteAndDispatchToStore(store.getState(), store.dispatch, false); await asyncData.fetchCurrentBuyQuoteAndDispatchToStore(store.getState(), store.dispatch, {
updateSilently: true,
});
}, shouldPerformImmediatelyOnStart); }, shouldPerformImmediatelyOnStart);
}; };