This repository has been archived by the owner on Jan 18, 2023. It is now read-only.
/
zeroExOrderDataHandlerMock.spec.ts
244 lines (191 loc) Β· 7.63 KB
/
zeroExOrderDataHandlerMock.spec.ts
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
require('module-alias/register');
import * as _ from 'lodash';
import * as chai from 'chai';
import * as setProtocolUtils from 'set-protocol-utils';
import { Address, Bytes } from 'set-protocol-utils';
import { assetDataUtils } from '@0xproject/order-utils';
import { BigNumber } from 'bignumber.js';
import { Order as ZeroExOrder } from '@0xproject/types';
import ChaiSetup from '@utils/chaiSetup';
import { BigNumberSetup } from '@utils/bigNumberSetup';
import { ZeroExOrderDataHandlerMockContract } from '@utils/contracts';
import { Blockchain } from '@utils/blockchain';
import { ether } from '@utils/units';
import { getWeb3 } from '@utils/web3Helper';
import { LibraryMockWrapper } from '@utils/wrappers/libraryMockWrapper';
BigNumberSetup.configure();
ChaiSetup.configure();
const web3 = getWeb3();
const { expect } = chai;
const { SetProtocolTestUtils: SetTestUtils, SetProtocolUtils: SetUtils } = setProtocolUtils;
const blockchain = new Blockchain(web3);
contract('ZeroExOrderDataHandlerMock', accounts => {
const [
ownerAccount,
takerAccount,
feeRecipientAddress,
senderAccount,
makerTokenAddress,
takerTokenAddress,
] = accounts;
const libraryMockWrapper: LibraryMockWrapper = new LibraryMockWrapper(ownerAccount);
let zeroExExchangeWrapper: ZeroExOrderDataHandlerMockContract;
let zeroExOrder: ZeroExOrder;
let senderAddress: Address;
let makerAddress: Address;
let takerAddress: Address;
let makerFee: BigNumber;
let takerFee: BigNumber;
let makerAssetAmount: BigNumber;
let takerAssetAmount: BigNumber;
let makerAssetData: Bytes;
let takerAssetData: Bytes;
let expirationTimeSeconds: BigNumber;
let salt: BigNumber;
let signature: Bytes;
let fillAmount: BigNumber;
let zeroExWrapperOrderData: Bytes;
beforeEach(async () => {
await blockchain.saveSnapshotAsync();
zeroExExchangeWrapper = await libraryMockWrapper.deployZeroExOrderDataHandlerLibraryAsync();
zeroExOrder = SetTestUtils.generateZeroExOrder(
senderAddress || senderAccount,
makerAddress || ownerAccount,
takerAddress || takerAccount,
makerFee || ether(1),
takerFee || ether(1),
makerAssetAmount || ether(1),
takerAssetAmount || ether(1),
makerTokenAddress,
takerTokenAddress,
salt || SetUtils.generateSalt(),
SetTestUtils.ZERO_EX_EXCHANGE_ADDRESS,
feeRecipientAddress,
expirationTimeSeconds || SetTestUtils.generateTimestamp(10),
);
makerAssetData = assetDataUtils.encodeERC20AssetData(makerTokenAddress);
takerAssetData = assetDataUtils.encodeERC20AssetData(takerTokenAddress);
signature = '0x0012034334393842';
fillAmount = ether(1);
zeroExWrapperOrderData = SetTestUtils.generateZeroExExchangeWrapperOrder(zeroExOrder, signature, fillAmount);
});
afterEach(async () => {
await blockchain.revertAsync();
});
describe('#parseOrderDataHeader', async () => {
let subjectZeroExWrapperOrderData: Bytes;
let subjectOffset: BigNumber;
beforeEach(async () => {
subjectZeroExWrapperOrderData = zeroExWrapperOrderData;
subjectOffset = new BigNumber(0);
});
async function subject(): Promise<any> {
return zeroExExchangeWrapper.parseOrderHeader.callAsync(
subjectZeroExWrapperOrderData,
subjectOffset
);
}
it('correctly parses the signature length', async () => {
const [parsedSignatureLength] = await subject();
const expectedLength = SetUtils.numBytesFromHex(signature);
expect(parsedSignatureLength).to.bignumber.equal(expectedLength);
});
it('correctly parses the fill amount', async () => {
const [, parsedFillAmount] = await subject();
expect(parsedFillAmount).to.bignumber.equal(fillAmount);
});
it('correctly parses the maker token address', async () => {
const [, , parsedMakerTokenAddress] = await subject();
expect(parsedMakerTokenAddress).to.bignumber.equal(makerTokenAddress);
});
});
describe('#parseZeroExOrderData', async () => {
let subjectZeroExWrapperOrderData: Bytes;
let subjectMakerTokenAddress: Address;
let subjectTakerTokenAddress: Address;
let subjectOffset: BigNumber;
before(async () => {
senderAddress = senderAccount;
makerAddress = ownerAccount;
takerAddress = takerAccount;
makerFee = ether(1);
takerFee = ether(1);
makerAssetAmount = ether(1);
takerAssetAmount = ether(1);
expirationTimeSeconds = SetTestUtils.generateTimestamp(10);
salt = SetUtils.generateSalt();
});
beforeEach(async () => {
subjectZeroExWrapperOrderData = zeroExWrapperOrderData;
subjectMakerTokenAddress = makerTokenAddress;
subjectTakerTokenAddress = takerTokenAddress;
subjectOffset = new BigNumber(0);
});
async function subject(): Promise<any> {
return zeroExExchangeWrapper.parseZeroExOrder.callAsync(
subjectZeroExWrapperOrderData,
subjectMakerTokenAddress,
subjectTakerTokenAddress,
subjectOffset
);
}
it('should correctly parse the maker address', async () => {
const [addresses] = await subject();
const [parsedMakerAddress] = addresses;
expect(parsedMakerAddress).to.equal(makerAddress);
});
it('should correctly parse the taker address', async () => {
const [addresses] = await subject();
const [, parsedTakerAddress] = addresses;
expect(parsedTakerAddress).to.equal(takerAddress);
});
it('should correctly parse the fee recipient address', async () => {
const [addresses] = await subject();
const [, , parsedFeeRecipientAddress] = addresses;
expect(parsedFeeRecipientAddress).to.equal(feeRecipientAddress);
});
it('should correctly parse the sender address', async () => {
const [addresses] = await subject();
const [, , , parsedSenderAddress] = addresses;
expect(parsedSenderAddress).to.equal(senderAddress);
});
it('should correctly parse the maker asset amount', async () => {
const [, uints] = await subject();
const [parsedMakerAssetAmount] = uints;
expect(parsedMakerAssetAmount).to.be.bignumber.equal(makerAssetAmount);
});
it('should correctly parse the taker asset amount', async () => {
const [, uints] = await subject();
const [, parsedTakerAssetAmount] = uints;
expect(parsedTakerAssetAmount).to.be.bignumber.equal(takerAssetAmount);
});
it('should correctly parse the maker fee', async () => {
const [, uints] = await subject();
const [, , parsedMakerFee] = uints;
expect(parsedMakerFee).to.be.bignumber.equal(makerFee);
});
it('should correctly parse the taker fee', async () => {
const [, uints] = await subject();
const [, , , parsedTakerFee] = uints;
expect(parsedTakerFee).to.be.bignumber.equal(takerFee);
});
it('should correctly parse the expiration time', async () => {
const [, uints] = await subject();
const [, , , , parsedExpirationTime] = uints;
expect(parsedExpirationTime).to.be.bignumber.equal(expirationTimeSeconds);
});
it('should correctly parse the salt', async () => {
const [, uints] = await subject();
const [, , , , , parsedSalt] = uints;
expect(parsedSalt).to.be.bignumber.equal(salt);
});
it('should correctly parse the maker asset data', async () => {
const [, , parsedMakerAssetData] = await subject();
expect(parsedMakerAssetData).to.equal(makerAssetData);
});
it('should correctly parse the taker asset data', async () => {
const [, , , parsedTakerAssetData] = await subject();
expect(parsedTakerAssetData).to.equal(takerAssetData);
});
});
});