git.delta.rocks / unique-network / refs/commits / 8d226d67b0fa

difftreelog

source

tests/src/util/playgrounds/index.ts2.7 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// SPDX-License-Identifier: Apache-2.034import {IKeyringPair} from '@polkadot/types/types';5import config from '../../config';6import '../../interfaces/augment-api-events';7import * as defs from '../../interfaces/definitions';8import {ApiPromise, WsProvider} from '@polkadot/api';9import { UniqueHelper } from './unique';101112class SilentLogger {13  log(msg: any, level: any): void { }14  level = {15    ERROR: 'ERROR' as const,16    WARNING: 'WARNING' as const,17    INFO: 'INFO' as const,18  };19}202122class DevUniqueHelper extends UniqueHelper {23  async connect(wsEndpoint: string, listeners?: any): Promise<void> {24    const wsProvider = new WsProvider(wsEndpoint);25    this.api = new ApiPromise({26      provider: wsProvider, 27      signedExtensions: {28        ContractHelpers: {29          extrinsic: {},30          payload: {},31        },32        FakeTransactionFinalizer: {33          extrinsic: {},34          payload: {},35        },36      },37      rpc: {38        unique: defs.unique.rpc,39        rmrk: defs.rmrk.rpc,40        eth: {41          feeHistory: {42            description: 'Dummy',43            params: [],44            type: 'u8',45          },46          maxPriorityFeePerGas: {47            description: 'Dummy',48            params: [],49            type: 'u8',50          },51        },52      },53    });54    await this.api.isReadyOrError;55    this.network = await UniqueHelper.detectNetwork(this.api);56  }57}585960export const usingPlaygrounds = async <T = void> (code: (helper: UniqueHelper, privateKey: (seed: string) => IKeyringPair) => Promise<T>) => {61  // TODO: Remove, this is temporary: Filter unneeded API output62  // (Jaco promised it will be removed in the next version)63  const consoleErr = console.error;64  const consoleLog = console.log;65  const consoleWarn = console.warn;66  let result: T = null as unknown as T;67  68  const outFn = (printer: any) => (...args: any[]) => {69    for (const arg of args) {70      if (typeof arg !== 'string')71        continue;72      if (arg.includes('1000:: Normal connection closure' || arg === 'Normal connection closure'))73        return;74    }75    printer(...args);76  };7778  console.error = outFn(consoleErr.bind(console));79  console.log = outFn(consoleLog.bind(console));80  console.warn = outFn(consoleWarn.bind(console));81  const helper = new DevUniqueHelper(new SilentLogger());8283  try {84    await helper.connect(config.substrateUrl);85    const ss58Format = helper.chain.getChainProperties().ss58Format;86    const privateKey = (seed: string) => helper.util.fromSeed(seed, ss58Format);87    result = await code(helper, privateKey);88  }89  finally {90    await helper.disconnect();91    console.error = consoleErr;92    console.log = consoleLog;93    console.warn = consoleWarn;94  }95  return result as T;96};