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, DevWestmintHelper} 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};3839export const usingWestmintPlaygrounds = async (url: string, code: (helper: DevWestmintHelper, privateKey: (seed: string) => IKeyringPair) => Promise<void>) => {40 return usingPlaygroundsGeneral<DevWestmintHelper>(DevWestmintHelper, url, code);41};4243export const usingRelayPlaygrounds = async (url: string, code: (helper: DevRelayHelper, privateKey: (seed: string) => IKeyringPair) => Promise<void>) => {44 return usingPlaygroundsGeneral<DevRelayHelper>(DevRelayHelper, url, code);45};4647export const usingAcalaPlaygrounds = async (url: string, code: (helper: DevAcalaHelper, privateKey: (seed: string) => IKeyringPair) => Promise<void>) => {48 return usingPlaygroundsGeneral<DevAcalaHelper>(DevAcalaHelper, url, code);49};5051export const usingKaruraPlaygrounds = async (url: string, code: (helper: DevKaruraHelper, privateKey: (seed: string) => IKeyringPair) => Promise<void>) => {52 return usingPlaygroundsGeneral<DevKaruraHelper>(DevAcalaHelper, url, code);53};5455export const usingMoonbeamPlaygrounds = async (url: string, code: (helper: DevMoonbeamHelper, privateKey: (seed: string) => IKeyringPair) => Promise<void>) => {56 return usingPlaygroundsGeneral<DevMoonbeamHelper>(DevMoonbeamHelper, url, code);57};5859export const usingMoonriverPlaygrounds = async (url: string, code: (helper: DevMoonbeamHelper, privateKey: (seed: string) => IKeyringPair) => Promise<void>) => {60 return usingPlaygroundsGeneral<DevMoonriverHelper>(DevMoonriverHelper, url, code);61};6263export enum Pallets {64 Inflation = 'inflation',65 RmrkCore = 'rmrkcore',66 RmrkEquip = 'rmrkequip',67 ReFungible = 'refungible',68 Fungible = 'fungible',69 NFT = 'nonfungible',70 Scheduler = 'scheduler',71 AppPromotion = 'apppromotion',72}7374export function requirePalletsOrSkip(test: Context, helper: DevUniqueHelper, requiredPallets: string[]) {75 const missingPallets = helper.fetchMissingPalletNames(requiredPallets);76 77 if (missingPallets.length > 0) {78 const skipMsg = `\tSkipping test '${test.test?.title}'.\n\tThe following pallets are missing:\n\t- ${missingPallets.join('\n\t- ')}`;79 console.warn('\x1b[38:5:208m%s\x1b[0m', skipMsg);80 test.skip();81 }82}8384export async function itSub(name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {85 (opts.only ? it.only : 86 opts.skip ? it.skip : it)(name, async function () {87 await usingPlaygrounds(async (helper, privateKey) => {88 if (opts.requiredPallets) {89 requirePalletsOrSkip(this, helper, opts.requiredPallets);90 }91 92 await cb({helper, privateKey});93 });94 });95}96export async function itSubIfWithPallet(name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {97 return itSub(name, cb, {requiredPallets: required, ...opts});98}99itSub.only = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any) => itSub(name, cb, {only: true});100itSub.skip = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any) => itSub(name, cb, {skip: true});101102itSubIfWithPallet.only = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any) => itSubIfWithPallet(name, required, cb, {only: true});103itSubIfWithPallet.skip = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any) => itSubIfWithPallet(name, required, cb, {skip: true});104itSub.ifWithPallets = itSubIfWithPallet;105106export const describeXcm = (107 process.env.RUN_XCM_TESTS108 ? describe109 : describe.skip110);