-
Notifications
You must be signed in to change notification settings - Fork 150
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
- Loading branch information
Showing
10 changed files
with
165 additions
and
7 deletions.
There are no files selected for viewing
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
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,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, | ||
}; | ||
} | ||
} |
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
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[]; | ||
} |
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