git.delta.rocks / unique-network / refs/commits / e000126d21b1

difftreelog

fix add wsEndpoint to playgrnd helpers

Daniel Shiposha2022-11-25parent: #c207e15.patch.diff
in: master

2 files changed

modifiedtests/src/util/playgrounds/unique.dev.tsdiffbeforeafterboth
before · tests/src/util/playgrounds/unique.dev.ts
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// SPDX-License-Identifier: Apache-2.034import {stringToU8a} from '@polkadot/util';5import {encodeAddress, mnemonicGenerate} from '@polkadot/util-crypto';6import {UniqueHelper, MoonbeamHelper, ChainHelperBase, AcalaHelper, RelayHelper, WestmintHelper} from './unique';7import {ApiPromise, Keyring, WsProvider} from '@polkadot/api';8import * as defs from '../../interfaces/definitions';9import {IKeyringPair} from '@polkadot/types/types';10import {EventRecord} from '@polkadot/types/interfaces';11import {ICrossAccountId, IPovInfo, TSigner} from './types';12import {FrameSystemEventRecord} from '@polkadot/types/lookup';13import {VoidFn} from '@polkadot/api/types';14import {Pallets} from '..';15import {spawnSync} from 'child_process';1617export class SilentLogger {18  log(_msg: any, _level: any): void { }19  level = {20    ERROR: 'ERROR' as const,21    WARNING: 'WARNING' as const,22    INFO: 'INFO' as const,23  };24}2526export class SilentConsole {27  // TODO: Remove, this is temporary: Filter unneeded API output28  // (Jaco promised it will be removed in the next version)29  consoleErr: any;30  consoleLog: any;31  consoleWarn: any;3233  constructor() {34    this.consoleErr = console.error;35    this.consoleLog = console.log;36    this.consoleWarn = console.warn;37  }3839  enable() {40    const outFn = (printer: any) => (...args: any[]) => {41      for (const arg of args) {42        if (typeof arg !== 'string')43          continue;44        if (arg.includes('1000:: Normal connection closure') || arg.includes('Not decorating unknown runtime apis:') || arg.includes('RPC methods not decorated:') || arg === 'Normal connection closure')45          return;46      }47      printer(...args);48    };4950    console.error = outFn(this.consoleErr.bind(console));51    console.log = outFn(this.consoleLog.bind(console));52    console.warn = outFn(this.consoleWarn.bind(console));53  }5455  disable() {56    console.error = this.consoleErr;57    console.log = this.consoleLog;58    console.warn = this.consoleWarn;59  }60}6162export class DevUniqueHelper extends UniqueHelper {63  /**64   * Arrange methods for tests65   */66  arrange: ArrangeGroup;67  wait: WaitGroup;68  admin: AdminGroup;69  testUtils: TestUtilGroup;7071  constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {72    options.helperBase = options.helperBase ?? DevUniqueHelper;7374    super(logger, options);75    this.arrange = new ArrangeGroup(this);76    this.wait = new WaitGroup(this);77    this.admin = new AdminGroup(this);78    this.testUtils = new TestUtilGroup(this);79  }8081  async connect(wsEndpoint: string, _listeners?: any): Promise<void> {82    const wsProvider = new WsProvider(wsEndpoint);83    this.api = new ApiPromise({84      provider: wsProvider,85      signedExtensions: {86        ContractHelpers: {87          extrinsic: {},88          payload: {},89        },90        CheckMaintenance: {91          extrinsic: {},92          payload: {},93        },94        FakeTransactionFinalizer: {95          extrinsic: {},96          payload: {},97        },98      },99      rpc: {100        unique: defs.unique.rpc,101        appPromotion: defs.appPromotion.rpc,102        povinfo: defs.povinfo.rpc,103        rmrk: defs.rmrk.rpc,104        eth: {105          feeHistory: {106            description: 'Dummy',107            params: [],108            type: 'u8',109          },110          maxPriorityFeePerGas: {111            description: 'Dummy',112            params: [],113            type: 'u8',114          },115        },116      },117    });118    await this.api.isReadyOrError;119    this.network = await UniqueHelper.detectNetwork(this.api);120  }121}122123export class DevRelayHelper extends RelayHelper {124  wait: WaitGroup;125126  constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {127    options.helperBase = options.helperBase ?? DevRelayHelper;128129    super(logger, options);130    this.wait = new WaitGroup(this);131  }132}133134export class DevWestmintHelper extends WestmintHelper {135  wait: WaitGroup;136137  constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {138    options.helperBase = options.helperBase ?? DevWestmintHelper;139140    super(logger, options);141    this.wait = new WaitGroup(this);142  }143}144145export class DevStatemineHelper extends DevWestmintHelper {}146147export class DevStatemintHelper extends DevWestmintHelper {}148149export class DevMoonbeamHelper extends MoonbeamHelper {150  account: MoonbeamAccountGroup;151  wait: WaitGroup;152153  constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {154    options.helperBase = options.helperBase ?? DevMoonbeamHelper;155    options.notePreimagePallet = options.notePreimagePallet ?? 'democracy';156157    super(logger, options);158    this.account = new MoonbeamAccountGroup(this);159    this.wait = new WaitGroup(this);160  }161}162163export class DevMoonriverHelper extends DevMoonbeamHelper {164  constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {165    options.notePreimagePallet = options.notePreimagePallet ?? 'preimage';166    super(logger, options);167  }168}169170export class DevAcalaHelper extends AcalaHelper {171  wait: WaitGroup;172173  constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {174    options.helperBase = options.helperBase ?? DevAcalaHelper;175176    super(logger, options);177    this.wait = new WaitGroup(this);178  }179}180181export class DevKaruraHelper extends DevAcalaHelper {}182183class ArrangeGroup {184  helper: DevUniqueHelper;185186  scheduledIdSlider = 0;187188  constructor(helper: DevUniqueHelper) {189    this.helper = helper;190  }191192  /**193   * Generates accounts with the specified UNQ token balance194   * @param balances balances for generated accounts. Each balance will be multiplied by the token nominal.195   * @param donor donor account for balances196   * @returns array of newly created accounts197   * @example const [acc1, acc2, acc3] = await createAccounts([0n, 10n, 20n], donor);198   */199  createAccounts = async (balances: bigint[], donor: IKeyringPair): Promise<IKeyringPair[]> => {200    let nonce = await this.helper.chain.getNonce(donor.address);201    const wait = new WaitGroup(this.helper);202    const ss58Format = this.helper.chain.getChainProperties().ss58Format;203    const tokenNominal = this.helper.balance.getOneTokenNominal();204    const transactions = [];205    const accounts: IKeyringPair[] = [];206    for (const balance of balances) {207      const recipient = this.helper.util.fromSeed(mnemonicGenerate(), ss58Format);208      accounts.push(recipient);209      if (balance !== 0n) {210        const tx = this.helper.constructApiCall('api.tx.balances.transfer', [{Id: recipient.address}, balance * tokenNominal]);211        transactions.push(this.helper.signTransaction(donor, tx, {nonce}, 'account generation'));212        nonce++;213      }214    }215216    await Promise.all(transactions).catch(_e => {});217218    //#region TODO remove this region, when nonce problem will be solved219    const checkBalances = async () => {220      let isSuccess = true;221      for (let i = 0; i < balances.length; i++) {222        const balance = await this.helper.balance.getSubstrate(accounts[i].address);223        if (balance !== balances[i] * tokenNominal) {224          isSuccess = false;225          break;226        }227      }228      return isSuccess;229    };230231    let accountsCreated = false;232    const maxBlocksChecked = await this.helper.arrange.isDevNode() ? 50 : 5;233    // checkBalances retry up to 5-50 blocks234    for (let index = 0; index < maxBlocksChecked; index++) {235      accountsCreated = await checkBalances();236      if(accountsCreated) break;237      await wait.newBlocks(1);238    }239240    if (!accountsCreated) throw Error('Accounts generation failed');241    //#endregion242243    return accounts;244  };245246  // TODO combine this method and createAccounts into one247  createCrowd = async (accountsToCreate: number, withBalance: bigint, donor: IKeyringPair): Promise<IKeyringPair[]> => {248    const createAsManyAsCan = async () => {249      let transactions: any = [];250      const accounts: IKeyringPair[] = [];251      let nonce = await this.helper.chain.getNonce(donor.address);252      const tokenNominal = this.helper.balance.getOneTokenNominal();253      for (let i = 0; i < accountsToCreate; i++) {254        if (i === 500) { // if there are too many accounts to create255          await Promise.allSettled(transactions); // wait while first 500 (should be 100 for devnode) tx will be settled256          transactions = []; //257          nonce = await this.helper.chain.getNonce(donor.address); // update nonce258        }259        const recepient = this.helper.util.fromSeed(mnemonicGenerate());260        accounts.push(recepient);261        if (withBalance !== 0n) {262          const tx = this.helper.constructApiCall('api.tx.balances.transfer', [{Id: recepient.address}, withBalance * tokenNominal]);263          transactions.push(this.helper.signTransaction(donor, tx, {nonce}, 'account generation'));264          nonce++;265        }266      }267268      const fullfilledAccounts = [];269      await Promise.allSettled(transactions);270      for (const account of accounts) {271        const accountBalance = await this.helper.balance.getSubstrate(account.address);272        if (accountBalance === withBalance * tokenNominal) {273          fullfilledAccounts.push(account);274        }275      }276      return fullfilledAccounts;277    };278279280    const crowd: IKeyringPair[] = [];281    // do up to 5 retries282    for (let index = 0; index < 5 && accountsToCreate !== 0; index++) {283      const asManyAsCan = await createAsManyAsCan();284      crowd.push(...asManyAsCan);285      accountsToCreate -= asManyAsCan.length;286    }287288    if (accountsToCreate !== 0) throw Error(`Crowd generation failed: ${accountsToCreate} accounts left`);289290    return crowd;291  };292293  isDevNode = async () => {294    let blockNumber = (await this.helper.callRpc('api.query.system.number')).toJSON();295    if (blockNumber == 0) {296      await this.helper.wait.newBlocks(1);297      blockNumber = (await this.helper.callRpc('api.query.system.number')).toJSON();298    }299    const block2 = await this.helper.callRpc('api.rpc.chain.getBlock', [await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockNumber])]);300    const block1 = await this.helper.callRpc('api.rpc.chain.getBlock', [await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockNumber - 1])]);301    const findCreationDate = (block: any) => {302      const humanBlock = block.toHuman();303      let date;304      humanBlock.block.extrinsics.forEach((ext: any) => {305        if(ext.method.section === 'timestamp') {306          date = Number(ext.method.args.now.replaceAll(',', ''));307        }308      });309      return date;310    };311    const block1date = await findCreationDate(block1);312    const block2date = await findCreationDate(block2);313    if(block2date! - block1date! < 9000) return true;314  };315316  async calculcateFee(payer: ICrossAccountId, promise: () => Promise<any>): Promise<bigint> {317    const address = payer.Substrate ? payer.Substrate : await this.helper.address.ethToSubstrate(payer.Ethereum!);318    let balance = await this.helper.balance.getSubstrate(address);319320    await promise();321322    balance -= await this.helper.balance.getSubstrate(address);323324    return balance;325  }326327  async calculatePoVInfo(txs: any[]): Promise<IPovInfo> {328    const rawPovInfo = await this.helper.callRpc('api.rpc.povinfo.estimateExtrinsicPoV', [txs]);329330    const kvJson: {[key: string]: string} = {};331332    for (const kv of rawPovInfo.keyValues) {333      kvJson[kv.key.toHex()] = kv.value.toHex();334    }335336    const kvStr = JSON.stringify(kvJson);337338    const chainql = spawnSync(339      'chainql', 340      [341        `--tla-code=data=${kvStr}`,342        '-e', 'function(data) cql.dump(cql.chain("wss://ws-opal.unique.network:443").latest._meta, data, {omit_empty:true})',343      ],344    );345346    if (!chainql.stdout) {347      throw Error('unable to get an output from the `chainql`');348    }349350    return {351      proofSize: rawPovInfo.proofSize.toNumber(),352      compactProofSize: rawPovInfo.compactProofSize.toNumber(),353      compressedProofSize: rawPovInfo.compressedProofSize.toNumber(),354      results: rawPovInfo.results,355      kv: JSON.parse(chainql.stdout.toString()),356    };357  }358359  calculatePalletAddress(palletId: any) {360    const address = stringToU8a(('modl' + palletId).padEnd(32, '\0'));361    return encodeAddress(address, this.helper.chain.getChainProperties().ss58Format);362  }363364  makeScheduledIds(num: number): string[] {365    function makeId(slider: number) {366      const scheduledIdSize = 64;367      const hexId = slider.toString(16);368      const prefixSize = scheduledIdSize - hexId.length;369370      const scheduledId = '0x' + '0'.repeat(prefixSize) + hexId;371372      return scheduledId;373    }374375    const ids = [];376    for (let i = 0; i < num; i++) {377      ids.push(makeId(this.scheduledIdSlider));378      this.scheduledIdSlider += 1;379    }380381    return ids;382  }383384  makeScheduledId(): string {385    return (this.makeScheduledIds(1))[0];386  }387388  async captureEvents(eventSection: string, eventMethod: string): Promise<EventCapture> {389    const capture = new EventCapture(this.helper, eventSection, eventMethod);390    await capture.startCapture();391392    return capture;393  }394}395396class MoonbeamAccountGroup {397  helper: MoonbeamHelper;398399  keyring: Keyring;400  _alithAccount: IKeyringPair;401  _baltatharAccount: IKeyringPair;402  _dorothyAccount: IKeyringPair;403404  constructor(helper: MoonbeamHelper) {405    this.helper = helper;406407    this.keyring = new Keyring({type: 'ethereum'});408    const alithPrivateKey = '0x5fb92d6e98884f76de468fa3f6278f8807c48bebc13595d45af5bdc4da702133';409    const baltatharPrivateKey = '0x8075991ce870b93a8870eca0c0f91913d12f47948ca0fd25b49c6fa7cdbeee8b';410    const dorothyPrivateKey = '0x39539ab1876910bbf3a223d84a29e28f1cb4e2e456503e7e91ed39b2e7223d68';411412    this._alithAccount = this.keyring.addFromUri(alithPrivateKey, undefined, 'ethereum');413    this._baltatharAccount = this.keyring.addFromUri(baltatharPrivateKey, undefined, 'ethereum');414    this._dorothyAccount = this.keyring.addFromUri(dorothyPrivateKey, undefined, 'ethereum');415  }416417  alithAccount() {418    return this._alithAccount;419  }420421  baltatharAccount() {422    return this._baltatharAccount;423  }424425  dorothyAccount() {426    return this._dorothyAccount;427  }428429  create() {430    return this.keyring.addFromUri(mnemonicGenerate());431  }432}433434class WaitGroup {435  helper: ChainHelperBase;436437  constructor(helper: ChainHelperBase) {438    this.helper = helper;439  }440441  sleep(milliseconds: number) {442    return new Promise((resolve) => setTimeout(resolve, milliseconds));443  }444445  private async waitWithTimeout(promise: Promise<any>, timeout: number) {446    let isBlock = false;447    promise.then(() => isBlock = true).catch(() => isBlock = true);448    let totalTime = 0;449    const step = 100;450    while(!isBlock) {451      await this.sleep(step);452      totalTime += step;453      if(totalTime >= timeout) throw Error('Blocks production failed');454    }455    return promise;456  }457458  /**459   * Wait for specified number of blocks460   * @param blocksCount number of blocks to wait461   * @returns462   */463  async newBlocks(blocksCount = 1, timeout?: number): Promise<void> {464    timeout = timeout ?? blocksCount * 60_000;465    // eslint-disable-next-line no-async-promise-executor466    const promise = new Promise<void>(async (resolve) => {467      const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads(() => {468        if (blocksCount > 0) {469          blocksCount--;470        } else {471          unsubscribe();472          resolve();473        }474      });475    });476    await this.waitWithTimeout(promise, timeout);477    return promise;478  }479480  async forParachainBlockNumber(blockNumber: bigint | number, timeout?: number) {481    timeout = timeout ?? 30 * 60 * 1000;482    // eslint-disable-next-line no-async-promise-executor483    const promise = new Promise<void>(async (resolve) => {484      const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads((data: any) => {485        if (data.number.toNumber() >= blockNumber) {486          unsubscribe();487          resolve();488        }489      });490    });491    await this.waitWithTimeout(promise, timeout);492    return promise;493  }494495  async forRelayBlockNumber(blockNumber: bigint | number, timeout?: number) {496    timeout = timeout ?? 30 * 60 * 1000;497    // eslint-disable-next-line no-async-promise-executor498    const promise = new Promise<void>(async (resolve) => {499      const unsubscribe = await this.helper.getApi().query.parachainSystem.validationData((data: any) => {500        if (data.value.relayParentNumber.toNumber() >= blockNumber) {501          // @ts-ignore502          unsubscribe();503          resolve();504        }505      });506    });507    await this.waitWithTimeout(promise, timeout);508    return promise;509  }510511  noScheduledTasks() {512    const api = this.helper.getApi();513514    // eslint-disable-next-line no-async-promise-executor515    const promise = new Promise<void>(async resolve => {516      const unsubscribe = await api.rpc.chain.subscribeNewHeads(async () => {517        const areThereScheduledTasks = await api.query.scheduler.lookup.entries();518519        if(areThereScheduledTasks.length == 0) {520          unsubscribe();521          resolve();522        }523      });524    });525526    return promise;527  }528529  event(maxBlocksToWait: number, eventSection: string, eventMethod: string) {530    // eslint-disable-next-line no-async-promise-executor531    const promise = new Promise<EventRecord | null>(async (resolve) => {532      const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads(async header => {533        const blockNumber = header.number.toHuman();534        const blockHash = header.hash;535        const eventIdStr = `${eventSection}.${eventMethod}`;536        const waitLimitStr = `wait blocks remaining: ${maxBlocksToWait}`;537538        this.helper.logger.log(`[Block #${blockNumber}] Waiting for event \`${eventIdStr}\` (${waitLimitStr})`);539540        const apiAt = await this.helper.getApi().at(blockHash);541        const eventRecords = (await apiAt.query.system.events()) as any;542543        const neededEvent = eventRecords.toArray().find((r: FrameSystemEventRecord) => {544          return r.event.section == eventSection && r.event.method == eventMethod;545        });546547        if (neededEvent) {548          unsubscribe();549          resolve(neededEvent);550        } else if (maxBlocksToWait > 0) {551          maxBlocksToWait--;552        } else {553          this.helper.logger.log(`Event \`${eventIdStr}\` is NOT found`);554          unsubscribe();555          resolve(null);556        }557      });558    });559    return promise;560  }561}562563class TestUtilGroup {564  helper: DevUniqueHelper;565566  constructor(helper: DevUniqueHelper) {567    this.helper = helper;568  }569570  async enable() {571    if (this.helper.fetchMissingPalletNames([Pallets.TestUtils]).length != 0) {572      return;573    }574575    const signer = this.helper.util.fromSeed('//Alice');576    await this.helper.getSudo<DevUniqueHelper>().executeExtrinsic(signer, 'api.tx.testUtils.enable', [], true);577  }578579  async setTestValue(signer: TSigner, testVal: number) {580    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.setTestValue', [testVal], true);581  }582583  async incTestValue(signer: TSigner) {584    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.incTestValue', [], true);585  }586587  async setTestValueAndRollback(signer: TSigner, testVal: number) {588    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.setTestValueAndRollback', [testVal], true);589  }590591  async testValue(blockIdx?: number) {592    const api = blockIdx593      ? await this.helper.getApi().at(await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockIdx]))594      : this.helper.getApi();595596    return (await api.query.testUtils.testValue()).toJSON();597  }598599  async justTakeFee(signer: TSigner) {600    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.justTakeFee', [], true);601  }602603  async selfCancelingInc(signer: TSigner, scheduledId: string, maxTestVal: number) {604    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.selfCancelingInc', [scheduledId, maxTestVal], true);605  }606}607608class EventCapture {609  helper: DevUniqueHelper;610  eventSection: string;611  eventMethod: string;612  events: EventRecord[] = [];613  unsubscribe: VoidFn | null = null;614615  constructor(616    helper: DevUniqueHelper,617    eventSection: string,618    eventMethod: string,619  ) {620    this.helper = helper;621    this.eventSection = eventSection;622    this.eventMethod = eventMethod;623  }624625  async startCapture() {626    this.stopCapture();627    this.unsubscribe = (await this.helper.getApi().query.system.events((eventRecords: FrameSystemEventRecord[]) => {628      const newEvents = eventRecords.filter(r => {629        return r.event.section == this.eventSection && r.event.method == this.eventMethod;630      });631632      this.events.push(...newEvents);633    })) as any;634  }635636  stopCapture() {637    if (this.unsubscribe !== null) {638      this.unsubscribe();639    }640  }641642  extractCapturedEvents() {643    return this.events;644  }645}646647class AdminGroup {648  helper: UniqueHelper;649650  constructor(helper: UniqueHelper) {651    this.helper = helper;652  }653654  async payoutStakers(signer: IKeyringPair, stakersToPayout: number) {655    const payoutResult = await this.helper.executeExtrinsic(signer, 'api.tx.appPromotion.payoutStakers', [stakersToPayout], true);656    return payoutResult.result.events.filter(e => e.event.method === 'StakingRecalculation').map(e => {657      return {658        staker: e.event.data[0].toString(),659        stake: e.event.data[1].toBigInt(),660        payout: e.event.data[2].toBigInt(),661      };662    });663  }664}
after · tests/src/util/playgrounds/unique.dev.ts
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// SPDX-License-Identifier: Apache-2.034import {stringToU8a} from '@polkadot/util';5import {encodeAddress, mnemonicGenerate} from '@polkadot/util-crypto';6import {UniqueHelper, MoonbeamHelper, ChainHelperBase, AcalaHelper, RelayHelper, WestmintHelper} from './unique';7import {ApiPromise, Keyring, WsProvider} from '@polkadot/api';8import * as defs from '../../interfaces/definitions';9import {IKeyringPair} from '@polkadot/types/types';10import {EventRecord} from '@polkadot/types/interfaces';11import {ICrossAccountId, IPovInfo, TSigner} from './types';12import {FrameSystemEventRecord} from '@polkadot/types/lookup';13import {VoidFn} from '@polkadot/api/types';14import {Pallets} from '..';15import {spawnSync} from 'child_process';1617export class SilentLogger {18  log(_msg: any, _level: any): void { }19  level = {20    ERROR: 'ERROR' as const,21    WARNING: 'WARNING' as const,22    INFO: 'INFO' as const,23  };24}2526export class SilentConsole {27  // TODO: Remove, this is temporary: Filter unneeded API output28  // (Jaco promised it will be removed in the next version)29  consoleErr: any;30  consoleLog: any;31  consoleWarn: any;3233  constructor() {34    this.consoleErr = console.error;35    this.consoleLog = console.log;36    this.consoleWarn = console.warn;37  }3839  enable() {40    const outFn = (printer: any) => (...args: any[]) => {41      for (const arg of args) {42        if (typeof arg !== 'string')43          continue;44        if (arg.includes('1000:: Normal connection closure') || arg.includes('Not decorating unknown runtime apis:') || arg.includes('RPC methods not decorated:') || arg === 'Normal connection closure')45          return;46      }47      printer(...args);48    };4950    console.error = outFn(this.consoleErr.bind(console));51    console.log = outFn(this.consoleLog.bind(console));52    console.warn = outFn(this.consoleWarn.bind(console));53  }5455  disable() {56    console.error = this.consoleErr;57    console.log = this.consoleLog;58    console.warn = this.consoleWarn;59  }60}6162export class DevUniqueHelper extends UniqueHelper {63  /**64   * Arrange methods for tests65   */66  arrange: ArrangeGroup;67  wait: WaitGroup;68  admin: AdminGroup;69  testUtils: TestUtilGroup;7071  constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {72    options.helperBase = options.helperBase ?? DevUniqueHelper;7374    super(logger, options);75    this.arrange = new ArrangeGroup(this);76    this.wait = new WaitGroup(this);77    this.admin = new AdminGroup(this);78    this.testUtils = new TestUtilGroup(this);79  }8081  async connect(wsEndpoint: string, _listeners?: any): Promise<void> {82    const wsProvider = new WsProvider(wsEndpoint);83    this.api = new ApiPromise({84      provider: wsProvider,85      signedExtensions: {86        ContractHelpers: {87          extrinsic: {},88          payload: {},89        },90        CheckMaintenance: {91          extrinsic: {},92          payload: {},93        },94        FakeTransactionFinalizer: {95          extrinsic: {},96          payload: {},97        },98      },99      rpc: {100        unique: defs.unique.rpc,101        appPromotion: defs.appPromotion.rpc,102        povinfo: defs.povinfo.rpc,103        rmrk: defs.rmrk.rpc,104        eth: {105          feeHistory: {106            description: 'Dummy',107            params: [],108            type: 'u8',109          },110          maxPriorityFeePerGas: {111            description: 'Dummy',112            params: [],113            type: 'u8',114          },115        },116      },117    });118    await this.api.isReadyOrError;119    this.network = await UniqueHelper.detectNetwork(this.api);120    this.wsEndpoint = wsEndpoint;121  }122}123124export class DevRelayHelper extends RelayHelper {125  wait: WaitGroup;126127  constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {128    options.helperBase = options.helperBase ?? DevRelayHelper;129130    super(logger, options);131    this.wait = new WaitGroup(this);132  }133}134135export class DevWestmintHelper extends WestmintHelper {136  wait: WaitGroup;137138  constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {139    options.helperBase = options.helperBase ?? DevWestmintHelper;140141    super(logger, options);142    this.wait = new WaitGroup(this);143  }144}145146export class DevStatemineHelper extends DevWestmintHelper {}147148export class DevStatemintHelper extends DevWestmintHelper {}149150export class DevMoonbeamHelper extends MoonbeamHelper {151  account: MoonbeamAccountGroup;152  wait: WaitGroup;153154  constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {155    options.helperBase = options.helperBase ?? DevMoonbeamHelper;156    options.notePreimagePallet = options.notePreimagePallet ?? 'democracy';157158    super(logger, options);159    this.account = new MoonbeamAccountGroup(this);160    this.wait = new WaitGroup(this);161  }162}163164export class DevMoonriverHelper extends DevMoonbeamHelper {165  constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {166    options.notePreimagePallet = options.notePreimagePallet ?? 'preimage';167    super(logger, options);168  }169}170171export class DevAcalaHelper extends AcalaHelper {172  wait: WaitGroup;173174  constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {175    options.helperBase = options.helperBase ?? DevAcalaHelper;176177    super(logger, options);178    this.wait = new WaitGroup(this);179  }180}181182export class DevKaruraHelper extends DevAcalaHelper {}183184class ArrangeGroup {185  helper: DevUniqueHelper;186187  scheduledIdSlider = 0;188189  constructor(helper: DevUniqueHelper) {190    this.helper = helper;191  }192193  /**194   * Generates accounts with the specified UNQ token balance195   * @param balances balances for generated accounts. Each balance will be multiplied by the token nominal.196   * @param donor donor account for balances197   * @returns array of newly created accounts198   * @example const [acc1, acc2, acc3] = await createAccounts([0n, 10n, 20n], donor);199   */200  createAccounts = async (balances: bigint[], donor: IKeyringPair): Promise<IKeyringPair[]> => {201    let nonce = await this.helper.chain.getNonce(donor.address);202    const wait = new WaitGroup(this.helper);203    const ss58Format = this.helper.chain.getChainProperties().ss58Format;204    const tokenNominal = this.helper.balance.getOneTokenNominal();205    const transactions = [];206    const accounts: IKeyringPair[] = [];207    for (const balance of balances) {208      const recipient = this.helper.util.fromSeed(mnemonicGenerate(), ss58Format);209      accounts.push(recipient);210      if (balance !== 0n) {211        const tx = this.helper.constructApiCall('api.tx.balances.transfer', [{Id: recipient.address}, balance * tokenNominal]);212        transactions.push(this.helper.signTransaction(donor, tx, {nonce}, 'account generation'));213        nonce++;214      }215    }216217    await Promise.all(transactions).catch(_e => {});218219    //#region TODO remove this region, when nonce problem will be solved220    const checkBalances = async () => {221      let isSuccess = true;222      for (let i = 0; i < balances.length; i++) {223        const balance = await this.helper.balance.getSubstrate(accounts[i].address);224        if (balance !== balances[i] * tokenNominal) {225          isSuccess = false;226          break;227        }228      }229      return isSuccess;230    };231232    let accountsCreated = false;233    const maxBlocksChecked = await this.helper.arrange.isDevNode() ? 50 : 5;234    // checkBalances retry up to 5-50 blocks235    for (let index = 0; index < maxBlocksChecked; index++) {236      accountsCreated = await checkBalances();237      if(accountsCreated) break;238      await wait.newBlocks(1);239    }240241    if (!accountsCreated) throw Error('Accounts generation failed');242    //#endregion243244    return accounts;245  };246247  // TODO combine this method and createAccounts into one248  createCrowd = async (accountsToCreate: number, withBalance: bigint, donor: IKeyringPair): Promise<IKeyringPair[]> => {249    const createAsManyAsCan = async () => {250      let transactions: any = [];251      const accounts: IKeyringPair[] = [];252      let nonce = await this.helper.chain.getNonce(donor.address);253      const tokenNominal = this.helper.balance.getOneTokenNominal();254      for (let i = 0; i < accountsToCreate; i++) {255        if (i === 500) { // if there are too many accounts to create256          await Promise.allSettled(transactions); // wait while first 500 (should be 100 for devnode) tx will be settled257          transactions = []; //258          nonce = await this.helper.chain.getNonce(donor.address); // update nonce259        }260        const recepient = this.helper.util.fromSeed(mnemonicGenerate());261        accounts.push(recepient);262        if (withBalance !== 0n) {263          const tx = this.helper.constructApiCall('api.tx.balances.transfer', [{Id: recepient.address}, withBalance * tokenNominal]);264          transactions.push(this.helper.signTransaction(donor, tx, {nonce}, 'account generation'));265          nonce++;266        }267      }268269      const fullfilledAccounts = [];270      await Promise.allSettled(transactions);271      for (const account of accounts) {272        const accountBalance = await this.helper.balance.getSubstrate(account.address);273        if (accountBalance === withBalance * tokenNominal) {274          fullfilledAccounts.push(account);275        }276      }277      return fullfilledAccounts;278    };279280281    const crowd: IKeyringPair[] = [];282    // do up to 5 retries283    for (let index = 0; index < 5 && accountsToCreate !== 0; index++) {284      const asManyAsCan = await createAsManyAsCan();285      crowd.push(...asManyAsCan);286      accountsToCreate -= asManyAsCan.length;287    }288289    if (accountsToCreate !== 0) throw Error(`Crowd generation failed: ${accountsToCreate} accounts left`);290291    return crowd;292  };293294  isDevNode = async () => {295    let blockNumber = (await this.helper.callRpc('api.query.system.number')).toJSON();296    if (blockNumber == 0) {297      await this.helper.wait.newBlocks(1);298      blockNumber = (await this.helper.callRpc('api.query.system.number')).toJSON();299    }300    const block2 = await this.helper.callRpc('api.rpc.chain.getBlock', [await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockNumber])]);301    const block1 = await this.helper.callRpc('api.rpc.chain.getBlock', [await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockNumber - 1])]);302    const findCreationDate = (block: any) => {303      const humanBlock = block.toHuman();304      let date;305      humanBlock.block.extrinsics.forEach((ext: any) => {306        if(ext.method.section === 'timestamp') {307          date = Number(ext.method.args.now.replaceAll(',', ''));308        }309      });310      return date;311    };312    const block1date = await findCreationDate(block1);313    const block2date = await findCreationDate(block2);314    if(block2date! - block1date! < 9000) return true;315  };316317  async calculcateFee(payer: ICrossAccountId, promise: () => Promise<any>): Promise<bigint> {318    const address = payer.Substrate ? payer.Substrate : await this.helper.address.ethToSubstrate(payer.Ethereum!);319    let balance = await this.helper.balance.getSubstrate(address);320321    await promise();322323    balance -= await this.helper.balance.getSubstrate(address);324325    return balance;326  }327328  async calculatePoVInfo(txs: any[]): Promise<IPovInfo> {329    const rawPovInfo = await this.helper.callRpc('api.rpc.povinfo.estimateExtrinsicPoV', [txs]);330331    const kvJson: {[key: string]: string} = {};332333    for (const kv of rawPovInfo.keyValues) {334      kvJson[kv.key.toHex()] = kv.value.toHex();335    }336337    const kvStr = JSON.stringify(kvJson);338339    const chainql = spawnSync(340      'chainql', 341      [342        `--tla-code=data=${kvStr}`,343        '-e', `function(data) cql.dump(cql.chain("${this.helper.getEndpoint()}").latest._meta, data, {omit_empty:true})`,344      ],345    );346347    if (!chainql.stdout) {348      throw Error('unable to get an output from the `chainql`');349    }350351    return {352      proofSize: rawPovInfo.proofSize.toNumber(),353      compactProofSize: rawPovInfo.compactProofSize.toNumber(),354      compressedProofSize: rawPovInfo.compressedProofSize.toNumber(),355      results: rawPovInfo.results,356      kv: JSON.parse(chainql.stdout.toString()),357    };358  }359360  calculatePalletAddress(palletId: any) {361    const address = stringToU8a(('modl' + palletId).padEnd(32, '\0'));362    return encodeAddress(address, this.helper.chain.getChainProperties().ss58Format);363  }364365  makeScheduledIds(num: number): string[] {366    function makeId(slider: number) {367      const scheduledIdSize = 64;368      const hexId = slider.toString(16);369      const prefixSize = scheduledIdSize - hexId.length;370371      const scheduledId = '0x' + '0'.repeat(prefixSize) + hexId;372373      return scheduledId;374    }375376    const ids = [];377    for (let i = 0; i < num; i++) {378      ids.push(makeId(this.scheduledIdSlider));379      this.scheduledIdSlider += 1;380    }381382    return ids;383  }384385  makeScheduledId(): string {386    return (this.makeScheduledIds(1))[0];387  }388389  async captureEvents(eventSection: string, eventMethod: string): Promise<EventCapture> {390    const capture = new EventCapture(this.helper, eventSection, eventMethod);391    await capture.startCapture();392393    return capture;394  }395}396397class MoonbeamAccountGroup {398  helper: MoonbeamHelper;399400  keyring: Keyring;401  _alithAccount: IKeyringPair;402  _baltatharAccount: IKeyringPair;403  _dorothyAccount: IKeyringPair;404405  constructor(helper: MoonbeamHelper) {406    this.helper = helper;407408    this.keyring = new Keyring({type: 'ethereum'});409    const alithPrivateKey = '0x5fb92d6e98884f76de468fa3f6278f8807c48bebc13595d45af5bdc4da702133';410    const baltatharPrivateKey = '0x8075991ce870b93a8870eca0c0f91913d12f47948ca0fd25b49c6fa7cdbeee8b';411    const dorothyPrivateKey = '0x39539ab1876910bbf3a223d84a29e28f1cb4e2e456503e7e91ed39b2e7223d68';412413    this._alithAccount = this.keyring.addFromUri(alithPrivateKey, undefined, 'ethereum');414    this._baltatharAccount = this.keyring.addFromUri(baltatharPrivateKey, undefined, 'ethereum');415    this._dorothyAccount = this.keyring.addFromUri(dorothyPrivateKey, undefined, 'ethereum');416  }417418  alithAccount() {419    return this._alithAccount;420  }421422  baltatharAccount() {423    return this._baltatharAccount;424  }425426  dorothyAccount() {427    return this._dorothyAccount;428  }429430  create() {431    return this.keyring.addFromUri(mnemonicGenerate());432  }433}434435class WaitGroup {436  helper: ChainHelperBase;437438  constructor(helper: ChainHelperBase) {439    this.helper = helper;440  }441442  sleep(milliseconds: number) {443    return new Promise((resolve) => setTimeout(resolve, milliseconds));444  }445446  private async waitWithTimeout(promise: Promise<any>, timeout: number) {447    let isBlock = false;448    promise.then(() => isBlock = true).catch(() => isBlock = true);449    let totalTime = 0;450    const step = 100;451    while(!isBlock) {452      await this.sleep(step);453      totalTime += step;454      if(totalTime >= timeout) throw Error('Blocks production failed');455    }456    return promise;457  }458459  /**460   * Wait for specified number of blocks461   * @param blocksCount number of blocks to wait462   * @returns463   */464  async newBlocks(blocksCount = 1, timeout?: number): Promise<void> {465    timeout = timeout ?? blocksCount * 60_000;466    // eslint-disable-next-line no-async-promise-executor467    const promise = new Promise<void>(async (resolve) => {468      const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads(() => {469        if (blocksCount > 0) {470          blocksCount--;471        } else {472          unsubscribe();473          resolve();474        }475      });476    });477    await this.waitWithTimeout(promise, timeout);478    return promise;479  }480481  async forParachainBlockNumber(blockNumber: bigint | number, timeout?: number) {482    timeout = timeout ?? 30 * 60 * 1000;483    // eslint-disable-next-line no-async-promise-executor484    const promise = new Promise<void>(async (resolve) => {485      const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads((data: any) => {486        if (data.number.toNumber() >= blockNumber) {487          unsubscribe();488          resolve();489        }490      });491    });492    await this.waitWithTimeout(promise, timeout);493    return promise;494  }495496  async forRelayBlockNumber(blockNumber: bigint | number, timeout?: number) {497    timeout = timeout ?? 30 * 60 * 1000;498    // eslint-disable-next-line no-async-promise-executor499    const promise = new Promise<void>(async (resolve) => {500      const unsubscribe = await this.helper.getApi().query.parachainSystem.validationData((data: any) => {501        if (data.value.relayParentNumber.toNumber() >= blockNumber) {502          // @ts-ignore503          unsubscribe();504          resolve();505        }506      });507    });508    await this.waitWithTimeout(promise, timeout);509    return promise;510  }511512  noScheduledTasks() {513    const api = this.helper.getApi();514515    // eslint-disable-next-line no-async-promise-executor516    const promise = new Promise<void>(async resolve => {517      const unsubscribe = await api.rpc.chain.subscribeNewHeads(async () => {518        const areThereScheduledTasks = await api.query.scheduler.lookup.entries();519520        if(areThereScheduledTasks.length == 0) {521          unsubscribe();522          resolve();523        }524      });525    });526527    return promise;528  }529530  event(maxBlocksToWait: number, eventSection: string, eventMethod: string) {531    // eslint-disable-next-line no-async-promise-executor532    const promise = new Promise<EventRecord | null>(async (resolve) => {533      const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads(async header => {534        const blockNumber = header.number.toHuman();535        const blockHash = header.hash;536        const eventIdStr = `${eventSection}.${eventMethod}`;537        const waitLimitStr = `wait blocks remaining: ${maxBlocksToWait}`;538539        this.helper.logger.log(`[Block #${blockNumber}] Waiting for event \`${eventIdStr}\` (${waitLimitStr})`);540541        const apiAt = await this.helper.getApi().at(blockHash);542        const eventRecords = (await apiAt.query.system.events()) as any;543544        const neededEvent = eventRecords.toArray().find((r: FrameSystemEventRecord) => {545          return r.event.section == eventSection && r.event.method == eventMethod;546        });547548        if (neededEvent) {549          unsubscribe();550          resolve(neededEvent);551        } else if (maxBlocksToWait > 0) {552          maxBlocksToWait--;553        } else {554          this.helper.logger.log(`Event \`${eventIdStr}\` is NOT found`);555          unsubscribe();556          resolve(null);557        }558      });559    });560    return promise;561  }562}563564class TestUtilGroup {565  helper: DevUniqueHelper;566567  constructor(helper: DevUniqueHelper) {568    this.helper = helper;569  }570571  async enable() {572    if (this.helper.fetchMissingPalletNames([Pallets.TestUtils]).length != 0) {573      return;574    }575576    const signer = this.helper.util.fromSeed('//Alice');577    await this.helper.getSudo<DevUniqueHelper>().executeExtrinsic(signer, 'api.tx.testUtils.enable', [], true);578  }579580  async setTestValue(signer: TSigner, testVal: number) {581    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.setTestValue', [testVal], true);582  }583584  async incTestValue(signer: TSigner) {585    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.incTestValue', [], true);586  }587588  async setTestValueAndRollback(signer: TSigner, testVal: number) {589    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.setTestValueAndRollback', [testVal], true);590  }591592  async testValue(blockIdx?: number) {593    const api = blockIdx594      ? await this.helper.getApi().at(await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockIdx]))595      : this.helper.getApi();596597    return (await api.query.testUtils.testValue()).toJSON();598  }599600  async justTakeFee(signer: TSigner) {601    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.justTakeFee', [], true);602  }603604  async selfCancelingInc(signer: TSigner, scheduledId: string, maxTestVal: number) {605    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.selfCancelingInc', [scheduledId, maxTestVal], true);606  }607}608609class EventCapture {610  helper: DevUniqueHelper;611  eventSection: string;612  eventMethod: string;613  events: EventRecord[] = [];614  unsubscribe: VoidFn | null = null;615616  constructor(617    helper: DevUniqueHelper,618    eventSection: string,619    eventMethod: string,620  ) {621    this.helper = helper;622    this.eventSection = eventSection;623    this.eventMethod = eventMethod;624  }625626  async startCapture() {627    this.stopCapture();628    this.unsubscribe = (await this.helper.getApi().query.system.events((eventRecords: FrameSystemEventRecord[]) => {629      const newEvents = eventRecords.filter(r => {630        return r.event.section == this.eventSection && r.event.method == this.eventMethod;631      });632633      this.events.push(...newEvents);634    })) as any;635  }636637  stopCapture() {638    if (this.unsubscribe !== null) {639      this.unsubscribe();640    }641  }642643  extractCapturedEvents() {644    return this.events;645  }646}647648class AdminGroup {649  helper: UniqueHelper;650651  constructor(helper: UniqueHelper) {652    this.helper = helper;653  }654655  async payoutStakers(signer: IKeyringPair, stakersToPayout: number) {656    const payoutResult = await this.helper.executeExtrinsic(signer, 'api.tx.appPromotion.payoutStakers', [stakersToPayout], true);657    return payoutResult.result.events.filter(e => e.event.method === 'StakingRecalculation').map(e => {658      return {659        staker: e.event.data[0].toString(),660        stake: e.event.data[1].toBigInt(),661        payout: e.event.data[2].toBigInt(),662      };663    });664  }665}
modifiedtests/src/util/playgrounds/unique.tsdiffbeforeafterboth
--- a/tests/src/util/playgrounds/unique.ts
+++ b/tests/src/util/playgrounds/unique.ts
@@ -371,6 +371,7 @@
   api: ApiPromise | null;
   forcedNetwork: TNetworks | null;
   network: TNetworks | null;
+  wsEndpoint: string | null;
   chainLog: IUniqueHelperLog[];
   children: ChainHelperBase[];
   address: AddressGroup;
@@ -386,6 +387,7 @@
     this.api = null;
     this.forcedNetwork = null;
     this.network = null;
+    this.wsEndpoint = null;
     this.chainLog = [];
     this.children = [];
     this.address = new AddressGroup(this);
@@ -405,6 +407,11 @@
     return newHelper;
   }
 
+  getEndpoint(): string {
+    if (this.wsEndpoint === null) throw Error('No connection was established');
+    return this.wsEndpoint;
+  }
+
   getApi(): ApiPromise {
     if(this.api === null) throw Error('API not initialized');
     return this.api;
@@ -436,6 +443,7 @@
   async connect(wsEndpoint: string, listeners?: IApiListeners) {
     if (this.api !== null) throw Error('Already connected');
     const {api, network} = await ChainHelperBase.createConnection(wsEndpoint, listeners, this.forcedNetwork);
+    this.wsEndpoint = wsEndpoint;
     this.api = api;
     this.network = network;
   }