git.delta.rocks / unique-network / refs/commits / 3aa44993a093

difftreelog

source

tests/src/eth/util/index.ts5.0 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 './playgrounds/unique.dev';10import {SilentLogger, SilentConsole} from '../../util/playgrounds/unique.dev';11import {SchedKind} from '../../util';1213export {EthUniqueHelper} from './playgrounds/unique.dev';1415import chai from 'chai';16import chaiAsPromised from 'chai-as-promised';17import chaiLike from 'chai-like';18import {getTestSeed, MINIMUM_DONOR_FUND, requirePalletsOrSkip} from '../../util';1920chai.use(chaiAsPromised);21chai.use(chaiLike);22export const expect = chai.expect;2324export enum SponsoringMode {25  Disabled = 0,26  Allowlisted = 1,27  Generous = 2,28}2930export const usingEthPlaygrounds = async (code: (helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair>) => Promise<void>) => {31  const silentConsole = new SilentConsole();32  silentConsole.enable();3334  const helper = new EthUniqueHelper(new SilentLogger());3536  try {37    await helper.connect(config.substrateUrl);38    await helper.connectWeb3(config.substrateUrl);39    const ss58Format = helper.chain.getChainProperties().ss58Format;40    const privateKey = async (seed: string | {filename: string}) => {41      if (typeof seed === 'string') {42        return helper.util.fromSeed(seed, ss58Format);43      }44      else {45        const actualSeed = getTestSeed(seed.filename);46        let account = helper.util.fromSeed(actualSeed, ss58Format);47        if (await helper.balance.getSubstrate(account.address) < MINIMUM_DONOR_FUND) {48          console.warn(`${path.basename(seed.filename)}: Not enough funds present on the filename account. Using the default one as the donor instead.`);49          account = helper.util.fromSeed('//Alice', ss58Format);50        }51        return account;52      }53    };54    await code(helper, privateKey);55  }56  finally {57    await helper.disconnect();58    silentConsole.disable();59  }60};6162export function itEth(name: string, cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {63  (opts.only ? it.only :64    opts.skip ? it.skip : it)(name, async function() {65    await usingEthPlaygrounds(async (helper, privateKey) => {66      if (opts.requiredPallets) {67        requirePalletsOrSkip(this, helper, opts.requiredPallets);68      }6970      await cb({helper, privateKey});71    });72  });73}7475export 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[] } = {}) {76  return itEth(name, cb, {requiredPallets: required, ...opts});77}7879itEth.only = (name: string, cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itEth(name, cb, {only: true});80itEth.skip = (name: string, cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itEth(name, cb, {skip: true});8182itEthIfWithPallet.only = (name: string, required: string[], cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itEthIfWithPallet(name, required, cb, {only: true});83itEthIfWithPallet.skip = (name: string, required: string[], cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itEthIfWithPallet(name, required, cb, {skip: true});84itEth.ifWithPallets = itEthIfWithPallet;8586export function itSchedEth(87  name: string,88  cb: (schedKind: SchedKind, apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any,89  opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {},90) {91  itEth(name + ' (anonymous scheduling)', (apis) => cb('anon', apis), opts);92  itEth(name + ' (named scheduling)', (apis) => cb('named', apis), opts);93}94itSchedEth.only = (name: string, cb: (schedKind: SchedKind, apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itSchedEth(name, cb, {only: true});95itSchedEth.skip = (name: string, cb: (schedKind: SchedKind, apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itSchedEth(name, cb, {skip: true});96itSchedEth.ifWithPallets = itSchedIfWithPallets;9798function itSchedIfWithPallets(name: string, required: string[], cb: (schedKind: SchedKind, apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {99  return itSchedEth(name, cb, {requiredPallets: required, ...opts});100}