Skip to content

Commit 4601a62

Browse files
committed
refactor: 💡 ClientDealsManager extracted from Client
1 parent 76afaa0 commit 4601a62

File tree

2 files changed

+63
-320
lines changed

2 files changed

+63
-320
lines changed

‎src/client/dealsRegistry.ts renamed to ‎src/client/dealsManager.ts

Lines changed: 59 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import {
66
WalletClient,
77
PublicClient,
88
zeroAddress,
9+
Chain,
910
} from 'viem';
10-
import { Client, createCheckInOutSignature } from '../index.js';
11+
import { ChainsConfigOption, createCheckInOutSignature } from '../index.js';
1112
import {
13+
Contracts,
1214
GenericOfferOptions,
1315
GenericQuery,
1416
OfferData,
@@ -20,15 +22,16 @@ import {
2022
} from '../shared/contracts.js';
2123
import { Storage } from '../storage/index.js';
2224
import { createLogger } from '../utils/logger.js';
25+
import { parseSeconds } from '../utils/time.js';
2326

24-
const logger = createLogger('DealsRegistry');
27+
const logger = createLogger('ClientDealsManager');
2528

2629
/**
2730
* Deals registry record
2831
*/
2932
export interface DealRecord<
30-
CustomRequestQuery extends GenericQuery,
31-
CustomOfferOptions extends GenericOfferOptions,
33+
CustomRequestQuery extends GenericQuery = GenericQuery,
34+
CustomOfferOptions extends GenericOfferOptions = GenericOfferOptions,
3235
> {
3336
/** Network chain Id */
3437
chainId: number;
@@ -53,16 +56,13 @@ export interface DealCurrentStatus {
5356
status: DealStatus;
5457
}
5558

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 {
6260
/** Instance of storage */
6361
storage: Storage;
6462
/** Registry storage prefix */
6563
prefix: string;
64+
/** Deal status check interval in seconds */
65+
checkInterval: number | string;
6666
/** Public client */
6767
publicClient: PublicClient;
6868
/** Wallet client */
@@ -73,8 +73,8 @@ export interface DealsRegistryOptions<
7373
* Deals manager events interface
7474
*/
7575
export interface DealEvents<
76-
CustomRequestQuery extends GenericQuery,
77-
CustomOfferOptions extends GenericOfferOptions,
76+
CustomRequestQuery extends GenericQuery = GenericQuery,
77+
CustomOfferOptions extends GenericOfferOptions = GenericOfferOptions,
7878
> {
7979
/**
8080
* @example
@@ -100,16 +100,15 @@ export interface DealEvents<
100100
}
101101

102102
/**
103-
* Creates an instance of DealsRegistry.
103+
* Creates an instance of ClientDealsManager.
104104
*
105105
* @param {DealsRegistryOptions<CustomRequestQuery, CustomOfferOptions>} options
106-
* @memberof RequestsRegistry
106+
* @memberof ClientDealsManager
107107
*/
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,
111111
> extends EventEmitter<DealEvents<CustomRequestQuery, CustomOfferOptions>> {
112-
private client: Client<CustomRequestQuery, CustomOfferOptions>;
113112
private contractsManager: ProtocolContracts<
114113
CustomRequestQuery,
115114
CustomOfferOptions
@@ -118,45 +117,54 @@ export class DealsRegistry<
118117
private deals: Map<
119118
string,
120119
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;
124125
private checkInterval?: NodeJS.Timer;
125126
private ongoingCheck = false;
126127

127128
/**
128-
* Creates an instance of DealsRegistry.
129+
* Creates an instance of ClientDealsManager.
129130
*
130-
* @param {DealsRegistryOptions<CustomRequestQuery, CustomOfferOptions>} options
131-
* @memberof DealsRegistry
131+
* @param {DealsRegistryOptions} options
132+
* @memberof ClientDealsManager
132133
*/
133-
constructor(
134-
options: DealsRegistryOptions<CustomRequestQuery, CustomOfferOptions>,
135-
) {
134+
constructor(options: DealsRegistryOptions) {
136135
super();
137136

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;
139146

140-
this.client = client;
141147
this.contractsManager = new ProtocolContracts<
142148
CustomRequestQuery,
143149
CustomOfferOptions
144150
>({
145-
contracts: this.client.contracts,
151+
contracts,
146152
publicClient,
147153
walletClient,
148154
});
155+
this.chain = chain;
156+
this.contracts = contracts;
149157
this.deals = new Map<
150158
string,
151159
DealRecord<CustomRequestQuery, CustomOfferOptions>
152160
>();
153-
this.storageKey = `${prefix}_deals_records`;
161+
this.storageKeyPrefix = prefix;
154162
this.storage = storage;
155163
this._storageUp()
156164
.then(() => {
157165
this.checkInterval = setInterval(() => {
158166
this._checkDealsStates().catch(logger.error);
159-
}, 5000);
167+
}, Number(parseSeconds(checkInterval)));
160168
})
161169
.catch(logger.error);
162170
}
@@ -166,16 +174,16 @@ export class DealsRegistry<
166174
*
167175
* @private
168176
* @returns {Promise<void>}
169-
* @memberof RequestsRegistry
177+
* @memberof ClientDealsManager
170178
*/
171-
private async _storageUp(): Promise<void> {
179+
protected async _storageUp(): Promise<void> {
172180
if (!this.storage) {
173181
throw new Error('Invalid requests registry storage');
174182
}
175183

176184
const rawRecords = await this.storage.get<
177185
DealRecord<CustomRequestQuery, CustomOfferOptions>[]
178-
>(this.storageKey);
186+
>(this.storageKeyPrefix);
179187

180188
if (rawRecords) {
181189
for (const dealRecord of rawRecords) {
@@ -192,23 +200,23 @@ export class DealsRegistry<
192200
* Stores class state to the storage
193201
*
194202
* @private
195-
* @memberof RequestsRegistry
203+
* @memberof ClientDealsManager
196204
*/
197-
private _storageDown(): void {
205+
protected _storageDown(): void {
198206
if (!this.storage) {
199207
throw new Error('Invalid requests registry storage');
200208
}
201209

202210
this.storage
203-
.set(this.storageKey, Array.from(this.deals.values()))
211+
.set(this.storageKeyPrefix, Array.from(this.deals.values()))
204212
.catch(logger.error);
205213
}
206214

207215
/**
208216
* Checks and updates state of all deals records
209217
*
210218
* @private
211-
* @memberof DealsRegistry
219+
* @memberof ClientDealsManager
212220
*/
213221
private async _checkDealsStates(): Promise<void> {
214222
if (this.ongoingCheck) {
@@ -254,7 +262,7 @@ export class DealsRegistry<
254262
* @private
255263
* @param {OfferData<CustomRequestQuery, CustomOfferOptions>} offer Offer data object
256264
* @returns {Promise<DealRecord<CustomRequestQuery, CustomOfferOptions>>}
257-
* @memberof DealsRegistry
265+
* @memberof ClientDealsManager
258266
*/
259267
private async _buildDealRecord(
260268
offer: OfferData<CustomRequestQuery, CustomOfferOptions>,
@@ -269,7 +277,7 @@ export class DealsRegistry<
269277

270278
// Preparing deal record for registry
271279
const dealRecord: DealRecord<CustomRequestQuery, CustomOfferOptions> = {
272-
chainId: this.client.chain.id,
280+
chainId: this.chain.id,
273281
created: created,
274282
offer,
275283
retailerId: retailerId,
@@ -302,7 +310,7 @@ export class DealsRegistry<
302310
* @param {WalletClient} [walletClient] Wallet client
303311
* @param {TxCallback} [txCallback] Optional transaction hash callback
304312
* @returns {Promise<DealRecord<CustomRequestQuery, CustomOfferOptions>>} Deal record
305-
* @memberof DealsRegistry
313+
* @memberof ClientDealsManager
306314
*/
307315
async create(
308316
offer: OfferData<CustomRequestQuery, CustomOfferOptions>,
@@ -343,7 +351,7 @@ export class DealsRegistry<
343351
*
344352
* @param {OfferData<CustomRequestQuery, CustomOfferOptions>} offer Offer data object
345353
* @returns {Promise<DealRecord<CustomRequestQuery, CustomOfferOptions>>}
346-
* @memberof DealsRegistry
354+
* @memberof ClientDealsManager
347355
*/
348356
async get(
349357
offer: OfferData<CustomRequestQuery, CustomOfferOptions>,
@@ -361,7 +369,7 @@ export class DealsRegistry<
361369
* Returns all an up-to-date deal records
362370
*
363371
* @returns {Promise<DealRecord<CustomRequestQuery, CustomOfferOptions>[]>}
364-
* @memberof DealsRegistry
372+
* @memberof ClientDealsManager
365373
*/
366374
async getAll(): Promise<
367375
DealRecord<CustomRequestQuery, CustomOfferOptions>[]
@@ -386,7 +394,7 @@ export class DealsRegistry<
386394
* @param {WalletClient} [walletClient] Wallet client
387395
* @param {TxCallback} [txCallback] Optional tx hash callback
388396
* @returns {Promise<void>}
389-
* @memberof DealsRegistry
397+
* @memberof ClientDealsManager
390398
*/
391399
async cancel(
392400
offer: OfferData<CustomRequestQuery, CustomOfferOptions>,
@@ -428,7 +436,7 @@ export class DealsRegistry<
428436
* @param {WalletClient} [walletClient] Wallet client
429437
* @param {TxCallback} [txCallback] Optional tx hash callback
430438
* @returns {Promise<void>}
431-
* @memberof DealsRegistry
439+
* @memberof ClientDealsManager
432440
*/
433441
async transfer(
434442
offer: OfferData<CustomRequestQuery, CustomOfferOptions>,
@@ -468,7 +476,7 @@ export class DealsRegistry<
468476
* @param {WalletClient} walletClient
469477
* @param {TxCallback} [txCallback] Optional tx hash callback
470478
* @returns {Promise<void>}
471-
* @memberof DealsRegistry
479+
* @memberof ClientDealsManager
472480
*/
473481
async checkIn(
474482
offer: OfferData<CustomRequestQuery, CustomOfferOptions>,
@@ -491,10 +499,10 @@ export class DealsRegistry<
491499
const buyerSignature = await createCheckInOutSignature({
492500
offerId: dealRecord.offer.payload.id,
493501
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,
498506
},
499507
account: walletClient.account as unknown as HDAccount,
500508
});

0 commit comments

Comments
 (0)