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, DevStatemineHelper, DevStatemintHelper, DevAstarHelper, DevShidenHelper} from './playgrounds/unique.dev';15import {dirname} from 'path';16import {fileURLToPath} from 'url';1718chai.use(chaiAsPromised);19chai.use(chaiSubset);20export const expect = chai.expect;2122const getTestHash = (filename: string) => crypto.createHash('md5').update(filename).digest('hex');2324export const getTestSeed = (filename: string) => `//Alice+${getTestHash(filename)}`;2526async function usingPlaygroundsGeneral<T extends ChainHelperBase>(helperType: new(logger: ILogger) => T, url: string, code: (helper: T, privateKey: (seed: string | {filename?: string, url?: 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, url?: string, ignoreFundsPresence?: boolean}) => {36 if(typeof seed === 'string') {37 return helper.util.fromSeed(seed, ss58Format);38 }39 if(seed.url) {40 const {filename} = makeNames(seed.url);41 seed.filename = filename;42 } else if(seed.filename) {43 44 } else {45 throw new Error('no url nor filename set');46 }47 const actualSeed = getTestSeed(seed.filename);48 let account = helper.util.fromSeed(actualSeed, ss58Format);49 50 if(!seed.ignoreFundsPresence && ((helper as any)['balance'] == undefined || await (helper as any).balance.getSubstrate(account.address) < MINIMUM_DONOR_FUND)) {51 console.warn(`${path.basename(seed.filename)}: Not enough funds present on the filename account. Using the default one as the donor instead.`);52 account = helper.util.fromSeed('//Alice', ss58Format);53 }54 return account;55 };56 await code(helper, privateKey);57 }58 finally {59 await helper.disconnect();60 silentConsole.disable();61 }62}6364export const usingPlaygrounds = (code: (helper: DevUniqueHelper, privateKey: (seed: string | {filename?: string, url?: string, ignoreFundsPresence?: boolean}) => Promise<IKeyringPair>) => Promise<void>, url: string = config.substrateUrl) => usingPlaygroundsGeneral<DevUniqueHelper>(DevUniqueHelper, url, code);6566export const usingWestmintPlaygrounds = (url: string, code: (helper: DevWestmintHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => usingPlaygroundsGeneral<DevWestmintHelper>(DevWestmintHelper, url, code);6768export const usingStateminePlaygrounds = (url: string, code: (helper: DevWestmintHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => usingPlaygroundsGeneral<DevStatemineHelper>(DevWestmintHelper, url, code);6970export const usingStatemintPlaygrounds = (url: string, code: (helper: DevWestmintHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => usingPlaygroundsGeneral<DevStatemintHelper>(DevWestmintHelper, url, code);7172export const usingRelayPlaygrounds = (url: string, code: (helper: DevRelayHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => usingPlaygroundsGeneral<DevRelayHelper>(DevRelayHelper, url, code);7374export const usingAcalaPlaygrounds = (url: string, code: (helper: DevAcalaHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => usingPlaygroundsGeneral<DevAcalaHelper>(DevAcalaHelper, url, code);7576export const usingKaruraPlaygrounds = (url: string, code: (helper: DevKaruraHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => usingPlaygroundsGeneral<DevKaruraHelper>(DevAcalaHelper, url, code);7778export const usingMoonbeamPlaygrounds = (url: string, code: (helper: DevMoonbeamHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => usingPlaygroundsGeneral<DevMoonbeamHelper>(DevMoonbeamHelper, url, code);7980export const usingMoonriverPlaygrounds = (url: string, code: (helper: DevMoonbeamHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => usingPlaygroundsGeneral<DevMoonriverHelper>(DevMoonriverHelper, url, code);8182export const usingAstarPlaygrounds = (url: string, code: (helper: DevAstarHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => usingPlaygroundsGeneral<DevAstarHelper>(DevAstarHelper, url, code);8384export const usingShidenPlaygrounds = (url: string, code: (helper: DevShidenHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => usingPlaygroundsGeneral<DevShidenHelper>(DevShidenHelper, url, code);8586export const MINIMUM_DONOR_FUND = 100_000n;87export const DONOR_FUNDING = 2_000_000n;888990export const LOCKING_PERIOD = 12n; 91export const UNLOCKING_PERIOD = 6n; 929394export const COLLECTION_HELPER = '0x6c4e9fe1ae37a41e93cee429e8e1881abdcbb54f';95export const CONTRACT_HELPER = '0x842899ECF380553E8a4de75bF534cdf6fBF64049';9697export enum Pallets {98 Inflation = 'inflation',99 ReFungible = 'refungible',100 Fungible = 'fungible',101 NFT = 'nonfungible',102 Scheduler = 'scheduler',103 AppPromotion = 'apppromotion',104 CollatorSelection = 'collatorselection',105 Session = 'session',106 Identity = 'identity',107 Preimage = 'preimage',108 Maintenance = 'maintenance',109 TestUtils = 'testutils',110}111112export function requirePalletsOrSkip(test: Context, helper: DevUniqueHelper, requiredPallets: readonly string[]) {113 const missingPallets = helper.fetchMissingPalletNames(requiredPallets);114115 if(missingPallets.length > 0) {116 const skipMsg = `\tSkipping test '${test.test?.title}'.\n\tThe following pallets are missing:\n\t- ${missingPallets.join('\n\t- ')}`;117 console.warn('\x1b[38:5:208m%s\x1b[0m', skipMsg);118 test.skip();119 }120}121122export function itSub(name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: readonly string[] } = {}) {123 (opts.only ? it.only :124 opts.skip ? it.skip : it)(name, async function () {125 await usingPlaygrounds(async (helper, privateKey) => {126 if(opts.requiredPallets) {127 requirePalletsOrSkip(this, helper, opts.requiredPallets);128 }129130 await cb({helper, privateKey});131 });132 });133}134export function itSubIfWithPallet(name: string, required: readonly string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: readonly string[] } = {}) {135 return itSub(name, cb, {requiredPallets: required, ...opts});136}137itSub.only = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSub(name, cb, {only: true});138itSub.skip = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSub(name, cb, {skip: true});139140itSubIfWithPallet.only = (name: string, required: readonly string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSubIfWithPallet(name, required, cb, {only: true});141itSubIfWithPallet.skip = (name: string, required: readonly string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSubIfWithPallet(name, required, cb, {skip: true});142itSub.ifWithPallets = itSubIfWithPallet;143144export type SchedKind = 'anon' | 'named';145146export function itSched(147 name: string,148 cb: (schedKind: SchedKind, apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any,149 opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {},150) {151 itSub(name + ' (anonymous scheduling)', (apis) => cb('anon', apis), opts);152 itSub(name + ' (named scheduling)', (apis) => cb('named', apis), opts);153}154itSched.only = (name: string, cb: (schedKind: SchedKind, apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSched(name, cb, {only: true});155itSched.skip = (name: string, cb: (schedKind: SchedKind, apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSched(name, cb, {skip: true});156itSched.ifWithPallets = itSchedIfWithPallets;157158function itSchedIfWithPallets(name: string, required: string[], cb: (schedKind: SchedKind, apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {159 return itSched(name, cb, {requiredPallets: required, ...opts});160}161162export function describeXCM(title: string, fn: (this: Mocha.Suite) => void, opts: {skip?: boolean} = {}) {163 (process.env.RUN_XCM_TESTS && !opts.skip164 ? describe165 : describe.skip)(title, fn);166}167168describeXCM.skip = (name: string, fn: (this: Mocha.Suite) => void) => describeXCM(name, fn, {skip: true});169170export function sizeOfInt(i: number) {171 if(i < 0 || i > 0xffffffff) throw new Error('out of range');172 if(i < 0b11_1111) {173 return 1;174 } else if(i < 0b11_1111_1111_1111) {175 return 2;176 } else if(i < 0b11_1111_1111_1111_1111_1111_1111_1111) {177 return 4;178 } else {179 return 5;180 }181}182183const UTF8_ENCODER = new TextEncoder();184export function sizeOfEncodedStr(v: string) {185 const encoded = UTF8_ENCODER.encode(v);186 return sizeOfInt(encoded.length) + encoded.length;187}188189export function sizeOfProperty(prop: {key: string, value: string}) {190 return sizeOfEncodedStr(prop.key) + sizeOfEncodedStr(prop.value);191}192193export function makeNames(url: string) {194 const filename = fileURLToPath(url);195 return {196 filename,197 dirname: dirname(filename),198 };199}