1234import * as path from 'path';5import * as crypto from 'crypto';6import {IKeyringPair} from '@polkadot/types/types/interfaces';7import chai from 'chai';8import chaiAsPromised from 'chai-as-promised';9import chaiSubset from 'chai-subset';10import {Context} from 'mocha';11import config from '../config';12import {ChainHelperBase} from './playgrounds/unique';13import {ILogger} from './playgrounds/types';14import {DevUniqueHelper, SilentLogger, SilentConsole, DevMoonbeamHelper, DevMoonriverHelper, DevAcalaHelper, DevKaruraHelper, DevRelayHelper, DevWestmintHelper} from './playgrounds/unique.dev';1516chai.use(chaiAsPromised);17chai.use(chaiSubset);18export const expect = chai.expect;1920const getTestHash = (filename: string) => {21 return crypto.createHash('md5').update(filename).digest('hex');22};2324export const getTestSeed = (filename: string) => {25 return `//Alice+${getTestHash(filename)}`;26};2728async function usingPlaygroundsGeneral<T extends ChainHelperBase>(helperType: new(logger: ILogger) => T, url: string, code: (helper: T, privateKey: (seed: string | {filename: string, ignoreFundsPresence?: boolean}) => Promise<IKeyringPair>) => Promise<void>) {29 const silentConsole = new SilentConsole();30 silentConsole.enable();3132 const helper = new helperType(new SilentLogger());3334 try {35 await helper.connect(url);36 const ss58Format = helper.chain.getChainProperties().ss58Format;37 const privateKey = async (seed: string | {filename: string, ignoreFundsPresence?: boolean}) => {38 if (typeof seed === 'string') {39 return helper.util.fromSeed(seed, ss58Format);40 }41 else {42 const actualSeed = getTestSeed(seed.filename);43 let account = helper.util.fromSeed(actualSeed, ss58Format);44 45 if (!seed.ignoreFundsPresence && ((helper as any)['balance'] == undefined || await (helper as any).balance.getSubstrate(account.address) < MINIMUM_DONOR_FUND)) {46 console.warn(`${path.basename(seed.filename)}: Not enough funds present on the filename account. Using the default one as the donor instead.`);47 account = helper.util.fromSeed('//Alice', ss58Format);48 }49 return account;50 }51 };52 await code(helper, privateKey);53 }54 finally {55 await helper.disconnect();56 silentConsole.disable();57 }58}5960export const usingPlaygrounds = (code: (helper: DevUniqueHelper, privateKey: (seed: string | {filename: string, ignoreFundsPresence?: boolean}) => Promise<IKeyringPair>) => Promise<void>, url: string = config.substrateUrl) => {61 return usingPlaygroundsGeneral<DevUniqueHelper>(DevUniqueHelper, url, code);62};6364export const usingWestmintPlaygrounds = (url: string, code: (helper: DevWestmintHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => {65 return usingPlaygroundsGeneral<DevWestmintHelper>(DevWestmintHelper, url, code);66};6768export const usingRelayPlaygrounds = (url: string, code: (helper: DevRelayHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => {69 return usingPlaygroundsGeneral<DevRelayHelper>(DevRelayHelper, url, code);70};7172export const usingAcalaPlaygrounds = (url: string, code: (helper: DevAcalaHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => {73 return usingPlaygroundsGeneral<DevAcalaHelper>(DevAcalaHelper, url, code);74};7576export const usingKaruraPlaygrounds = (url: string, code: (helper: DevKaruraHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => {77 return usingPlaygroundsGeneral<DevKaruraHelper>(DevAcalaHelper, url, code);78};7980export const usingMoonbeamPlaygrounds = (url: string, code: (helper: DevMoonbeamHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => {81 return usingPlaygroundsGeneral<DevMoonbeamHelper>(DevMoonbeamHelper, url, code);82};8384export const usingMoonriverPlaygrounds = (url: string, code: (helper: DevMoonbeamHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => {85 return usingPlaygroundsGeneral<DevMoonriverHelper>(DevMoonriverHelper, url, code);86};8788export const MINIMUM_DONOR_FUND = 100_000n;89export const DONOR_FUNDING = 1_000_000n;909192export const LOCKING_PERIOD = 12n; 93export const UNLOCKING_PERIOD = 6n; 949596export enum Pallets {97 Inflation = 'inflation',98 RmrkCore = 'rmrkcore',99 RmrkEquip = 'rmrkequip',100 ReFungible = 'refungible',101 Fungible = 'fungible',102 NFT = 'nonfungible',103 Scheduler = 'scheduler',104 AppPromotion = 'apppromotion',105 TestUtils = 'testutils',106}107108export function requirePalletsOrSkip(test: Context, helper: DevUniqueHelper, requiredPallets: string[]) {109 const missingPallets = helper.fetchMissingPalletNames(requiredPallets);110 111 if (missingPallets.length > 0) {112 const skipMsg = `\tSkipping test '${test.test?.title}'.\n\tThe following pallets are missing:\n\t- ${missingPallets.join('\n\t- ')}`;113 console.warn('\x1b[38:5:208m%s\x1b[0m', skipMsg);114 test.skip();115 }116}117118export function itSub(name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {119 (opts.only ? it.only : 120 opts.skip ? it.skip : it)(name, async function () {121 await usingPlaygrounds(async (helper, privateKey) => {122 if (opts.requiredPallets) {123 requirePalletsOrSkip(this, helper, opts.requiredPallets);124 }125 126 await cb({helper, privateKey});127 });128 });129}130export function itSubIfWithPallet(name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {131 return itSub(name, cb, {requiredPallets: required, ...opts});132}133itSub.only = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSub(name, cb, {only: true});134itSub.skip = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSub(name, cb, {skip: true});135136itSubIfWithPallet.only = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSubIfWithPallet(name, required, cb, {only: true});137itSubIfWithPallet.skip = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSubIfWithPallet(name, required, cb, {skip: true});138itSub.ifWithPallets = itSubIfWithPallet;139140export type SchedKind = 'anon' | 'named';141142export function itSched(143 name: string,144 cb: (schedKind: SchedKind, apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any,145 opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {},146) {147 itSub(name + ' (anonymous scheduling)', (apis) => cb('anon', apis), opts);148 itSub(name + ' (named scheduling)', (apis) => cb('named', apis), opts);149}150itSched.only = (name: string, cb: (schedKind: SchedKind, apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSched(name, cb, {only: true});151itSched.skip = (name: string, cb: (schedKind: SchedKind, apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSched(name, cb, {skip: true});152itSched.ifWithPallets = itSchedIfWithPallets;153154function itSchedIfWithPallets(name: string, required: string[], cb: (schedKind: SchedKind, apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {155 return itSched(name, cb, {requiredPallets: required, ...opts});156}157158export function describeXCM(title: string, fn: (this: Mocha.Suite) => void, opts: {skip?: boolean} = {}) {159 (process.env.RUN_XCM_TESTS && !opts.skip160 ? describe161 : describe.skip)(title, fn);162}163164describeXCM.skip = (name: string, fn: (this: Mocha.Suite) => void) => describeXCM(name, fn, {skip: true});