|
| 1 | +import { BlockHash } from '@polkadot/types/interfaces'; |
| 2 | + |
| 3 | +import { |
| 4 | + ITransactionDryRun, |
| 5 | + TransactionResultType, |
| 6 | + ValidityErrorType, |
| 7 | +} from '../../types/responses'; |
| 8 | +import { AbstractService } from '../AbstractService'; |
| 9 | +import { extractCauseAndStack } from './extractCauseAndStack'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Dry run an extrinsic. |
| 13 | + * |
| 14 | + * Returns: |
| 15 | + * - `at`: |
| 16 | + * - `hash`: The block's hash. |
| 17 | + * - `height`: The block's height. |
| 18 | + * - `dryRunResult`: |
| 19 | + * - `resultType`: Either `DispatchOutcome` if the construction is valid |
| 20 | + * or `TransactionValidityError` if the transaction has invalid construction. |
| 21 | + * - `result`: If there was an error it will be the cause of the error. If the |
| 22 | + * transaction executed correctly it will be `Ok: []`. |
| 23 | + * - `validityErrorType`: Only present if the `resultType` is |
| 24 | + * `TransactionValidityError`. Either `InvalidTransaction` or `UnknownTransaction`. |
| 25 | + * |
| 26 | + * References: |
| 27 | + * - `UnknownTransaction`: https://crates.parity.io/sp_runtime/transaction_validity/enum.UnknownTransaction.html |
| 28 | + * - `InvalidTransaction`: https://crates.parity.io/sp_runtime/transaction_validity/enum.InvalidTransaction.html |
| 29 | + */ |
| 30 | +export class TransactionDryRunService extends AbstractService { |
| 31 | + async dryRuntExtrinsic( |
| 32 | + hash: BlockHash, |
| 33 | + extrinsic: string |
| 34 | + ): Promise<ITransactionDryRun> { |
| 35 | + const api = await this.ensureMeta(hash); |
| 36 | + |
| 37 | + try { |
| 38 | + const [applyExtrinsicResult, { number }] = await Promise.all([ |
| 39 | + api.rpc.system.dryRun(extrinsic, hash), |
| 40 | + api.rpc.chain.getHeader(hash), |
| 41 | + ]); |
| 42 | + |
| 43 | + let dryRunResult; |
| 44 | + if (applyExtrinsicResult.isOk) { |
| 45 | + dryRunResult = { |
| 46 | + resultType: TransactionResultType.DispatchOutcome, |
| 47 | + result: applyExtrinsicResult.asOk, |
| 48 | + }; |
| 49 | + } else { |
| 50 | + const { asError } = applyExtrinsicResult; |
| 51 | + dryRunResult = { |
| 52 | + resultType: TransactionResultType.TransactionValidityError, |
| 53 | + result: asError.isInvalid |
| 54 | + ? asError.asInvalid |
| 55 | + : asError.asUnknown, |
| 56 | + validityErrorType: asError.isInvalid |
| 57 | + ? ValidityErrorType.Invalid |
| 58 | + : ValidityErrorType.Unknown, |
| 59 | + }; |
| 60 | + } |
| 61 | + |
| 62 | + return { |
| 63 | + at: { |
| 64 | + hash, |
| 65 | + height: number.unwrap().toString(10), |
| 66 | + }, |
| 67 | + dryRunResult, |
| 68 | + }; |
| 69 | + } catch (err) { |
| 70 | + const { cause, stack } = extractCauseAndStack(err); |
| 71 | + |
| 72 | + throw { |
| 73 | + at: { |
| 74 | + hash, |
| 75 | + }, |
| 76 | + error: 'Unable to dry-run transaction', |
| 77 | + extrinsic, |
| 78 | + cause, |
| 79 | + stack, |
| 80 | + }; |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments