Skip to content

Commit

Permalink
small but nice improvements
Browse files Browse the repository at this point in the history
- added block height in the error msg (Tarik's review)
- added a Promise.all (Filippo's review)
  • Loading branch information
Imod7 committed Aug 14, 2024
1 parent 2bf433d commit 73e7242
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
37 changes: 22 additions & 15 deletions src/services/pallets/PalletsOnGoingReferendaService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,56 @@ import type { Hash } from '@polkadot/types/interfaces';
import { sanitizeNumbers } from '../../sanitize/sanitizeNumbers';
import { polkadotRegistryV9300 } from '../../test-helpers/registries';
import { polkadotRegistryV1000001 } from '../../test-helpers/registries';
import { blockHash13641102, blockHash21275366, defaultMockApi, mockBlock21275366 } from '../test-helpers/mock';
import { blockHash13641102, blockHash21275366, mockBlock13641102, 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 = {
// Mocking APIs for block #13641102
const getHeader13641102 = (_hash: Hash) => Promise.resolve().then(() => mockBlock13641102.header);

const mockHistoricApi13641102 = {
query: {},
registry: polkadotRegistryV9300,
} as unknown as ApiDecoration<'promise'>;

const mockApi = {
...defaultMockApi,
at: (_hash: Hash) => mockHistoricApi,
const mockApi13641102 = {
rpc: {
chain: {
getHeader: getHeader13641102,
},
},
at: (_hash: Hash) => mockHistoricApi13641102,
} as unknown as ApiPromise;
/**
* Mock PalletsOnGoingReferendaService instance.
*/
const palletsOnGoingReferendaService13641102 = new PalletsOnGoingReferendaService(mockApi);

const referendaEntriesAt = () => Promise.resolve().then(() => referendaEntries());
// Mock PalletsOnGoingReferendaService instance for block #13641102
const palletsOnGoingReferendaService13641102 = new PalletsOnGoingReferendaService(mockApi13641102);

// Mocking APIs for block #21275366
const getHeader21275366 = (_hash: Hash) => Promise.resolve().then(() => mockBlock21275366.header);
const referendaEntriesAt21275366 = () => Promise.resolve().then(() => referendaEntries());

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

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

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

// Mock PalletsOnGoingReferendaService instance for block #21275366
const palletsOnGoingReferendaService21275366 = new PalletsOnGoingReferendaService(mockApi21275366);

describe('PalletOnGoingReferendaService', () => {
Expand All @@ -72,7 +79,7 @@ describe('PalletOnGoingReferendaService', () => {
await expect(
palletsOnGoingReferendaService13641102.derivePalletOnGoingReferenda(blockHash13641102),
).rejects.toStrictEqual(
new Error(`The runtime does not include the module 'api.query.referenda' at this block height.`),
new Error(`The runtime does not include the module 'api.query.referenda' at this block height: 13641102`),
);
});
it('works for block 21275366 (Polkadot) & returns 7 referendas, 2 of which are runtime upgrades', async () => {
Expand Down
9 changes: 6 additions & 3 deletions src/services/pallets/PalletsOnGoingReferendaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ export class PalletsOnGoingReferendaService extends AbstractService {
*/
async derivePalletOnGoingReferenda(hash: BlockHash): Promise<IPalletOnGoingReferenda> {
const { api } = this;
const historicApi = await api.at(hash);
const { number } = await api.rpc.chain.getHeader(hash);
const [historicApi, { number }] = await Promise.all([api.at(hash), api.rpc.chain.getHeader(hash)]);
const referenda: IReferendaInfo[] = [];
if (historicApi.query.referenda) {
const referendaEntries = await historicApi.query.referenda.referendumInfoFor.entries();
Expand All @@ -54,7 +53,11 @@ export class PalletsOnGoingReferendaService extends AbstractService {
}
}
} else {
throw new Error(`The runtime does not include the module 'api.query.referenda' at this block height.`);
throw new Error(
`The runtime does not include the module 'api.query.referenda' at this block height: ${number
.unwrap()
.toString(10)}`,
);
}

const at = {
Expand Down

0 comments on commit 73e7242

Please sign in to comment.