git.delta.rocks / unique-network / refs/commits / 535a8e928ce8

difftreelog

source

tests/src/eth/util/playgrounds/index.ts4.1 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// SPDX-License-Identifier: Apache-2.034import * as path from 'path';5import {IKeyringPair} from '@polkadot/types/types';67import config from '../../../config';89import {EthUniqueHelper} from './unique.dev';10import {SilentLogger, SilentConsole} from '../../../util/playgrounds/unique.dev';1112export {EthUniqueHelper} from './unique.dev';1314import chai from 'chai';15import chaiAsPromised from 'chai-as-promised';16import chaiLike from 'chai-like';17import {getTestSeed, requirePalletsOrSkip} from '../../../util/playgrounds';1819chai.use(chaiAsPromised);20chai.use(chaiLike);21export const expect = chai.expect;2223export const usingEthPlaygrounds = async (code: (helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair>) => Promise<void>) => {24  const silentConsole = new SilentConsole();25  silentConsole.enable();2627  const helper = new EthUniqueHelper(new SilentLogger());2829  try {30    await helper.connect(config.substrateUrl);31    await helper.connectWeb3(config.substrateUrl);32    const ss58Format = helper.chain.getChainProperties().ss58Format;33    const privateKey = async (seed: string | {filename: string}) => {34      if (typeof seed === 'string') {35        return helper.util.fromSeed(seed, ss58Format);36      }37      else {38        const actualSeed = getTestSeed(seed.filename);39        let account = helper.util.fromSeed(actualSeed, ss58Format);40        if (await helper.balance.getSubstrate(account.address) == 0n) {41          console.warn(`${path.basename(seed.filename)}: Not enough funds present on the filename account. Using the default one as the donor instead.`);42          account = helper.util.fromSeed('//Alice', ss58Format);43        }44        return account;45      }46    };47    await code(helper, privateKey);48  }49  finally {50    await helper.disconnect();51    await helper.disconnectWeb3();52    silentConsole.disable();53  }54};55  56export async function itEth(name: string, cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {57  (opts.only ? it.only : 58    opts.skip ? it.skip : it)(name, async function() {59    await usingEthPlaygrounds(async (helper, privateKey) => {60      if (opts.requiredPallets) {61        requirePalletsOrSkip(this, helper, opts.requiredPallets);62      }6364      await cb({helper, privateKey});65    });66  });67}6869export async function itEthIfWithPallet(name: string, required: string[], cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {70  return itEth(name, cb, {requiredPallets: required, ...opts});71}7273itEth.only = (name: string, cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itEth(name, cb, {only: true});74itEth.skip = (name: string, cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itEth(name, cb, {skip: true});7576itEthIfWithPallet.only = (name: string, required: string[], cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itEthIfWithPallet(name, required, cb, {only: true});77itEthIfWithPallet.skip = (name: string, required: string[], cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itEthIfWithPallet(name, required, cb, {skip: true});78itEth.ifWithPallets = itEthIfWithPallet;7980type ElementOf<A> = A extends readonly (infer T)[] ? T : never;81// I want a fancier api, not a memory efficiency82export function* cartesian<T extends Array<Array<any>>, R extends Array<any>>(internalRest: [...R], ...args: [...T]): Generator<[...R, ...{[K in keyof T]: ElementOf<T[K]>}]> {83  if(args.length === 0) {84    yield internalRest as any;85    return;86  }87  for(const value of args[0]) {88    yield* cartesian([...internalRest, value], ...args.slice(1)) as any;89  }90}