diff options
author | Brandon Millman <brandon@0xproject.com> | 2018-11-09 06:28:22 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-09 06:28:22 +0800 |
commit | 5ffd20214c02d5a6cb7aaffe79d3baa5ee527da7 (patch) | |
tree | ac5a85a545b01f916ffc449d34fa6216b615b660 /packages/instant/src/util | |
parent | b388d5496272d535b5c192a7098f4227bb1fb3f2 (diff) | |
parent | 8493d619b45d84eedf9f8d349e45a4d3e759c538 (diff) | |
download | dexon-sol-tools-5ffd20214c02d5a6cb7aaffe79d3baa5ee527da7.tar dexon-sol-tools-5ffd20214c02d5a6cb7aaffe79d3baa5ee527da7.tar.gz dexon-sol-tools-5ffd20214c02d5a6cb7aaffe79d3baa5ee527da7.tar.bz2 dexon-sol-tools-5ffd20214c02d5a6cb7aaffe79d3baa5ee527da7.tar.lz dexon-sol-tools-5ffd20214c02d5a6cb7aaffe79d3baa5ee527da7.tar.xz dexon-sol-tools-5ffd20214c02d5a6cb7aaffe79d3baa5ee527da7.tar.zst dexon-sol-tools-5ffd20214c02d5a6cb7aaffe79d3baa5ee527da7.zip |
Merge pull request #1231 from 0xProject/fix/instant/default-amount-quote
[instant] Update buy quote at start up in the case of default amount
Diffstat (limited to 'packages/instant/src/util')
-rw-r--r-- | packages/instant/src/util/buy_quote_updater.ts | 56 |
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)); + }, +}; |