@substrate/api-sidecar
Version:
REST service that makes it easy to interact with blockchain nodes built using Substrate's FRAME framework.
93 lines • 4.29 kB
JavaScript
;
// 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/>.
Object.defineProperty(exports, "__esModule", { value: true });
exports.TransactionDryRunService = void 0;
const http_errors_1 = require("http-errors");
const responses_1 = require("../../types/responses");
const AbstractService_1 = require("../AbstractService");
const extractCauseAndStack_1 = require("./extractCauseAndStack");
class TransactionDryRunService extends AbstractService_1.AbstractService {
async dryRuntExtrinsic(api, senderAddress, transaction, hash, xcmVersion) {
var _a;
if (!api.call.dryRunApi) {
throw new http_errors_1.BadRequest('DryRunApi not found in metadata.');
}
else if (!api.call.dryRunApi.dryRunCall) {
throw new http_errors_1.BadRequest('dryRunCall not found in metadata.');
}
// We can assume we have the DryRunApi and dry_run_call since we check the api above.
const hasXcmParam = api.registry.metadata.apis
.find((a) => a.name.toString() === 'DryRunApi')
.methods.find((method) => method.name.toString() === 'dry_run_call')
.inputs.find((p) => p.name.toString() === 'result_xcms_version');
let foundXcmVersion = xcmVersion;
if (hasXcmParam && !foundXcmVersion && ((_a = api.query.xcmPallet) === null || _a === void 0 ? void 0 : _a.safeXcmVersion)) {
const version = await api.query.xcmPallet.safeXcmVersion();
if (version.isNone) {
throw (0, http_errors_1.BadRequest)('No safe xcm version found on chain.');
}
foundXcmVersion = version.unwrap().toNumber();
}
try {
const originCaller = {
System: {
Signed: senderAddress,
},
};
const [dryRunResponse, { number }] = await Promise.all([
!hasXcmParam
? api.call.dryRunApi.dryRunCall(originCaller, transaction)
: api.call.dryRunApi.dryRunCall(originCaller, transaction, foundXcmVersion),
hash ? api.rpc.chain.getHeader(hash) : { number: null },
]);
const response = dryRunResponse;
return {
at: {
hash: hash ? hash : '',
height: number ? number.unwrap().toString(10) : '0',
},
result: {
resultType: response.isOk
? response.asOk.executionResult.isOk
? responses_1.TransactionResultType.DispatchOutcome
: responses_1.TransactionResultType.DispatchError
: responses_1.ValidityErrorType.Invalid,
result: response.isOk
? response.asOk.executionResult.isOk
? response.asOk.executionResult.asOk
: response.asOk.executionResult.asErr
: response.asErr,
},
};
}
catch (err) {
const { cause, stack } = (0, extractCauseAndStack_1.extractCauseAndStack)(err);
throw {
at: {
hash,
},
code: 400,
error: 'Unable to dry-run transaction',
transaction,
cause,
stack,
};
}
}
}
exports.TransactionDryRunService = TransactionDryRunService;
//# sourceMappingURL=TransactionDryRunService.js.map