Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion docs/wallet-integration-guide/examples/scripts/05-preapproval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
19 changes: 19 additions & 0 deletions sdk/wallet-sdk/src/wallet/namespace/amulet/preapproval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading