-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathrequest.ts
More file actions
820 lines (717 loc) · 26.2 KB
/
request.ts
File metadata and controls
820 lines (717 loc) · 26.2 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
import { EventEmitter } from 'events';
import {
DeclarativePaymentDetector,
EscrowERC20InfoRetriever,
} from '@requestnetwork/payment-detection';
import {
CurrencyTypes,
EncryptionTypes,
IdentityTypes,
LogTypes,
PaymentTypes,
RequestLogicTypes,
} from '@requestnetwork/types';
import * as Types from '../types';
import ContentDataExtension from './content-data-extension';
import localUtils from './utils';
import { erc20EscrowToPayArtifact } from '@requestnetwork/smart-contracts';
import { deepCopy, SimpleLogger } from '@requestnetwork/utils';
/**
* Class representing a request.
* Instances of this class can be accepted, paid, refunded, etc.
* Use the member function `getData` to access the properties of the Request.
*
* Requests should be created with `RequestNetwork.createRequest()`.
*/
export default class Request {
/**
* Unique ID of the request
*/
public readonly requestId: RequestLogicTypes.RequestId;
private requestLogic: RequestLogicTypes.IRequestLogic;
private paymentNetwork: PaymentTypes.IPaymentNetwork | null = null;
private contentDataExtension: ContentDataExtension | null;
private emitter: EventEmitter;
private logger: SimpleLogger = new SimpleLogger(LogTypes.LogLevel.WARN);
/**
* true if the creation emitted an event 'error'
*/
private confirmationErrorOccurredAtCreation = false;
/**
* Data of the request (see request-logic)
*/
private requestData: RequestLogicTypes.IRequest | null = null;
/**
* Pending data of the request (see request-logic)
*/
private pendingData: RequestLogicTypes.IPendingRequest | null = null;
/**
* Content data parsed from the extensions data
*/
private contentData: any | null = null;
/**
* Meta data of the request (e.g: where the data have been retrieved from)
*/
private requestMeta: RequestLogicTypes.IReturnMeta | null = null;
/**
* Balance and payments/refund events
*/
private balance: PaymentTypes.IBalanceWithEvents | null = null;
/**
* if true, skip the payment detection
*/
private skipPaymentDetection = false;
/**
* if true, do not send blockchain confirmation events (on creation, approval, etc.)
*/
private disableEvents = false;
/**
* A list of known tokens
*/
private currencyManager: CurrencyTypes.ICurrencyManager;
/**
* Information for an in-memory request, including transaction data, topics, and payment request data.
* This is used for requests that haven't been persisted yet, allowing for operations like payments
* before the request is stored in the data access layer.
*
* @property transactionData - Transaction data necessary for persisting the request later on.
* @property topics - Topics of the request, used for indexing and retrieval when persisting.
* @property requestData - Structured data primarily used for processing payments before the request is persisted.
*/
public readonly inMemoryInfo: RequestLogicTypes.IInMemoryInfo | null = null;
/**
* Creates an instance of Request
*
* @param requestLogic Instance of the request-logic layer
* @param requestId ID of the Request
* @param paymentNetwork Instance of a payment network to manage the request
* @param contentDataManager Instance of content data manager
* @param requestLogicCreateResult return from the first request creation (optimization)
* @param options options
*/
constructor(
requestId: RequestLogicTypes.RequestId,
requestLogic: RequestLogicTypes.IRequestLogic,
currencyManager: CurrencyTypes.ICurrencyManager,
options?: {
paymentNetwork?: PaymentTypes.IPaymentNetwork | null;
contentDataExtension?: ContentDataExtension | null;
requestLogicCreateResult?: RequestLogicTypes.IReturnCreateRequest;
skipPaymentDetection?: boolean;
disableEvents?: boolean;
inMemoryInfo?: RequestLogicTypes.IInMemoryInfo | null;
},
) {
this.requestLogic = requestLogic;
this.requestId = requestId;
this.contentDataExtension = options?.contentDataExtension || null;
this.paymentNetwork = options?.paymentNetwork || null;
this.emitter = new EventEmitter();
this.skipPaymentDetection = options?.skipPaymentDetection || false;
this.disableEvents = options?.disableEvents || false;
this.currencyManager = currencyManager;
this.inMemoryInfo = options?.inMemoryInfo || null;
if (options?.requestLogicCreateResult && !this.disableEvents) {
const originalEmitter = options.requestLogicCreateResult;
originalEmitter
.on('confirmed', async () => {
try {
this.emitter.emit('confirmed', await this.refresh());
} catch (error) {
originalEmitter.emit('error', error);
}
})
.on('error', (error) => {
this.confirmationErrorOccurredAtCreation = true;
this.emitter.emit('error', error);
});
}
}
/**
* Listen the confirmation of the creation
*
* @param type only "confirmed" event for now
* @param callback callback to call when confirmed event is risen
* @returns this
*/
public on<K extends keyof Types.IRequestEvents>(
event: K,
listener: Types.IRequestEvents[K],
): this {
this.emitter.on(event, listener);
return this;
}
/**
* Wait for the confirmation
*
* @returns the request data
*/
public waitForConfirmation(): Promise<Types.IRequestDataWithEvents> {
return new Promise((resolve, reject): any => {
this.on('confirmed', resolve);
this.on('error', reject);
});
}
/**
* Accepts a request
*
* @param signerIdentity Identity of the signer. The identity type must be supported by the signature provider.
* @param refundInformation Refund information to add (any because it is specific to the payment network used by the request)
* @returns The updated request
*/
public async accept(
signerIdentity: IdentityTypes.IIdentity,
refundInformation?: any,
): Promise<Types.IRequestDataWithEvents> {
const extensionsData: any[] = [];
if (refundInformation) {
if (!this.paymentNetwork) {
throw new Error('Cannot add refund information without payment network');
}
extensionsData.push(
this.paymentNetwork.createExtensionsDataForAddRefundInformation(refundInformation),
);
}
const parameters: RequestLogicTypes.IAcceptParameters = {
extensionsData,
requestId: this.requestId,
};
const acceptResult = await this.requestLogic.acceptRequest(parameters, signerIdentity, true);
return this.handleRequestDataEvents(acceptResult);
}
/**
* Cancels a request
*
* @param signerIdentity Identity of the signer. The identity type must be supported by the signature provider.
* @param refundInformation refund information to add (any because it is specific to the payment network used by the request)
* @returns The updated request
*/
public async cancel(
signerIdentity: IdentityTypes.IIdentity,
refundInformation?: any,
): Promise<Types.IRequestDataWithEvents> {
const extensionsData: any[] = [];
if (refundInformation) {
if (!this.paymentNetwork) {
throw new Error('Cannot add refund information without payment network');
}
extensionsData.push(
this.paymentNetwork.createExtensionsDataForAddRefundInformation(refundInformation),
);
}
const parameters: RequestLogicTypes.ICancelParameters = {
extensionsData,
requestId: this.requestId,
};
const cancelResult = await this.requestLogic.cancelRequest(parameters, signerIdentity, true);
return this.handleRequestDataEvents(cancelResult);
}
/**
* Increases the expected amount of the request.
*
* @param deltaAmount Amount by which to increase the expected amount
* @param signerIdentity Identity of the signer. The identity type must be supported by the signature provider.
* @param refundInformation Refund information to add (any because it is specific to the payment network used by the request)
* @returns The updated request
*/
public async increaseExpectedAmountRequest(
deltaAmount: RequestLogicTypes.Amount,
signerIdentity: IdentityTypes.IIdentity,
refundInformation?: any,
): Promise<Types.IRequestDataWithEvents> {
const extensionsData: any[] = [];
if (refundInformation) {
if (!this.paymentNetwork) {
throw new Error('Cannot add refund information without payment network');
}
extensionsData.push(
this.paymentNetwork.createExtensionsDataForAddRefundInformation(refundInformation),
);
}
const parameters: RequestLogicTypes.IIncreaseExpectedAmountParameters = {
deltaAmount,
extensionsData,
requestId: this.requestId,
};
const increaseExpectedResult = await this.requestLogic.increaseExpectedAmountRequest(
parameters,
signerIdentity,
true,
);
return this.handleRequestDataEvents(increaseExpectedResult);
}
/**
* Reduces the expected amount of the request. This can be called by the payee e.g. to apply discounts or special offers.
*
* @param deltaAmount Amount by which to reduce the expected amount
* @param signerIdentity Identity of the signer. The identity type must be supported by the signature provider.
* @param paymentInformation Payment information to add (any because it is specific to the payment network used by the request)
* @returns The updated request
*/
public async reduceExpectedAmountRequest(
deltaAmount: RequestLogicTypes.Amount,
signerIdentity: IdentityTypes.IIdentity,
paymentInformation?: any,
): Promise<Types.IRequestDataWithEvents> {
const extensionsData: any[] = [];
if (paymentInformation) {
if (!this.paymentNetwork) {
throw new Error('Cannot add payment information without payment network');
}
extensionsData.push(
this.paymentNetwork.createExtensionsDataForAddPaymentInformation(paymentInformation),
);
}
const parameters: RequestLogicTypes.IReduceExpectedAmountParameters = {
deltaAmount,
extensionsData,
requestId: this.requestId,
};
const reduceExpectedResult = await this.requestLogic.reduceExpectedAmountRequest(
parameters,
signerIdentity,
true,
);
return this.handleRequestDataEvents(reduceExpectedResult);
}
/**
* Adds stakeholders to a request
*
* @param IEncryptionParameters encryptionParams list of addtional encryption parameters to encrypt the channel key with
* @param signerIdentity Identity of the signer. The identity type must be supported by the signature provider.
* @param refundInformation refund information to add (any because it is specific to the payment network used by the request)
* @returns The updated request
*/
public async addStakeholders(
encryptionParams: EncryptionTypes.IEncryptionParameters[],
signerIdentity: IdentityTypes.IIdentity,
refundInformation?: any,
): Promise<Types.IRequestDataWithEvents> {
const extensionsData: any[] = [];
if (refundInformation) {
if (!this.paymentNetwork) {
throw new Error('Cannot add refund information without payment network');
}
extensionsData.push(
this.paymentNetwork.createExtensionsDataForAddRefundInformation(refundInformation),
);
}
const parameters: RequestLogicTypes.IAddStakeholdersParameters = {
extensionsData,
requestId: this.requestId,
};
const addStakeholdersResult = await this.requestLogic.addStakeholders(
parameters,
signerIdentity,
encryptionParams,
true,
);
return this.handleRequestDataEvents(addStakeholdersResult);
}
/**
* Adds payment information
*
* @param paymentInformation Payment information to add (any because it is specific to the payment network used by the request)
* @param signerIdentity Identity of the signer. The identity type must be supported by the signature provider.
* @returns The updated request
*/
public async addPaymentInformation(
paymentInformation: any,
signerIdentity: IdentityTypes.IIdentity,
): Promise<Types.IRequestDataWithEvents> {
const extensionsData: any[] = [];
if (!this.paymentNetwork) {
throw new Error('Cannot add payment information without payment network');
}
extensionsData.push(
this.paymentNetwork.createExtensionsDataForAddPaymentInformation(paymentInformation),
);
const parameters: RequestLogicTypes.IAddExtensionsDataParameters = {
extensionsData,
requestId: this.requestId,
};
const addExtensionResult = await this.requestLogic.addExtensionsDataRequest(
parameters,
signerIdentity,
true,
);
return this.handleRequestDataEvents(addExtensionResult);
}
/**
* Adds refund information
*
* @param refundInformation Refund information to add (any because it is specific to the payment network used by the request)
* @param signerIdentity Identity of the signer. The identity type must be supported by the signature provider.
* @returns The updated request
*/
public async addRefundInformation(
refundInformation: any,
signerIdentity: IdentityTypes.IIdentity,
): Promise<Types.IRequestDataWithEvents> {
const extensionsData: any[] = [];
if (!this.paymentNetwork) {
throw new Error('Cannot add refund information without payment network');
}
extensionsData.push(
this.paymentNetwork.createExtensionsDataForAddRefundInformation(refundInformation),
);
const parameters: RequestLogicTypes.IAddExtensionsDataParameters = {
extensionsData,
requestId: this.requestId,
};
const addExtensionResult = await this.requestLogic.addExtensionsDataRequest(
parameters,
signerIdentity,
true,
);
return this.handleRequestDataEvents(addExtensionResult);
}
/**
* Declare a payment is sent for the declarative payment network
*
* @param amount Amount sent
* @param note Note from payer about the sent payment
* @param signerIdentity Identity of the signer. The identity type must be supported by the signature provider.
* @param txHash transaction hash
* @param network network of the transaction
* @returns The updated request
*/
public async declareSentPayment(
amount: RequestLogicTypes.Amount,
note: string,
signerIdentity: IdentityTypes.IIdentity,
txHash?: string,
network?: string,
): Promise<Types.IRequestDataWithEvents> {
const extensionsData: any[] = [];
if (!this.paymentNetwork) {
throw new Error('Cannot declare sent payment without payment network');
}
// We need to cast the object since IPaymentNetwork doesn't implement createExtensionsDataForDeclareSentPayment
const declarativePaymentNetwork = this.paymentNetwork as DeclarativePaymentDetector;
if (!declarativePaymentNetwork.createExtensionsDataForDeclareSentPayment) {
throw new Error('Cannot declare sent payment without declarative payment network');
}
extensionsData.push(
declarativePaymentNetwork.createExtensionsDataForDeclareSentPayment({
amount,
note,
txHash,
network,
}),
);
const parameters: RequestLogicTypes.IAddExtensionsDataParameters = {
extensionsData,
requestId: this.requestId,
};
const addExtensionResult = await this.requestLogic.addExtensionsDataRequest(
parameters,
signerIdentity,
true,
);
return this.handleRequestDataEvents(addExtensionResult);
}
/**
* Declare a refund is sent for the declarative payment network
*
* @param amount Amount sent
* @param note Note from payee about the sent refund
* @param signerIdentity Identity of the signer. The identity type must be supported by the signature provider.
* @param txHash transaction hash
* @param network network of the transaction
* @returns The updated request
*/
public async declareSentRefund(
amount: RequestLogicTypes.Amount,
note: string,
signerIdentity: IdentityTypes.IIdentity,
txHash?: string,
network?: string,
): Promise<Types.IRequestDataWithEvents> {
const extensionsData: any[] = [];
if (!this.paymentNetwork) {
throw new Error('Cannot declare sent refund without payment network');
}
// We need to cast the object since IPaymentNetwork doesn't implement createExtensionsDataForDeclareSentRefund
const declarativePaymentNetwork = this.paymentNetwork as DeclarativePaymentDetector;
if (!declarativePaymentNetwork.createExtensionsDataForDeclareSentRefund) {
throw new Error('Cannot declare sent refund without declarative payment network');
}
extensionsData.push(
declarativePaymentNetwork.createExtensionsDataForDeclareSentRefund({
amount,
note,
txHash,
network,
}),
);
const parameters: RequestLogicTypes.IAddExtensionsDataParameters = {
extensionsData,
requestId: this.requestId,
};
const addExtensionResult = await this.requestLogic.addExtensionsDataRequest(
parameters,
signerIdentity,
true,
);
return this.handleRequestDataEvents(addExtensionResult);
}
/**
* Declare a payment is received for the declarative payment network
*
* @param amount Amount received
* @param note Note from payee about the received payment
* @param signerIdentity Identity of the signer. The identity type must be supported by the signature provider.
* @param txHash transaction hash
* @param network network of the transaction
* @returns The updated request
*/
public async declareReceivedPayment(
amount: RequestLogicTypes.Amount,
note: string,
signerIdentity: IdentityTypes.IIdentity,
txHash?: string,
network?: string,
): Promise<Types.IRequestDataWithEvents> {
const extensionsData: any[] = [];
if (!this.paymentNetwork) {
throw new Error('Cannot declare received payment without payment network');
}
// We need to cast the object since IPaymentNetwork doesn't implement createExtensionsDataForDeclareReceivedPayment
const declarativePaymentNetwork = this.paymentNetwork as DeclarativePaymentDetector;
if (!declarativePaymentNetwork.createExtensionsDataForDeclareReceivedPayment) {
throw new Error('Cannot declare received payment without declarative payment network');
}
extensionsData.push(
declarativePaymentNetwork.createExtensionsDataForDeclareReceivedPayment({
amount,
note,
txHash,
network,
}),
);
const parameters: RequestLogicTypes.IAddExtensionsDataParameters = {
extensionsData,
requestId: this.requestId,
};
const addExtensionResult = await this.requestLogic.addExtensionsDataRequest(
parameters,
signerIdentity,
true,
);
return this.handleRequestDataEvents(addExtensionResult);
}
/**
* Declare a refund is received for the declarative payment network
*
* @param amount Amount received
* @param note Note from payer about the received refund
* @param signerIdentity Identity of the signer. The identity type must be supported by the signature provider.
* @param txHash transaction hash
* @param network network of the transaction
* @returns The updated request
*/
public async declareReceivedRefund(
amount: RequestLogicTypes.Amount,
note: string,
signerIdentity: IdentityTypes.IIdentity,
txHash?: string,
network?: string,
): Promise<Types.IRequestDataWithEvents> {
const extensionsData: any[] = [];
if (!this.paymentNetwork) {
throw new Error('Cannot declare received refund without payment network');
}
// We need to cast the object since IPaymentNetwork doesn't implement createExtensionsDataForDeclareReceivedRefund
const declarativePaymentNetwork = this.paymentNetwork as DeclarativePaymentDetector;
if (!declarativePaymentNetwork.createExtensionsDataForDeclareReceivedRefund) {
throw new Error('Cannot declare received refund without declarative payment network');
}
extensionsData.push(
declarativePaymentNetwork.createExtensionsDataForDeclareReceivedRefund({
amount,
note,
txHash,
network,
}),
);
const parameters: RequestLogicTypes.IAddExtensionsDataParameters = {
extensionsData,
requestId: this.requestId,
};
const addExtensionResult = await this.requestLogic.addExtensionsDataRequest(
parameters,
signerIdentity,
true,
);
return this.handleRequestDataEvents(addExtensionResult);
}
/**
* Add a delegate for the declarative payment network
*
* @param delegate Identity of the delegate
* @param signerIdentity Identity of the signer. The identity type must be supported by the signature provider.
* @returns The updated request
*/
public async addDeclarativeDelegate(
delegate: IdentityTypes.IIdentity,
signerIdentity: IdentityTypes.IIdentity,
): Promise<Types.IRequestDataWithEvents> {
const extensionsData: Types.Extension.IAction[] = [];
if (!this.paymentNetwork) {
throw new Error('Cannot declare delegate without payment network');
}
// We need to cast the object since IPaymentNetwork doesn't implement createExtensionsDataForDeclareReceivedRefund
const declarativePaymentNetwork = this.paymentNetwork as DeclarativePaymentDetector;
if (!declarativePaymentNetwork.createExtensionsDataForAddDelegate) {
throw new Error('Cannot declare delegate without declarative payment network');
}
extensionsData.push(
declarativePaymentNetwork.createExtensionsDataForAddDelegate({
delegate,
}),
);
const parameters: RequestLogicTypes.IAddExtensionsDataParameters = {
extensionsData,
requestId: this.requestId,
};
const addExtensionResult = await this.requestLogic.addExtensionsDataRequest(
parameters,
signerIdentity,
true,
);
return this.handleRequestDataEvents(addExtensionResult);
}
protected handleRequestDataEvents(
eventEmitter: EventEmitter,
): Promise<Types.IRequestDataWithEvents> {
// refresh the local request data
const requestDataPromise = this.refresh();
if (!this.disableEvents) {
eventEmitter
.on('confirmed', async () => {
try {
const requestData = await requestDataPromise;
requestData.emit('confirmed', await this.refresh());
} catch (error) {
eventEmitter.emit('error', error);
}
})
.on('error', (error) => {
this.emitter.emit('error', error);
});
}
return requestDataPromise;
}
/**
* Gets the request data
*
* @returns The updated request data
*/
public getData(): Types.IRequestDataWithEvents {
if (this.inMemoryInfo) {
return Object.assign(new EventEmitter(), {
...this.inMemoryInfo.requestData,
});
}
if (this.confirmationErrorOccurredAtCreation) {
throw Error('request confirmation failed');
}
let requestData: RequestLogicTypes.IRequest = deepCopy(
this.requestData,
) as RequestLogicTypes.IRequest;
let pending = deepCopy(this.pendingData);
if (!requestData && pending) {
requestData = pending as RequestLogicTypes.IRequest;
requestData.state = RequestLogicTypes.STATE.PENDING;
pending = { state: this.pendingData?.state };
}
const currency = this.currencyManager.fromStorageCurrency(requestData.currency);
return Object.assign(new EventEmitter(), {
...requestData,
balance: this.balance,
contentData: this.contentData,
currency: currency ? currency.id : 'unknown',
currencyInfo: requestData?.currency,
meta: this.requestMeta,
pending,
});
}
public async getEscrowData(
paymentReference: string,
network: CurrencyTypes.EvmChainName,
): Promise<PaymentTypes.EscrowChainData> {
const escrowContractAddress = erc20EscrowToPayArtifact.getAddress(network);
const escrowInfoRetriever = new EscrowERC20InfoRetriever(
paymentReference,
escrowContractAddress,
0,
'',
'',
network,
);
return await escrowInfoRetriever.getEscrowRequestMapping();
}
/**
* Refresh the request data and balance from the network (check if new events happened - e.g: accept, payments etc..) and return these data
*
* @param requestAndMeta return from getRequestFromId to avoid asking twice
* @returns Refreshed request data
*/
public async refresh(
requestAndMeta?: RequestLogicTypes.IReturnGetRequestFromId,
): Promise<Types.IRequestDataWithEvents> {
if (this.confirmationErrorOccurredAtCreation) {
throw Error('request confirmation failed');
}
if (!requestAndMeta) {
requestAndMeta = await this.requestLogic.getRequestFromId(this.requestId);
}
if (!requestAndMeta.result.request && !requestAndMeta.result.pending) {
const requestFromIdError = localUtils.formatGetRequestFromIdError(requestAndMeta);
if (requestFromIdError.indexOf('Decryption is not available') !== -1) {
this.logger.warn(`Decryption is not available for request ${this.requestId}`);
} else {
throw new Error(`No request found for the id: ${this.requestId} - ${requestFromIdError}`);
}
}
if (this.contentDataExtension) {
// TODO: PROT-1131 - add a pending content
this.contentData = await this.contentDataExtension.getContent(
requestAndMeta.result.request || requestAndMeta.result.pending,
);
}
this.requestData = requestAndMeta.result.request;
this.pendingData = requestAndMeta.result.pending;
this.requestMeta = requestAndMeta.meta;
if (!this.skipPaymentDetection) {
// let's refresh the balance
await this.refreshBalance();
}
return this.getData();
}
/**
* Refresh only the balance of the request and return it
*
* @returns return the balance
*/
public async refreshBalance(): Promise<Types.Payment.IBalanceWithEvents<any> | null> {
// TODO: PROT-1131 - add a pending balance
if (this.paymentNetwork && this.requestData) {
this.balance = await this.paymentNetwork.getBalance(this.requestData);
}
return this.balance;
}
/**
* Enables the payment detection
*/
public enablePaymentDetection(): void {
this.skipPaymentDetection = false;
}
/**
* Disables the payment detection
*/
public disablePaymentDetection(): void {
this.skipPaymentDetection = true;
}
}