@substrate/api-sidecar
Version:
REST service that makes it easy to interact with blockchain nodes built using Substrate's FRAME framework.
120 lines • 7.18 kB
JavaScript
;
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-call */
// 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 });
const apiRegistry_1 = require("../../apiRegistry");
const polkadotV15Metadata_1 = require("../../test-helpers/metadata/polkadotV15Metadata");
const registries_1 = require("../../test-helpers/registries");
const responses_1 = require("../../types/responses");
const mock_1 = require("../test-helpers/mock");
const mockDryRunCall_1 = require("../test-helpers/mock/mockDryRunCall");
const mockDryRunError_1 = require("../test-helpers/mock/mockDryRunError");
const TransactionDryRunService_1 = require("./TransactionDryRunService");
const mockMetadataAtVersion = () =>
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
Promise.resolve().then(() => {
return registries_1.polkadotRegistryV15.createType('Option<OpaqueMetadata>', polkadotV15Metadata_1.polkadotMetadataRpcV15);
});
const mockMetadataVersions = jest.fn().mockResolvedValue({
toHuman: jest.fn().mockReturnValue(['14', '15', '4,294,967,295']), // or whatever versions you want
});
const mockSafeXcmVersion = () => Promise.resolve().then(() => {
return registries_1.polkadotRegistryV15.createType('Option<u32>', 5);
});
const runtimeDryRun = registries_1.polkadotRegistryV15.createType('Result<CallDryRunEffects, XcmDryRunApiError>', mockDryRunCall_1.mockDryRunCallResult);
const runtimeDryRunError = registries_1.polkadotRegistryV15.createType('Result<CallDryRunEffects, XcmDryRunApiError>', mockDryRunError_1.mockDryRunCallError);
const mockDryRunCall = () => Promise.resolve().then(() => runtimeDryRun);
const mockDryRunError = () => Promise.resolve().then(() => runtimeDryRunError);
const mockHistoricApi = {
call: {
dryRunApi: {
dryRunCall: mockDryRunCall,
},
metadata: {
metadataVersions: mockMetadataVersions,
metadataAtVersion: mockMetadataAtVersion,
},
},
query: {
xcmPallet: {
safeXcmVersion: mockSafeXcmVersion,
},
},
registry: registries_1.polkadotRegistryV15,
};
const mockApi = {
...mockHistoricApi,
rpc: {
chain: {
getHeader: () => Promise.resolve(mock_1.blockHash22887036),
},
},
at: (_hash) => mockHistoricApi,
};
describe('TransactionDryRunService', () => {
beforeAll(() => {
jest.spyOn(apiRegistry_1.ApiPromiseRegistry, 'getApi').mockImplementation(() => {
return mockApi;
});
});
const sendersAddress = '5HBuLJz9LdkUNseUEL6DLeVkx2bqEi6pQr8Ea7fS4bzx7i7E';
it('Should correctly execute a dry run for a submittable executable', async () => {
const executionResult = await new TransactionDryRunService_1.TransactionDryRunService('mock').dryRuntExtrinsic(sendersAddress, '0xfc041f0801010100411f0100010100c224aad9c6f3bbd784120e9fceee5bfd22a62c69144ee673f76d6a34d280de160104000002043205040091010000000000', mock_1.blockHash22887036, 4);
expect(executionResult === null || executionResult === void 0 ? void 0 : executionResult.at.hash).toEqual(mock_1.blockHash22887036);
const resData = executionResult === null || executionResult === void 0 ? void 0 : executionResult.result.result;
expect(executionResult === null || executionResult === void 0 ? void 0 : executionResult.result.resultType).toEqual(responses_1.TransactionResultType.DispatchOutcome);
expect(resData.paysFee.toString()).toEqual(mockDryRunCall_1.mockDryRunCallResult.Ok.executionResult.Ok.paysFee);
});
it('should correctly execute a dry run for a payload extrinsic', async () => {
const payloadTx = '0xf81f0801010100411f0100010100c224aad9c6f3bbd784120e9fceee5bfd22a62c69144ee673f76d6a34d280de16010400000204320504009101000000000045022800010000e0510f00040000000000000000000000000000000000000000000000000000000000000000000000be2554aa8a0151eb4d706308c47d16996af391e4c5e499c7cbef24259b7d4503';
const executionResult = await new TransactionDryRunService_1.TransactionDryRunService('mock').dryRuntExtrinsic(sendersAddress, payloadTx, mock_1.blockHash22887036, 4);
const resData = executionResult === null || executionResult === void 0 ? void 0 : executionResult.result.result;
expect(resData.paysFee.toString()).toEqual(mockDryRunCall_1.mockDryRunCallResult.Ok.executionResult.Ok.paysFee);
});
it('should correctly execute a dry run for a call', async () => {
const callTx = '0x1f0801010100411f0100010100c224aad9c6f3bbd784120e9fceee5bfd22a62c69144ee673f76d6a34d280de160104000002043205040091010000000000';
const executionResult = await new TransactionDryRunService_1.TransactionDryRunService('mock').dryRuntExtrinsic(sendersAddress, callTx);
expect(executionResult === null || executionResult === void 0 ? void 0 : executionResult.at.hash).toEqual('');
const resData = executionResult === null || executionResult === void 0 ? void 0 : executionResult.result.result;
expect(resData.paysFee.toString()).toEqual(mockDryRunCall_1.mockDryRunCallResult.Ok.executionResult.Ok.paysFee);
});
it('should correctly execute a dry run for a call and return an error', async () => {
const mockApiErr = {
...mockHistoricApi,
call: {
dryRunApi: {
dryRunCall: mockDryRunError,
},
},
query: {
xcmPallet: {
safeXcmVersion: mockSafeXcmVersion,
},
},
};
jest.spyOn(apiRegistry_1.ApiPromiseRegistry, 'getApi').mockImplementation(() => {
return mockApiErr;
});
const callTx = '0x0a0000fe06fc3db07fb1a4ce89a76eaed1e54519b5940d2652b8d6794ad4ddfcdcb16c0f00d0eca2b99401';
const executionResult = await new TransactionDryRunService_1.TransactionDryRunService('mock').dryRuntExtrinsic(sendersAddress, callTx);
expect(executionResult === null || executionResult === void 0 ? void 0 : executionResult.at.hash).toEqual('');
const resData = executionResult === null || executionResult === void 0 ? void 0 : executionResult.result.result;
expect(resData.asToken.toString()).toEqual(mockDryRunError_1.mockDryRunCallError.Ok.executionResult.Err.Token);
});
});
//# sourceMappingURL=TransactionDryRunService.spec.js.map