1234import * 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}293031export const usingEthPlaygrounds = async (code: (helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair>) => 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 = async (seed: string | {filename: string}) => {42 if (typeof seed === 'string') {43 return helper.util.fromSeed(seed, ss58Format);44 }45 else {46 const actualSeed = getTestSeed(seed.filename);47 let account = helper.util.fromSeed(actualSeed, ss58Format);48 if (await helper.balance.getSubstrate(account.address) < MINIMUM_DONOR_FUND) {49 console.warn(`${path.basename(seed.filename)}: Not enough funds present on the filename account. Using the default one as the donor instead.`);50 account = helper.util.fromSeed('//Alice', ss58Format);51 }52 return account;53 }54 };55 await code(helper, privateKey);56 }57 finally {58 await helper.disconnect();59 silentConsole.disable();60 }61};62 63export function itEth(name: string, cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {64 (opts.only ? it.only : 65 opts.skip ? it.skip : it)(name, async function() {66 await usingEthPlaygrounds(async (helper, privateKey) => {67 if (opts.requiredPallets) {68 requirePalletsOrSkip(this, helper, opts.requiredPallets);69 }7071 await cb({helper, privateKey});72 });73 });74}7576export 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[] } = {}) {77 return itEth(name, cb, {requiredPallets: required, ...opts});78}7980itEth.only = (name: string, cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itEth(name, cb, {only: true});81itEth.skip = (name: string, cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itEth(name, cb, {skip: true});8283itEthIfWithPallet.only = (name: string, required: string[], cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itEthIfWithPallet(name, required, cb, {only: true});84itEthIfWithPallet.skip = (name: string, required: string[], cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itEthIfWithPallet(name, required, cb, {skip: true});85itEth.ifWithPallets = itEthIfWithPallet;8687export function itSchedEth(88 name: string,89 cb: (schedKind: SchedKind, apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any,90 opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {},91) {92 itEth(name + ' (anonymous scheduling)', (apis) => cb('anon', apis), opts);93 itEth(name + ' (named scheduling)', (apis) => cb('named', apis), opts);94}95itSchedEth.only = (name: string, cb: (schedKind: SchedKind, apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itSchedEth(name, cb, {only: true});96itSchedEth.skip = (name: string, cb: (schedKind: SchedKind, apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itSchedEth(name, cb, {skip: true});97itSchedEth.ifWithPallets = itSchedIfWithPallets;9899function 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[] } = {}) {100 return itSchedEth(name, cb, {requiredPallets: required, ...opts});101}