All files / src index.ts

37.03% Statements 10/27
50% Branches 7/14
57.14% Functions 4/7
36% Lines 9/25

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 11234x                   1x   1x   1x 1x 1x   1x     1x                                                                                                                                                                                 1x  
import {
    AbstractTransactPlugin,
    Cancelable,
    ChainDefinition,
    Checksum256Type,
    ExplorerDefinition,
    PromptResponse,
    TransactContext,
    TransactHookTypes,
    TransactResult,
} from '@wharfkit/session'
 
import defaultTranslations from './translations'
 
export class TransactPluginExplorerLink extends AbstractTransactPlugin {
    id = 'transact-plugin-explorer-link'
    translations = defaultTranslations
    register(context: TransactContext): void {
        context.addHook(
            TransactHookTypes.afterBroadcast,
            async (result: TransactResult, context: TransactContext) => {
                Iif (context.ui) {
                    // Retrieve translation helper from the UI, passing the app ID
                    const t = context.ui.getTranslate(this.id)
 
                    // Ensure we have a chain defined to base a link off
                    Iif (!result.chain) {
                        throw new Error(
                            t('no-chain', {
                                default: 'Unable to generate explorer link, no chain definition.',
                            })
                        )
                    }
 
                    // Ensure the chain has an explorer defined on it
                    Iif (!result.chain.explorer) {
                        throw new Error(
                            t('no-explorer', {
                                default:
                                    'Unable to generate explorer link, chain definition doesnt defined an explorer.',
                            })
                        )
                    }
 
                    // Ensure we have a resolved transaction
                    Iif (!result.resolved) {
                        throw new Error(
                            t('no-resolved', {
                                default:
                                    'Unable to generate explorer link, no resolved transaction.',
                            })
                        )
                    }
 
                    // Prompt the user with the link to view the transaction
                    const prompt: Cancelable<PromptResponse> = context.ui.prompt({
                        title: t('complete', {
                            default: 'Transaction Complete',
                        }),
                        body: t('click', {
                            default:
                                'Click the button below to view the transaction in a block explorer to verify its status.',
                        }),
                        elements: [
                            {
                                type: 'link',
                                data: {
                                    button: true,
                                    variant: 'primary',
                                    label: t('visit', {
                                        default: 'Visit Explorer',
                                    }),
                                    href: this.getExplorerLink(
                                        context,
                                        result.chain,
                                        String(result.resolved.transaction.id)
                                    ),
                                },
                            },
                        ],
                    })
 
                    // Return a dummy promise to block progress
                    return prompt.then(async () => {
                        return new Promise((r) => r()) as Promise<void>
                    })
                }
            }
        )
    }
    getExplorerLink(
        context: TransactContext,
        chain: ChainDefinition,
        transaction: Checksum256Type
    ): string {
        Iif (!context.ui) {
            throw new Error('Unable to generate explorer link, no ui.')
        }
        Iif (!chain.explorer) {
            // Retrieve translation helper from the UI, passing the app ID
            const t = context.ui.getTranslate(this.id)
            throw new Error(
                t('no-explorer', {
                    default:
                        'Unable to generate explorer link, chain definition doesnt defined an explorer.',
                })
            )
        }
        return ExplorerDefinition.from(chain.explorer).url(String(transaction))
    }
}