diff options
author | Steve Klebanoff <steve.klebanoff@gmail.com> | 2018-11-09 09:09:26 +0800 |
---|---|---|
committer | Steve Klebanoff <steve.klebanoff@gmail.com> | 2018-11-09 09:09:26 +0800 |
commit | 1e39d56cf751fd922d6fec86ecc8bc70b20bc6bb (patch) | |
tree | 15d1fd1a472d6875340a0e35cdd1d8259958508d /packages/instant/src/util | |
parent | e45b6c7e98a33de0e13f4ab7db8b630900dbb960 (diff) | |
download | dexon-sol-tools-1e39d56cf751fd922d6fec86ecc8bc70b20bc6bb.tar.gz dexon-sol-tools-1e39d56cf751fd922d6fec86ecc8bc70b20bc6bb.tar.zst dexon-sol-tools-1e39d56cf751fd922d6fec86ecc8bc70b20bc6bb.zip |
wip: BuyQuote heartbeat
Diffstat (limited to 'packages/instant/src/util')
-rw-r--r-- | packages/instant/src/util/buy_quote_fetcher.ts | 18 | ||||
-rw-r--r-- | packages/instant/src/util/hearbeats.ts | 18 |
2 files changed, 33 insertions, 3 deletions
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(); + }); +}; |