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, makeNames} 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}2930type PrivateKeyFn = (seed: string | {filename?: string, url?: string}) => Promise<IKeyringPair>;3132export const usingEthPlaygrounds = async (code: (helper: EthUniqueHelper, privateKey: PrivateKeyFn) => Promise<void>) => {33 const silentConsole = new SilentConsole();34 silentConsole.enable();3536 const helper = new EthUniqueHelper(new SilentLogger());3738 try {39 await helper.connect(config.substrateUrl);40 await helper.connectWeb3(config.substrateUrl);41 const ss58Format = helper.chain.getChainProperties().ss58Format;42 const privateKey: PrivateKeyFn = async (seed) => {43 if(typeof seed === 'string') {44 return helper.util.fromSeed(seed, ss58Format);45 }46 if(seed.url) {47 const {filename} = makeNames(seed.url);48 seed.filename = filename;49 } else if(seed.filename) {50 51 } else {52 throw new Error('no url nor filename set');53 }54 const actualSeed = getTestSeed(seed.filename);55 let account = helper.util.fromSeed(actualSeed, ss58Format);56 if(await helper.balance.getSubstrate(account.address) < MINIMUM_DONOR_FUND) {57 console.warn(`${path.basename(seed.filename)}: Not enough funds present on the filename account. Using the default one as the donor instead.`);58 account = helper.util.fromSeed('//Alice', ss58Format);59 }60 return account;61 };62 await code(helper, privateKey);63 }64 finally {65 await helper.disconnect();66 silentConsole.disable();67 }68};6970export function itEth(name: string, cb: (apis: { helper: EthUniqueHelper, privateKey: PrivateKeyFn }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {71 (opts.only ? it.only :72 opts.skip ? it.skip : it)(name, async function() {73 await usingEthPlaygrounds(async (helper, privateKey) => {74 if(opts.requiredPallets) {75 requirePalletsOrSkip(this, helper, opts.requiredPallets);76 }7778 await cb({helper, privateKey});79 });80 });81}8283export function itEthIfWithPallet(name: string, required: string[], cb: (apis: { helper: EthUniqueHelper, privateKey: PrivateKeyFn }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {84 return itEth(name, cb, {requiredPallets: required, ...opts});85}8687itEth.only = (name: string, cb: (apis: { helper: EthUniqueHelper, privateKey: PrivateKeyFn }) => any) => itEth(name, cb, {only: true});88itEth.skip = (name: string, cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itEth(name, cb, {skip: true});8990itEthIfWithPallet.only = (name: string, required: string[], cb: (apis: { helper: EthUniqueHelper, privateKey: PrivateKeyFn }) => any) => itEthIfWithPallet(name, required, cb, {only: true});91itEthIfWithPallet.skip = (name: string, required: string[], cb: (apis: { helper: EthUniqueHelper, privateKey: PrivateKeyFn }) => any) => itEthIfWithPallet(name, required, cb, {skip: true});92itEth.ifWithPallets = itEthIfWithPallet;9394export function itSchedEth(95 name: string,96 cb: (schedKind: SchedKind, apis: { helper: EthUniqueHelper, privateKey: PrivateKeyFn }) => any,97 opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {},98) {99 itEth(name + ' (anonymous scheduling)', (apis) => cb('anon', apis), opts);100 itEth(name + ' (named scheduling)', (apis) => cb('named', apis), opts);101}102itSchedEth.only = (name: string, cb: (schedKind: SchedKind, apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itSchedEth(name, cb, {only: true});103itSchedEth.skip = (name: string, cb: (schedKind: SchedKind, apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itSchedEth(name, cb, {skip: true});104itSchedEth.ifWithPallets = itSchedIfWithPallets;105106function itSchedIfWithPallets(name: string, required: string[], cb: (schedKind: SchedKind, apis: { helper: EthUniqueHelper, privateKey: PrivateKeyFn }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {107 return itSchedEth(name, cb, {requiredPallets: required, ...opts});108}