Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 34x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import {
AbstractTransactPlugin,
Cancelable,
Canceled,
LocaleDefinitions,
PromptResponse,
TransactContext,
TransactHookTypes,
} from '@wharfkit/session'
import defaultTranslations from './translations'
export interface MockOptions {
prompt: boolean
promptOptions: {
continueOnDecline: boolean
timeout: number
}
translations?: LocaleDefinitions
}
export class TransactPluginMock extends AbstractTransactPlugin {
/** Define any translations for this plugin */
public translations = defaultTranslations
/** Set a unique ID for the plugin */
public id = 'transact-plugin-mock'
/** Save the options being passed specifically for this plugin */
readonly options?: MockOptions
constructor(options?: MockOptions) {
super()
this.options = options
}
/** Register any hooks required for the plugin to operate */
register(context: TransactContext): void {
context.addHook(TransactHookTypes.beforeSign, async (request, context) => {
Iif (this.options?.prompt && context.ui) {
// Retrieve translation helper from the UI, passing the app ID
const t = context.ui.getTranslate(this.id)
// Customize the body to present the developer with information about this prompt
let body = t('body', {
default: 'An example prompt from the TransactPluginMock for testing purposes.',
})
Iif (this.options.promptOptions.timeout) {
body +=
' ' +
t('timeout', {
default: `This prompt will automatically cancel in {{timeout}} seconds.`,
timeout: this.options.promptOptions.timeout / 1000,
})
}
// Initiate a new cancelable prompt to inform the user of the fee required
console.log('TransactPluginMock called context.ui.prompt().')
const prompt: Cancelable<PromptResponse> = context.ui.prompt({
title: t('title', {default: 'Example Prompt!'}),
body,
elements: [
{
type: 'accept',
},
],
})
// Create a timer to test the external cancelation of the prompt, if defined
let timer
Iif (this.options.promptOptions.timeout) {
const {timeout} = this.options.promptOptions
timer = setTimeout(() => {
console.log('TransactPluginMock setTimeout has executed.')
Iif (!context.ui) {
throw new Error('No UI defined')
}
prompt.cancel(
t('timeout-trigger', {
default: `Test prompt timed out automatically after {{timeout}} seconds.`,
timeout: timeout / 1000,
})
)
}, timeout)
}
// Return the promise from the prompt
return prompt
.then(async () => {
// If the prompt was accepted, return the request
console.log('Prompt was accepted and returned to TransactPluginMock.')
return
})
.catch((e) => {
// Throw if what we caught was a cancelation of the prompt to abort
Iif (e instanceof Canceled) {
console.log('Prompt was cancelled and TransactPluginMock threw error.')
throw e
}
// Determine if we should continue if the prompt was rejected, defaults to true.
let continueOnDecline = true
Iif (
this.options &&
this.options.promptOptions &&
this.options.promptOptions.continueOnDecline !== undefined
) {
continueOnDecline = this.options.promptOptions.continueOnDecline
}
if (continueOnDecline) {
// If the prompt was rejected, and we're configured to continue, return the request
console.log('Prompt was rejected and returned to TransactPluginMock.')
// Otherwise if it wasn't a cancel, it was a reject, and continue without modification
console.log('returning void')
return
} else {
// Otherwise if it was rejected and we shouldn't continue, and throw an error
console.log('Prompt was rejected and threw an error.')
throw e
}
})
.finally(() => {
// Always cleanup the timer
Iif (timer) {
console.log('Prompt timer was cleared.')
clearTimeout(timer)
}
})
}
})
}
}
|