Skip to content

Commit 7d5d147

Browse files
authored
Merge pull request #25 from trader-xyz/feat/serialize-order-on-build
Serializes order on buildOrder function
2 parents 7984548 + 0d46a3d commit 7d5d147

File tree

5 files changed

+128
-56
lines changed

5 files changed

+128
-56
lines changed

src/sdk/v4/NftSwapV4.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import type {
4242
ApprovalOverrides,
4343
FillOrderOverrides,
4444
NftOrderV4,
45+
NftOrderV4Serialized,
4546
OrderStructOptionsCommonStrict,
4647
SignedNftOrderV4,
4748
SigningOptions,
@@ -71,7 +72,7 @@ export interface INftSwapV4 extends BaseNftSwap {
7172
sellOrBuyNft: 'sell' | 'buy',
7273
makerAddress: string,
7374
userConfig?: Partial<OrderStructOptionsCommonStrict>
74-
) => NftOrderV4;
75+
) => NftOrderV4Serialized;
7576
loadApprovalStatus: (
7677
asset: SwappableAsset,
7778
walletAddress: string,
@@ -225,25 +226,25 @@ class NftSwapV4 implements INftSwapV4 {
225226
takerAsset: UserFacingERC20AssetDataSerialized,
226227
makerAddress: string,
227228
orderConfig?: Partial<OrderStructOptionsCommonStrict>
228-
): NftOrderV4;
229+
): NftOrderV4Serialized;
229230
buildOrder(
230231
makerAsset: UserFacingERC20AssetDataSerialized,
231232
takerAsset: UserFacingERC1155AssetDataSerializedNormalizedSingle,
232233
makerAddress: string,
233234
orderConfig?: Partial<OrderStructOptionsCommonStrict>
234-
): NftOrderV4;
235+
): NftOrderV4Serialized;
235236
buildOrder(
236237
makerAsset: UserFacingERC721AssetDataSerialized,
237238
takerAsset: UserFacingERC20AssetDataSerialized,
238239
makerAddress: string,
239240
orderConfig?: Partial<OrderStructOptionsCommonStrict>
240-
): NftOrderV4;
241+
): NftOrderV4Serialized;
241242
buildOrder(
242243
makerAsset: UserFacingERC20AssetDataSerialized,
243244
takerAsset: UserFacingERC721AssetDataSerialized,
244245
makerAddress: string,
245246
orderConfig?: Partial<OrderStructOptionsCommonStrict>
246-
): NftOrderV4;
247+
): NftOrderV4Serialized;
247248
buildOrder(
248249
makerAsset: SwappableAsset,
249250
takerAsset: SwappableAsset,
@@ -299,7 +300,7 @@ class NftSwapV4 implements INftSwapV4 {
299300
type: 'ERC721' | 'ERC1155';
300301
},
301302
makerAddress: string
302-
) => {
303+
): NftOrderV4Serialized => {
303304
return this.buildNftAndErc20Order(
304305
{
305306
...nftCollectionToBid,
@@ -322,7 +323,7 @@ class NftSwapV4 implements INftSwapV4 {
322323
sellOrBuyNft: 'sell' | 'buy' = 'sell',
323324
makerAddress: string,
324325
userConfig?: Partial<OrderStructOptionsCommonStrict>
325-
): NftOrderV4 => {
326+
): NftOrderV4Serialized => {
326327
const defaultConfig = { chainId: this.chainId, makerAddress: makerAddress };
327328
const config = { ...defaultConfig, ...userConfig };
328329

@@ -350,13 +351,13 @@ class NftSwapV4 implements INftSwapV4 {
350351
}
351352
};
352353

353-
signOrder = async (orderStruct: NftOrderV4): Promise<SignedNftOrderV4> => {
354+
signOrder = async (order: NftOrderV4): Promise<SignedNftOrderV4> => {
354355
if (!this.signer) {
355356
throw new Error('Signed not defined');
356357
}
357358

358359
const rawSignature = await signOrderWithEoaWallet(
359-
orderStruct,
360+
order,
360361
this.signer as unknown as TypedDataSigner,
361362
this.chainId,
362363
this.exchangeProxy.address
@@ -365,7 +366,7 @@ class NftSwapV4 implements INftSwapV4 {
365366
const ecSignature = parseRawSignature(rawSignature);
366367

367368
const signedOrder = {
368-
...orderStruct,
369+
...order,
369370
signature: {
370371
signatureType: 2,
371372
r: ecSignature.r,

src/sdk/v4/pure.ts

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import { UnexpectedAssetTypeError } from '../error';
1414
import type {
1515
ECSignature,
1616
ERC1155OrderStruct,
17+
ERC1155OrderStructSerialized,
1718
ERC721OrderStruct,
19+
ERC721OrderStructSerialized,
1820
NftOrderV4,
1921
OrderStructOptionsCommon,
2022
OrderStructOptionsCommonStrict,
@@ -328,29 +330,33 @@ export const generateErc721Order = (
328330
nft: UserFacingERC721AssetDataSerialized,
329331
erc20: UserFacingERC20AssetDataSerialized,
330332
orderData: Partial<OrderStructOptionsCommon> & OrderStructOptionsCommonStrict
331-
): ERC721OrderStruct => {
332-
const erc721Order: ERC721OrderStruct = {
333-
erc721Token: nft.tokenAddress,
333+
): ERC721OrderStructSerialized => {
334+
const erc721Order: ERC721OrderStructSerialized = {
335+
erc721Token: nft.tokenAddress.toLowerCase(),
334336
erc721TokenId: nft.tokenId,
335-
direction: orderData.direction,
336-
erc20Token: erc20.tokenAddress,
337+
direction: parseInt(orderData.direction.toString()), // KLUDGE(johnrjj) - There's some footgun here when only doing orderData.direction.toString(), need to parseInt it
338+
erc20Token: erc20.tokenAddress.toLowerCase(),
337339
erc20TokenAmount: erc20.amount,
338-
maker: orderData.maker,
340+
maker: orderData.maker.toLowerCase(),
339341
// Defaults not required...
340-
erc721TokenProperties: orderData.tokenProperties ?? [],
342+
erc721TokenProperties:
343+
orderData.tokenProperties?.map((property) => ({
344+
propertyData: property.propertyData,
345+
propertyValidator: property.propertyValidator,
346+
})) ?? [],
341347
fees:
342348
orderData.fees?.map((x) => {
343349
return {
344-
amount: x.amount,
345-
recipient: x.recipient,
346-
feeData: x.feeData ?? '0x',
350+
amount: x.amount.toString(),
351+
recipient: x.recipient.toLowerCase(),
352+
feeData: x.feeData?.toString() ?? '0x',
347353
};
348354
}) ?? [],
349355
expiry: orderData.expiry
350-
? getUnixTime(orderData.expiry)
351-
: INFINITE_TIMESTAMP_SEC,
352-
nonce: orderData.nonce ?? generateRandomNonce(),
353-
taker: orderData.taker ?? NULL_ADDRESS,
356+
? getUnixTime(orderData.expiry).toString()
357+
: INFINITE_TIMESTAMP_SEC.toString(),
358+
nonce: orderData.nonce?.toString() ?? generateRandomNonce(),
359+
taker: orderData.taker?.toLowerCase() ?? NULL_ADDRESS,
354360
};
355361

356362
return erc721Order;
@@ -360,30 +366,34 @@ export const generateErc1155Order = (
360366
nft: UserFacingERC1155AssetDataSerializedNormalizedSingle,
361367
erc20: UserFacingERC20AssetDataSerialized,
362368
orderData: Partial<OrderStructOptionsCommon> & OrderStructOptionsCommonStrict
363-
): ERC1155OrderStruct => {
364-
const erc1155Order: ERC1155OrderStruct = {
365-
erc1155Token: nft.tokenAddress,
369+
): ERC1155OrderStructSerialized => {
370+
const erc1155Order: ERC1155OrderStructSerialized = {
371+
erc1155Token: nft.tokenAddress.toLowerCase(),
366372
erc1155TokenId: nft.tokenId,
367373
erc1155TokenAmount: nft.amount ?? '1',
368-
direction: orderData.direction,
369-
erc20Token: erc20.tokenAddress,
374+
direction: parseInt(orderData.direction.toString()), // KLUDGE(johnrjj) - There's some footgun here when only doing orderData.direction.toString(), need to parseInt it
375+
erc20Token: erc20.tokenAddress.toLowerCase(),
370376
erc20TokenAmount: erc20.amount,
371-
maker: orderData.maker,
377+
maker: orderData.maker.toLowerCase(),
372378
// Defaults not required...
373-
erc1155TokenProperties: orderData.tokenProperties ?? [],
379+
erc1155TokenProperties:
380+
orderData.tokenProperties?.map((property) => ({
381+
propertyData: property.propertyData.toString(),
382+
propertyValidator: property.propertyValidator,
383+
})) ?? [],
374384
fees:
375-
orderData.fees?.map((x) => {
385+
orderData.fees?.map((fee) => {
376386
return {
377-
amount: x.amount,
378-
recipient: x.recipient,
379-
feeData: x.feeData ?? '0x',
387+
amount: fee.amount.toString(),
388+
recipient: fee.recipient.toLowerCase(),
389+
feeData: fee.feeData?.toString() ?? '0x',
380390
};
381391
}) ?? [],
382392
expiry: orderData.expiry
383-
? getUnixTime(orderData.expiry)
384-
: INFINITE_TIMESTAMP_SEC,
385-
nonce: orderData.nonce ?? generateRandomNonce(),
386-
taker: orderData.taker ?? NULL_ADDRESS,
393+
? getUnixTime(orderData.expiry).toString()
394+
: INFINITE_TIMESTAMP_SEC.toString(),
395+
nonce: orderData.nonce?.toString() ?? generateRandomNonce(),
396+
taker: orderData.taker?.toLowerCase() ?? NULL_ADDRESS,
387397
};
388398

389399
return erc1155Order;

src/sdk/v4/types.ts

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,23 @@ import type { IZeroEx } from '../../contracts';
66
export type FeeStruct = {
77
recipient: string;
88
amount: BigNumberish;
9-
feeData: BytesLike;
9+
feeData: string | Array<number>;
10+
};
11+
12+
export type FeeStructSerialized = {
13+
recipient: string;
14+
amount: string;
15+
feeData: string;
1016
};
1117

1218
export type PropertyStruct = {
1319
propertyValidator: string;
14-
propertyData: BytesLike;
20+
propertyData: string | Array<number>;
21+
};
22+
23+
export type PropertyStructSerialized = {
24+
propertyValidator: string;
25+
propertyData: string | Array<number>;
1526
};
1627

1728
export type ERC1155OrderStruct = {
@@ -29,6 +40,21 @@ export type ERC1155OrderStruct = {
2940
erc1155TokenAmount: BigNumberish;
3041
};
3142

43+
export type ERC1155OrderStructSerialized = {
44+
direction: number;
45+
maker: string;
46+
taker: string;
47+
expiry: string;
48+
nonce: string;
49+
erc20Token: string;
50+
erc20TokenAmount: string;
51+
fees: FeeStructSerialized[];
52+
erc1155Token: string;
53+
erc1155TokenId: string;
54+
erc1155TokenProperties: PropertyStructSerialized[];
55+
erc1155TokenAmount: string;
56+
};
57+
3258
export type ERC721OrderStruct = {
3359
direction: BigNumberish;
3460
maker: string;
@@ -43,6 +69,20 @@ export type ERC721OrderStruct = {
4369
erc721TokenProperties: PropertyStruct[];
4470
};
4571

72+
export type ERC721OrderStructSerialized = {
73+
direction: number;
74+
maker: string;
75+
taker: string;
76+
expiry: string;
77+
nonce: string;
78+
erc20Token: string;
79+
erc20TokenAmount: string;
80+
fees: FeeStructSerialized[];
81+
erc721Token: string;
82+
erc721TokenId: string;
83+
erc721TokenProperties: PropertyStructSerialized[];
84+
};
85+
4686
export type UserFacingFeeStruct = {
4787
recipient: string;
4888
amount: BigNumberish;
@@ -75,10 +115,6 @@ export interface OrderStructOptionsCommonStrict {
75115
tokenProperties?: PropertyStruct[];
76116
}
77117

78-
interface OrderStructPropertyOptions {
79-
tokenProperties: PropertyStruct[];
80-
}
81-
82118
export interface Fee {
83119
recipient: string;
84120
amount: BigNumber;
@@ -92,6 +128,10 @@ export interface Property {
92128

93129
export type NftOrderV4 = ERC1155OrderStruct | ERC721OrderStruct;
94130

131+
export type NftOrderV4Serialized =
132+
| ERC1155OrderStructSerialized
133+
| ERC721OrderStructSerialized;
134+
95135
export interface SignedERC721OrderStruct extends ERC721OrderStruct {
96136
signature: SignatureStruct;
97137
}
@@ -100,21 +140,42 @@ export interface SignedERC1155OrderStruct extends ERC1155OrderStruct {
100140
signature: SignatureStruct;
101141
}
102142

143+
export interface SignedERC721OrderStructSerialized
144+
extends ERC721OrderStructSerialized {
145+
signature: SignatureStructSerialized;
146+
}
147+
148+
export interface SignedERC1155OrderStructSerialized
149+
extends ERC1155OrderStructSerialized {
150+
signature: SignatureStructSerialized;
151+
}
152+
103153
export type SignedNftOrderV4 =
104154
| SignedERC721OrderStruct
105155
| SignedERC1155OrderStruct;
106156

157+
export type SignedNftOrderV4Serialized =
158+
| SignedERC721OrderStructSerialized
159+
| SignedERC1155OrderStructSerialized;
160+
107161
export type ECSignature = {
108-
v: BigNumberish;
109-
r: BytesLike;
110-
s: BytesLike;
162+
v: number;
163+
r: string;
164+
s: string;
111165
};
112166

113167
export type SignatureStruct = {
114-
signatureType: BigNumberish; // 2 for EIP-712
115-
v: BigNumberish;
116-
r: BytesLike;
117-
s: BytesLike;
168+
signatureType: number; // 2 for EIP-712
169+
v: number;
170+
r: string;
171+
s: string;
172+
};
173+
174+
export type SignatureStructSerialized = {
175+
signatureType: number; // 2 for EIP-712
176+
v: number;
177+
r: string;
178+
s: string;
118179
};
119180

120181
export interface ApprovalOverrides {

test/v4/collection-orders.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ describe('NFTSwapV4', () => {
9797
// {
9898
// fillOrderWithNativeTokenInsteadOfWrappedToken: false,
9999
// tokenIdToSellForCollectionOrder: '11045',
100-
// },
101-
// { gasLimit: '500000' }
100+
// }
101+
// // { gasLimit: '500000' }
102102
// );
103103
// const txReceipt = await fillTx.wait();
104104
// console.log('erc721 fill tx', txReceipt.transactionHash);

test/v4/fees.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ describe('NFTSwapV4', () => {
9393
)) as SignedERC721OrderStruct;
9494

9595
expect(signedOrder.fees[0].recipient).toEqual(
96-
'0xaaa1388cD71e88Ae3D8432f16bed3c603a58aD34'
96+
'0xaaa1388cD71e88Ae3D8432f16bed3c603a58aD34'.toLowerCase()
9797
);
9898
// console.log('erc721 signatuee', signedOrder.signature);
9999
// expect(signedOrder.signature.signatureType.toString()).toEqual('2');
@@ -124,7 +124,7 @@ describe('NFTSwapV4', () => {
124124
// );
125125

126126
// Uncomment to actually fill order
127-
// const tx = await nftSwapperMaker.fillSignedOrder(signedOrder)//, undefined, { gasLimit: '500000'})
127+
// const tx = await nftSwapperMaker.fillSignedOrder(signedOrder); //, undefined, { gasLimit: '500000'})
128128

129129
// const txReceipt = await tx.wait();
130130
// expect(txReceipt.transactionHash).toBeTruthy();

0 commit comments

Comments
 (0)