@polkadot/types/interfaces#RuntimeVersion TypeScript Examples

The following examples show how to use @polkadot/types/interfaces#RuntimeVersion. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ChainInfo.tsx    From crust-apps with Apache License 2.0 6 votes vote down vote up
function ChainInfo ({ className }: Props): React.ReactElement<Props> {
  const { t } = useTranslation();
  const { api, isApiReady } = useApi();
  const runtimeVersion = useCall<RuntimeVersion>(isApiReady && api.rpc.state.subscribeRuntimeVersion);
  const { ipnsChain } = useIpfs();
  const [isEndpointsVisible, toggleEndpoints] = useToggle();
  const canToggle = !ipnsChain;

  return (
    <div className={className}>
      <div
        className={`apps--SideBar-logo-inner${canToggle ? ' isClickable' : ''} highlight--color-contrast`}
        onClick={toggleEndpoints}
      >
        <ChainImg />
        <div className='info media--1000'>
          <Chain className='chain' />
          {runtimeVersion && (
            <div className='runtimeVersion'>{t<string>('version {{version}}', { replace: { version: runtimeVersion.specVersion.toNumber() } })}</div>
          )}
          <BestNumber
            className='bestNumber'
            label='#'
          />
        </div>
        {canToggle && (
          <Icon
            className='dropdown'
            icon={isEndpointsVisible ? 'caret-right' : 'caret-down'}
          />
        )}
      </div>
      {isEndpointsVisible && (
        <Endpoints onClose={toggleEndpoints} />
      )}
    </div>
  );
}
Example #2
Source File: rpcs.ts    From crust-apps with Apache License 2.0 6 votes vote down vote up
export function getAllRpc (registry: Registry, chain: Text, { specName }: RuntimeVersion): Record<string, Record<string, DefinitionRpcExt>> {
  return Object
    .entries(getSpecRpc(registry, chain, specName))
    .reduce((all: Record<string, Record<string, DefinitionRpcExt>>, [section, contents]): Record<string, Record<string, DefinitionRpcExt>> => {
      all[section] ??= toExt(section, contents);

      return all;
    }, { ...jsonrpc });
}
Example #3
Source File: rpcs.ts    From subscan-multisig-react with Apache License 2.0 6 votes vote down vote up
export function getAllRpc(
  registry: Registry,
  chain: Text,
  { specName }: RuntimeVersion
): Record<string, Record<string, DefinitionRpcExt>> {
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  // @ts-ignore
  return Object.entries(getSpecRpc(registry, chain, specName)).reduce(
    (
      all: Record<string, Record<string, DefinitionRpcExt>>,
      [section, contents]
    ): Record<string, Record<string, DefinitionRpcExt>> => {
      all[section] = all[section] ?? toExt(section, contents);

      return all;
    },
    { ...jsonrpc }
  );
}
Example #4
Source File: SubstrateService.ts    From squid with GNU General Public License v3.0 5 votes vote down vote up
async runtimeVersion(hash: Hash): Promise<RuntimeVersion> {
    return this.apiCall((api) => api.rpc.state.getRuntimeVersion(hash))
  }
Example #5
Source File: base-provider.ts    From bodhi.js with Apache License 2.0 4 votes vote down vote up
startSubscription = async (): Promise<any> => {
    this._cache = new BlockCache(this.maxBlockCacheSize);

    if (this.maxBlockCacheSize < 1) {
      return logger.throwError(
        `expect maxBlockCacheSize > 0, but got ${this.maxBlockCacheSize}`,
        Logger.errors.INVALID_ARGUMENT
      );
    } else {
      this.maxBlockCacheSize > 9999 && logger.warn(CACHE_SIZE_WARNING);
    }

    await this.isReady();

    const subscriptionMethod = this.safeMode
      ? this.api.rpc.chain.subscribeFinalizedHeads.bind(this)
      : this.api.rpc.chain.subscribeNewHeads.bind(this);

    subscriptionMethod(async (header: Header) => {
      // cache
      const blockNumber = header.number.toNumber();
      const blockHash = (await this.api.rpc.chain.getBlockHash(blockNumber)).toHex();
      const txHashes = await this._getTxHashesAtBlock(blockHash);

      this._cache!.addTxsAtBlock(blockNumber, txHashes);

      // eth_subscribe
      // TODO: can do some optimizations
      if (this._listeners[NEW_HEADS]?.length > 0) {
        const block = await this.getBlock(blockNumber);
        const response = hexlifyRpcResult(block);
        this._listeners[NEW_HEADS].forEach((l) => l.cb(response));
      }

      if (this._listeners[NEW_LOGS]?.length > 0) {
        const block = await this._getBlock(header.number.toHex(), false);
        const receipts = await Promise.all(
          block.transactions.map((tx) => this.getTransactionReceiptAtBlock(tx as string, header.number.toHex()))
        );

        const logs = receipts.map((r) => r.logs).flat();

        this._listeners[NEW_LOGS]?.forEach(({ cb, filter }) => {
          const filteredLogs = logs.filter((l) => filterLog(l, filter));
          const response = hexlifyRpcResult(filteredLogs);
          response.forEach((log: any) => cb(log));
        });
      }
    }) as unknown as void;

    // for getTXhashFromNextBlock
    this.api.rpc.chain.subscribeNewHeads((header: Header) => {
      this._newBlockListeners.forEach((cb) => {
        try {
          cb(header);
        } catch {
          /* swallow */
        }
      });
      this._newBlockListeners = [];
    }) as unknown as void;

    this.api.rpc.chain.subscribeFinalizedHeads(async (header: Header) => {
      const blockNumber = header.number.toNumber();
      this.latestFinalizedBlockNumber = blockNumber;

      // safe mode only, if useful in the future, can remove this if condition
      if (this.safeMode) {
        const blockHash = (await this.api.rpc.chain.getBlockHash(blockNumber)).toHex();
        this.latestFinalizedBlockHash = blockHash;
      }
    }) as unknown as void;

    this.api.rpc.state.subscribeRuntimeVersion((runtime: RuntimeVersion) => {
      const version = runtime.specVersion.toNumber();
      this.verbose && logger.info(`runtime version: ${version}`);

      if (!this.runtimeVersion || this.runtimeVersion === version) {
        this.runtimeVersion = version;
      } else {
        logger.warn(
          `runtime version changed: ${this.runtimeVersion} => ${version}, shutting down myself... good bye ?`
        );
        process?.exit(1);
      }
    }) as unknown as void;
  };