-
Notifications
You must be signed in to change notification settings - Fork 162
Implement transaction/dry-run
#236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/controllers/transaction/TransactionDryRunController.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { ApiPromise } from '@polkadot/api'; | ||
| import { BadRequest } from 'http-errors'; | ||
|
|
||
| import { TransactionDryRunService } from '../../services'; | ||
| import { IPostRequestHandler, ITx } from '../../types/requests'; | ||
| import AbstractController from '../AbstractController'; | ||
|
|
||
| export default class TransactionDryRunController extends AbstractController< | ||
| TransactionDryRunService | ||
| > { | ||
| constructor(api: ApiPromise) { | ||
| super(api, '/transaction/dry-run', new TransactionDryRunService(api)); | ||
| this.initRoutes(); | ||
| } | ||
|
|
||
| protected initRoutes(): void { | ||
| this.router.post( | ||
| this.path, | ||
| TransactionDryRunController.catchWrap(this.dryRunTransaction) | ||
| ); | ||
| } | ||
|
|
||
| private dryRunTransaction: IPostRequestHandler<ITx> = async ( | ||
| { body: { tx }, query: { at } }, | ||
| res | ||
| ): Promise<void> => { | ||
| if (!tx) { | ||
| throw new BadRequest('Missing field `tx` on request body.'); | ||
| } | ||
|
|
||
| const hash = | ||
| typeof at === 'string' | ||
| ? await this.getHashForBlock(at) | ||
| : await this.api.rpc.chain.getFinalizedHead(); | ||
|
|
||
| TransactionDryRunController.sanitizedSend( | ||
| res, | ||
| await this.service.dryRuntExtrinsic(hash, tx) | ||
| ); | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export { default as TransactionFeeEstimate } from './TransactionFeeEstimateController'; | ||
| export { default as TransactionSubmit } from './TransactionSubmitController'; | ||
| export { default as TransactionMaterial } from './TransactionMaterialController'; | ||
| export { default as TransactionDryRun } from './TransactionDryRunController'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { BlockHash } from '@polkadot/types/interfaces'; | ||
|
|
||
| import { | ||
| ITransactionDryRun, | ||
| TransactionResultType, | ||
| ValidityErrorType, | ||
| } from '../../types/responses'; | ||
| import { AbstractService } from '../AbstractService'; | ||
| import { extractCauseAndStack } from './extractCauseAndStack'; | ||
|
|
||
| /** | ||
| * Dry run an extrinsic. | ||
| * | ||
| * Returns: | ||
| * - `at`: | ||
| * - `hash`: The block's hash. | ||
| * - `height`: The block's height. | ||
| * - `dryRunResult`: | ||
| * - `resultType`: Either `DispatchOutcome` if the construction is valid | ||
| * or `TransactionValidityError` if the transaction has invalid construction. | ||
| * - `result`: If there was an error it will be the cause of the error. If the | ||
| * transaction executed correctly it will be `Ok: []`. | ||
| * - `validityErrorType`: Only present if the `resultType` is | ||
| * `TransactionValidityError`. Either `InvalidTransaction` or `UnknownTransaction`. | ||
| * | ||
| * References: | ||
| * - `UnknownTransaction`: https://crates.parity.io/sp_runtime/transaction_validity/enum.UnknownTransaction.html | ||
| * - `InvalidTransaction`: https://crates.parity.io/sp_runtime/transaction_validity/enum.InvalidTransaction.html | ||
| */ | ||
| export class TransactionDryRunService extends AbstractService { | ||
| async dryRuntExtrinsic( | ||
| hash: BlockHash, | ||
| extrinsic: string | ||
| ): Promise<ITransactionDryRun> { | ||
| const api = await this.ensureMeta(hash); | ||
|
|
||
| try { | ||
| const [applyExtrinsicResult, { number }] = await Promise.all([ | ||
| api.rpc.system.dryRun(extrinsic, hash), | ||
| api.rpc.chain.getHeader(hash), | ||
| ]); | ||
|
|
||
| let dryRunResult; | ||
| if (applyExtrinsicResult.isOk) { | ||
| dryRunResult = { | ||
| resultType: TransactionResultType.DispatchOutcome, | ||
| result: applyExtrinsicResult.asOk, | ||
| }; | ||
| } else { | ||
| const { asError } = applyExtrinsicResult; | ||
| dryRunResult = { | ||
| resultType: TransactionResultType.TransactionValidityError, | ||
| result: asError.isInvalid | ||
| ? asError.asInvalid | ||
| : asError.asUnknown, | ||
| validityErrorType: asError.isInvalid | ||
| ? ValidityErrorType.Invalid | ||
| : ValidityErrorType.Unknown, | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| at: { | ||
| hash, | ||
| height: number.unwrap().toString(10), | ||
| }, | ||
| dryRunResult, | ||
| }; | ||
| } catch (err) { | ||
| const { cause, stack } = extractCauseAndStack(err); | ||
|
|
||
| throw { | ||
| at: { | ||
| hash, | ||
| }, | ||
| error: 'Unable to dry-run transaction', | ||
| extrinsic, | ||
| cause, | ||
| stack, | ||
| }; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export * from './TransactionSubmitService'; | ||
| export * from './TransactionFeeEstimateService'; | ||
| export * from './TransactionMaterialService'; | ||
| export * from './TransactionDryRunService'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { | ||
| DispatchOutcome, | ||
| InvalidTransaction, | ||
| UnknownTransaction, | ||
| } from '@polkadot/types/interfaces'; | ||
|
|
||
| import { IAt } from '.'; | ||
|
|
||
| export enum TransactionResultType { | ||
| TransactionValidityError = 'TransactionValidityError', | ||
| DispatchOutcome = 'DispatchOutcome', | ||
| } | ||
|
|
||
| export enum ValidityErrorType { | ||
| Invalid = 'InvalidTransaction', | ||
| Unknown = 'UnknownTransaction', | ||
| } | ||
|
|
||
| export type TransactionResult = | ||
| | DispatchOutcome | ||
| | InvalidTransaction | ||
| | UnknownTransaction; | ||
|
|
||
| export interface ITransactionDryRun { | ||
| at: IAt; | ||
| dryRunResult: { | ||
| resultType: TransactionResultType; | ||
| result: TransactionResult; | ||
| validityErrorType?: ValidityErrorType; | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice docs 🎉