@@ -6,9 +6,11 @@ import {
6
6
WalletClient ,
7
7
PublicClient ,
8
8
zeroAddress ,
9
+ Chain ,
9
10
} from 'viem' ;
10
- import { Client , createCheckInOutSignature } from '../index.js' ;
11
+ import { ChainsConfigOption , createCheckInOutSignature } from '../index.js' ;
11
12
import {
13
+ Contracts ,
12
14
GenericOfferOptions ,
13
15
GenericQuery ,
14
16
OfferData ,
@@ -20,15 +22,16 @@ import {
20
22
} from '../shared/contracts.js' ;
21
23
import { Storage } from '../storage/index.js' ;
22
24
import { createLogger } from '../utils/logger.js' ;
25
+ import { parseSeconds } from '../utils/time.js' ;
23
26
24
- const logger = createLogger ( 'DealsRegistry ' ) ;
27
+ const logger = createLogger ( 'ClientDealsManager ' ) ;
25
28
26
29
/**
27
30
* Deals registry record
28
31
*/
29
32
export interface DealRecord <
30
- CustomRequestQuery extends GenericQuery ,
31
- CustomOfferOptions extends GenericOfferOptions ,
33
+ CustomRequestQuery extends GenericQuery = GenericQuery ,
34
+ CustomOfferOptions extends GenericOfferOptions = GenericOfferOptions ,
32
35
> {
33
36
/** Network chain Id */
34
37
chainId : number ;
@@ -53,16 +56,13 @@ export interface DealCurrentStatus {
53
56
status : DealStatus ;
54
57
}
55
58
56
- export interface DealsRegistryOptions <
57
- CustomRequestQuery extends GenericQuery ,
58
- CustomOfferOptions extends GenericOfferOptions ,
59
- > {
60
- /** Instance of Client */
61
- client : Client < CustomRequestQuery , CustomOfferOptions > ;
59
+ export interface DealsRegistryOptions extends ChainsConfigOption {
62
60
/** Instance of storage */
63
61
storage : Storage ;
64
62
/** Registry storage prefix */
65
63
prefix : string ;
64
+ /** Deal status check interval in seconds */
65
+ checkInterval : number | string ;
66
66
/** Public client */
67
67
publicClient : PublicClient ;
68
68
/** Wallet client */
@@ -73,8 +73,8 @@ export interface DealsRegistryOptions<
73
73
* Deals manager events interface
74
74
*/
75
75
export interface DealEvents <
76
- CustomRequestQuery extends GenericQuery ,
77
- CustomOfferOptions extends GenericOfferOptions ,
76
+ CustomRequestQuery extends GenericQuery = GenericQuery ,
77
+ CustomOfferOptions extends GenericOfferOptions = GenericOfferOptions ,
78
78
> {
79
79
/**
80
80
* @example
@@ -100,16 +100,15 @@ export interface DealEvents<
100
100
}
101
101
102
102
/**
103
- * Creates an instance of DealsRegistry .
103
+ * Creates an instance of ClientDealsManager .
104
104
*
105
105
* @param {DealsRegistryOptions<CustomRequestQuery, CustomOfferOptions> } options
106
- * @memberof RequestsRegistry
106
+ * @memberof ClientDealsManager
107
107
*/
108
- export class DealsRegistry <
109
- CustomRequestQuery extends GenericQuery ,
110
- CustomOfferOptions extends GenericOfferOptions ,
108
+ export class ClientDealsManager <
109
+ CustomRequestQuery extends GenericQuery = GenericQuery ,
110
+ CustomOfferOptions extends GenericOfferOptions = GenericOfferOptions ,
111
111
> extends EventEmitter < DealEvents < CustomRequestQuery , CustomOfferOptions > > {
112
- private client : Client < CustomRequestQuery , CustomOfferOptions > ;
113
112
private contractsManager : ProtocolContracts <
114
113
CustomRequestQuery ,
115
114
CustomOfferOptions
@@ -118,45 +117,54 @@ export class DealsRegistry<
118
117
private deals : Map <
119
118
string ,
120
119
DealRecord < CustomRequestQuery , CustomOfferOptions >
121
- > ; // id => Deal
122
- private storage ?: Storage ;
123
- private storageKey : string ;
120
+ > ;
121
+ private storage : Storage ;
122
+ private contracts : Contracts ;
123
+ private chain : Chain ;
124
+ private storageKeyPrefix : string ;
124
125
private checkInterval ?: NodeJS . Timer ;
125
126
private ongoingCheck = false ;
126
127
127
128
/**
128
- * Creates an instance of DealsRegistry .
129
+ * Creates an instance of ClientDealsManager .
129
130
*
130
- * @param {DealsRegistryOptions<CustomRequestQuery, CustomOfferOptions> } options
131
- * @memberof DealsRegistry
131
+ * @param {DealsRegistryOptions } options
132
+ * @memberof ClientDealsManager
132
133
*/
133
- constructor (
134
- options : DealsRegistryOptions < CustomRequestQuery , CustomOfferOptions > ,
135
- ) {
134
+ constructor ( options : DealsRegistryOptions ) {
136
135
super ( ) ;
137
136
138
- const { client, storage, prefix, publicClient, walletClient } = options ;
137
+ const {
138
+ storage,
139
+ contracts,
140
+ chain,
141
+ prefix,
142
+ checkInterval,
143
+ publicClient,
144
+ walletClient,
145
+ } = options ;
139
146
140
- this . client = client ;
141
147
this . contractsManager = new ProtocolContracts <
142
148
CustomRequestQuery ,
143
149
CustomOfferOptions
144
150
> ( {
145
- contracts : this . client . contracts ,
151
+ contracts,
146
152
publicClient,
147
153
walletClient,
148
154
} ) ;
155
+ this . chain = chain ;
156
+ this . contracts = contracts ;
149
157
this . deals = new Map <
150
158
string ,
151
159
DealRecord < CustomRequestQuery , CustomOfferOptions >
152
160
> ( ) ;
153
- this . storageKey = ` ${ prefix } _deals_records` ;
161
+ this . storageKeyPrefix = prefix ;
154
162
this . storage = storage ;
155
163
this . _storageUp ( )
156
164
. then ( ( ) => {
157
165
this . checkInterval = setInterval ( ( ) => {
158
166
this . _checkDealsStates ( ) . catch ( logger . error ) ;
159
- } , 5000 ) ;
167
+ } , Number ( parseSeconds ( checkInterval ) ) ) ;
160
168
} )
161
169
. catch ( logger . error ) ;
162
170
}
@@ -166,16 +174,16 @@ export class DealsRegistry<
166
174
*
167
175
* @private
168
176
* @returns {Promise<void> }
169
- * @memberof RequestsRegistry
177
+ * @memberof ClientDealsManager
170
178
*/
171
- private async _storageUp ( ) : Promise < void > {
179
+ protected async _storageUp ( ) : Promise < void > {
172
180
if ( ! this . storage ) {
173
181
throw new Error ( 'Invalid requests registry storage' ) ;
174
182
}
175
183
176
184
const rawRecords = await this . storage . get <
177
185
DealRecord < CustomRequestQuery , CustomOfferOptions > [ ]
178
- > ( this . storageKey ) ;
186
+ > ( this . storageKeyPrefix ) ;
179
187
180
188
if ( rawRecords ) {
181
189
for ( const dealRecord of rawRecords ) {
@@ -192,23 +200,23 @@ export class DealsRegistry<
192
200
* Stores class state to the storage
193
201
*
194
202
* @private
195
- * @memberof RequestsRegistry
203
+ * @memberof ClientDealsManager
196
204
*/
197
- private _storageDown ( ) : void {
205
+ protected _storageDown ( ) : void {
198
206
if ( ! this . storage ) {
199
207
throw new Error ( 'Invalid requests registry storage' ) ;
200
208
}
201
209
202
210
this . storage
203
- . set ( this . storageKey , Array . from ( this . deals . values ( ) ) )
211
+ . set ( this . storageKeyPrefix , Array . from ( this . deals . values ( ) ) )
204
212
. catch ( logger . error ) ;
205
213
}
206
214
207
215
/**
208
216
* Checks and updates state of all deals records
209
217
*
210
218
* @private
211
- * @memberof DealsRegistry
219
+ * @memberof ClientDealsManager
212
220
*/
213
221
private async _checkDealsStates ( ) : Promise < void > {
214
222
if ( this . ongoingCheck ) {
@@ -254,7 +262,7 @@ export class DealsRegistry<
254
262
* @private
255
263
* @param {OfferData<CustomRequestQuery, CustomOfferOptions> } offer Offer data object
256
264
* @returns {Promise<DealRecord<CustomRequestQuery, CustomOfferOptions>> }
257
- * @memberof DealsRegistry
265
+ * @memberof ClientDealsManager
258
266
*/
259
267
private async _buildDealRecord (
260
268
offer : OfferData < CustomRequestQuery , CustomOfferOptions > ,
@@ -269,7 +277,7 @@ export class DealsRegistry<
269
277
270
278
// Preparing deal record for registry
271
279
const dealRecord : DealRecord < CustomRequestQuery , CustomOfferOptions > = {
272
- chainId : this . client . chain . id ,
280
+ chainId : this . chain . id ,
273
281
created : created ,
274
282
offer,
275
283
retailerId : retailerId ,
@@ -302,7 +310,7 @@ export class DealsRegistry<
302
310
* @param {WalletClient } [walletClient] Wallet client
303
311
* @param {TxCallback } [txCallback] Optional transaction hash callback
304
312
* @returns {Promise<DealRecord<CustomRequestQuery, CustomOfferOptions>> } Deal record
305
- * @memberof DealsRegistry
313
+ * @memberof ClientDealsManager
306
314
*/
307
315
async create (
308
316
offer : OfferData < CustomRequestQuery , CustomOfferOptions > ,
@@ -343,7 +351,7 @@ export class DealsRegistry<
343
351
*
344
352
* @param {OfferData<CustomRequestQuery, CustomOfferOptions> } offer Offer data object
345
353
* @returns {Promise<DealRecord<CustomRequestQuery, CustomOfferOptions>> }
346
- * @memberof DealsRegistry
354
+ * @memberof ClientDealsManager
347
355
*/
348
356
async get (
349
357
offer : OfferData < CustomRequestQuery , CustomOfferOptions > ,
@@ -361,7 +369,7 @@ export class DealsRegistry<
361
369
* Returns all an up-to-date deal records
362
370
*
363
371
* @returns {Promise<DealRecord<CustomRequestQuery, CustomOfferOptions>[]> }
364
- * @memberof DealsRegistry
372
+ * @memberof ClientDealsManager
365
373
*/
366
374
async getAll ( ) : Promise <
367
375
DealRecord < CustomRequestQuery , CustomOfferOptions > [ ]
@@ -386,7 +394,7 @@ export class DealsRegistry<
386
394
* @param {WalletClient } [walletClient] Wallet client
387
395
* @param {TxCallback } [txCallback] Optional tx hash callback
388
396
* @returns {Promise<void> }
389
- * @memberof DealsRegistry
397
+ * @memberof ClientDealsManager
390
398
*/
391
399
async cancel (
392
400
offer : OfferData < CustomRequestQuery , CustomOfferOptions > ,
@@ -428,7 +436,7 @@ export class DealsRegistry<
428
436
* @param {WalletClient } [walletClient] Wallet client
429
437
* @param {TxCallback } [txCallback] Optional tx hash callback
430
438
* @returns {Promise<void> }
431
- * @memberof DealsRegistry
439
+ * @memberof ClientDealsManager
432
440
*/
433
441
async transfer (
434
442
offer : OfferData < CustomRequestQuery , CustomOfferOptions > ,
@@ -468,7 +476,7 @@ export class DealsRegistry<
468
476
* @param {WalletClient } walletClient
469
477
* @param {TxCallback } [txCallback] Optional tx hash callback
470
478
* @returns {Promise<void> }
471
- * @memberof DealsRegistry
479
+ * @memberof ClientDealsManager
472
480
*/
473
481
async checkIn (
474
482
offer : OfferData < CustomRequestQuery , CustomOfferOptions > ,
@@ -491,10 +499,10 @@ export class DealsRegistry<
491
499
const buyerSignature = await createCheckInOutSignature ( {
492
500
offerId : dealRecord . offer . payload . id ,
493
501
domain : {
494
- chainId : this . client . chain . id ,
495
- name : this . client . contracts . market . name ,
496
- version : this . client . contracts . market . version ,
497
- verifyingContract : this . client . contracts . market . address ,
502
+ chainId : this . chain . id ,
503
+ name : this . contracts . market . name ,
504
+ version : this . contracts . market . version ,
505
+ verifyingContract : this . contracts . market . address ,
498
506
} ,
499
507
account : walletClient . account as unknown as HDAccount ,
500
508
} ) ;
0 commit comments