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 {Context} from 'mocha';10import config from '../config';11import {ChainHelperBase} from './playgrounds/unique';12import {ILogger} from './playgrounds/types';13import {DevUniqueHelper, SilentLogger, SilentConsole, DevMoonbeamHelper, DevMoonriverHelper, DevAcalaHelper, DevKaruraHelper, DevRelayHelper, DevWestmintHelper} from './playgrounds/unique.dev';1415chai.use(chaiAsPromised);16export const expect = chai.expect;1718const getTestHash = (filename: string) => {19 return crypto.createHash('md5').update(filename).digest('hex');20};2122export const getTestSeed = (filename: string) => {23 return `//Alice+${getTestHash(filename)}`;24};2526async 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>) {27 const silentConsole = new SilentConsole();28 silentConsole.enable();2930 const helper = new helperType(new SilentLogger());3132 try {33 await helper.connect(url);34 const ss58Format = helper.chain.getChainProperties().ss58Format;35 const privateKey = async (seed: string | {filename: string, ignoreFundsPresence?: boolean}) => {36 if (typeof seed === 'string') {37 return helper.util.fromSeed(seed, ss58Format);38 }39 else {40 const actualSeed = getTestSeed(seed.filename);41 let account = helper.util.fromSeed(actualSeed, ss58Format);42 43 if (!seed.ignoreFundsPresence && ((helper as any)['balance'] == undefined || await (helper as any).balance.getSubstrate(account.address) < MINIMUM_DONOR_FUND)) {44 console.warn(`${path.basename(seed.filename)}: Not enough funds present on the filename account. Using the default one as the donor instead.`);45 account = helper.util.fromSeed('//Alice', ss58Format);46 }47 return account;48 }49 };50 await code(helper, privateKey);51 }52 finally {53 await helper.disconnect();54 silentConsole.disable();55 }56}5758export const usingPlaygrounds = (code: (helper: DevUniqueHelper, privateKey: (seed: string | {filename: string, ignoreFundsPresence?: boolean}) => Promise<IKeyringPair>) => Promise<void>, url: string = config.substrateUrl) => {59 return usingPlaygroundsGeneral<DevUniqueHelper>(DevUniqueHelper, url, code);60};6162export const usingWestmintPlaygrounds = (url: string, code: (helper: DevWestmintHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => {63 return usingPlaygroundsGeneral<DevWestmintHelper>(DevWestmintHelper, url, code);64};6566export const usingRelayPlaygrounds = (url: string, code: (helper: DevRelayHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => {67 return usingPlaygroundsGeneral<DevRelayHelper>(DevRelayHelper, url, code);68};6970export const usingAcalaPlaygrounds = (url: string, code: (helper: DevAcalaHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => {71 return usingPlaygroundsGeneral<DevAcalaHelper>(DevAcalaHelper, url, code);72};7374export const usingKaruraPlaygrounds = (url: string, code: (helper: DevKaruraHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => {75 return usingPlaygroundsGeneral<DevKaruraHelper>(DevAcalaHelper, url, code);76};7778export const usingMoonbeamPlaygrounds = (url: string, code: (helper: DevMoonbeamHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => {79 return usingPlaygroundsGeneral<DevMoonbeamHelper>(DevMoonbeamHelper, url, code);80};8182export const usingMoonriverPlaygrounds = (url: string, code: (helper: DevMoonbeamHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => {83 return usingPlaygroundsGeneral<DevMoonriverHelper>(DevMoonriverHelper, url, code);84};8586export const MINIMUM_DONOR_FUND = 100_000n;87export const DONOR_FUNDING = 1_000_000n;888990export const LOCKING_PERIOD = 12n; 91export const UNLOCKING_PERIOD = 6n; 929394export enum Pallets {95 Inflation = 'inflation',96 RmrkCore = 'rmrkcore',97 RmrkEquip = 'rmrkequip',98 ReFungible = 'refungible',99 Fungible = 'fungible',100 NFT = 'nonfungible',101 Scheduler = 'scheduler',102 AppPromotion = 'apppromotion',103 CollatorSelection = 'collatorselection',104 TestUtils = 'testutils',105}106107export function requirePalletsOrSkip(test: Context, helper: DevUniqueHelper, requiredPallets: string[]) {108 const missingPallets = helper.fetchMissingPalletNames(requiredPallets);109 110 if (missingPallets.length > 0) {111 const skipMsg = `\tSkipping test '${test.test?.title}'.\n\tThe following pallets are missing:\n\t- ${missingPallets.join('\n\t- ')}`;112 console.warn('\x1b[38:5:208m%s\x1b[0m', skipMsg);113 test.skip();114 }115}116117export function itSub(name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {118 (opts.only ? it.only : 119 opts.skip ? it.skip : it)(name, async function () {120 await usingPlaygrounds(async (helper, privateKey) => {121 if (opts.requiredPallets) {122 requirePalletsOrSkip(this, helper, opts.requiredPallets);123 }124 125 await cb({helper, privateKey});126 });127 });128}129export function itSubIfWithPallet(name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {130 return itSub(name, cb, {requiredPallets: required, ...opts});131}132itSub.only = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSub(name, cb, {only: true});133itSub.skip = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSub(name, cb, {skip: true});134135itSubIfWithPallet.only = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSubIfWithPallet(name, required, cb, {only: true});136itSubIfWithPallet.skip = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSubIfWithPallet(name, required, cb, {skip: true});137itSub.ifWithPallets = itSubIfWithPallet;138139export type SchedKind = 'anon' | 'named';140141export function itSched(142 name: string,143 cb: (schedKind: SchedKind, apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any,144 opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {},145) {146 itSub(name + ' (anonymous scheduling)', (apis) => cb('anon', apis), opts);147 itSub(name + ' (named scheduling)', (apis) => cb('named', apis), opts);148}149itSched.only = (name: string, cb: (schedKind: SchedKind, apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSched(name, cb, {only: true});150itSched.skip = (name: string, cb: (schedKind: SchedKind, apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSched(name, cb, {skip: true});151itSched.ifWithPallets = itSchedIfWithPallets;152153function itSchedIfWithPallets(name: string, required: string[], cb: (schedKind: SchedKind, apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {154 return itSched(name, cb, {requiredPallets: required, ...opts});155}156157export function describeXCM(title: string, fn: (this: Mocha.Suite) => void, opts: {skip?: boolean} = {}) {158 (process.env.RUN_XCM_TESTS && !opts.skip159 ? describe160 : describe.skip)(title, fn);161}162163describeXCM.skip = (name: string, fn: (this: Mocha.Suite) => void) => describeXCM(name, fn, {skip: true});