aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/src/util
diff options
context:
space:
mode:
authorSteve Klebanoff <steve.klebanoff@gmail.com>2018-11-09 08:14:00 +0800
committerSteve Klebanoff <steve.klebanoff@gmail.com>2018-11-09 08:14:00 +0800
commitdd4d3b10cf8c0bb08f981128232d3663d01806eb (patch)
tree0dd7b5c409012af7fe6564758f087cc7824c4549 /packages/instant/src/util
parent624f5cee8dd4a21e7becd5664f47f5780ade7e69 (diff)
downloaddexon-sol-tools-dd4d3b10cf8c0bb08f981128232d3663d01806eb.tar
dexon-sol-tools-dd4d3b10cf8c0bb08f981128232d3663d01806eb.tar.gz
dexon-sol-tools-dd4d3b10cf8c0bb08f981128232d3663d01806eb.tar.bz2
dexon-sol-tools-dd4d3b10cf8c0bb08f981128232d3663d01806eb.tar.lz
dexon-sol-tools-dd4d3b10cf8c0bb08f981128232d3663d01806eb.tar.xz
dexon-sol-tools-dd4d3b10cf8c0bb08f981128232d3663d01806eb.tar.zst
dexon-sol-tools-dd4d3b10cf8c0bb08f981128232d3663d01806eb.zip
wip: abstract out updating buy quote
Diffstat (limited to 'packages/instant/src/util')
-rw-r--r--packages/instant/src/util/buy_quote_fetcher.ts55
1 files changed, 55 insertions, 0 deletions
diff --git a/packages/instant/src/util/buy_quote_fetcher.ts b/packages/instant/src/util/buy_quote_fetcher.ts
new file mode 100644
index 000000000..c3e65cbc3
--- /dev/null
+++ b/packages/instant/src/util/buy_quote_fetcher.ts
@@ -0,0 +1,55 @@
+// TODO: rename file and export object
+import { AssetBuyer, AssetBuyerError, BuyQuote } from '@0x/asset-buyer';
+import { BigNumber } from '@0x/utils';
+import { Web3Wrapper } from '@0x/web3-wrapper';
+import * as _ from 'lodash';
+import { Dispatch } from 'redux';
+import { oc } from 'ts-optchain';
+
+import { Action, actions } from '../redux/actions';
+import { AffiliateInfo, ERC20Asset } from '../types';
+import { assetUtils } from '../util/asset';
+
+import { errorFlasher } from './error_flasher';
+
+export const updateBuyQuoteOrFlashErrorAsync = async (
+ assetBuyer: AssetBuyer,
+ asset: ERC20Asset,
+ assetAmount: BigNumber,
+ dispatch: Dispatch<Action>,
+ affiliateInfo?: AffiliateInfo,
+) => {
+ // get a new buy quote.
+ const baseUnitValue = Web3Wrapper.toBaseUnitAmount(assetAmount, asset.metaData.decimals);
+
+ const feePercentage = oc(affiliateInfo).feePercentage();
+ let newBuyQuote: BuyQuote | undefined;
+ try {
+ newBuyQuote = await assetBuyer.getBuyQuoteAsync(asset.assetData, baseUnitValue, { feePercentage });
+ } catch (error) {
+ dispatch(actions.setQuoteRequestStateFailure());
+ let errorMessage;
+ if (error.message === AssetBuyerError.InsufficientAssetLiquidity) {
+ const assetName = assetUtils.bestNameForAsset(asset, 'of this asset');
+ errorMessage = `Not enough ${assetName} available`;
+ } else if (error.message === AssetBuyerError.InsufficientZrxLiquidity) {
+ errorMessage = 'Not enough ZRX available';
+ } else if (
+ error.message === AssetBuyerError.StandardRelayerApiError ||
+ error.message.startsWith(AssetBuyerError.AssetUnavailable)
+ ) {
+ const assetName = assetUtils.bestNameForAsset(asset, 'This asset');
+ errorMessage = `${assetName} is currently unavailable`;
+ }
+ if (!_.isUndefined(errorMessage)) {
+ errorFlasher.flashNewErrorMessage(dispatch, errorMessage);
+ } else {
+ throw error;
+ }
+ return;
+ }
+ // We have a successful new buy quote
+ errorFlasher.clearError(dispatch);
+ // invalidate the last buy quote.
+ dispatch(actions.updateLatestBuyQuote(newBuyQuote));
+};