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
|
import { assetProxyUtils, generatePseudoRandomSalt } from '@0xproject/order-utils';
import { Order } from '@0xproject/types';
import { BigNumber, errorUtils } from '@0xproject/utils';
import { constants } from './constants';
import {
AssetDataScenario,
ERC721Token,
ExpirationTimeSecondsScenario,
FeeRecipientAddressScenario,
OrderAmountScenario,
} from './types';
const TEN_UNITS_EIGHTEEN_DECIMALS = new BigNumber(10000000000000000000);
const POINT_ONE_UNITS_EIGHTEEN_DECIMALS = new BigNumber(100000000000000000);
const TEN_UNITS_FIVE_DECIMALS = new BigNumber(1000000);
const ONE_NFT_UNIT = new BigNumber(1);
const TEN_MINUTES_MS = 1000 * 60 * 10;
export const combinatorialUtils = {
generateOrder(
userAddresses: string[],
zrxAddress: string,
nonZrxERC20EighteenDecimalTokenAddress: string,
erc20FiveDecimalTokenAddress: string,
erc721Token: ERC721Token,
exchangeAddress: string,
feeRecipientScenario: FeeRecipientAddressScenario,
makerAssetAmountScenario: OrderAmountScenario,
takerAssetAmountScenario: OrderAmountScenario,
makerFeeScenario: OrderAmountScenario,
takerFeeScenario: OrderAmountScenario,
expirationTimeSecondsScenario: ExpirationTimeSecondsScenario,
makerAssetDataScenario: AssetDataScenario,
takerAssetDataScenario: AssetDataScenario,
): Order {
let feeRecipientAddress;
let makerAssetAmount;
let takerAssetAmount;
let makerFee;
let takerFee;
let expirationTimeSeconds;
let makerAssetData;
let takerAssetData;
switch (feeRecipientScenario) {
case FeeRecipientAddressScenario.BurnAddress:
feeRecipientAddress = constants.NULL_ADDRESS;
break;
case FeeRecipientAddressScenario.EthUserAddress:
feeRecipientAddress = userAddresses[4];
break;
default:
throw errorUtils.spawnSwitchErr('FeeRecipientAddressScenario', feeRecipientScenario);
}
const invalidAssetProxyIdHex = '0A';
switch (makerAssetDataScenario) {
case AssetDataScenario.ZRXFeeToken:
makerAssetData = assetProxyUtils.encodeERC20ProxyData(zrxAddress);
break;
case AssetDataScenario.ERC20NonZRXEighteenDecimals:
makerAssetData = assetProxyUtils.encodeERC20ProxyData(nonZrxERC20EighteenDecimalTokenAddress);
break;
case AssetDataScenario.ERC20FiveDecimals:
makerAssetData = assetProxyUtils.encodeERC20ProxyData(erc20FiveDecimalTokenAddress);
break;
case AssetDataScenario.ERC20InvalidAssetProxyId: {
const validAssetData = assetProxyUtils.encodeERC20ProxyData(nonZrxERC20EighteenDecimalTokenAddress);
makerAssetData = `${validAssetData.slice(0, -2)}${invalidAssetProxyIdHex}`;
break;
}
case AssetDataScenario.ERC721ValidAssetProxyId:
makerAssetData = assetProxyUtils.encodeERC721ProxyData(erc721Token.address, erc721Token.id);
break;
case AssetDataScenario.ERC721InvalidAssetProxyId: {
const validAssetData = assetProxyUtils.encodeERC721ProxyData(erc721Token.address, erc721Token.id);
makerAssetData = `${validAssetData.slice(0, -2)}${invalidAssetProxyIdHex}`;
break;
}
default:
throw errorUtils.spawnSwitchErr('AssetDataScenario', makerAssetDataScenario);
}
switch (takerAssetDataScenario) {
case AssetDataScenario.ZRXFeeToken:
takerAssetData = assetProxyUtils.encodeERC20ProxyData(zrxAddress);
break;
case AssetDataScenario.ERC20NonZRXEighteenDecimals:
takerAssetData = assetProxyUtils.encodeERC20ProxyData(nonZrxERC20EighteenDecimalTokenAddress);
break;
case AssetDataScenario.ERC20FiveDecimals:
takerAssetData = assetProxyUtils.encodeERC20ProxyData(erc20FiveDecimalTokenAddress);
break;
case AssetDataScenario.ERC20InvalidAssetProxyId: {
const validAssetData = assetProxyUtils.encodeERC20ProxyData(nonZrxERC20EighteenDecimalTokenAddress);
takerAssetData = `${validAssetData.slice(0, -2)}${invalidAssetProxyIdHex}`;
break;
}
case AssetDataScenario.ERC721ValidAssetProxyId:
takerAssetData = assetProxyUtils.encodeERC721ProxyData(erc721Token.address, erc721Token.id);
break;
case AssetDataScenario.ERC721InvalidAssetProxyId: {
const validAssetData = assetProxyUtils.encodeERC721ProxyData(erc721Token.address, erc721Token.id);
takerAssetData = `${validAssetData.slice(0, -2)}${invalidAssetProxyIdHex}`;
break;
}
default:
throw errorUtils.spawnSwitchErr('AssetDataScenario', takerAssetDataScenario);
}
switch (makerAssetAmountScenario) {
case OrderAmountScenario.NonZero:
switch (makerAssetDataScenario) {
case AssetDataScenario.ERC20NonZRXEighteenDecimals:
case AssetDataScenario.ERC20InvalidAssetProxyId:
makerAssetAmount = TEN_UNITS_EIGHTEEN_DECIMALS;
break;
case AssetDataScenario.ERC20FiveDecimals:
makerAssetAmount = TEN_UNITS_FIVE_DECIMALS;
break;
case AssetDataScenario.ERC721ValidAssetProxyId:
case AssetDataScenario.ERC721InvalidAssetProxyId:
makerAssetAmount = ONE_NFT_UNIT;
break;
default:
throw errorUtils.spawnSwitchErr('AssetDataScenario', makerAssetDataScenario);
}
break;
case OrderAmountScenario.Zero:
makerAssetAmount = new BigNumber(0);
break;
default:
throw errorUtils.spawnSwitchErr('OrderAmountScenario', makerAssetAmountScenario);
}
switch (takerAssetAmountScenario) {
case OrderAmountScenario.NonZero:
switch (takerAssetDataScenario) {
case AssetDataScenario.ERC20NonZRXEighteenDecimals:
case AssetDataScenario.ERC20InvalidAssetProxyId:
takerAssetAmount = TEN_UNITS_EIGHTEEN_DECIMALS;
break;
case AssetDataScenario.ERC20FiveDecimals:
takerAssetAmount = TEN_UNITS_FIVE_DECIMALS;
break;
case AssetDataScenario.ERC721ValidAssetProxyId:
case AssetDataScenario.ERC721InvalidAssetProxyId:
takerAssetAmount = ONE_NFT_UNIT;
break;
default:
throw errorUtils.spawnSwitchErr('AssetDataScenario', takerAssetDataScenario);
}
break;
case OrderAmountScenario.Zero:
takerAssetAmount = new BigNumber(0);
break;
default:
throw errorUtils.spawnSwitchErr('OrderAmountScenario', takerAssetAmountScenario);
}
switch (makerFeeScenario) {
case OrderAmountScenario.NonZero:
makerFee = POINT_ONE_UNITS_EIGHTEEN_DECIMALS;
break;
case OrderAmountScenario.Zero:
makerFee = new BigNumber(0);
break;
default:
throw errorUtils.spawnSwitchErr('OrderAmountScenario', makerFeeScenario);
}
switch (takerFeeScenario) {
case OrderAmountScenario.NonZero:
takerFee = POINT_ONE_UNITS_EIGHTEEN_DECIMALS;
break;
case OrderAmountScenario.Zero:
takerFee = new BigNumber(0);
break;
default:
throw errorUtils.spawnSwitchErr('OrderAmountScenario', takerFeeScenario);
}
switch (expirationTimeSecondsScenario) {
case ExpirationTimeSecondsScenario.InFuture:
expirationTimeSeconds = new BigNumber(Date.now() + TEN_MINUTES_MS);
break;
case ExpirationTimeSecondsScenario.InPast:
expirationTimeSeconds = new BigNumber(Date.now() - TEN_MINUTES_MS);
break;
default:
throw errorUtils.spawnSwitchErr('ExpirationTimeSecondsScenario', expirationTimeSecondsScenario);
}
const order: Order = {
senderAddress: constants.NULL_ADDRESS,
makerAddress: userAddresses[1],
takerAddress: userAddresses[2],
makerFee,
takerFee,
makerAssetAmount,
takerAssetAmount,
makerAssetData,
takerAssetData,
salt: generatePseudoRandomSalt(),
exchangeAddress,
feeRecipientAddress,
expirationTimeSeconds,
};
return order;
},
};
|