Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/chains-config/assetHubKusamaControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ export const assetHubKusamaControllers: ControllerConfig = {
'RcNodeNetwork',
'RcNodeTransactionPool',
'RcNodeVersion',
'RcTransactionDryRun',
'RcTransactionFeeEstimate',
'RcTransactionMaterial',
'RcTransactionSubmit',
'RuntimeCode',
'RuntimeMetadata',
'RuntimeSpec',
Expand Down
4 changes: 4 additions & 0 deletions src/chains-config/assetHubNextWestendControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ export const assetHubNextWestendControllers: ControllerConfig = {
'RcNodeNetwork',
'RcNodeTransactionPool',
'RcNodeVersion',
'RcTransactionDryRun',
'RcTransactionFeeEstimate',
'RcTransactionMaterial',
'RcTransactionSubmit',
'RuntimeCode',
'RuntimeMetadata',
'RuntimeSpec',
Expand Down
4 changes: 4 additions & 0 deletions src/chains-config/assetHubPolkadotControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ export const assetHubPolkadotControllers: ControllerConfig = {
'RcNodeNetwork',
'RcNodeTransactionPool',
'RcNodeVersion',
'RcTransactionDryRun',
'RcTransactionFeeEstimate',
'RcTransactionMaterial',
'RcTransactionSubmit',
'RuntimeCode',
'RuntimeMetadata',
'RuntimeSpec',
Expand Down
4 changes: 4 additions & 0 deletions src/chains-config/assetHubWestendControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export const assetHubWestendControllers: ControllerConfig = {
'RcNodeNetwork',
'RcNodeTransactionPool',
'RcNodeVersion',
'RcTransactionDryRun',
'RcTransactionFeeEstimate',
'RcTransactionMaterial',
'RcTransactionSubmit',
'RuntimeCode',
'RuntimeMetadata',
'RuntimeSpec',
Expand Down
10 changes: 10 additions & 0 deletions src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ import {
RcPalletsStorage,
} from './rc/pallets';
import { RcRuntimeCode, RcRuntimeMetadata, RcRuntimeSpec } from './rc/runtime';
import {
RcTransactionDryRun,
RcTransactionFeeEstimate,
RcTransactionMaterial,
RcTransactionSubmit,
} from './rc/transaction';
import { RuntimeCode, RuntimeMetadata, RuntimeSpec } from './runtime';
import { TransactionDryRun, TransactionFeeEstimate, TransactionMaterial, TransactionSubmit } from './transaction';
/**
Expand Down Expand Up @@ -136,6 +142,10 @@ export const controllers = {
RcNodeNetwork,
RcNodeTransactionPool,
RcNodeVersion,
RcTransactionDryRun,
RcTransactionFeeEstimate,
RcTransactionMaterial,
RcTransactionSubmit,
RuntimeCode,
RuntimeMetadata,
RuntimeSpec,
Expand Down
1 change: 1 addition & 0 deletions src/controllers/rc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export * from './accounts';
export * from './blocks';
export * from './pallets';
export * from './runtime';
export * from './transaction';
93 changes: 93 additions & 0 deletions src/controllers/rc/transaction/RcTransactionDryRunController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2017-2025 Parity Technologies (UK) Ltd.
// This file is part of Substrate API Sidecar.
//
// Substrate API Sidecar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import { BadRequest } from 'http-errors';

import { ApiPromiseRegistry } from '../../../apiRegistry';
import { TransactionDryRunService } from '../../../services';
import { IPostRequestHandler, ITx } from '../../../types/requests';
import AbstractController from '../../AbstractController';

/**
* Dry run a transaction on the relay chain.
*
* Returns:
* - `at`:
* - `hash`: The block's hash.
* - `height`: The block's height.
* - `result`:
* - Successfull dry run:
* - `actualWeight`: The actual weight of the transaction.
* - `paysFee`: The fee to be paid.
* - Failed dry run:
* - error reason.
* - Dry run not possible to run:
* - `isUnimplemented`: The dry run is not implemented.
* - `isVersionedConversionFailed`: The versioned conversion failed.
* - `type`: 'Unimplemented' | 'VersionedConversionFailed';.
*
* References:
* - `DispatchError`: https://docs.rs/sp-runtime/39.0.1/sp_runtime/enum.DispatchError.html
* - `PostDispatchInfo`: https://docs.rs/frame-support/38.0.0/frame_support/dispatch/struct.PostDispatchInfo.html
* - `Error Type`: https://paritytech.github.io/polkadot-sdk/master/xcm_runtime_apis/dry_run/enum.Error.html
*
* Note: If you get the error `-32601: Method not found` it means that the node sidecar
* is connected to does not expose the `system_dryRun` RPC. One way to resolve this
* issue is to pass the `--rpc-external` flag to that node.
*/
export default class RcTransactionDryRunController extends AbstractController<TransactionDryRunService> {
static controllerName = 'RcTransactionDryRun';
static requiredPallets = [];
constructor(_api: string) {
const rcApiSpecName = ApiPromiseRegistry.getSpecNameByType('relay')?.values();
const rcSpecName = rcApiSpecName ? Array.from(rcApiSpecName)[0] : undefined;
if (!rcSpecName) {
throw new Error('Relay chain API spec name is not defined.');
}
super(rcSpecName, '/rc/transaction/dry-run', new TransactionDryRunService(rcSpecName));
this.initRoutes();
}

protected initRoutes(): void {
this.router.post(this.path, RcTransactionDryRunController.catchWrap(this.dryRunTransaction));
}

private dryRunTransaction: IPostRequestHandler<ITx> = async (
{ body: { tx, at, senderAddress, xcmVersion } },
res,
): Promise<void> => {
if (!tx) {
throw new BadRequest('Missing field `tx` on request body.');
}

if (!senderAddress) {
throw new BadRequest('Missing field `senderAddress` on request body.');
}

const rcApi = ApiPromiseRegistry.getApiByType('relay')[0]?.api;

if (!rcApi) {
throw new Error('Relay chain API not found, please use SAS_SUBSTRATE_MULTI_CHAIN_URL env variable');
}

const hash = await this.getHashFromAt(at, { api: rcApi });

RcTransactionDryRunController.sanitizedSend(
res,
await this.service.dryRuntExtrinsic(rcApi, senderAddress, tx, hash, xcmVersion),
);
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2017-2025 Parity Technologies (UK) Ltd.
// This file is part of Substrate API Sidecar.
//
// Substrate API Sidecar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import { ApiPromiseRegistry } from '../../../apiRegistry';
import { TransactionFeeEstimateService } from '../../../services';
import { IPostRequestHandler, ITx } from '../../../types/requests';
import AbstractController from '../../AbstractController';

/**
* POST a serialized transaction and receive a fee estimate for the relay chain.
*
* 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 RcTransactionFeeEstimateController extends AbstractController<TransactionFeeEstimateService> {
static controllerName = 'RcTransactionFeeEstimate';
static requiredPallets = [];
constructor(_api: string) {
const rcApiSpecName = ApiPromiseRegistry.getSpecNameByType('relay')?.values();
const rcSpecName = rcApiSpecName ? Array.from(rcApiSpecName)[0] : undefined;
if (!rcSpecName) {
throw new Error('Relay chain API spec name is not defined.');
}
super(rcSpecName, '/rc/transaction/fee-estimate', new TransactionFeeEstimateService(rcSpecName));
this.initRoutes();
}

protected initRoutes(): void {
this.router.post(this.path, RcTransactionFeeEstimateController.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 rcApi = ApiPromiseRegistry.getApiByType('relay')[0]?.api;

if (!rcApi) {
throw new Error('Relay chain API not found, please use SAS_SUBSTRATE_MULTI_CHAIN_URL env variable');
}

const hash = await rcApi.rpc.chain.getFinalizedHead();

RcTransactionFeeEstimateController.sanitizedSend(
res,
await this.service.fetchTransactionFeeEstimate(rcApi, hash, tx),
);
};
}
Loading
Loading