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';1112export {EthUniqueHelper} from './playgrounds/unique.dev';1314import chai from 'chai';15import chaiAsPromised from 'chai-as-promised';16import chaiLike from 'chai-like';17import {getTestSeed, requirePalletsOrSkip} from '../../util';1819chai.use(chaiAsPromised);20chai.use(chaiLike);21export const expect = chai.expect;2223export enum SponsoringMode {24 Disabled = 0,25 Allowlisted = 1,26 Generous = 2,27}2829export const usingEthPlaygrounds = async (code: (helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair>) => Promise<void>) => {30 const silentConsole = new SilentConsole();31 silentConsole.enable();3233 const helper = new EthUniqueHelper(new SilentLogger());3435 try {36 await helper.connect(config.substrateUrl);37 await helper.connectWeb3(config.substrateUrl);38 const ss58Format = helper.chain.getChainProperties().ss58Format;39 const privateKey = async (seed: string | {filename: string}) => {40 if (typeof seed === 'string') {41 return helper.util.fromSeed(seed, ss58Format);42 }43 else {44 const actualSeed = getTestSeed(seed.filename);45 let account = helper.util.fromSeed(actualSeed, ss58Format);46 if (await helper.balance.getSubstrate(account.address) == 0n) {47 console.warn(`${path.basename(seed.filename)}: Not enough funds present on the filename account. Using the default one as the donor instead.`);48 account = helper.util.fromSeed('//Alice', ss58Format);49 }50 return account;51 }52 };53 await code(helper, privateKey);54 }55 finally {56 await helper.disconnect();57 silentConsole.disable();58 }59};60 61export async function itEth(name: string, cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {62 (opts.only ? it.only : 63 opts.skip ? it.skip : it)(name, async function() {64 await usingEthPlaygrounds(async (helper, privateKey) => {65 if (opts.requiredPallets) {66 requirePalletsOrSkip(this, helper, opts.requiredPallets);67 }6869 await cb({helper, privateKey});70 });71 });72}7374export async 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[] } = {}) {75 return itEth(name, cb, {requiredPallets: required, ...opts});76}7778itEth.only = (name: string, cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itEth(name, cb, {only: true});79itEth.skip = (name: string, cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itEth(name, cb, {skip: true});8081itEthIfWithPallet.only = (name: string, required: string[], cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itEthIfWithPallet(name, required, cb, {only: true});82itEthIfWithPallet.skip = (name: string, required: string[], cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itEthIfWithPallet(name, required, cb, {skip: true});83itEth.ifWithPallets = itEthIfWithPallet;