Skip to content

Commit

Permalink
feat: add pallets/on-going-referenda endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Imod7 committed Aug 11, 2024
1 parent a4bbcb8 commit 646d6fb
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/chains-config/defaultControllers.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 @@ -43,6 +43,7 @@ export const defaultControllers: ControllerConfig = {
'PalletsErrors',
'PalletsEvents',
'PalletsForeignAssets',
'PalletsOnGoingReferenda',
'PalletsStakingProgress',
'PalletsStorage',
'Paras',
Expand Down
3 changes: 2 additions & 1 deletion src/chains-config/kusamaControllers.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 @@ -41,6 +41,7 @@ export const kusamaControllers: ControllerConfig = {
'PalletsErrors',
'PalletsEvents',
'PalletsNominationPools',
'PalletsOnGoingReferenda',
'PalletsStakingProgress',
'PalletsStakingValidators',
'PalletsStorage',
Expand Down
3 changes: 2 additions & 1 deletion src/chains-config/polkadotControllers.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 @@ -41,6 +41,7 @@ export const polkadotControllers: ControllerConfig = {
'PalletsErrors',
'PalletsEvents',
'PalletsNominationPools',
'PalletsOnGoingReferenda',
'PalletsStakingProgress',
'PalletsStakingValidators',
'PalletsStorage',
Expand Down
4 changes: 3 additions & 1 deletion src/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2023 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 @@ -37,6 +37,7 @@ import {
PalletsEvents,
PalletsForeignAssets,
PalletsNominationPools,
PalletsOnGoingReferenda,
PalletsPoolAssets,
PalletsStakingProgress,
PalletsStakingValidators,
Expand Down Expand Up @@ -72,6 +73,7 @@ export const controllers = {
PalletsEvents,
PalletsForeignAssets,
PalletsNominationPools,
PalletsOnGoingReferenda,
PalletsPoolAssets,
PalletsStakingProgress,
PalletsStakingValidators,
Expand Down
44 changes: 44 additions & 0 deletions src/controllers/pallets/PalletsOnGoingReferendaController.ts
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));
};
}
3 changes: 2 additions & 1 deletion src/controllers/pallets/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2023 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 @@ -22,6 +22,7 @@ export { default as PalletsErrors } from './PalletsErrorsController';
export { default as PalletsEvents } from './PalletsEventsController';
export { default as PalletsForeignAssets } from './PalletsForeignAssetsController';
export { default as PalletsNominationPools } from './PalletsNominationPoolsController';
export { default as PalletsOnGoingReferenda } from './PalletsOnGoingReferendaController';
export { default as PalletsPoolAssets } from './PalletsPoolAssetsController';
export { default as PalletsStakingProgress } from './PalletsStakingProgressController';
export { default as PalletsStakingValidators } from './PalletsStakingValidatorsController';
Expand Down
69 changes: 69 additions & 0 deletions src/services/pallets/PalletsOnGoingReferendaService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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 = await api.at(hash);
const [{ number }, referendaEntries] = await Promise.all([
api.rpc.chain.getHeader(hash),
historicApi.query.referenda.referendumInfoFor.entries(),
]);

const referenda: IReferendaInfo[] = [];
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);
}
}
}

const at = {
hash,
height: number.unwrap().toString(10),
};

return {
at,
referenda,
};
}
}
3 changes: 2 additions & 1 deletion src/services/pallets/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2023 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 @@ -22,6 +22,7 @@ export * from './PalletsConstantsService';
export * from './PalletsDispatchablesService';
export * from './PalletsForeignAssetsService';
export * from './PalletsNominationPoolsService';
export * from './PalletsOnGoingReferendaService';
export * from './PalletsPoolAssetsService';
export * from './PalletsStakingProgressService';
export * from './PalletsStakingValidatorsService';
Expand Down
37 changes: 37 additions & 0 deletions src/types/responses/PalletOnGoingReferenda.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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 { u32 } from '@polkadot/types';
import {
FrameSupportScheduleDispatchTime,
PalletReferendaDecidingStatus,
PalletReferendaDeposit,
} from '@polkadot/types/lookup';

import { IAt } from './At';

export interface IReferendaInfo {
id: string;
decisionDeposit: PalletReferendaDeposit | null;
enactment: FrameSupportScheduleDispatchTime;
submitted: u32;
deciding: PalletReferendaDecidingStatus | null;
}

export interface IPalletOnGoingReferenda {
at: IAt;
referenda: IReferendaInfo[];
}
3 changes: 2 additions & 1 deletion src/types/responses/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2023 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 @@ -46,6 +46,7 @@ export * from './PalletErrorsItem';
export * from './PalletEvents';
export * from './PalletEventsItem';
export * from './PalletNominationPools';
export * from './PalletOnGoingReferenda';
export * from './PalletStakingProgress';
export * from './PalletStakingValidators';
export * from './PalletStorage';
Expand Down

0 comments on commit 646d6fb

Please sign in to comment.