git.delta.rocks / unique-network / refs/commits / 9fb4be30085d

difftreelog

source

js-packages/test-utils/eth/util.ts4.8 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 '../../tests/config.js';89import {EthUniqueHelper} from './index.js';10import {SilentLogger, SilentConsole} from '@unique/test-utils';11import type {SchedKind} from '@unique/test-utils/util.js';1213import chai from 'chai';14import chaiAsPromised from 'chai-as-promised';15import chaiLike from 'chai-like';16import {getTestSeed, MINIMUM_DONOR_FUND, requirePalletsOrSkip, makeNames} from '@unique/test-utils/util.js';1718chai.use(chaiAsPromised);19chai.use(chaiLike);20export const expect = chai.expect;2122export enum SponsoringMode {23  Disabled = 0,24  Allowlisted = 1,25  Generous = 2,26}2728type PrivateKeyFn = (seed: string | {filename?: string, url?: string}) => Promise<IKeyringPair>;2930export const usingEthPlaygrounds = async (code: (helper: EthUniqueHelper, privateKey: PrivateKeyFn) => 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: PrivateKeyFn = async (seed) => {41      if(typeof seed === 'string') {42        return helper.util.fromSeed(seed, ss58Format);43      }44      if(seed.url) {45        const {filename} = makeNames(seed.url);46        seed.filename = filename;47      } else if(seed.filename) {48        // Pass49      } else {50        throw new Error('no url nor filename set');51      }52      const actualSeed = getTestSeed(seed.filename);53      let account = helper.util.fromSeed(actualSeed, ss58Format);54      if(await helper.balance.getSubstrate(account.address) < MINIMUM_DONOR_FUND) {55        console.warn(`${path.basename(seed.filename)}: Not enough funds present on the filename account. Using the default one as the donor instead.`);56        account = helper.util.fromSeed('//Alice', ss58Format);57      }58      return account;59    };60    await code(helper, privateKey);61  }62  finally {63    await helper.disconnect();64    silentConsole.disable();65  }66};6768export function itEth(name: string, cb: (apis: { helper: EthUniqueHelper, privateKey: PrivateKeyFn }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {69  (opts.only ? it.only :70    opts.skip ? it.skip : it)(name, async function() {71    await usingEthPlaygrounds(async (helper, privateKey) => {72      if(opts.requiredPallets) {73        requirePalletsOrSkip(this, helper, opts.requiredPallets);74      }7576      await cb({helper, privateKey});77    });78  });79}8081export function itEthIfWithPallet(name: string, required: string[], cb: (apis: { helper: EthUniqueHelper, privateKey: PrivateKeyFn }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {82  return itEth(name, cb, {requiredPallets: required, ...opts});83}8485itEth.only = (name: string, cb: (apis: { helper: EthUniqueHelper, privateKey: PrivateKeyFn }) => any) => itEth(name, cb, {only: true});86itEth.skip = (name: string, cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itEth(name, cb, {skip: true});8788itEthIfWithPallet.only = (name: string, required: string[], cb: (apis: { helper: EthUniqueHelper, privateKey: PrivateKeyFn }) => any) => itEthIfWithPallet(name, required, cb, {only: true});89itEthIfWithPallet.skip = (name: string, required: string[], cb: (apis: { helper: EthUniqueHelper, privateKey: PrivateKeyFn }) => any) => itEthIfWithPallet(name, required, cb, {skip: true});90itEth.ifWithPallets = itEthIfWithPallet;9192export function itSchedEth(93  name: string,94  cb: (schedKind: SchedKind, apis: { helper: EthUniqueHelper, privateKey: PrivateKeyFn }) => any,95  opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {},96) {97  itEth(name + ' (anonymous scheduling)', (apis) => cb('anon', apis), opts);98  itEth(name + ' (named scheduling)', (apis) => cb('named', apis), opts);99}100itSchedEth.only = (name: string, cb: (schedKind: SchedKind, apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itSchedEth(name, cb, {only: true});101itSchedEth.skip = (name: string, cb: (schedKind: SchedKind, apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itSchedEth(name, cb, {skip: true});102itSchedEth.ifWithPallets = itSchedIfWithPallets;103104function itSchedIfWithPallets(name: string, required: string[], cb: (schedKind: SchedKind, apis: { helper: EthUniqueHelper, privateKey: PrivateKeyFn }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {105  return itSchedEth(name, cb, {requiredPallets: required, ...opts});106}