diff options
author | Brandon Millman <brandon.millman@gmail.com> | 2018-11-03 04:48:31 +0800 |
---|---|---|
committer | Brandon Millman <brandon.millman@gmail.com> | 2018-11-03 05:39:12 +0800 |
commit | 5fc2483be776cab90e7ff868b27536a85d6764c9 (patch) | |
tree | 1dedb81a97b4c57c763d4f56cb4952d5df4b586f /packages/instant/src/components | |
parent | ec83a1d9e735573fe51df7a5854424c0ad4101fb (diff) | |
download | dexon-sol-tools-5fc2483be776cab90e7ff868b27536a85d6764c9.tar dexon-sol-tools-5fc2483be776cab90e7ff868b27536a85d6764c9.tar.gz dexon-sol-tools-5fc2483be776cab90e7ff868b27536a85d6764c9.tar.bz2 dexon-sol-tools-5fc2483be776cab90e7ff868b27536a85d6764c9.tar.lz dexon-sol-tools-5fc2483be776cab90e7ff868b27536a85d6764c9.tar.xz dexon-sol-tools-5fc2483be776cab90e7ff868b27536a85d6764c9.tar.zst dexon-sol-tools-5fc2483be776cab90e7ff868b27536a85d6764c9.zip |
feat(instant): pass in provider through props, fallback to injected provider
Diffstat (limited to 'packages/instant/src/components')
-rw-r--r-- | packages/instant/src/components/buy_button.tsx | 5 | ||||
-rw-r--r-- | packages/instant/src/components/zero_ex_instant_provider.tsx | 30 |
2 files changed, 22 insertions, 13 deletions
diff --git a/packages/instant/src/components/buy_button.tsx b/packages/instant/src/components/buy_button.tsx index 12ac62601..9d9a8540c 100644 --- a/packages/instant/src/components/buy_button.tsx +++ b/packages/instant/src/components/buy_button.tsx @@ -1,4 +1,5 @@ import { AssetBuyer, AssetBuyerError, BuyQuote } from '@0x/asset-buyer'; +import { Web3Wrapper } from '@0x/web3-wrapper'; import * as _ from 'lodash'; import * as React from 'react'; import { oc } from 'ts-optchain'; @@ -10,7 +11,6 @@ import { getBestAddress } from '../util/address'; import { balanceUtil } from '../util/balance'; import { gasPriceEstimator } from '../util/gas_price_estimator'; import { util } from '../util/util'; -import { web3Wrapper } from '../util/web3_wrapper'; import { Button, Text } from './ui'; @@ -50,7 +50,8 @@ export class BuyButton extends React.Component<BuyButtonProps> { } this.props.onValidationPending(buyQuote); - const takerAddress = await getBestAddress(); + const web3Wrapper = new Web3Wrapper(assetBuyer.provider); + const takerAddress = await getBestAddress(web3Wrapper); const hasSufficientEth = await balanceUtil.hasSufficientEth(takerAddress, buyQuote, web3Wrapper); if (!hasSufficientEth) { diff --git a/packages/instant/src/components/zero_ex_instant_provider.tsx b/packages/instant/src/components/zero_ex_instant_provider.tsx index fce03a280..4bc840862 100644 --- a/packages/instant/src/components/zero_ex_instant_provider.tsx +++ b/packages/instant/src/components/zero_ex_instant_provider.tsx @@ -1,8 +1,11 @@ import { AssetBuyer } from '@0x/asset-buyer'; import { ObjectMap, SignedOrder } from '@0x/types'; +import { Web3Wrapper } from '@0x/web3-wrapper'; +import { Provider } from 'ethereum-types'; import * as _ from 'lodash'; import * as React from 'react'; -import { Provider } from 'react-redux'; +import { Provider as ReduxProvider } from 'react-redux'; +import { oc } from 'ts-optchain'; import { SelectedAssetThemeProvider } from '../containers/selected_asset_theme_provider'; import { asyncData } from '../redux/async_data'; @@ -14,8 +17,7 @@ import { assetUtils } from '../util/asset'; import { BigNumberInput } from '../util/big_number_input'; import { errorFlasher } from '../util/error_flasher'; import { gasPriceEstimator } from '../util/gas_price_estimator'; -import { getProvider } from '../util/provider'; -import { web3Wrapper } from '../util/web3_wrapper'; +import { getInjectedProvider } from '../util/injected_provider'; fonts.include(); @@ -29,6 +31,7 @@ export interface ZeroExInstantProviderRequiredProps { } export interface ZeroExInstantProviderOptionalProps { + provider: Provider; defaultAssetBuyAmount: number; additionalAssetMetaDataMap: ObjectMap<AssetMetaData>; networkId: Network; @@ -39,8 +42,8 @@ export class ZeroExInstantProvider extends React.Component<ZeroExInstantProvider private readonly _store: Store; private static _mergeInitialStateWithProps(props: ZeroExInstantProviderProps, state: State = INITIAL_STATE): State { const networkId = props.networkId || state.network; - // TODO: Provider needs to not be hard-coded to injected web3. - const provider = getProvider(); + // TODO: Proper wallet connect flow + const provider = props.provider || getInjectedProvider(); const assetBuyerOptions = { networkId, }; @@ -92,19 +95,24 @@ export class ZeroExInstantProvider extends React.Component<ZeroExInstantProvider public render(): React.ReactNode { return ( - <Provider store={this._store}> + <ReduxProvider store={this._store}> <SelectedAssetThemeProvider>{this.props.children}</SelectedAssetThemeProvider> - </Provider> + </ReduxProvider> ); } private readonly _flashErrorIfWrongNetwork = async (): Promise<void> => { const msToShowError = 30000; // 30 seconds const network = this._store.getState().network; - const networkOfProvider = await web3Wrapper.getNetworkIdAsync(); - if (network !== networkOfProvider) { - const errorMessage = `Wrong network detected. Try switching to ${Network[network]}.`; - errorFlasher.flashNewErrorMessage(this._store.dispatch, errorMessage, msToShowError); + const assetBuyerIfExists = this._store.getState().assetBuyer; + const providerIfExists = oc(assetBuyerIfExists).provider(); + if (!_.isUndefined(providerIfExists)) { + const web3Wrapper = new Web3Wrapper(providerIfExists); + const networkOfProvider = await web3Wrapper.getNetworkIdAsync(); + if (network !== networkOfProvider) { + const errorMessage = `Wrong network detected. Try switching to ${Network[network]}.`; + errorFlasher.flashNewErrorMessage(this._store.dispatch, errorMessage, msToShowError); + } } }; } |