aboutsummaryrefslogtreecommitdiffstats
path: root/packages
diff options
context:
space:
mode:
authorfragosti <francesco.agosti93@gmail.com>2018-10-19 01:43:41 +0800
committerfragosti <francesco.agosti93@gmail.com>2018-10-19 01:43:41 +0800
commita764dfa789ba44e519371b4a1e4569db7f551fb7 (patch)
tree80cd8aff6fcf941269afdec8022d0dd5759700fa /packages
parent5ec4b27200297708298deca97603849a37b2f66a (diff)
downloaddexon-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')
-rw-r--r--packages/instant/package.json1
-rw-r--r--packages/instant/public/index.html3
-rw-r--r--packages/instant/src/components/asset_amount_input.tsx29
-rw-r--r--packages/instant/src/components/buy_button.tsx8
-rw-r--r--packages/instant/src/components/zero_ex_instant.tsx58
-rw-r--r--packages/instant/src/constants.ts3
-rw-r--r--packages/instant/src/containers/selected_asset_amount_input.ts70
-rw-r--r--packages/instant/src/containers/selected_asset_buy_button.ts4
-rw-r--r--packages/instant/src/data/asset_data_network_mapping.ts25
-rw-r--r--packages/instant/src/data/asset_meta_data_map.ts (renamed from packages/instant/src/data/asset_meta_data.ts)5
-rw-r--r--packages/instant/src/index.umd.ts6
-rw-r--r--packages/instant/src/redux/actions.ts2
-rw-r--r--packages/instant/src/redux/async_data.ts4
-rw-r--r--packages/instant/src/redux/reducer.ts25
-rw-r--r--packages/instant/src/redux/store.ts14
-rw-r--r--packages/instant/src/types.ts22
-rw-r--r--packages/instant/src/util/asset.ts34
-rw-r--r--packages/instant/src/util/asset_buyer.ts9
18 files changed, 240 insertions, 82 deletions
diff --git a/packages/instant/package.json b/packages/instant/package.json
index 0c4b470fa..ec5cbebb4 100644
--- a/packages/instant/package.json
+++ b/packages/instant/package.json
@@ -44,6 +44,7 @@
"homepage": "https://github.com/0xProject/0x-monorepo/packages/instant/README.md",
"dependencies": {
"@0xproject/asset-buyer": "^2.0.0",
+ "@0xproject/order-utils": "^1.0.7",
"@0xproject/types": "^1.1.4",
"@0xproject/typescript-typings": "^2.0.2",
"@0xproject/utils": "^2.0.2",
diff --git a/packages/instant/public/index.html b/packages/instant/public/index.html
index fb041745e..1f6a0a9e9 100644
--- a/packages/instant/public/index.html
+++ b/packages/instant/public/index.html
@@ -25,7 +25,8 @@
<div id="zeroExInstantContainer"></div>
<script>
zeroExInstant.render({
-
+ sraApiUrl: 'https://api.radarrelay.com/0x/v2/',
+ assetData: '0xf47261b0000000000000000000000000e41d2489571d322189246dafa5ebde1f4699f498',
});
</script>
</body>
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>
+ );
+ }
+}
diff --git a/packages/instant/src/constants.ts b/packages/instant/src/constants.ts
index 1fd321c5a..b27378d4c 100644
--- a/packages/instant/src/constants.ts
+++ b/packages/instant/src/constants.ts
@@ -1,6 +1,3 @@
import { BigNumber } from '@0xproject/utils';
export const BIG_NUMBER_ZERO = new BigNumber(0);
-export const sraApiUrl = 'https://api.radarrelay.com/0x/v2/';
-export const zrxAssetData = '0xf47261b0000000000000000000000000e41d2489571d322189246dafa5ebde1f4699f498';
-export const zrxDecimals = 18;
export const ethDecimals = 18;
diff --git a/packages/instant/src/containers/selected_asset_amount_input.ts b/packages/instant/src/containers/selected_asset_amount_input.ts
index f2ca96ae4..b75a22a0e 100644
--- a/packages/instant/src/containers/selected_asset_amount_input.ts
+++ b/packages/instant/src/containers/selected_asset_amount_input.ts
@@ -1,3 +1,5 @@
+import { AssetBuyer } from '@0xproject/asset-buyer';
+import { AssetProxyId } from '@0xproject/types';
import { BigNumber } from '@0xproject/utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper';
import * as _ from 'lodash';
@@ -5,12 +7,10 @@ import * as React from 'react';
import { connect } from 'react-redux';
import { Dispatch } from 'redux';
-import { zrxDecimals } from '../constants';
import { Action, actions } from '../redux/actions';
import { State } from '../redux/reducer';
import { ColorOption } from '../style/theme';
-import { AsyncProcessState } from '../types';
-import { assetBuyer } from '../util/asset_buyer';
+import { AsyncProcessState, ERC20Asset } from '../types';
import { AssetAmountInput } from '../components/asset_amount_input';
@@ -20,30 +20,54 @@ export interface SelectedAssetAmountInputProps {
}
interface ConnectedState {
+ assetBuyer?: AssetBuyer;
value?: BigNumber;
- assetData?: string;
+ asset?: ERC20Asset;
}
interface ConnectedDispatch {
- onChange: (value?: BigNumber, assetData?: string) => void;
+ updateBuyQuote: (assetBuyer?: AssetBuyer, value?: BigNumber, asset?: ERC20Asset) => void;
}
-const mapStateToProps = (state: State, _ownProps: SelectedAssetAmountInputProps): ConnectedState => ({
- value: state.selectedAssetAmount,
- assetData: state.selectedAssetData,
-});
+interface ConnectedProps {
+ value?: BigNumber;
+ asset?: ERC20Asset;
+ onChange: (value?: BigNumber, asset?: ERC20Asset) => void;
+}
+
+type FinalProps = ConnectedProps & SelectedAssetAmountInputProps;
+
+const mapStateToProps = (state: State, _ownProps: SelectedAssetAmountInputProps): ConnectedState => {
+ const selectedAsset = state.selectedAsset;
+ if (_.isUndefined(selectedAsset) || selectedAsset.assetProxyId !== AssetProxyId.ERC20) {
+ return {
+ value: state.selectedAssetAmount,
+ };
+ }
+ return {
+ assetBuyer: state.assetBuyer,
+ value: state.selectedAssetAmount,
+ asset: selectedAsset as ERC20Asset,
+ };
+};
const updateBuyQuoteAsync = async (
dispatch: Dispatch<Action>,
- assetData?: string,
+ assetBuyer?: AssetBuyer,
+ asset?: ERC20Asset,
assetAmount?: BigNumber,
): Promise<void> => {
- if (_.isUndefined(assetAmount) || _.isUndefined(assetData)) {
+ if (
+ _.isUndefined(assetBuyer) ||
+ _.isUndefined(assetAmount) ||
+ _.isUndefined(asset) ||
+ _.isUndefined(asset.metaData)
+ ) {
return;
}
// get a new buy quote.
- const baseUnitValue = Web3Wrapper.toBaseUnitAmount(assetAmount, zrxDecimals);
- const newBuyQuote = await assetBuyer.getBuyQuoteAsync(assetData, baseUnitValue);
+ const baseUnitValue = Web3Wrapper.toBaseUnitAmount(assetAmount, asset.metaData.decimals);
+ const newBuyQuote = await assetBuyer.getBuyQuoteAsync(asset.assetData, baseUnitValue);
// invalidate the last buy quote.
dispatch(actions.updateLatestBuyQuote(newBuyQuote));
};
@@ -54,7 +78,7 @@ const mapDispatchToProps = (
dispatch: Dispatch<Action>,
_ownProps: SelectedAssetAmountInputProps,
): ConnectedDispatch => ({
- onChange: (value, assetData) => {
+ updateBuyQuote: (assetBuyer, value, asset) => {
// Update the input
dispatch(actions.updateSelectedAssetAmount(value));
// invalidate the last buy quote.
@@ -62,11 +86,27 @@ const mapDispatchToProps = (
// reset our buy state
dispatch(actions.updateSelectedAssetBuyState(AsyncProcessState.NONE));
// tslint:disable-next-line:no-floating-promises
- debouncedUpdateBuyQuoteAsync(dispatch, assetData, value);
+ debouncedUpdateBuyQuoteAsync(dispatch, assetBuyer, asset, value);
},
});
+const mergeProps = (
+ connectedState: ConnectedState,
+ connectedDispatch: ConnectedDispatch,
+ ownProps: SelectedAssetAmountInputProps,
+): FinalProps => {
+ return {
+ ...ownProps,
+ asset: connectedState.asset,
+ value: connectedState.value,
+ onChange: (value, asset) => {
+ connectedDispatch.updateBuyQuote(connectedState.assetBuyer, value, asset);
+ },
+ };
+};
+
export const SelectedAssetAmountInput: React.ComponentClass<SelectedAssetAmountInputProps> = connect(
mapStateToProps,
mapDispatchToProps,
+ mergeProps,
)(AssetAmountInput);
diff --git a/packages/instant/src/containers/selected_asset_buy_button.ts b/packages/instant/src/containers/selected_asset_buy_button.ts
index 4cbaf5537..4118932b2 100644
--- a/packages/instant/src/containers/selected_asset_buy_button.ts
+++ b/packages/instant/src/containers/selected_asset_buy_button.ts
@@ -1,4 +1,4 @@
-import { BuyQuote } from '@0xproject/asset-buyer';
+import { AssetBuyer, BuyQuote } from '@0xproject/asset-buyer';
import * as _ from 'lodash';
import * as React from 'react';
import { connect } from 'react-redux';
@@ -13,6 +13,7 @@ import { BuyButton } from '../components/buy_button';
export interface SelectedAssetBuyButtonProps {}
interface ConnectedState {
+ assetBuyer?: AssetBuyer;
text: string;
buyQuote?: BuyQuote;
}
@@ -39,6 +40,7 @@ const textForState = (state: AsyncProcessState): string => {
};
const mapStateToProps = (state: State, _ownProps: SelectedAssetBuyButtonProps): ConnectedState => ({
+ assetBuyer: state.assetBuyer,
text: textForState(state.selectedAssetBuyState),
buyQuote: state.latestBuyQuote,
});
diff --git a/packages/instant/src/data/asset_data_network_mapping.ts b/packages/instant/src/data/asset_data_network_mapping.ts
new file mode 100644
index 000000000..9e14462f8
--- /dev/null
+++ b/packages/instant/src/data/asset_data_network_mapping.ts
@@ -0,0 +1,25 @@
+import * as _ from 'lodash';
+
+import { Network } from '../types';
+
+interface AssetDataByNetwork {
+ [Network.Kovan]?: string;
+ [Network.Mainnet]?: string;
+}
+
+const sameAssetDatas: AssetDataByNetwork[] = [
+ {
+ [Network.Mainnet]: '0xf47261b0000000000000000000000000e41d2489571d322189246dafa5ebde1f4699f498',
+ [Network.Kovan]: '0xf47261b00000000000000000000000002002d3812f58e35f0ea1ffbf80a75a38c32175fa',
+ },
+];
+
+export const assetDataNetworkMapping = {
+ getAssociatedAssetDataIfExists: (assetData: string, network: Network): string | undefined => {
+ const assetDataGroupIfExists = _.find(sameAssetDatas, value => value[network] === assetData);
+ if (_.isUndefined(assetDataGroupIfExists)) {
+ return;
+ }
+ return assetDataGroupIfExists[Network.Mainnet];
+ },
+};
diff --git a/packages/instant/src/data/asset_meta_data.ts b/packages/instant/src/data/asset_meta_data_map.ts
index e4d3e8f73..7d83865f1 100644
--- a/packages/instant/src/data/asset_meta_data.ts
+++ b/packages/instant/src/data/asset_meta_data_map.ts
@@ -1,12 +1,11 @@
import { AssetProxyId, ObjectMap } from '@0xproject/types';
-import { zrxAssetData } from '../constants';
import { AssetMetaData } from '../types';
// Map from assetData string to AssetMetaData object
// TODO: import this from somewhere else.
-export const assetMetaData: ObjectMap<AssetMetaData> = {
- [zrxAssetData]: {
+export const assetMetaDataMap: ObjectMap<AssetMetaData> = {
+ '0xf47261b0000000000000000000000000e41d2489571d322189246dafa5ebde1f4699f498': {
assetProxyId: AssetProxyId.ERC20,
decimals: 18,
primaryColor: 'rgb(54, 50, 60)',
diff --git a/packages/instant/src/index.umd.ts b/packages/instant/src/index.umd.ts
index d4eca177d..fe78ce401 100644
--- a/packages/instant/src/index.umd.ts
+++ b/packages/instant/src/index.umd.ts
@@ -1,10 +1,8 @@
import * as React from 'react';
import * as ReactDOM from 'react-dom';
-import { ZeroExInstant } from './index';
+import { ZeroExInstant, ZeroExInstantProps } from './index';
-export interface ZeroExInstantOptions {}
-
-export const render = (props: ZeroExInstantOptions, selector: string = '#zeroExInstantContainer') => {
+export const render = (props: ZeroExInstantProps, selector: string = '#zeroExInstantContainer') => {
ReactDOM.render(React.createElement(ZeroExInstant, props), document.querySelector(selector));
};
diff --git a/packages/instant/src/redux/actions.ts b/packages/instant/src/redux/actions.ts
index 7d07b4950..fe055b75f 100644
--- a/packages/instant/src/redux/actions.ts
+++ b/packages/instant/src/redux/actions.ts
@@ -25,6 +25,7 @@ export enum ActionTypes {
UPDATE_SELECTED_ASSET_AMOUNT = 'UPDATE_SELECTED_ASSET_AMOUNT',
UPDATE_SELECTED_ASSET_BUY_STATE = 'UPDATE_SELECTED_ASSET_BUY_STATE',
UPDATE_LATEST_BUY_QUOTE = 'UPDATE_LATEST_BUY_QUOTE',
+ UPDATE_SELECTED_ASSET = 'UPDATE_SELECTED_ASSET',
}
export const actions = {
@@ -33,4 +34,5 @@ export const actions = {
updateSelectedAssetBuyState: (buyState: AsyncProcessState) =>
createAction(ActionTypes.UPDATE_SELECTED_ASSET_BUY_STATE, buyState),
updateLatestBuyQuote: (buyQuote?: BuyQuote) => createAction(ActionTypes.UPDATE_LATEST_BUY_QUOTE, buyQuote),
+ updateSelectedAsset: (assetData?: string) => createAction(ActionTypes.UPDATE_SELECTED_ASSET, assetData),
};
diff --git a/packages/instant/src/redux/async_data.ts b/packages/instant/src/redux/async_data.ts
index 348838307..4ed89bdc3 100644
--- a/packages/instant/src/redux/async_data.ts
+++ b/packages/instant/src/redux/async_data.ts
@@ -3,10 +3,10 @@ import { coinbaseApi } from '../util/coinbase_api';
import { ActionTypes } from './actions';
-import { store } from './store';
+import { Store } from './store';
export const asyncData = {
- fetchAndDispatchToStore: async () => {
+ fetchAndDispatchToStore: async (store: Store) => {
let ethUsdPrice = BIG_NUMBER_ZERO;
try {
ethUsdPrice = await coinbaseApi.getEthUsdPrice();
diff --git a/packages/instant/src/redux/reducer.ts b/packages/instant/src/redux/reducer.ts
index adecf2ab7..9922131b4 100644
--- a/packages/instant/src/redux/reducer.ts
+++ b/packages/instant/src/redux/reducer.ts
@@ -1,14 +1,17 @@
-import { BuyQuote } from '@0xproject/asset-buyer';
+import { AssetBuyer, BuyQuote } from '@0xproject/asset-buyer';
+import { ObjectMap } from '@0xproject/types';
import { BigNumber } from '@0xproject/utils';
import * as _ from 'lodash';
-import { zrxAssetData } from '../constants';
-import { AsyncProcessState } from '../types';
+import { Asset, AssetMetaData, AsyncProcessState } from '../types';
+import { assetUtils } from '../util/asset';
import { Action, ActionTypes } from './actions';
export interface State {
- selectedAssetData?: string;
+ assetBuyer?: AssetBuyer;
+ assetMetaDataMap: ObjectMap<AssetMetaData>;
+ selectedAsset?: Asset;
selectedAssetAmount?: BigNumber;
selectedAssetBuyState: AsyncProcessState;
ethUsdPrice?: BigNumber;
@@ -16,14 +19,14 @@ export interface State {
}
export const INITIAL_STATE: State = {
- // TODO: Remove hardcoded zrxAssetData
- selectedAssetData: zrxAssetData,
selectedAssetAmount: undefined,
+ assetMetaDataMap: {},
selectedAssetBuyState: AsyncProcessState.NONE,
ethUsdPrice: undefined,
latestBuyQuote: undefined,
};
+// TODO: Figure out why there is an INITIAL_STATE key in the store...
export const reducer = (state: State = INITIAL_STATE, action: Action): State => {
switch (action.type) {
case ActionTypes.UPDATE_ETH_USD_PRICE:
@@ -46,6 +49,16 @@ export const reducer = (state: State = INITIAL_STATE, action: Action): State =>
...state,
selectedAssetBuyState: action.data,
};
+ case ActionTypes.UPDATE_SELECTED_ASSET:
+ const newSelectedAssetData = action.data;
+ let newSelectedAsset: Asset | undefined;
+ if (!_.isUndefined(newSelectedAssetData)) {
+ newSelectedAsset = assetUtils.createAssetFromAssetData(newSelectedAssetData, state.assetMetaDataMap);
+ }
+ return {
+ ...state,
+ selectedAsset: newSelectedAsset,
+ };
default:
return state;
}
diff --git a/packages/instant/src/redux/store.ts b/packages/instant/src/redux/store.ts
index fcd19f9a8..fc943f1be 100644
--- a/packages/instant/src/redux/store.ts
+++ b/packages/instant/src/redux/store.ts
@@ -1,6 +1,16 @@
import * as _ from 'lodash';
import { createStore, Store as ReduxStore } from 'redux';
-import { reducer, State } from './reducer';
+import { INITIAL_STATE, reducer, State } from './reducer';
-export const store: ReduxStore<State> = createStore(reducer);
+export type Store = ReduxStore<State>;
+
+export const store = {
+ create: (withState: Partial<State>): Store => {
+ const allInitialState = {
+ INITIAL_STATE,
+ ...withState,
+ };
+ return createStore(reducer, allInitialState);
+ },
+};
diff --git a/packages/instant/src/types.ts b/packages/instant/src/types.ts
index bf3ee392f..867605573 100644
--- a/packages/instant/src/types.ts
+++ b/packages/instant/src/types.ts
@@ -22,12 +22,34 @@ export interface ERC20AssetMetaData {
export interface ERC721AssetMetaData {
assetProxyId: AssetProxyId.ERC721;
name: string;
+ representationUrl?: string;
primaryColor?: string;
}
export type AssetMetaData = ERC20AssetMetaData | ERC721AssetMetaData;
+export interface ERC20Asset {
+ assetProxyId: AssetProxyId.ERC20;
+ assetData: string;
+ metaData: ERC20AssetMetaData;
+}
+
+export interface ERC721Asset {
+ assetProxyId: AssetProxyId.ERC721;
+ assetData: string;
+ metaData: ERC721AssetMetaData;
+}
+export interface Asset {
+ assetProxyId: AssetProxyId;
+ assetData: string;
+ metaData: AssetMetaData;
+}
+
export enum Network {
Kovan = 42,
Mainnet = 1,
}
+
+export enum ZeroExInstantError {
+ AssetMetaDataNotAvailable = 'ASSET_META_DATA_NOT_AVAILABLE',
+}
diff --git a/packages/instant/src/util/asset.ts b/packages/instant/src/util/asset.ts
new file mode 100644
index 000000000..ec22276ae
--- /dev/null
+++ b/packages/instant/src/util/asset.ts
@@ -0,0 +1,34 @@
+import { assetDataUtils } from '@0xproject/order-utils';
+import { AssetProxyId, ObjectMap } from '@0xproject/types';
+import * as _ from 'lodash';
+
+import { assetDataNetworkMapping } from '../data/asset_data_network_mapping';
+import { Asset, AssetMetaData, Network, ZeroExInstantError } from '../types';
+
+export const assetUtils = {
+ createAssetFromAssetData: (assetData: string, assetMetaDataMap: ObjectMap<AssetMetaData>): Asset => {
+ return {
+ assetProxyId: assetDataUtils.decodeAssetProxyId(assetData),
+ assetData,
+ metaData: assetUtils.getMetaDataOrThrow(assetData, assetMetaDataMap),
+ };
+ },
+ getMetaDataOrThrow: (
+ assetData: string,
+ metaDataMap: ObjectMap<AssetMetaData>,
+ network: Network = Network.Mainnet,
+ ): AssetMetaData => {
+ let mainnetAssetData: string | undefined = assetData;
+ if (network !== Network.Mainnet) {
+ mainnetAssetData = assetDataNetworkMapping.getAssociatedAssetDataIfExists(assetData, network);
+ }
+ if (_.isUndefined(mainnetAssetData)) {
+ throw new Error(ZeroExInstantError.AssetMetaDataNotAvailable);
+ }
+ const metaData = metaDataMap[mainnetAssetData];
+ if (_.isUndefined(metaData)) {
+ throw new Error();
+ }
+ return metaData;
+ },
+};
diff --git a/packages/instant/src/util/asset_buyer.ts b/packages/instant/src/util/asset_buyer.ts
deleted file mode 100644
index 27d66d600..000000000
--- a/packages/instant/src/util/asset_buyer.ts
+++ /dev/null
@@ -1,9 +0,0 @@
-import { AssetBuyer } from '@0xproject/asset-buyer';
-
-import { sraApiUrl } from '../constants';
-
-import { getProvider } from './provider';
-
-const provider = getProvider();
-
-export const assetBuyer = AssetBuyer.getAssetBuyerForStandardRelayerAPIUrl(provider, sraApiUrl);