Skip to content

Commit

Permalink
test: add test for fee-estimate fix (#1506)
Browse files Browse the repository at this point in the history
  • Loading branch information
Imod7 authored Oct 9, 2024
1 parent 68be48b commit c365490
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 3 deletions.
53 changes: 53 additions & 0 deletions src/services/test-helpers/mock/data/block22887036.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/services/test-helpers/mock/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export * from './addresses';
export * from './mockApi';
export * from './mockApiBlock18468942';
export * from './mockApiBlock19772575';
export * from './mockApiBlock22887036';
export * from './mockAssetHubKusamaApi';
export * from './mockAssetHubKusamaApiBlock3356195';
export * from './mockAssetHubKusamaApiBlock6202603';
Expand All @@ -31,5 +32,6 @@ export * from './mockBlock13641102';
export * from './mockBlock18468942';
export * from './mockBlock19772575';
export * from './mockBlock21275366';
export * from './mockBlock22887036';
export * from './mockBlockHashes';
export * from './transactions';
126 changes: 126 additions & 0 deletions src/services/test-helpers/mock/mockApiBlock22887036.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright 2017-2024 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 { ApiPromise } from '@polkadot/api';
import { GenericExtrinsic } from '@polkadot/types';
import { AccountId, Block, Extrinsic, Hash, RuntimeDispatchInfo } from '@polkadot/types/interfaces';

import { polkadotMetadata } from '../../../test-helpers/metadata/metadata';
import { polkadotRegistryV1003000 } from '../../../test-helpers/registries';
import { balancesTransferKeepAliveValid, blockHash789629, mockBlock789629 } from '.';

export const getBlock22887036 = (_hash: Hash): Promise<{ block: Block }> =>
Promise.resolve().then(() => {
return {
block: mockBlock789629,
};
});

export const deriveGetBlock22887036 = (_hash: Hash): Promise<{ block: Block; author: AccountId }> =>
Promise.resolve().then(() => {
return {
author: polkadotRegistryV1003000.createType('AccountId', '1zugcajGg5yDD9TEqKKzGx7iKuGWZMkRbYcyaFnaUaEkwMK'),
block: mockBlock789629,
};
});

const getHeader = (_hash: Hash) => Promise.resolve().then(() => mockBlock789629.header);

const runtimeVersion = {
specName: polkadotRegistryV1003000.createType('Text', 'polkadot'),
specVersion: polkadotRegistryV1003000.createType('u32', 16),
transactionVersion: polkadotRegistryV1003000.createType('u32', 2),
implVersion: polkadotRegistryV1003000.createType('u32', 0),
implName: polkadotRegistryV1003000.createType('Text', 'parity-polkadot'),
authoringVersion: polkadotRegistryV1003000.createType('u32', 0),
};

const getRuntimeVersion = () =>
Promise.resolve().then(() => {
return runtimeVersion;
});

const getMetadata = () => Promise.resolve().then(() => polkadotMetadata);

// For getting the blockhash of the genesis block
const getBlockHashGenesis = (_zero: number) =>
Promise.resolve().then(() =>
polkadotRegistryV1003000.createType(
'BlockHash',
'0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3',
),
);

const runtimeDispatchInfo = polkadotRegistryV1003000.createType('RuntimeDispatchInfo', {
weight: {
refTime: '145570000',
proofSize: '3593',
},
class: 'Normal',
partialFee: '159154905',
});

export const queryInfoCall22887036 = (
_extrinsic: GenericExtrinsic,
_length: Uint8Array,
): Promise<RuntimeDispatchInfo> => Promise.resolve().then(() => runtimeDispatchInfo);

export const queryInfoAt22887036 = (_extrinsic: string, _hash: Hash): Promise<RuntimeDispatchInfo> =>
Promise.resolve().then(() => runtimeDispatchInfo);

export const submitExtrinsic22887036 = (_extrinsic: string): Promise<Hash> =>
Promise.resolve().then(() => polkadotRegistryV1003000.createType('Hash'));

const getStorage = () => Promise.resolve().then(() => polkadotRegistryV1003000.createType('Option<Raw>', '0x00'));

const getFinalizedHead = () => Promise.resolve().then(() => blockHash789629);

export const tx22887036 = (): Extrinsic =>
polkadotRegistryV1003000.createType('Extrinsic', balancesTransferKeepAliveValid);

/**
* Minimal mock polkadot-js ApiPromise. Values are largely meant to be accurate for block
* #22887036, which is what most Service unit tests are based on.
*/
export const mockApiBlock22887036 = {
runtimeVersion,
call: {
transactionPaymentApi: {
queryInfo: queryInfoCall22887036,
},
},
createType: polkadotRegistryV1003000.createType.bind(polkadotRegistryV1003000),
registry: polkadotRegistryV1003000,
rpc: {
chain: {
getHeader,
getBlock22887036,
getBlockHash: getBlockHashGenesis,
getFinalizedHead,
},
state: {
getRuntimeVersion,
getMetadata,
getStorage,
},
payment: {
queryInfo: queryInfoAt22887036,
},
author: {
submitExtrinsic22887036,
},
},
} as unknown as ApiPromise;
31 changes: 31 additions & 0 deletions src/services/test-helpers/mock/mockBlock22887036.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2017-2024 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 { polkadotRegistryV1003000 } from '../../../test-helpers/registries';
import block22887036 from './data/block22887036.json';

/**
* Mock for Polkadot block #22887036.
*/
export const mockBlock22887036 = polkadotRegistryV1003000.createType('Block', block22887036);

/**
* BlockHash for Polkadot block #22887036.
*/
export const blockHash22887036 = polkadotRegistryV1003000.createType(
'BlockHash',
'0x7b713de604a99857f6c25eacc115a4f28d2611a23d9ddff99ab0e4f1c17a8578',
);
8 changes: 7 additions & 1 deletion src/services/test-helpers/mock/transactions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2022 Parity Technologies (UK) Ltd.
// Copyright 2017-2024 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
Expand Down Expand Up @@ -26,3 +26,9 @@ export const balancesTransferValid =
*/
export const balancesTransferInvalid =
'0x250284d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d01022f4deae1532ddd0';

/**
* balances.transferKeepAlive example
*/
export const balancesTransferKeepAliveValid =
'0x49028400fbd63444e492dd33873a848101f6e2269a35a74508ddcd3d1a52c8e36b83a1c100899fead4175903917ad802ccb5ecde1b3ef652fc861f3fc1d6ec021fea9cd79415329f52399ed60def2405eaf150ae139fc7399d5047f63aceffed6793cec604a50139050001050300a6f3fbf3897d10562ac0ca6687fa00bcaa7e8216d732360a787cb60d54160c9f07002c5aba2d';
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"weight": {
"refTime": "145570000",
"proofSize": "3593"
},
"class": "Normal",
"partialFee": "159154905"
}
49 changes: 47 additions & 2 deletions src/services/transaction/TransactionFeeEstimateService.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2022 Parity Technologies (UK) Ltd.
// Copyright 2017-2024 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
Expand All @@ -21,17 +21,22 @@ import { ApiPromise } from '@polkadot/api';
import { Hash } from '@polkadot/types/interfaces';

import { sanitizeNumbers } from '../../sanitize/sanitizeNumbers';
import { polkadotRegistryV9300 } from '../../test-helpers/registries';
import { polkadotRegistryV9300, polkadotRegistryV1003000 } from '../../test-helpers/registries';
import {
balancesTransferInvalid,
balancesTransferKeepAliveValid,
balancesTransferValid,
blockHash789629,
blockHash22887036,
defaultMockApi,
mockApiBlock22887036,
queryInfoAt,
tx22887036,
} from '../test-helpers/mock';
import invalidResponse from '../test-helpers/responses/transaction/feeEstimateInvalid.json';
import validRpcResponse from '../test-helpers/responses/transaction/feeEstimateValidRpcCall.json';
import validRuntimeResponse from '../test-helpers/responses/transaction/feeEstimateValidRuntimeCall.json';
import validRuntimeResponse22887036 from '../test-helpers/responses/transaction/feeEstimateValidRuntimeCall22887036.json';
import { TransactionFeeEstimateService } from './TransactionFeeEstimateService';

const queryInfoCallAt = () =>
Expand Down Expand Up @@ -61,6 +66,35 @@ const mockApi = {

const transactionFeeEstimateService = new TransactionFeeEstimateService(mockApi);

// Mocking the API at block 22887036
const queryInfoCallAt22887036 = () =>
Promise.resolve().then(() =>
polkadotRegistryV1003000.createType('RuntimeDispatchInfoV2', {
weight: {
refTime: '145570000',
proofSize: '3593',
},
class: 'Normal',
partialFee: '159154905',
}),
);

const mockApiAt22887036 = {
call: {
transactionPaymentApi: {
queryInfo: queryInfoCallAt22887036,
},
},
};

const mockApi22887036 = {
...mockApiBlock22887036,
tx: tx22887036,
at: (_hash: Hash) => mockApiAt22887036,
} as unknown as ApiPromise;

const transactionFeeEstimateService22887036 = new TransactionFeeEstimateService(mockApi22887036);

describe('TransactionFeeEstimateService', () => {
describe('fetchTransactionFeeEstimate', () => {
it('Works with a valid transaction', async () => {
Expand All @@ -71,6 +105,17 @@ describe('TransactionFeeEstimateService', () => {
).toStrictEqual(validRuntimeResponse);
});

it('Works with a valid transaction at block 22887036', async () => {
expect(
sanitizeNumbers(
await transactionFeeEstimateService22887036.fetchTransactionFeeEstimate(
blockHash22887036,
balancesTransferKeepAliveValid,
),
),
).toStrictEqual(validRuntimeResponse22887036);
});

it("Should default to the rpc call when the runtime call doesn't exist", async () => {
(mockApiAt.call.transactionPaymentApi.queryInfo as unknown) = undefined;

Expand Down
18 changes: 18 additions & 0 deletions src/test-helpers/metadata/polkadotV1003000Metadata.ts

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/test-helpers/registries/polkadotRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { polkadotMetadataRpcV9190 } from '../metadata/polkadotV9190Metadata';
import { polkadotMetadataRpcV9300 } from '../metadata/polkadotV9300Metadata';
import { polkadotMetadataRpcV9370 } from '../metadata/polkadotV9370Metadata';
import { polkadotMetadataRpcV1000001 } from '../metadata/polkadotV1000001Metadata';
import { polkadotMetadataRpcV1003000 } from '../metadata/polkadotV1003000Metadata';
/**
* Create a type registry for Polkadot.
* Useful for creating types in order to facilitate testing.
Expand Down Expand Up @@ -84,3 +85,8 @@ export const polkadotRegistryV9370 = createPolkadotRegistry(9370, polkadotMetada
* Polkadot v1000001 TypeRegistry
*/
export const polkadotRegistryV1000001 = createPolkadotRegistry(1000001, polkadotMetadataRpcV1000001);

/**
* Polkadot v1003000 TypeRegistry
*/
export const polkadotRegistryV1003000 = createPolkadotRegistry(1003000, polkadotMetadataRpcV1003000);

0 comments on commit c365490

Please sign in to comment.