Skip to content

Commit

Permalink
added test that returns referenda (call available)
Browse files Browse the repository at this point in the history
  • Loading branch information
Imod7 committed Aug 12, 2024
1 parent 7f7828c commit aef2a8d
Show file tree
Hide file tree
Showing 6 changed files with 314 additions and 3 deletions.
41 changes: 38 additions & 3 deletions src/services/pallets/PalletsOnGoingReferendaService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ import type { ApiPromise } from '@polkadot/api';
import type { ApiDecoration } from '@polkadot/api/types';
import type { Hash } from '@polkadot/types/interfaces';

import { sanitizeNumbers } from '../../sanitize/sanitizeNumbers';
import { polkadotRegistryV9300 } from '../../test-helpers/registries';
import { blockHash13641102, defaultMockApi } from '../test-helpers/mock';
import { polkadotRegistryV1000001 } from '../../test-helpers/registries';
import { blockHash13641102, blockHash21275366, defaultMockApi, mockBlock21275366 } from '../test-helpers/mock';
import { referendaEntries } from '../test-helpers/mock/data/referendaEntries';
import fetchOnGoingReferenda21275366Response from '../test-helpers/responses/pallets/fetchOnGoingReferenda21275366.json';
import { PalletsOnGoingReferendaService } from './PalletsOnGoingReferendaService';

const mockHistoricApi = {
Expand All @@ -34,16 +38,47 @@ const mockApi = {
/**
* Mock PalletsOnGoingReferendaService instance.
*/
const palletsOnGoingReferendaService = new PalletsOnGoingReferendaService(mockApi);
const palletsOnGoingReferendaService13641102 = new PalletsOnGoingReferendaService(mockApi);

const referendaEntriesAt = () => Promise.resolve().then(() => referendaEntries());

const mockHistoricApi21275366 = {
registry: polkadotRegistryV1000001,
query: {
referenda: {
referendumInfoFor: {
entries: referendaEntriesAt,
},
},
},
} as unknown as ApiDecoration<'promise'>;

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

const mockApi21275366 = {
rpc: {
chain: {
getHeader,
},
},
at: (_hash: Hash) => mockHistoricApi21275366,
} as unknown as ApiPromise;

const palletsOnGoingReferendaService21275366 = new PalletsOnGoingReferendaService(mockApi21275366);

describe('PalletOnGoingReferendaService', () => {
describe('derivePalletOnGoingReferenda', () => {
it('throws error for block 13641102', async () => {
await expect(
palletsOnGoingReferendaService.derivePalletOnGoingReferenda(blockHash13641102),
palletsOnGoingReferendaService13641102.derivePalletOnGoingReferenda(blockHash13641102),
).rejects.toStrictEqual(
new Error(`The runtime does not include the module 'api.query.referenda' at this block height.`),
);
});
it('works for block 21275366 (Polkadot) & returns 7 referendas, 2 of which are runtime upgrades', async () => {
expect(
sanitizeNumbers(await palletsOnGoingReferendaService21275366.derivePalletOnGoingReferenda(blockHash21275366)),
).toStrictEqual(fetchOnGoingReferenda21275366Response);
});
});
});
36 changes: 36 additions & 0 deletions src/services/test-helpers/mock/data/block21275366.json

Large diffs are not rendered by default.

110 changes: 110 additions & 0 deletions src/services/test-helpers/mock/data/referendaEntries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// 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 { polkadotMetadataRpcV1000001 } from '../../../../test-helpers/metadata/polkadotV1000001Metadata';
import { polkadotRegistryV1000001 } from '../../../../test-helpers/registries';
import { createApiWithAugmentations, TypeFactory } from '../../../../test-helpers/typeFactory';

const typeFactoryApiV9370 = createApiWithAugmentations(polkadotMetadataRpcV1000001);
const factory = new TypeFactory(typeFactoryApiV9370);

export const referendaEntries = () => {
function createReferendumKey(key: string) {
return factory.storageKey(key, 'u32', typeFactoryApiV9370.query.referenda.referendumInfoFor);
}

function createReferendumInfo(
decisionDeposit: { who: string; amount: string } | null,
submitted: number,
deciding: { since: number; confirming: number | null } | null,
enactment: { after: number } | { at: number },
) {
const decisionDepositVal =
decisionDeposit === null
? null
: {
amount:
decisionDeposit.amount === null
? null
: polkadotRegistryV1000001.createType('Balance', decisionDeposit.amount),
who: polkadotRegistryV1000001.createType('AccountId', decisionDeposit.who),
};
const decidingVal =
deciding === null
? null
: {
confirming:
deciding.confirming === null
? null
: polkadotRegistryV1000001.createType('Compact<BlockNumber>', deciding.confirming),
since: polkadotRegistryV1000001.createType('Compact<BlockNumber>', deciding.since),
};

const onGoingRefInfo = {
ongoing: {
decisionDeposit: decisionDepositVal,
enactment: enactment,
submitted: polkadotRegistryV1000001.createType('Compact<BlockNumber>', submitted),
deciding: decidingVal,
},
};
const onGoingRefInfoEntry = polkadotRegistryV1000001.createType(
'Option<PalletReferendaReferendumInfo>',
onGoingRefInfo,
);
return onGoingRefInfoEntry;
}

const referendaEntries = [];
for (let i = 0; i < 7; i++) {
const referendaIds = ['890', '857', '841', '852', '872', '832', '888'];
const referendumKey = createReferendumKey(referendaIds[i]);

const decisionDeposit = [
null,
{ who: '1EpEiYpWRAWmte4oPLtR5B1TZFxcBShBdjK4X9wWnq2KfLK', amount: '1000000000000000' },
{ who: '13sDzot2hwoEAzXJiNe3cBiMEq19XRqrS3DMAxt9jiSNKMkA', amount: '100000000000000' },
null,
{ who: '13sDzot2hwoEAzXJiNe3cBiMEq19XRqrS3DMAxt9jiSNKMkA', amount: '100000000000000' },
{ who: '13sDzot2hwoEAzXJiNe3cBiMEq19XRqrS3DMAxt9jiSNKMkA', amount: '1000000000000000' },
{ who: '13RKipguGKrVofVeLgMJTThA8dzJvT6Ce1jycY4Nm8cwitsQ', amount: '100000000000000' },
];
const enactment = [
{ after: 10 },
{ after: 100 },
{ at: 21455000 },
{ after: 100 },
{ at: 21558000 },
{ after: 10 },
{ after: 10 },
];
const submitted = [21274219, 21171395, 21108384, 21157690, 21212837, 21070067, 21268637];
const deciding = [
null,
null,
{ since: 21109667, confirming: 21275466 },
null,
{ since: 21214334, confirming: null },
{ since: 21071802, confirming: null },
{ since: 21269732, confirming: null },
];
const referendumInfo = createReferendumInfo(decisionDeposit[i], submitted[i], deciding[i], enactment[i]);

referendaEntries.push([referendumKey, referendumInfo]);
}

return referendaEntries;
};
1 change: 1 addition & 0 deletions src/services/test-helpers/mock/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ export * from './mockBlock6202603';
export * from './mockBlock13641102';
export * from './mockBlock18468942';
export * from './mockBlock19772575';
export * from './mockBlock21275366';
export * from './mockBlockHashes';
export * from './transactions';
31 changes: 31 additions & 0 deletions src/services/test-helpers/mock/mockBlock21275366.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 { polkadotRegistryV1000001 } from '../../../test-helpers/registries';
import block21275366 from './data/block21275366.json';

/**
* Mock for Polkadot Block #21275366.
*/
export const mockBlock21275366 = polkadotRegistryV1000001.createType('Block', block21275366);

/**
* BlockHash for Polkadot Block #21275366.
*/
export const blockHash21275366 = polkadotRegistryV1000001.createType(
'BlockHash',
'0x109d98b52cd3c76801e6881ec5c746eac9a3d5e6017b0b2883a85d6b2907f5dc',
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"at": {
"hash": "0x109d98b52cd3c76801e6881ec5c746eac9a3d5e6017b0b2883a85d6b2907f5dc",
"height": "21275366"
},
"referenda": [
{
"id": "890",
"decisionDeposit": null,
"enactment": {
"after": "10"
},
"submitted": "21274219",
"deciding": null
},
{
"id": "857",
"decisionDeposit": {
"who": "1EpEiYpWRAWmte4oPLtR5B1TZFxcBShBdjK4X9wWnq2KfLK",
"amount": "1000000000000000"
},
"enactment": {
"after": "100"
},
"submitted": "21171395",
"deciding": null
},
{
"id": "841",
"decisionDeposit": {
"who": "13sDzot2hwoEAzXJiNe3cBiMEq19XRqrS3DMAxt9jiSNKMkA",
"amount": "100000000000000"
},
"enactment": {
"at": "21455000"
},
"submitted": "21108384",
"deciding": {
"since": "21109667",
"confirming": "21275466"
}
},
{
"id": "852",
"decisionDeposit": null,
"enactment": {
"after": "100"
},
"submitted": "21157690",
"deciding": null
},
{
"id": "872",
"decisionDeposit": {
"who": "13sDzot2hwoEAzXJiNe3cBiMEq19XRqrS3DMAxt9jiSNKMkA",
"amount": "100000000000000"
},
"enactment": {
"at": "21558000"
},
"submitted": "21212837",
"deciding": {
"since": "21214334",
"confirming": null
}
},
{
"id": "832",
"decisionDeposit": {
"who": "13sDzot2hwoEAzXJiNe3cBiMEq19XRqrS3DMAxt9jiSNKMkA",
"amount": "1000000000000000"
},
"enactment": {
"after": "10"
},
"submitted": "21070067",
"deciding": {
"since": "21071802",
"confirming": null
}
},
{
"id": "888",
"decisionDeposit": {
"who": "13RKipguGKrVofVeLgMJTThA8dzJvT6Ce1jycY4Nm8cwitsQ",
"amount": "100000000000000"
},
"enactment": {
"after": "10"
},
"submitted": "21268637",
"deciding": {
"since": "21269732",
"confirming": null
}
}
]
}

0 comments on commit aef2a8d

Please sign in to comment.