1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
import { StandardRelayerAPIOrderProvider } from '@0x/asset-buyer';
import { getContractAddressesForNetworkOrThrow } from '@0x/contract-addresses';
import { assetDataUtils } from '@0x/order-utils';
import { ObjectMap } from '@0x/types';
import * as _ from 'lodash';
import * as React from 'react';
import { CheckMark } from 'ts/components/ui/check_mark';
import { Container } from 'ts/components/ui/container';
import { MultiSelect } from 'ts/components/ui/multi_select';
import { Select, SelectItemConfig } from 'ts/components/ui/select';
import { Spinner } from 'ts/components/ui/spinner';
import { Text } from 'ts/components/ui/text';
import { ConfigGeneratorAddressInput } from 'ts/pages/instant/config_generator_address_input';
import { FeePercentageSlider } from 'ts/pages/instant/fee_percentage_slider';
import { colors } from 'ts/style/colors';
import { WebsiteBackendTokenInfo } from 'ts/types';
import { backendClient } from 'ts/utils/backend_client';
import { constants } from 'ts/utils/constants';
import { assetMetaDataMap } from '../../../../instant/src/data/asset_meta_data_map';
import { ERC20AssetMetaData, ZeroExInstantBaseConfig } from '../../../../instant/src/types';
export interface ConfigGeneratorProps {
value: ZeroExInstantBaseConfig;
onConfigChange: (config: ZeroExInstantBaseConfig) => void;
}
export interface ConfigGeneratorState {
isLoadingAvailableTokens: boolean;
// Address to token info
availableTokens?: ObjectMap<ERC20AssetMetaData>;
}
const SRA_ENDPOINTS = [
'https://api.radarrelay.com/0x/v2/',
'https://api.sharkrelay.com/sra/v2/',
'https://sra.bamboorelay.com/0x/v2/',
];
export class ConfigGenerator extends React.Component<ConfigGeneratorProps, ConfigGeneratorState> {
public state: ConfigGeneratorState = {
isLoadingAvailableTokens: true,
};
public componentDidMount(): void {
this._setAvailableAssetsFromOrderProvider();
}
public componentDidUpdate(prevProps: ConfigGeneratorProps): void {
if (prevProps.value.orderSource !== this.props.value.orderSource) {
this._setAvailableAssetsFromOrderProvider();
}
}
public render(): React.ReactNode {
const { value } = this.props;
if (!_.isString(value.orderSource)) {
throw new Error('ConfigGenerator component only supports string values as an orderSource.');
}
return (
<Container minWidth="350px">
<ConfigGeneratorSection title="Standard relayer API endpoint">
<Select value={value.orderSource} items={this._generateItems()} />
</ConfigGeneratorSection>
<ConfigGeneratorSection {...this._getTokenSelectorProps()}>
{this._renderTokenMultiSelectOrSpinner()}
</ConfigGeneratorSection>
<ConfigGeneratorSection title="Transaction fee ETH address" marginBottom="10px" isOptional={true}>
<ConfigGeneratorAddressInput
value={value.affiliateInfo ? value.affiliateInfo.feeRecipient : ''}
onChange={this._handleAffiliateAddressChange}
/>
</ConfigGeneratorSection>
<ConfigGeneratorSection
title="Fee percentage"
actionText="Learn more"
onActionTextClick={this._handleAffiliatePercentageLearnMoreClick}
>
<FeePercentageSlider
value={value.affiliateInfo.feePercentage}
onChange={this._handleAffiliatePercentageChange}
/>
</ConfigGeneratorSection>
</Container>
);
}
private readonly _getTokenSelectorProps = (): ConfigGeneratorSectionProps => {
if (_.isUndefined(this.props.value.availableAssetDatas)) {
return {
title: 'What tokens can users buy?',
actionText: 'Unselect All',
onActionTextClick: this._handleUnselectAllClick,
};
}
return {
title: 'What tokens can users buy?',
actionText: 'Select All',
onActionTextClick: this._handleSelectAllClick,
};
};
private readonly _generateItems = (): SelectItemConfig[] => {
return _.map(SRA_ENDPOINTS, endpoint => ({
text: endpoint,
onClick: this._handleSRASelection.bind(this, endpoint),
}));
};
private readonly _handleAffiliatePercentageLearnMoreClick = (): void => {
window.open('/wiki#Learn-About-Affiliate-Fees', '_blank');
};
private readonly _handleSRASelection = (sraEndpoint: string) => {
const newConfig: ZeroExInstantBaseConfig = {
...this.props.value,
orderSource: sraEndpoint,
};
this.props.onConfigChange(newConfig);
};
private readonly _handleAffiliateAddressChange = (address: string) => {
const oldConfig: ZeroExInstantBaseConfig = this.props.value;
const newConfig: ZeroExInstantBaseConfig = {
...oldConfig,
affiliateInfo: {
feeRecipient: address,
feePercentage: oldConfig.affiliateInfo.feePercentage,
},
};
this.props.onConfigChange(newConfig);
};
private readonly _handleAffiliatePercentageChange = (value: number) => {
const oldConfig: ZeroExInstantBaseConfig = this.props.value;
const newConfig: ZeroExInstantBaseConfig = {
...oldConfig,
affiliateInfo: {
feeRecipient: oldConfig.affiliateInfo.feeRecipient,
feePercentage: value,
},
};
this.props.onConfigChange(newConfig);
};
private readonly _handleSelectAllClick = () => {
const newConfig: ZeroExInstantBaseConfig = {
...this.props.value,
availableAssetDatas: undefined,
};
this.props.onConfigChange(newConfig);
};
private readonly _handleUnselectAllClick = () => {
const newConfig: ZeroExInstantBaseConfig = {
...this.props.value,
availableAssetDatas: [],
};
this.props.onConfigChange(newConfig);
};
private readonly _handleTokenClick = (assetData: string) => {
const { value } = this.props;
let newAvailableAssetDatas: string[] = [];
const allKnownAssetDatas = _.keys(this.state.availableTokens);
const availableAssetDatas = value.availableAssetDatas;
if (_.isUndefined(availableAssetDatas)) {
// It being undefined means it's all tokens.
newAvailableAssetDatas = _.pull(allKnownAssetDatas, assetData);
} else if (!_.includes(availableAssetDatas, assetData)) {
// Add it
newAvailableAssetDatas = [...availableAssetDatas, assetData];
if (newAvailableAssetDatas.length === allKnownAssetDatas.length) {
// If all tokens are manually selected, just show none.
newAvailableAssetDatas = undefined;
}
} else {
// Remove it
newAvailableAssetDatas = _.pull(availableAssetDatas, assetData);
}
const newConfig: ZeroExInstantBaseConfig = {
...this.props.value,
availableAssetDatas: newAvailableAssetDatas,
};
this.props.onConfigChange(newConfig);
};
private readonly _setAvailableAssetsFromOrderProvider = async (): Promise<void> => {
const { value } = this.props;
if (!_.isUndefined(value.orderSource) && _.isString(value.orderSource)) {
this.setState({ isLoadingAvailableTokens: true });
const networkId = constants.NETWORK_ID_MAINNET;
const sraOrderProvider = new StandardRelayerAPIOrderProvider(value.orderSource, networkId);
const etherTokenAddress = getContractAddressesForNetworkOrThrow(networkId).etherToken;
const etherTokenAssetData = assetDataUtils.encodeERC20AssetData(etherTokenAddress);
const assetDatas = await sraOrderProvider.getAvailableMakerAssetDatasAsync(etherTokenAssetData);
const availableTokens = _.reduce(
assetDatas,
(acc, assetData) => {
const metaDataIfExists = assetMetaDataMap[assetData] as ERC20AssetMetaData;
if (metaDataIfExists) {
acc[assetData] = metaDataIfExists;
}
return acc;
},
{} as ObjectMap<ERC20AssetMetaData>,
);
this.setState({ availableTokens, isLoadingAvailableTokens: false });
}
};
private readonly _renderTokenMultiSelectOrSpinner = (): React.ReactNode => {
const { value } = this.props;
const { availableTokens, isLoadingAvailableTokens } = this.state;
const multiSelectHeight = '200px';
if (isLoadingAvailableTokens) {
return (
<Container
className="flex flex-column items-center justify-center"
height={multiSelectHeight}
backgroundColor={colors.white}
borderRadius="4px"
width="100%"
>
<Container position="relative" left="12px" marginBottom="20px">
<Spinner />
</Container>
<Text fontSize="16px">Loading...</Text>
</Container>
);
}
const availableAssetDatas = _.keys(availableTokens);
if (availableAssetDatas.length === 0) {
return (
<Container
className="flex flex-column items-center justify-center"
height={multiSelectHeight}
backgroundColor={colors.white}
borderRadius="4px"
width="100%"
>
<Text fontSize="16px">No tokens available. Try another endpoint?</Text>
</Container>
);
}
const items = _.map(_.keys(availableTokens), assetData => {
const metaData = availableTokens[assetData];
return {
value: assetData,
renderItemContent: (isSelected: boolean) => (
<Container className="flex items-center">
<Container marginRight="10px">
<CheckMark isChecked={isSelected} />
</Container>
<Text
fontSize="16px"
fontColor={isSelected ? colors.mediumBlue : colors.darkerGrey}
fontWeight={300}
>
<b>{metaData.symbol.toUpperCase()}</b> — {metaData.name}
</Text>
</Container>
),
onClick: this._handleTokenClick.bind(this, assetData),
};
});
return <MultiSelect items={items} selectedValues={value.availableAssetDatas} height={multiSelectHeight} />;
};
}
export interface ConfigGeneratorSectionProps {
title: string;
actionText?: string;
onActionTextClick?: () => void;
isOptional?: boolean;
marginBottom?: string;
}
export const ConfigGeneratorSection: React.StatelessComponent<ConfigGeneratorSectionProps> = ({
title,
actionText,
onActionTextClick,
isOptional,
marginBottom,
children,
}) => (
<Container marginBottom={marginBottom}>
<Container marginBottom="10px" className="flex justify-between items-center">
<Container>
<Text fontColor={colors.white} fontSize="16px" lineHeight="18px" display="inline">
{title}
</Text>
{isOptional && (
<Text fontColor={colors.grey} fontSize="16px" lineHeight="18px" display="inline">
{' '}
(optional)
</Text>
)}
</Container>
{actionText && (
<Text fontSize="12px" fontColor={colors.grey} onClick={onActionTextClick}>
{actionText}
</Text>
)}
</Container>
{children}
</Container>
);
ConfigGeneratorSection.defaultProps = {
marginBottom: '30px',
};
|