|
15 | 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | 16 |
|
17 | 17 | import { ApiPromise } from '@polkadot/api'; |
| 18 | +import { ISidecarConfig } from 'src/types/sidecar-config'; |
18 | 19 |
|
19 | 20 | import { controllers } from '../controllers'; |
20 | 21 | import AbstractController from '../controllers/AbstractController'; |
@@ -49,7 +50,7 @@ import { shidenControllers } from './shidenControllers'; |
49 | 50 | import { soraControllers } from './soraControllers'; |
50 | 51 | import { westendControllers } from './westendControllers'; |
51 | 52 |
|
52 | | -const specToControllerMap: { [x: string]: ControllerConfig } = { |
| 53 | +export const specToControllerMap: { [x: string]: ControllerConfig } = { |
53 | 54 | westend: westendControllers, |
54 | 55 | polkadot: polkadotControllers, |
55 | 56 | polymesh: polymeshControllers, |
@@ -117,3 +118,39 @@ function getControllersFromConfig(api: ApiPromise, config: ControllerConfig) { |
117 | 118 | return acc; |
118 | 119 | }, [] as AbstractController<AbstractService>[]); |
119 | 120 | } |
| 121 | + |
| 122 | +/** |
| 123 | + * Return an array of instantiated controller instances based off of a `specName`. |
| 124 | + * @param pallets pallets available to define controllers |
| 125 | + * @param api ApiPromise to inject into controllers |
| 126 | + * @param specName specName of chain to get options |
| 127 | + */ |
| 128 | + |
| 129 | +export const getControllersByPallets = (pallets: string[], api: ApiPromise, specName: string) => { |
| 130 | + const controllersSet: AbstractController<AbstractService>[] = []; |
| 131 | + const config = specToControllerMap?.[specName]?.options || defaultControllers?.options; |
| 132 | + |
| 133 | + Object.values(controllers).forEach((controller) => { |
| 134 | + if (controller.canInjectByPallets(pallets)) { |
| 135 | + controllersSet.push(new controller(api, config)); |
| 136 | + } |
| 137 | + }); |
| 138 | + |
| 139 | + return controllersSet; |
| 140 | +}; |
| 141 | + |
| 142 | +export const getControllers = ( |
| 143 | + api: ApiPromise, |
| 144 | + config: ISidecarConfig, |
| 145 | + specName: string, |
| 146 | +): AbstractController<AbstractService>[] => { |
| 147 | + if (config.EXPRESS.INJECTED_CONTROLLERS) { |
| 148 | + return getControllersByPallets( |
| 149 | + (api.registry.metadata.toJSON().pallets as unknown as Record<string, unknown>[]).map((p) => p.name as string), |
| 150 | + api, |
| 151 | + specName, |
| 152 | + ); |
| 153 | + } else { |
| 154 | + return getControllersForSpec(api, specName); |
| 155 | + } |
| 156 | +}; |
0 commit comments