-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add pallets/on-going-referenda endpoint (#1471)
* feat: add pallets/on-going-referenda endpoint * updated docs * error handling when call not available - added also a test for that case * added test that returns referenda (call available) * replaced unnecessary Promise.all * small but nice improvements - added block height in the error msg (Tarik's review) - added a Promise.all (Filippo's review)
- Loading branch information
Showing
18 changed files
with
611 additions
and
8 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/controllers/pallets/PalletsOnGoingReferendaController.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// 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 { RequestHandler } from 'express'; | ||
|
||
import { PalletsOnGoingReferendaService } from '../../services'; | ||
import AbstractController from '../AbstractController'; | ||
|
||
export default class PalletsOnGoingReferendaController extends AbstractController<PalletsOnGoingReferendaService> { | ||
constructor(api: ApiPromise) { | ||
super(api, '/pallets/on-going-referenda', new PalletsOnGoingReferendaService(api)); | ||
this.initRoutes(); | ||
} | ||
|
||
protected initRoutes(): void { | ||
this.safeMountAsyncGetHandlers([['', this.getPalletOnGoingReferenda]]); | ||
} | ||
|
||
/** | ||
* Get the on-going referenda. | ||
* | ||
* @param _req Express Request | ||
* @param res Express Response | ||
*/ | ||
private getPalletOnGoingReferenda: RequestHandler = async ({ query: { at } }, res): Promise<void> => { | ||
const hash = await this.getHashFromAt(at); | ||
|
||
PalletsOnGoingReferendaController.sanitizedSend(res, await this.service.derivePalletOnGoingReferenda(hash)); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/services/pallets/PalletsOnGoingReferendaService.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// 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 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 { polkadotRegistryV1000001 } from '../../test-helpers/registries'; | ||
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'; | ||
|
||
// 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 mockApi13641102 = { | ||
rpc: { | ||
chain: { | ||
getHeader: getHeader13641102, | ||
}, | ||
}, | ||
at: (_hash: Hash) => mockHistoricApi13641102, | ||
} as unknown as ApiPromise; | ||
|
||
// 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: referendaEntriesAt21275366, | ||
}, | ||
}, | ||
}, | ||
} as unknown as ApiDecoration<'promise'>; | ||
|
||
const mockApi21275366 = { | ||
rpc: { | ||
chain: { | ||
getHeader: getHeader21275366, | ||
}, | ||
}, | ||
at: (_hash: Hash) => mockHistoricApi21275366, | ||
} as unknown as ApiPromise; | ||
|
||
// Mock PalletsOnGoingReferendaService instance for block #21275366 | ||
const palletsOnGoingReferendaService21275366 = new PalletsOnGoingReferendaService(mockApi21275366); | ||
|
||
describe('PalletOnGoingReferendaService', () => { | ||
describe('derivePalletOnGoingReferenda', () => { | ||
it('throws error for block 13641102', async () => { | ||
await expect( | ||
palletsOnGoingReferendaService13641102.derivePalletOnGoingReferenda(blockHash13641102), | ||
).rejects.toStrictEqual( | ||
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 () => { | ||
expect( | ||
sanitizeNumbers(await palletsOnGoingReferendaService21275366.derivePalletOnGoingReferenda(blockHash21275366)), | ||
).toStrictEqual(fetchOnGoingReferenda21275366Response); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// 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 { BlockHash } from '@polkadot/types/interfaces'; | ||
|
||
import { IPalletOnGoingReferenda, IReferendaInfo } from '../../types/responses'; | ||
import { AbstractService } from '../AbstractService'; | ||
|
||
export class PalletsOnGoingReferendaService extends AbstractService { | ||
/** | ||
* Fetch all on-going referenda that have track: root (0) and whitelisted (1), along | ||
* with their associated information. | ||
* | ||
* @param hash `BlockHash` to make call at | ||
*/ | ||
async derivePalletOnGoingReferenda(hash: BlockHash): Promise<IPalletOnGoingReferenda> { | ||
const { api } = this; | ||
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(); | ||
for (const referendum of referendaEntries) { | ||
const referendumInfo = referendum[1]; | ||
if (referendumInfo.isSome) { | ||
const refUnwrapped = referendumInfo.unwrap(); | ||
const refId = referendum[0].toHuman() as string[]; | ||
if ( | ||
refUnwrapped.type == 'Ongoing' && | ||
(refUnwrapped.asOngoing.track.toHuman() == '0' || refUnwrapped.asOngoing.track.toHuman() == '1') | ||
) { | ||
const decisionDeposit = refUnwrapped.asOngoing.decisionDeposit.isSome | ||
? refUnwrapped.asOngoing.decisionDeposit.unwrap() | ||
: null; | ||
const enactment = refUnwrapped.asOngoing.enactment; | ||
const submitted = refUnwrapped.asOngoing.submitted; | ||
const deciding = refUnwrapped.asOngoing.deciding.isSome ? refUnwrapped.asOngoing.deciding.unwrap() : null; | ||
|
||
const refInfo = { id: refId[0], decisionDeposit, enactment, submitted, deciding }; | ||
referenda.push(refInfo); | ||
} | ||
} | ||
} | ||
} else { | ||
throw new Error( | ||
`The runtime does not include the module 'api.query.referenda' at this block height: ${number | ||
.unwrap() | ||
.toString(10)}`, | ||
); | ||
} | ||
|
||
const at = { | ||
hash, | ||
height: number.unwrap().toString(10), | ||
}; | ||
|
||
return { | ||
at, | ||
referenda, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.