git.delta.rocks / unique-network / refs/commits / a478235c0b6e

difftreelog

source

js-packages/tests/eth/util/index.ts4.9 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// SPDX-License-Identifier: Apache-2.034import * as path from 'path';5import type {IKeyringPair} from '@polkadot/types/types';67import config from '../../config.js';89import {EthUniqueHelper} from './playgrounds/unique.dev.js';10import {SilentLogger, SilentConsole} from '@unique/playgrounds/unique.dev.js';11import {makeNames} from '../../util/index.js';12import type {SchedKind} from '../../util/index.js';1314import chai from 'chai';15import chaiAsPromised from 'chai-as-promised';16import chaiLike from 'chai-like';17import {getTestSeed, MINIMUM_DONOR_FUND, requirePalletsOrSkip} from '../../util/index.js';1819chai.use(chaiAsPromised);20chai.use(chaiLike);21export const expect = chai.expect;2223export enum SponsoringMode {24  Disabled = 0,25  Allowlisted = 1,26  Generous = 2,27}2829type PrivateKeyFn = (seed: string | {filename?: string, url?: string}) => Promise<IKeyringPair>;3031export const usingEthPlaygrounds = async (code: (helper: EthUniqueHelper, privateKey: PrivateKeyFn) => Promise<void>) => {32  const silentConsole = new SilentConsole();33  silentConsole.enable();3435  const helper = new EthUniqueHelper(new SilentLogger());3637  try {38    await helper.connect(config.substrateUrl);39    await helper.connectWeb3(config.substrateUrl);40    const ss58Format = helper.chain.getChainProperties().ss58Format;41    const privateKey: PrivateKeyFn = async (seed) => {42      if(typeof seed === 'string') {43        return helper.util.fromSeed(seed, ss58Format);44      }45      if(seed.url) {46        const {filename} = makeNames(seed.url);47        seed.filename = filename;48      } else if(seed.filename) {49        // Pass50      } else {51        throw new Error('no url nor filename set');52      }53      const actualSeed = getTestSeed(seed.filename);54      let account = helper.util.fromSeed(actualSeed, ss58Format);55      if(await helper.balance.getSubstrate(account.address) < MINIMUM_DONOR_FUND) {56        console.warn(`${path.basename(seed.filename)}: Not enough funds present on the filename account. Using the default one as the donor instead.`);57        account = helper.util.fromSeed('//Alice', ss58Format);58      }59      return account;60    };61    await code(helper, privateKey);62  }63  finally {64    await helper.disconnect();65    silentConsole.disable();66  }67};6869export function itEth(name: string, cb: (apis: { helper: EthUniqueHelper, privateKey: PrivateKeyFn }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {70  (opts.only ? it.only :71    opts.skip ? it.skip : it)(name, async function() {72    await usingEthPlaygrounds(async (helper, privateKey) => {73      if(opts.requiredPallets) {74        requirePalletsOrSkip(this, helper, opts.requiredPallets);75      }7677      await cb({helper, privateKey});78    });79  });80}8182export function itEthIfWithPallet(name: string, required: string[], cb: (apis: { helper: EthUniqueHelper, privateKey: PrivateKeyFn }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {83  return itEth(name, cb, {requiredPallets: required, ...opts});84}8586itEth.only = (name: string, cb: (apis: { helper: EthUniqueHelper, privateKey: PrivateKeyFn }) => any) => itEth(name, cb, {only: true});87itEth.skip = (name: string, cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itEth(name, cb, {skip: true});8889itEthIfWithPallet.only = (name: string, required: string[], cb: (apis: { helper: EthUniqueHelper, privateKey: PrivateKeyFn }) => any) => itEthIfWithPallet(name, required, cb, {only: true});90itEthIfWithPallet.skip = (name: string, required: string[], cb: (apis: { helper: EthUniqueHelper, privateKey: PrivateKeyFn }) => any) => itEthIfWithPallet(name, required, cb, {skip: true});91itEth.ifWithPallets = itEthIfWithPallet;9293export function itSchedEth(94  name: string,95  cb: (schedKind: SchedKind, apis: { helper: EthUniqueHelper, privateKey: PrivateKeyFn }) => any,96  opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {},97) {98  itEth(name + ' (anonymous scheduling)', (apis) => cb('anon', apis), opts);99  itEth(name + ' (named scheduling)', (apis) => cb('named', apis), opts);100}101itSchedEth.only = (name: string, cb: (schedKind: SchedKind, apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itSchedEth(name, cb, {only: true});102itSchedEth.skip = (name: string, cb: (schedKind: SchedKind, apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itSchedEth(name, cb, {skip: true});103itSchedEth.ifWithPallets = itSchedIfWithPallets;104105function itSchedIfWithPallets(name: string, required: string[], cb: (schedKind: SchedKind, apis: { helper: EthUniqueHelper, privateKey: PrivateKeyFn }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {106  return itSchedEth(name, cb, {requiredPallets: required, ...opts});107}