diff options
author | fragosti <francesco.agosti93@gmail.com> | 2018-10-19 01:43:41 +0800 |
---|---|---|
committer | fragosti <francesco.agosti93@gmail.com> | 2018-10-19 01:43:41 +0800 |
commit | a764dfa789ba44e519371b4a1e4569db7f551fb7 (patch) | |
tree | 80cd8aff6fcf941269afdec8022d0dd5759700fa /packages/instant/src/components | |
parent | 5ec4b27200297708298deca97603849a37b2f66a (diff) | |
download | dexon-sol-tools-a764dfa789ba44e519371b4a1e4569db7f551fb7.tar dexon-sol-tools-a764dfa789ba44e519371b4a1e4569db7f551fb7.tar.gz dexon-sol-tools-a764dfa789ba44e519371b4a1e4569db7f551fb7.tar.bz2 dexon-sol-tools-a764dfa789ba44e519371b4a1e4569db7f551fb7.tar.lz dexon-sol-tools-a764dfa789ba44e519371b4a1e4569db7f551fb7.tar.xz dexon-sol-tools-a764dfa789ba44e519371b4a1e4569db7f551fb7.tar.zst dexon-sol-tools-a764dfa789ba44e519371b4a1e4569db7f551fb7.zip |
feat: MVP of passing in sraApiUrl, assetData and other settings from render method
Diffstat (limited to 'packages/instant/src/components')
-rw-r--r-- | packages/instant/src/components/asset_amount_input.tsx | 29 | ||||
-rw-r--r-- | packages/instant/src/components/buy_button.tsx | 8 | ||||
-rw-r--r-- | packages/instant/src/components/zero_ex_instant.tsx | 58 |
3 files changed, 59 insertions, 36 deletions
diff --git a/packages/instant/src/components/asset_amount_input.tsx b/packages/instant/src/components/asset_amount_input.tsx index 7c6b03ee9..9f4b5861a 100644 --- a/packages/instant/src/components/asset_amount_input.tsx +++ b/packages/instant/src/components/asset_amount_input.tsx @@ -1,18 +1,19 @@ -import { AssetProxyId } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; import * as _ from 'lodash'; import * as React from 'react'; +import { oc } from 'ts-optchain'; -import { assetMetaData } from '../data/asset_meta_data'; import { ColorOption } from '../style/theme'; +import { ERC20Asset } from '../types'; import { util } from '../util/util'; import { AmountInput, AmountInputProps } from './amount_input'; import { Container, Text } from './ui'; +// Asset amounts only apply to ERC20 assets export interface AssetAmountInputProps extends AmountInputProps { - assetData?: string; - onChange: (value?: BigNumber, assetData?: string) => void; + asset?: ERC20Asset; + onChange: (value?: BigNumber, asset?: ERC20Asset) => void; } export class AssetAmountInput extends React.Component<AssetAmountInputProps> { @@ -20,33 +21,19 @@ export class AssetAmountInput extends React.Component<AssetAmountInputProps> { onChange: util.boundNoop, }; public render(): React.ReactNode { - const { assetData, onChange, ...rest } = this.props; + const { asset, onChange, ...rest } = this.props; return ( <Container> <AmountInput {...rest} onChange={this._handleChange} /> <Container display="inline-block" marginLeft="10px"> <Text fontSize={rest.fontSize} fontColor={ColorOption.white} textTransform="uppercase"> - {this._getAssetSymbolLabel()} + {oc(asset).metaData.symbol()} </Text> </Container> </Container> ); } - private readonly _getAssetSymbolLabel = (): string => { - const unknownLabel = '???'; - if (_.isUndefined(this.props.assetData)) { - return unknownLabel; - } - const metaData = assetMetaData[this.props.assetData]; - if (_.isUndefined(metaData)) { - return unknownLabel; - } - if (metaData.assetProxyId === AssetProxyId.ERC20) { - return metaData.symbol; - } - return unknownLabel; - }; private readonly _handleChange = (value?: BigNumber): void => { - this.props.onChange(value, this.props.assetData); + this.props.onChange(value, this.props.asset); }; } diff --git a/packages/instant/src/components/buy_button.tsx b/packages/instant/src/components/buy_button.tsx index 0706817c9..191426be1 100644 --- a/packages/instant/src/components/buy_button.tsx +++ b/packages/instant/src/components/buy_button.tsx @@ -1,9 +1,8 @@ -import { BuyQuote } from '@0xproject/asset-buyer'; +import { AssetBuyer, BuyQuote } from '@0xproject/asset-buyer'; import * as _ from 'lodash'; import * as React from 'react'; import { ColorOption } from '../style/theme'; -import { assetBuyer } from '../util/asset_buyer'; import { util } from '../util/util'; import { web3Wrapper } from '../util/web3_wrapper'; @@ -11,6 +10,7 @@ import { Button, Container, Text } from './ui'; export interface BuyButtonProps { buyQuote?: BuyQuote; + assetBuyer?: AssetBuyer; onClick: (buyQuote: BuyQuote) => void; onBuySuccess: (buyQuote: BuyQuote, txnHash: string) => void; onBuyFailure: (buyQuote: BuyQuote, tnxHash?: string) => void; @@ -37,13 +37,13 @@ export class BuyButton extends React.Component<BuyButtonProps> { } private readonly _handleClick = async () => { // The button is disabled when there is no buy quote anyway. - if (_.isUndefined(this.props.buyQuote)) { + if (_.isUndefined(this.props.buyQuote) || _.isUndefined(this.props.assetBuyer)) { return; } this.props.onClick(this.props.buyQuote); let txnHash; try { - txnHash = await assetBuyer.executeBuyQuoteAsync(this.props.buyQuote); + txnHash = await this.props.assetBuyer.executeBuyQuoteAsync(this.props.buyQuote); await web3Wrapper.awaitTransactionSuccessAsync(txnHash); this.props.onBuySuccess(this.props.buyQuote, txnHash); } catch { diff --git a/packages/instant/src/components/zero_ex_instant.tsx b/packages/instant/src/components/zero_ex_instant.tsx index f6472e811..cc7b0ecb8 100644 --- a/packages/instant/src/components/zero_ex_instant.tsx +++ b/packages/instant/src/components/zero_ex_instant.tsx @@ -1,23 +1,59 @@ +import { AssetBuyer } from '@0xproject/asset-buyer'; +import { ObjectMap } from '@0xproject/types'; import * as React from 'react'; import { Provider } from 'react-redux'; +import { assetMetaDataMap } from '../data/asset_meta_data_map'; import { asyncData } from '../redux/async_data'; -import { store } from '../redux/store'; +import { State } from '../redux/reducer'; +import { store, Store } from '../redux/store'; import { fonts } from '../style/fonts'; import { theme, ThemeProvider } from '../style/theme'; +import { AssetMetaData } from '../types'; +import { assetUtils } from '../util/asset'; +import { getProvider } from '../util/provider'; import { ZeroExInstantContainer } from './zero_ex_instant_container'; fonts.include(); -// tslint:disable-next-line:no-floating-promises -asyncData.fetchAndDispatchToStore(); -export interface ZeroExInstantProps {} +export interface ZeroExInstantProps { + // TODO: Change API when we allow the selection of different assetDatas + assetData: string; + sraApiUrl: string; + additionalAssetMetaDataMap: ObjectMap<AssetMetaData>; +} -export const ZeroExInstant: React.StatelessComponent<ZeroExInstantProps> = () => ( - <Provider store={store}> - <ThemeProvider theme={theme}> - <ZeroExInstantContainer /> - </ThemeProvider> - </Provider> -); +export class ZeroExInstant extends React.Component<ZeroExInstantProps> { + public static defaultProps = { + additionalAssetMetaDataMap: {}, + }; + public store: Store; + constructor(props: ZeroExInstantProps) { + super(props); + // TODO: Provider needs to not be hard-coded to injected web3. + const assetBuyer = AssetBuyer.getAssetBuyerForStandardRelayerAPIUrl(getProvider(), props.sraApiUrl); + const completeAssetMetaDataMap = { + ...props.additionalAssetMetaDataMap, + ...assetMetaDataMap, + }; + const storeStateFromProps: Partial<State> = { + assetBuyer, + selectedAsset: assetUtils.createAssetFromAssetData(props.assetData, completeAssetMetaDataMap), + assetMetaDataMap: completeAssetMetaDataMap, + }; + this.store = store.create(storeStateFromProps); + // tslint:disable-next-line:no-floating-promises + asyncData.fetchAndDispatchToStore(this.store); + } + + public render(): React.ReactNode { + return ( + <Provider store={this.store}> + <ThemeProvider theme={theme}> + <ZeroExInstantContainer /> + </ThemeProvider> + </Provider> + ); + } +} |