1234import {IKeyringPair} from '@polkadot/types/types';5import chai from 'chai';6import chaiAsPromised from 'chai-as-promised';7import {Context} from 'mocha';8import config from '../../config';9import '../../interfaces/augment-api-events';10import {ChainHelperBase} from './unique';11import {ILogger} from './types';12import {DevUniqueHelper, SilentLogger, SilentConsole, DevMoonbeamHelper, DevMoonriverHelper, DevAcalaHelper, DevKaruraHelper, DevRelayHelper} from './unique.dev';1314chai.use(chaiAsPromised);15export const expect = chai.expect;1617async function usingPlaygroundsGeneral<T extends ChainHelperBase>(helperType: new(logger: ILogger) => T, url: string, code: (helper: T, privateKey: (seed: string) => IKeyringPair) => Promise<void>) {18 const silentConsole = new SilentConsole();19 silentConsole.enable();2021 const helper = new helperType(new SilentLogger());2223 try {24 await helper.connect(url);25 const ss58Format = helper.chain.getChainProperties().ss58Format;26 const privateKey = (seed: string) => helper.util.fromSeed(seed, ss58Format);27 await code(helper, privateKey);28 }29 finally {30 await helper.disconnect();31 silentConsole.disable();32 }33}3435export const usingPlaygrounds = (code: (helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair) => Promise<void>, url: string = config.substrateUrl) => {36 return usingPlaygroundsGeneral<DevUniqueHelper>(DevUniqueHelper, url, code);37};383940export const usingStatemintPlaygrounds = async (url: string, code: (helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair) => Promise<void>) => {41 return usingPlaygroundsGeneral<DevUniqueHelper>(DevUniqueHelper, url, code);42};4344export const usingRelayPlaygrounds = async (url: string, code: (helper: DevRelayHelper, privateKey: (seed: string) => IKeyringPair) => Promise<void>) => {45 return usingPlaygroundsGeneral<DevRelayHelper>(DevRelayHelper, url, code);46};4748export const usingAcalaPlaygrounds = async (url: string, code: (helper: DevAcalaHelper, privateKey: (seed: string) => IKeyringPair) => Promise<void>) => {49 return usingPlaygroundsGeneral<DevAcalaHelper>(DevAcalaHelper, url, code);50};5152export const usingKaruraPlaygrounds = async (url: string, code: (helper: DevKaruraHelper, privateKey: (seed: string) => IKeyringPair) => Promise<void>) => {53 return usingPlaygroundsGeneral<DevKaruraHelper>(DevAcalaHelper, url, code);54};5556export const usingMoonbeamPlaygrounds = async (url: string, code: (helper: DevMoonbeamHelper, privateKey: (seed: string) => IKeyringPair) => Promise<void>) => {57 return usingPlaygroundsGeneral<DevMoonbeamHelper>(DevMoonbeamHelper, url, code);58};5960export const usingMoonriverPlaygrounds = async (url: string, code: (helper: DevMoonbeamHelper, privateKey: (seed: string) => IKeyringPair) => Promise<void>) => {61 return usingPlaygroundsGeneral<DevMoonriverHelper>(DevMoonriverHelper, url, code);62};6364export enum Pallets {65 Inflation = 'inflation',66 RmrkCore = 'rmrkcore',67 RmrkEquip = 'rmrkequip',68 ReFungible = 'refungible',69 Fungible = 'fungible',70 NFT = 'nonfungible',71 Scheduler = 'scheduler',72 AppPromotion = 'apppromotion',73}7475export function requirePalletsOrSkip(test: Context, helper: DevUniqueHelper, requiredPallets: string[]) {76 const missingPallets = helper.fetchMissingPalletNames(requiredPallets);77 78 if (missingPallets.length > 0) {79 const skipMsg = `\tSkipping test '${test.test?.title}'.\n\tThe following pallets are missing:\n\t- ${missingPallets.join('\n\t- ')}`;80 console.warn('\x1b[38:5:208m%s\x1b[0m', skipMsg);81 test.skip();82 }83}8485export async function itSub(name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {86 (opts.only ? it.only : 87 opts.skip ? it.skip : it)(name, async function () {88 await usingPlaygrounds(async (helper, privateKey) => {89 if (opts.requiredPallets) {90 requirePalletsOrSkip(this, helper, opts.requiredPallets);91 }92 93 await cb({helper, privateKey});94 });95 });96}97export async function itSubIfWithPallet(name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {98 return itSub(name, cb, {requiredPallets: required, ...opts});99}100itSub.only = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any) => itSub(name, cb, {only: true});101itSub.skip = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any) => itSub(name, cb, {skip: true});102103itSubIfWithPallet.only = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any) => itSubIfWithPallet(name, required, cb, {only: true});104itSubIfWithPallet.skip = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any) => itSubIfWithPallet(name, required, cb, {skip: true});105itSub.ifWithPallets = itSubIfWithPallet;106107export const describeXcm = (108 process.env.RUN_XCM_TESTS109 ? describe110 : describe.skip111);