|
| 1 | +import { Argv } from 'yargs'; |
| 2 | +import { input, select } from '@inquirer/prompts'; |
| 3 | +import { error, info, success } from 'signale'; |
| 4 | +import { cancelTransaction } from '@trustvc/trustvc'; |
| 5 | +import { |
| 6 | + getWalletOrSigner, |
| 7 | + getErrorMessage, |
| 8 | + getEtherscanAddress, |
| 9 | + promptNetworkSelection, |
| 10 | + promptWalletSelection, |
| 11 | +} from '../../utils'; |
| 12 | + |
| 13 | +export type TransactionCancelCommand = { |
| 14 | + network: string; |
| 15 | + nonce?: string; |
| 16 | + gasPrice?: string; |
| 17 | + transactionHash?: string; |
| 18 | + encryptedWalletPath?: string; |
| 19 | + key?: string; |
| 20 | + keyFile?: string; |
| 21 | + rpcUrl?: string; |
| 22 | +}; |
| 23 | + |
| 24 | +export const command = 'cancel'; |
| 25 | + |
| 26 | +export const describe = 'Cancel a pending transaction on the blockchain'; |
| 27 | + |
| 28 | +export const builder = (yargs: Argv): Argv => yargs; |
| 29 | + |
| 30 | +/** Prompt for how to specify the pending transaction and collect nonce/gas or hash */ |
| 31 | +async function promptTransactionSpec(): Promise<{ |
| 32 | + nonce?: string; |
| 33 | + gasPrice?: string; |
| 34 | + transactionHash?: string; |
| 35 | +}> { |
| 36 | + const method = await select({ |
| 37 | + message: 'How do you want to specify the pending transaction?', |
| 38 | + choices: [ |
| 39 | + { |
| 40 | + name: 'By transaction hash (recommended – gas price will be increased by 100% automatically)', |
| 41 | + value: 'hash', |
| 42 | + description: 'Enter the pending transaction hash (0x...)', |
| 43 | + }, |
| 44 | + { |
| 45 | + name: 'By nonce and gas price', |
| 46 | + value: 'nonceGas', |
| 47 | + description: 'Enter the nonce and a higher gas price in wei', |
| 48 | + }, |
| 49 | + ], |
| 50 | + default: 'hash', |
| 51 | + }); |
| 52 | + |
| 53 | + if (method === 'hash') { |
| 54 | + const transactionHash = await input({ |
| 55 | + message: 'Enter the pending transaction hash (0x...):', |
| 56 | + required: true, |
| 57 | + validate: (value: string) => { |
| 58 | + const v = value.trim(); |
| 59 | + if (!v) return 'Transaction hash is required'; |
| 60 | + if (!/^0x[a-fA-F0-9]{64}$/.test(v)) |
| 61 | + return 'Invalid transaction hash (expected 0x followed by 64 hex characters)'; |
| 62 | + return true; |
| 63 | + }, |
| 64 | + }); |
| 65 | + return { transactionHash: transactionHash.trim() }; |
| 66 | + } |
| 67 | + |
| 68 | + const nonce = await input({ |
| 69 | + message: 'Enter the pending transaction nonce:', |
| 70 | + required: true, |
| 71 | + validate: (value: string) => { |
| 72 | + const n = value.trim(); |
| 73 | + if (!n) return 'Nonce is required'; |
| 74 | + if (!/^\d+$/.test(n)) return 'Nonce must be a non-negative integer'; |
| 75 | + return true; |
| 76 | + }, |
| 77 | + }); |
| 78 | + |
| 79 | + const gasPrice = await input({ |
| 80 | + message: |
| 81 | + 'Enter the gas price (wei) for the replacement transaction (must be higher than the pending transaction):', |
| 82 | + required: true, |
| 83 | + validate: (value: string) => { |
| 84 | + const v = value.trim(); |
| 85 | + if (!v) return 'Gas price is required'; |
| 86 | + if (!/^\d+$/.test(v)) return 'Gas price must be a non-negative integer (wei)'; |
| 87 | + return true; |
| 88 | + }, |
| 89 | + }); |
| 90 | + |
| 91 | + return { nonce: nonce.trim(), gasPrice: gasPrice.trim() }; |
| 92 | +} |
| 93 | + |
| 94 | +/** Collect all inputs via prompts */ |
| 95 | +export const promptForInputs = async (): Promise<TransactionCancelCommand> => { |
| 96 | + const txSpec = await promptTransactionSpec(); |
| 97 | + const network = await promptNetworkSelection(); |
| 98 | + const walletSelection = await promptWalletSelection(); |
| 99 | + |
| 100 | + return { |
| 101 | + ...txSpec, |
| 102 | + network, |
| 103 | + ...walletSelection, |
| 104 | + }; |
| 105 | +}; |
| 106 | + |
| 107 | +/** |
| 108 | + * Run cancel transaction with pre-filled answers (no prompts). Used for scripting/tests. |
| 109 | + */ |
| 110 | +export const runCancelTransaction = async ( |
| 111 | + answers: TransactionCancelCommand, |
| 112 | +): Promise<string | undefined> => { |
| 113 | + const { network, nonce, gasPrice, transactionHash, encryptedWalletPath, key, keyFile, rpcUrl } = |
| 114 | + answers; |
| 115 | + |
| 116 | + if (transactionHash) { |
| 117 | + info('Fetching transaction to get nonce and gas price; replacement will use 2x gas price.'); |
| 118 | + } |
| 119 | + |
| 120 | + const wallet = await getWalletOrSigner({ |
| 121 | + network, |
| 122 | + encryptedWalletPath, |
| 123 | + key, |
| 124 | + keyFile, |
| 125 | + rpcUrl, |
| 126 | + }); |
| 127 | + |
| 128 | + const replacementHash = await cancelTransaction(wallet as any, { |
| 129 | + nonce, |
| 130 | + gasPrice, |
| 131 | + transactionHash, |
| 132 | + }); |
| 133 | + |
| 134 | + success('Transaction has been cancelled'); |
| 135 | + if (replacementHash) { |
| 136 | + info(`Replacement transaction hash: ${replacementHash}`); |
| 137 | + info(`Find more details at ${getEtherscanAddress({ network })}/tx/${replacementHash}`); |
| 138 | + } |
| 139 | + return replacementHash; |
| 140 | +}; |
| 141 | + |
| 142 | +export const handler = async (): Promise<void> => { |
| 143 | + try { |
| 144 | + const answers = await promptForInputs(); |
| 145 | + await runCancelTransaction(answers); |
| 146 | + } catch (e) { |
| 147 | + error(getErrorMessage(e)); |
| 148 | + } |
| 149 | +}; |
0 commit comments