1234import * as path from 'path';5import {IKeyringPair} from '@polkadot/types/types';67import config from '../../config';89import {EthUniqueHelper} from './playgrounds/unique.dev';10import {SilentLogger, SilentConsole} from '../../util/playgrounds/unique.dev';11import {SchedKind} from '../../util';1213export {EthUniqueHelper} from './playgrounds/unique.dev';1415import chai from 'chai';16import chaiAsPromised from 'chai-as-promised';17import chaiLike from 'chai-like';18import {getTestSeed, MINIMUM_DONOR_FUND, requirePalletsOrSkip} from '../../util';1920chai.use(chaiAsPromised);21chai.use(chaiLike);22export const expect = chai.expect;2324export enum SponsoringMode {25 Disabled = 0,26 Allowlisted = 1,27 Generous = 2,28}29export enum CollectionLimits {30 AccountTokenOwnership,31 SponsoredDataSize,32 SponsoredDataRateLimit,33 TokenLimit,34 SponsorTransferTimeout,35 SponsorApproveTimeout,36 OwnerCanTransfer,37 OwnerCanDestroy,38 TransferEnabled39}4041export const usingEthPlaygrounds = async (code: (helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair>) => Promise<void>) => {42 const silentConsole = new SilentConsole();43 silentConsole.enable();4445 const helper = new EthUniqueHelper(new SilentLogger());4647 try {48 await helper.connect(config.substrateUrl);49 await helper.connectWeb3(config.substrateUrl);50 const ss58Format = helper.chain.getChainProperties().ss58Format;51 const privateKey = async (seed: string | {filename: string}) => {52 if (typeof seed === 'string') {53 return helper.util.fromSeed(seed, ss58Format);54 }55 else {56 const actualSeed = getTestSeed(seed.filename);57 let account = helper.util.fromSeed(actualSeed, ss58Format);58 if (await helper.balance.getSubstrate(account.address) < MINIMUM_DONOR_FUND) {59 console.warn(`${path.basename(seed.filename)}: Not enough funds present on the filename account. Using the default one as the donor instead.`);60 account = helper.util.fromSeed('//Alice', ss58Format);61 }62 return account;63 }64 };65 await code(helper, privateKey);66 }67 finally {68 await helper.disconnect();69 silentConsole.disable();70 }71};72 73export function itEth(name: string, cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {74 (opts.only ? it.only : 75 opts.skip ? it.skip : it)(name, async function() {76 await usingEthPlaygrounds(async (helper, privateKey) => {77 if (opts.requiredPallets) {78 requirePalletsOrSkip(this, helper, opts.requiredPallets);79 }8081 await cb({helper, privateKey});82 });83 });84}8586export function itEthIfWithPallet(name: string, required: string[], cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {87 return itEth(name, cb, {requiredPallets: required, ...opts});88}8990itEth.only = (name: string, cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itEth(name, cb, {only: true});91itEth.skip = (name: string, cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itEth(name, cb, {skip: true});9293itEthIfWithPallet.only = (name: string, required: string[], cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itEthIfWithPallet(name, required, cb, {only: true});94itEthIfWithPallet.skip = (name: string, required: string[], cb: (apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itEthIfWithPallet(name, required, cb, {skip: true});95itEth.ifWithPallets = itEthIfWithPallet;9697export function itSchedEth(98 name: string,99 cb: (schedKind: SchedKind, apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any,100 opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {},101) {102 itEth(name + ' (anonymous scheduling)', (apis) => cb('anon', apis), opts);103 itEth(name + ' (named scheduling)', (apis) => cb('named', apis), opts);104}105itSchedEth.only = (name: string, cb: (schedKind: SchedKind, apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itSchedEth(name, cb, {only: true});106itSchedEth.skip = (name: string, cb: (schedKind: SchedKind, apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any) => itSchedEth(name, cb, {skip: true});107itSchedEth.ifWithPallets = itSchedIfWithPallets;108109function itSchedIfWithPallets(name: string, required: string[], cb: (schedKind: SchedKind, apis: { helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {110 return itSchedEth(name, cb, {requiredPallets: required, ...opts});111}