Skip to content

Commit bc75f05

Browse files
committed
refactor: 💡 Request and Offer are use new types and structure
1 parent ad7bc93 commit bc75f05

File tree

1 file changed

+44
-132
lines changed

1 file changed

+44
-132
lines changed

‎src/shared/messages.ts

Lines changed: 44 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,25 @@
1-
import { AbstractSigner, TypedDataField, getAddress, Signature, verifyTypedData } from 'ethers';
1+
import {
2+
BigNumberish,
3+
AbstractSigner,
4+
TypedDataField,
5+
getAddress,
6+
Signature,
7+
verifyTypedData,
8+
} from 'ethers';
9+
import {
10+
BuildRequestOptions,
11+
CancelOption,
12+
GenericOfferOptions,
13+
GenericQuery,
14+
OfferData,
15+
PaymentOption,
16+
RequestData,
17+
UnsignedOfferPayload,
18+
} from './types.js';
219
import { ContractConfig } from '../utils/contract.js';
3-
import { hashObject } from '../utils/hash.js';
4-
import { uuid4 } from '../utils/uid.js';
5-
import { nowSec, parseSeconds } from '../utils/time.js';
6-
7-
/**
8-
* Generic message data type
9-
*/
10-
export interface GenericMessage {
11-
/** Unique message Id */
12-
id: string;
13-
/** Expiration time in seconds */
14-
expire: number;
15-
/** A number that reflects the version of the message */
16-
nonce: number;
17-
}
18-
19-
/**
20-
* Generic query type
21-
*/
22-
export type GenericQuery = Record<string, unknown>;
23-
24-
/**
25-
* Request data type
26-
*/
27-
export interface RequestData<CustomRequestQuery extends GenericQuery> extends GenericMessage {
28-
/** Request topic */
29-
topic: string;
30-
31-
/** Custom query validation schema */
32-
query: CustomRequestQuery;
33-
}
34-
35-
/**
36-
* buildRequest method options type
37-
*/
38-
export interface BuildRequestOptions<CustomRequestQuery extends GenericQuery> {
39-
/** Expiration time */
40-
expire: string | number;
41-
/** Nonce */
42-
nonce: number;
43-
/** Topic */
44-
topic: string;
45-
/** Request query */
46-
query: CustomRequestQuery;
47-
/** If allowed request Id override */
48-
idOverride?: string;
49-
}
20+
import { hashCancelOptionArray, hashObject, hashPaymentOptionArray } from '../utils/hash.js';
21+
import { randomSalt } from '../utils/uid.js';
22+
import { parseExpire } from '../utils/time.js';
5023

5124
/**
5225
* Builds a request
@@ -62,84 +35,14 @@ export const buildRequest = async <CustomRequestQuery extends GenericQuery>(
6235
const { expire, nonce, topic, query, idOverride } = requestOptions;
6336
// @todo Validate request options
6437
return {
65-
id: idOverride ?? uuid4(),
66-
expire: typeof expire === 'number' ? parseSeconds(expire) : nowSec() + parseSeconds(expire),
38+
id: idOverride ?? randomSalt(),
39+
expire: parseExpire(expire),
6740
nonce,
6841
topic,
6942
query,
7043
};
7144
};
7245

73-
/**
74-
* Offered payment option type
75-
*/
76-
export interface PaymentOption {
77-
/** Unique payment option Id */
78-
id: string;
79-
/** Asset price in WEI */
80-
price: string;
81-
/** ERC20 asset contract address */
82-
asset: string;
83-
}
84-
85-
/**
86-
* Offered cancellation option type
87-
*/
88-
export interface CancelOption {
89-
/** Seconds before checkIn */
90-
time: number;
91-
/** Percents of total sum */
92-
penalty: number;
93-
}
94-
95-
/**
96-
* Unsigned offer payload type
97-
*/
98-
export interface UnsignedOfferPayload {
99-
/** Unique supplier Id registered on the protocol contract */
100-
supplierId: string;
101-
/** Target network chain Id */
102-
chainId: number;
103-
/** <keccak256(request.hash())> */
104-
requestHash: string;
105-
/** <keccak256(JSON.stringify(offer.options))> */
106-
optionsHash: string;
107-
/** <keccak256(JSON.stringify(offer.payment))> */
108-
paymentHash: string;
109-
/** <keccak256(JSON.stringify(offer.cancel(sorted by time DESC) || []))> */
110-
cancelHash: string;
111-
/** makes the deal NFT transferable or not */
112-
transferable: boolean;
113-
/** check-in time in seconds */
114-
checkIn: number;
115-
}
116-
117-
/**
118-
* Generic offer is just an object with props type
119-
*/
120-
export type GenericOfferOptions = Record<string, unknown>;
121-
122-
/**
123-
* Offer data type
124-
*/
125-
export interface OfferData<
126-
CustomRequestQuery extends GenericQuery,
127-
CustomOfferOptions extends GenericOfferOptions,
128-
> extends GenericMessage {
129-
/** Copy of request */
130-
request: RequestData<CustomRequestQuery>;
131-
/** Offer options */
132-
options: CustomOfferOptions;
133-
/** Payment options */
134-
payment: PaymentOption[];
135-
/** Cancellation options */
136-
cancel: CancelOption[];
137-
/** Raw offer payload */
138-
payload: UnsignedOfferPayload;
139-
//** EIP-712 TypedSignature(UnsignedOffer) */
140-
signature: string;
141-
}
142-
14346
/**
14447
* EIP-712 JSON schema types for offer
14548
*/
@@ -194,7 +97,7 @@ export interface BuildOfferOptions<
19497
/** Unique supplier Id */
19598
supplierId: string;
19699
/** Expiration time: duration (string) or seconds (number) */
197-
expire: string | number;
100+
expire: string | BigNumberish;
198101
/** Request data */
199102
request: RequestData<CustomRequestQuery>;
200103
/** Offer options */
@@ -203,8 +106,10 @@ export interface BuildOfferOptions<
203106
payment: PaymentOption[];
204107
/** Offer cancellation options */
205108
cancel: CancelOption[];
206-
/** Check In time in seconds */
207-
checkIn: number;
109+
/** Check-in time in seconds */
110+
checkIn: BigNumberish;
111+
/** Check-out time in seconds */
112+
checkOut: BigNumberish;
208113
/** Transferrable offer flag */
209114
transferable?: boolean;
210115
/** The possibility to override an offer Id flag */
@@ -235,23 +140,30 @@ export const buildOffer = async <
235140
payment,
236141
cancel,
237142
checkIn,
143+
checkOut,
238144
transferable,
239145
signer,
240146
signatureOverride,
241147
idOverride,
242-
expire,
243148
} = offerOptions;
149+
let { expire } = offerOptions;
244150

245151
// @todo Validate offer options
246152

153+
const id = idOverride ?? randomSalt();
154+
expire = parseExpire(expire);
155+
247156
const unsignedOfferPayload: UnsignedOfferPayload = {
157+
id,
158+
expire,
248159
supplierId,
249-
chainId: Number(contract.chainId),
160+
chainId: BigInt(contract.chainId),
250161
requestHash: hashObject(request),
251162
optionsHash: hashObject(options),
252-
paymentHash: hashObject(payment),
253-
cancelHash: hashObject(cancel),
254-
checkIn,
163+
paymentHash: hashPaymentOptionArray(payment),
164+
cancelHash: hashCancelOptionArray(cancel),
165+
checkIn: BigInt(checkIn),
166+
checkOut: BigInt(checkOut),
255167
transferable: transferable ?? true,
256168
};
257169

@@ -262,7 +174,7 @@ export const buildOffer = async <
262174
{
263175
name: contract.name,
264176
version: contract.version,
265-
chainId: contract.chainId,
177+
chainId: BigInt(contract.chainId),
266178
verifyingContract: contract.address,
267179
},
268180
offerEip712Types,
@@ -275,9 +187,9 @@ export const buildOffer = async <
275187
}
276188

277189
return {
278-
id: idOverride ?? uuid4(),
279-
expire: typeof expire === 'number' ? parseSeconds(expire) : nowSec() + parseSeconds(expire),
280-
nonce: 1,
190+
id,
191+
expire,
192+
nonce: BigInt(1),
281193
request,
282194
options,
283195
payment,

0 commit comments

Comments
 (0)