1234import * as path from 'path';5import * as crypto from 'crypto';6import {IKeyringPair} from '@polkadot/types/types';7import chai from 'chai';8import chaiAsPromised from 'chai-as-promised';9import {Context} from 'mocha';10import config from '../config';11import '../interfaces/augment-api-events';12import {DevUniqueHelper, SilentLogger, SilentConsole} from './playgrounds/unique.dev';1314chai.use(chaiAsPromised);15export const expect = chai.expect;1617const getTestHash = (filename: string) => {18 return crypto.createHash('md5').update(path.basename(filename)).digest('hex');19};2021export const getTestSeed = (filename: string) => {22 return `//Alice+${getTestHash(filename)}`;23};2425export const usingPlaygrounds = async (code: (helper: DevUniqueHelper, privateKey: (seed: string | {filename: string, ignoreFundsPresence?: boolean}) => Promise<IKeyringPair>) => Promise<void>, url: string = config.substrateUrl) => {26 const silentConsole = new SilentConsole();27 silentConsole.enable();2829 const helper = new DevUniqueHelper(new SilentLogger());3031 try {32 await helper.connect(url);33 const ss58Format = helper.chain.getChainProperties().ss58Format;34 const privateKey = async (seed: string | {filename: string, ignoreFundsPresence?: boolean}) => {35 if (typeof seed === 'string') {36 return helper.util.fromSeed(seed, ss58Format);37 }38 else {39 const actualSeed = getTestSeed(seed.filename);40 let account = helper.util.fromSeed(actualSeed, ss58Format);41 if (!seed.ignoreFundsPresence && await helper.balance.getSubstrate(account.address) == 0n) {42 console.warn(`${path.basename(seed.filename)}: Not enough funds present on the filename account. Using the default one as the donor instead.`);43 account = helper.util.fromSeed('//Alice', ss58Format);44 }45 return account;46 }47 };48 await code(helper, privateKey);49 }50 finally {51 await helper.disconnect();52 silentConsole.disable();53 }54};5556export enum Pallets {57 Inflation = 'inflation',58 RmrkCore = 'rmrkcore',59 RmrkEquip = 'rmrkequip',60 ReFungible = 'refungible',61 Fungible = 'fungible',62 NFT = 'nonfungible',63 Scheduler = 'scheduler',64 AppPromotion = 'apppromotion',65}6667export function requirePalletsOrSkip(test: Context, helper: DevUniqueHelper, requiredPallets: string[]) {68 const missingPallets = helper.fetchMissingPalletNames(requiredPallets);69 70 if (missingPallets.length > 0) {71 const skipMsg = `\tSkipping test '${test.test?.title}'.\n\tThe following pallets are missing:\n\t- ${missingPallets.join('\n\t- ')}`;72 console.warn('\x1b[38:5:208m%s\x1b[0m', skipMsg);73 test.skip();74 }75}7677export async function itSub(name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {78 (opts.only ? it.only : 79 opts.skip ? it.skip : it)(name, async function () {80 await usingPlaygrounds(async (helper, privateKey) => {81 if (opts.requiredPallets) {82 requirePalletsOrSkip(this, helper, opts.requiredPallets);83 }84 85 await cb({helper, privateKey});86 });87 });88}89export async function itSubIfWithPallet(name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {90 return itSub(name, cb, {requiredPallets: required, ...opts});91}92itSub.only = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSub(name, cb, {only: true});93itSub.skip = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSub(name, cb, {skip: true});9495itSubIfWithPallet.only = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSubIfWithPallet(name, required, cb, {only: true});96itSubIfWithPallet.skip = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSubIfWithPallet(name, required, cb, {skip: true});97itSub.ifWithPallets = itSubIfWithPallet;