diff --git a/packages/apps-config/src/api/rpc/moonbeam.ts b/packages/apps-config/src/api/rpc/moonbeam.ts index c4f60ec17a..cc78640e89 100644 --- a/packages/apps-config/src/api/rpc/moonbeam.ts +++ b/packages/apps-config/src/api/rpc/moonbeam.ts @@ -4,7 +4,7 @@ import type { DefinitionRpc, DefinitionRpcParam } from '@polkadot/types/types'; export default function (): Record> { - function generateDescription (description:string, returnType:string, params:DefinitionRpcParam[] = []):DefinitionRpc { + function generateDescription (description:string, returnType:string, params:DefinitionRpcParam[] = []): DefinitionRpc { return { description, params, diff --git a/packages/react-components/src/InputRpc/SelectMethod.tsx b/packages/react-components/src/InputRpc/SelectMethod.tsx index ef2a0ebd44..19a5b2adb7 100644 --- a/packages/react-components/src/InputRpc/SelectMethod.tsx +++ b/packages/react-components/src/InputRpc/SelectMethod.tsx @@ -4,11 +4,10 @@ import type { DefinitionRpcExt } from '@polkadot/types/types'; import type { DropdownOptions } from '../util/types'; -import React from 'react'; - -import jsonrpc from '@polkadot/types/interfaces/jsonrpc'; +import React, { useCallback } from 'react'; import Dropdown from '../Dropdown'; +import rpcs from './rpcs'; import { classes } from '../util'; interface Props { @@ -19,14 +18,11 @@ interface Props { value: DefinitionRpcExt; } -function transform ({ value: { section } }: Props): (method: string) => DefinitionRpcExt { - return function (method: string): DefinitionRpcExt { - return jsonrpc[section][method]; - }; -} - -function SelectMethod (props: Props): React.ReactElement | null { - const { className = '', isError, onChange, options, value } = props; +function SelectMethod ({ className = '', isError, onChange, options, value }: Props): React.ReactElement | null { + const _transform = useCallback( + () => rpcs[value.section][value.method], + [value] + ); if (!options.length) { return null; @@ -38,7 +34,7 @@ function SelectMethod (props: Props): React.ReactElement | null { isError={isError} onChange={onChange} options={options} - transform={transform(props)} + transform={_transform} value={value.method} withLabel={false} /> diff --git a/packages/react-components/src/InputRpc/index.tsx b/packages/react-components/src/InputRpc/index.tsx index f28f655a76..b65497716d 100644 --- a/packages/react-components/src/InputRpc/index.tsx +++ b/packages/react-components/src/InputRpc/index.tsx @@ -8,13 +8,13 @@ import type { DropdownOptions } from '../util/types'; import React, { useCallback, useEffect, useState } from 'react'; import { useApi } from '@polkadot/react-hooks'; -import jsonrpc from '@polkadot/types/interfaces/jsonrpc'; import LinkedWrapper from '../InputExtrinsic/LinkedWrapper'; import SelectMethod from './SelectMethod'; import SelectSection from './SelectSection'; import methodOptions from './options/method'; import sectionOptions from './options/section'; +import rpcs from './rpcs'; interface Props { className?: string; @@ -43,7 +43,7 @@ function InputRpc ({ className = '', defaultValue, help, label, onChange, withLa } // set via callback since the method is a function itself - setValue((): DefinitionRpcExt => newValue); + setValue(() => newValue); }, [value] ); @@ -57,7 +57,7 @@ function InputRpc ({ className = '', defaultValue, help, label, onChange, withLa const optionsMethod = methodOptions(api, section); setOptionsMethod(optionsMethod); - _onMethodChange(jsonrpc[section][optionsMethod[0].value]); + _onMethodChange(rpcs[section][optionsMethod[0].value]); }, [_onMethodChange, api, value] ); diff --git a/packages/react-components/src/InputRpc/options/method.tsx b/packages/react-components/src/InputRpc/options/method.tsx index 8123765135..504e478b9c 100644 --- a/packages/react-components/src/InputRpc/options/method.tsx +++ b/packages/react-components/src/InputRpc/options/method.tsx @@ -6,10 +6,11 @@ import type { DropdownOption, DropdownOptions } from '../../util/types'; import React from 'react'; import { ApiPromise } from '@polkadot/api'; -import jsonrpc from '@polkadot/types/interfaces/jsonrpc'; + +import rpcs from '../rpcs'; export default function createOptions (api: ApiPromise, sectionName: string): DropdownOptions { - const section = jsonrpc[sectionName]; + const section = rpcs[sectionName]; if (!section || Object.keys((api.rpc as Record>)[sectionName]).length === 0) { return []; @@ -20,9 +21,9 @@ export default function createOptions (api: ApiPromise, sectionName: string): Dr .sort() .map((methodName) => section[methodName]) .filter((ext): ext is DefinitionRpcExt => !!ext) - .filter(({ isSubscription }): boolean => !isSubscription) + .filter(({ isSubscription }) => !isSubscription) .map(({ description, method, params }): DropdownOption => { - const inputs = params.map(({ name }): string => name).join(', '); + const inputs = params.map(({ name }) => name).join(', '); return { className: 'ui--DropdownLinked-Item', diff --git a/packages/react-components/src/InputRpc/options/section.ts b/packages/react-components/src/InputRpc/options/section.ts index 975f86b0d0..b77e53be09 100644 --- a/packages/react-components/src/InputRpc/options/section.ts +++ b/packages/react-components/src/InputRpc/options/section.ts @@ -9,7 +9,7 @@ export default function createOptions (api: ApiPromise): DropdownOptions { return Object .keys(api.rpc) .sort() - .filter((section): boolean => Object.keys((api.rpc as Record>)[section]).length !== 0) + .filter((section) => Object.keys((api.rpc as Record>)[section]).length !== 0) .map((name): { text: string; value: string } => ({ text: name, value: name diff --git a/packages/react-components/src/InputRpc/rpcs.ts b/packages/react-components/src/InputRpc/rpcs.ts new file mode 100644 index 0000000000..70eecf8d19 --- /dev/null +++ b/packages/react-components/src/InputRpc/rpcs.ts @@ -0,0 +1,29 @@ +// Copyright 2017-2020 @polkadot/react-components authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import type { DefinitionRpc, DefinitionRpcExt } from '@polkadot/types/types'; + +import { typesRpc } from '@polkadot/apps-config'; +import jsonrpc from '@polkadot/types/interfaces/jsonrpc'; + +function toExt (section: string, input: Record): Record { + return Object.entries(input).reduce((output: Record, [method, def]): Record => { + output[method] = { + isSubscription: false, + jsonrpc: `${method}_${section}`, + method, + section, + ...def + }; + + return output; + }, {}); +} + +const all = Object.entries(typesRpc).reduce((all: Record>, [section, contents]): Record> => { + all[section] ??= toExt(section, contents); + + return all; +}, { ...jsonrpc }); + +export default all;