-
Notifications
You must be signed in to change notification settings - Fork 162
Implement transaction/{ , material, fee-estimate}
#246
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
9 commits
Select commit
Hold shift + click to select a range
9f74085
Implement fee-estimate, material, and submit.
emostov 376ac3e
Merge branch 'master' of https://github.com/paritytech/substrate-api-…
emostov 3f966a4
small update
emostov 7349016
Change to transaction
emostov 573f309
Merge remote-tracking branch 'origin' into zeke-material
emostov 9397047
update tx error middleware
emostov 3ed8ac7
update open-api
emostov 29904f4
small update
emostov 8a0496d
SCALE
joepetrowski 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
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
76 changes: 76 additions & 0 deletions
76
src/controllers/transaction/TransactionFeeEstimateController.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,76 @@ | ||
| import { ApiPromise } from '@polkadot/api'; | ||
|
|
||
| import { TransactionFeeEstimateService } from '../../services'; | ||
| import { IPostRequestHandler, ITx } from '../../types/requests'; | ||
| import AbstractController from '../AbstractController'; | ||
|
|
||
| /** | ||
| * POST a serialized transaction and receive a fee estimate. | ||
| * | ||
| * Post info: | ||
| * - `data`: Expects a hex-encoded transaction, e.g. '{"tx": "0x..."}'. | ||
| * - `headers`: Expects 'Content-Type: application/json'. | ||
| * | ||
| * Returns: | ||
| * - Success: | ||
| * - `weight`: Extrinsic weight. | ||
| * - `class`: Extrinsic class, one of 'Normal', 'Operational', or 'Mandatory'. | ||
| * - `partialFee`: _Expected_ inclusion fee for the transaction. Note that the fee rate changes | ||
| * up to 30% in a 24 hour period and this will not be the exact fee. | ||
| * - Failure: | ||
| * - `error`: Error description. | ||
| * - `extrinsic`: The extrinsic and reference block hash. | ||
| * - `cause`: Error message from the client. | ||
| * | ||
| * Note: `partialFee` does not include any tips that you may add to increase a transaction's | ||
| * priority. See the reference on `compute_fee`. | ||
| * | ||
| * Substrate Reference: | ||
| * - `RuntimeDispatchInfo`: https://crates.parity.io/pallet_transaction_payment_rpc_runtime_api/struct.RuntimeDispatchInfo.html | ||
| * - `query_info`: https://crates.parity.io/pallet_transaction_payment/struct.Module.html#method.query_info | ||
| * - `compute_fee`: https://crates.parity.io/pallet_transaction_payment/struct.Module.html#method.compute_fee | ||
| */ | ||
| export default class TransactionFeeEstimateController extends AbstractController< | ||
| TransactionFeeEstimateService | ||
| > { | ||
| constructor(api: ApiPromise) { | ||
| super( | ||
| api, | ||
| '/transaction/fee-estimate', | ||
| new TransactionFeeEstimateService(api) | ||
| ); | ||
| this.initRoutes(); | ||
| } | ||
|
|
||
| protected initRoutes(): void { | ||
| this.router.post( | ||
| this.path, | ||
| TransactionFeeEstimateController.catchWrap(this.txFeeEstimate) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Submit a serialized transaction in order to receive an estimate for its | ||
| * partial fees. | ||
| * | ||
| * @param req Sidecar TxRequest | ||
| * @param res Express Response | ||
| */ | ||
| private txFeeEstimate: IPostRequestHandler<ITx> = async ( | ||
| { body: { tx } }, | ||
| res | ||
| ): Promise<void> => { | ||
| if (!tx) { | ||
| throw { | ||
| error: 'Missing field `tx` on request body.', | ||
| }; | ||
| } | ||
|
|
||
| const hash = await this.api.rpc.chain.getFinalizedHead(); | ||
|
|
||
| TransactionFeeEstimateController.sanitizedSend( | ||
| res, | ||
| await this.service.fetchTransactionFeeEstimate(hash, tx) | ||
| ); | ||
| }; | ||
| } |
72 changes: 72 additions & 0 deletions
72
src/controllers/transaction/TransactionMaterialController.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,72 @@ | ||
| import { ApiPromise } from '@polkadot/api'; | ||
| import { RequestHandler } from 'express'; | ||
|
|
||
| import { TransactionMaterialService } from '../../services'; | ||
| import AbstractController from '../AbstractController'; | ||
|
|
||
| /** | ||
| * GET all the network information needed to construct a transaction offline. | ||
| * | ||
| * Query | ||
| * - (Optional) `noMeta`: If true, does not return metadata hex. This is useful when metadata is not | ||
| * needed and response time is a concern. Defaults to false. | ||
| * - (Optional) `at`: Block hash or number at which to query. If not provided, queries | ||
| * finalized head. | ||
| * | ||
| * Returns: | ||
| * - `at`: Block number and hash at which the call was made. | ||
| * - `genesisHash`: The hash of the chain's genesis block. | ||
| * - `chainName`: The chain's name. | ||
| * - `specName`: The chain's spec. | ||
| * - `specVersion`: The spec version. Always increased in a runtime upgrade. | ||
| * - `txversion`: The transaction version. Common `txVersion` numbers indicate that the | ||
| * transaction encoding format and method indices are the same. Needed for decoding in an | ||
| * offline environment. Adding new transactions does not change `txVersion`. | ||
| * - `metadata`: The chain's metadata in hex format. | ||
| * | ||
| * Note: `chainName`, `specName`, and `specVersion` are used to define a type registry with a set | ||
| * of signed extensions and types. For Polkadot and Kusama, `chainName` is not used in defining | ||
| * this registry, but in other Substrate-based chains that re-launch their network without | ||
| * changing the `specName`, the `chainName` would be needed to create the correct registry. | ||
| * | ||
| * Substrate Reference: | ||
| * - `RuntimeVersion`: https://crates.parity.io/sp_version/struct.RuntimeVersion.html | ||
| * - `SignedExtension`: https://crates.parity.io/sp_runtime/traits/trait.SignedExtension.html | ||
| * - FRAME Support: https://crates.parity.io/frame_support/metadata/index.html | ||
| */ | ||
| export default class TransactionMaterialController extends AbstractController< | ||
| TransactionMaterialService | ||
| > { | ||
| constructor(api: ApiPromise) { | ||
| super( | ||
| api, | ||
| '/transaction/material', | ||
| new TransactionMaterialService(api) | ||
| ); | ||
| this.initRoutes(); | ||
| } | ||
|
|
||
| protected initRoutes(): void { | ||
| this.safeMountAsyncGetHandlers([['', this.getTransactionMaterial]]); | ||
| } | ||
|
|
||
| /** | ||
| * GET all the network information needed to construct a transaction offline. | ||
| * | ||
| * @param _req Express Request | ||
| * @param res Express Response | ||
| */ | ||
| private getTransactionMaterial: RequestHandler = async ( | ||
| { query: { noMeta, at } }, | ||
| res | ||
| ): Promise<void> => { | ||
| const hash = await this.getHashFromAt(at); | ||
|
|
||
| const noMetaArg = noMeta === 'true' ? true : false; | ||
|
|
||
| TransactionMaterialController.sanitizedSend( | ||
| res, | ||
| await this.service.fetchTransactionMaterial(hash, noMetaArg) | ||
| ); | ||
| }; | ||
| } |
59 changes: 59 additions & 0 deletions
59
src/controllers/transaction/TransactionSubmitController.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,59 @@ | ||
| import { ApiPromise } from '@polkadot/api'; | ||
|
|
||
| import { TransactionSubmitService } from '../../services'; | ||
| import { IPostRequestHandler, ITx } from '../../types/requests'; | ||
| import AbstractController from '../AbstractController'; | ||
|
|
||
| /** | ||
| * POST a serialized transaction to submit to the transaction queue. | ||
| * | ||
| * Post info: | ||
| * - `data`: Expects a hex-encoded transaction, e.g. '{"tx": "0x..."}'. | ||
| * - `headers`: Expects 'Content-Type: application/json'. | ||
| * | ||
| * Returns: | ||
| * - Success: | ||
| * - `hash`: The hash of the encoded transaction. | ||
| * - Failure: | ||
| * - `error`: 'Failed to parse transaction' or 'Failed to submit transaction'. In the case of the former, | ||
| * Sidecar was unable to parse the transaction and never submitted it to the client. In | ||
| * the case of the latter, the transaction queue rejected the transaction. | ||
| * - `extrinsic`: The hex-encoded extrinsic. Only present if Sidecar fails to parse a transaction. | ||
| * - `cause`: The error message from parsing or from the client. | ||
| */ | ||
| export default class TransactionSubmitController extends AbstractController< | ||
| TransactionSubmitService | ||
| > { | ||
| constructor(api: ApiPromise) { | ||
| super(api, '/transaction', new TransactionSubmitService(api)); | ||
| this.initRoutes(); | ||
| } | ||
|
|
||
| protected initRoutes(): void { | ||
| this.router.post( | ||
| this.path, | ||
| TransactionSubmitController.catchWrap(this.txSubmit) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Submit a serialized transaction to the transaction queue. | ||
| * | ||
| * @param req Sidecar TxRequest | ||
| * @param res Express Response | ||
| */ | ||
| private txSubmit: IPostRequestHandler<ITx> = async ( | ||
| { body: { tx } }, | ||
| res | ||
| ): Promise<void> => { | ||
| if (!tx) { | ||
| throw { | ||
| error: 'Missing field `tx` on request body.', | ||
| }; | ||
| } | ||
|
|
||
| const hash = await this.api.rpc.chain.getFinalizedHead(); | ||
|
|
||
| res.send(await this.service.submitTransaction(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 +1,4 @@ | ||
| export { default as TransactionDryRun } from './TransactionDryRunController'; | ||
| export { default as TransactionMaterial } from './TransactionMaterialController'; | ||
| export { default as TransactionFeeEstimate } from './TransactionFeeEstimateController'; | ||
| export { default as TransactionSubmit } from './TransactionSubmitController'; |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.