aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/src/util
diff options
context:
space:
mode:
authorfragosti <francesco.agosti93@gmail.com>2018-11-09 07:42:46 +0800
committerfragosti <francesco.agosti93@gmail.com>2018-11-09 07:42:46 +0800
commit4f4d901ecaa4fd772288c3421f92da293690ceb8 (patch)
treeb5c1f038eb74abeea86094f2bef7c6c3581614fc /packages/instant/src/util
parent6a84877f9aab63db1c493d1a3d73d06e65735a78 (diff)
parent117e2f583ff44bdb63340a2134edea0f3ecb77b3 (diff)
downloaddexon-sol-tools-4f4d901ecaa4fd772288c3421f92da293690ceb8.tar
dexon-sol-tools-4f4d901ecaa4fd772288c3421f92da293690ceb8.tar.gz
dexon-sol-tools-4f4d901ecaa4fd772288c3421f92da293690ceb8.tar.bz2
dexon-sol-tools-4f4d901ecaa4fd772288c3421f92da293690ceb8.tar.lz
dexon-sol-tools-4f4d901ecaa4fd772288c3421f92da293690ceb8.tar.xz
dexon-sol-tools-4f4d901ecaa4fd772288c3421f92da293690ceb8.tar.zst
dexon-sol-tools-4f4d901ecaa4fd772288c3421f92da293690ceb8.zip
Merge branch 'development' of https://github.com/0xProject/0x-monorepo into feature/instant/dropdown-ui
Diffstat (limited to 'packages/instant/src/util')
-rw-r--r--packages/instant/src/util/buy_quote_updater.ts56
1 files changed, 56 insertions, 0 deletions
diff --git a/packages/instant/src/util/buy_quote_updater.ts b/packages/instant/src/util/buy_quote_updater.ts
new file mode 100644
index 000000000..e697d3ef7
--- /dev/null
+++ b/packages/instant/src/util/buy_quote_updater.ts
@@ -0,0 +1,56 @@
+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 '../util/error_flasher';
+
+export const buyQuoteUpdater = {
+ updateBuyQuoteAsync: async (
+ assetBuyer: AssetBuyer,
+ dispatch: Dispatch<Action>,
+ asset: ERC20Asset,
+ assetAmount: BigNumber,
+ affiliateInfo?: AffiliateInfo,
+ ): Promise<void> => {
+ // get a new buy quote.
+ const baseUnitValue = Web3Wrapper.toBaseUnitAmount(assetAmount, asset.metaData.decimals);
+ // mark quote as pending
+ dispatch(actions.setQuoteRequestStatePending());
+ 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));
+ },
+};