diff --git a/docs/wallet-integration-guide/examples/scripts/05-preapproval.ts b/docs/wallet-integration-guide/examples/scripts/05-preapproval.ts index f072a2c1f..6a4457c57 100644 --- a/docs/wallet-integration-guide/examples/scripts/05-preapproval.ts +++ b/docs/wallet-integration-guide/examples/scripts/05-preapproval.ts @@ -77,7 +77,22 @@ logger.info('Successfully registered the preapproval.') // --- TEST FETCH -logger.info('Fetching for preapproval status') +const start = performance.now() +const fetchOnceStatus = await sdk.amulet.preapproval.fetchQuick(bob.partyId) +const end = performance.now() + +const duration = end - start +if (duration < 1000) { + logger.info( + `Success! The operation was fast (${duration.toFixed(2)} ms) and fetchOnce status is ${fetchOnceStatus}.` + ) +} else { + logger.warn( + `Warning: Operation took longer than 1 second (${(duration / 1000).toFixed(2)} s).` + ) +} + +logger.info('Fetching for preapproval status with retry') const fetchedPreapprovalStatus = await sdk.amulet.preapproval.fetchStatus( bob.partyId @@ -129,6 +144,23 @@ logger.info({ aliceAmuletValue, bobAmuletValue }, 'Result:') logger.info('Renewing preapproval...') +const start2 = performance.now() +const fetchOnceStatusWithPreapproval = await sdk.amulet.preapproval.fetchQuick( + bob.partyId +) +const end2 = performance.now() + +const duration2 = end2 - start2 +if (duration < 1000) { + logger.info( + `Success! The operation was fast (${duration2.toFixed(2)} ms) and fetchOnce status is ${fetchOnceStatusWithPreapproval}.` + ) +} else { + logger.warn( + `Warning: Operation took longer than 1 second (${duration2.toFixed(2)} s).` + ) +} + const newExpiresAt = new Date(fetchedPreapprovalStatus!.expiresAt) newExpiresAt.setDate(newExpiresAt.getDate() + 2) diff --git a/sdk/wallet-sdk/src/wallet/namespace/amulet/preapproval.ts b/sdk/wallet-sdk/src/wallet/namespace/amulet/preapproval.ts index 536869814..98943bd6d 100644 --- a/sdk/wallet-sdk/src/wallet/namespace/amulet/preapproval.ts +++ b/sdk/wallet-sdk/src/wallet/namespace/amulet/preapproval.ts @@ -158,6 +158,25 @@ export class PreapprovalNamespace { }) } + /** + * Fetch TransferPreapproval from ScanProxy. This does NOT retry or wait. + * If you want additional logic for create/renew/cancel events and retry use fetchStatus instead + * @param receiverParty Receiver party id + * @returns Resolves with the preapproval or null, if not found + */ + public async fetchQuick(receiverParty: PartyId) { + try { + return await this.ctx.amuletService.getTransferPreApprovalByParty( + receiverParty + ) + } catch (e) { + if (isNotFoundError(e)) { + this.logger.info('Preapproval is no longer visible') + return null + } + } + } + /** * Wait for Scan Proxy to show a receiver's TransferPreapproval, or for its CID to change after renewal, * or for it to disappear after cancel.