|
| 1 | +// Copyright 2017-2025 Parity Technologies (UK) Ltd. |
| 2 | +// This file is part of Substrate API Sidecar. |
| 3 | +// |
| 4 | +// Substrate API Sidecar is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +import { RequestHandler } from 'express'; |
| 18 | +import { IAddressParam } from 'src/types/requests'; |
| 19 | + |
| 20 | +import { ApiPromiseRegistry } from '../../../apiRegistry'; |
| 21 | +import { validateAddress } from '../../../middleware'; |
| 22 | +import { RcAccountsProxyInfoService } from '../../../services'; |
| 23 | +import AbstractController from '../../AbstractController'; |
| 24 | + |
| 25 | +export default class RcAccountsProxyInfoController extends AbstractController<RcAccountsProxyInfoService> { |
| 26 | + static controllerName = 'RcAccountsProxyInfo'; |
| 27 | + static requiredPallets = [['Proxy']]; |
| 28 | + constructor(api: string) { |
| 29 | + super(api, '/rc/accounts/:address/proxy-info', new RcAccountsProxyInfoService(api)); |
| 30 | + this.initRoutes(); |
| 31 | + } |
| 32 | + |
| 33 | + protected initRoutes(): void { |
| 34 | + this.router.use(this.path, validateAddress); |
| 35 | + |
| 36 | + this.safeMountAsyncGetHandlers([['', this.getAccountProxyInfo]]); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Get the latest account proxy info of `address` from the relay chain. |
| 41 | + * |
| 42 | + * @param req Express Request |
| 43 | + * @param res Express Response |
| 44 | + */ |
| 45 | + private getAccountProxyInfo: RequestHandler<IAddressParam> = async ( |
| 46 | + { params: { address }, query: { at } }, |
| 47 | + res, |
| 48 | + ): Promise<void> => { |
| 49 | + const rcApi = ApiPromiseRegistry.getApiByType('relay')[0]?.api; |
| 50 | + |
| 51 | + if (!rcApi) { |
| 52 | + throw new Error('Relay chain API not found, please use SAS_SUBSTRATE_MULTI_CHAIN_URL env variable'); |
| 53 | + } |
| 54 | + |
| 55 | + const hash = await this.getHashFromAt(at, { api: rcApi }); |
| 56 | + const historicApi = await rcApi.at(hash); |
| 57 | + |
| 58 | + RcAccountsProxyInfoController.sanitizedSend( |
| 59 | + res, |
| 60 | + await this.service.fetchAccountProxyInfo(hash, rcApi, historicApi, address), |
| 61 | + ); |
| 62 | + }; |
| 63 | +} |
0 commit comments