git.delta.rocks / unique-network / refs/commits / 378b7f3ae3aa

difftreelog

source

tests/src/util/playgrounds/unique.dev.ts19.4 KiBsourcehistory
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, TSigner} from './types';12import {FrameSystemEventRecord} from '@polkadot/types/lookup';13import {VoidFn} from '@polkadot/api/types';14import {Pallets} from '..';1516export class SilentLogger {17  log(_msg: any, _level: any): void { }18  level = {19    ERROR: 'ERROR' as const,20    WARNING: 'WARNING' as const,21    INFO: 'INFO' as const,22  };23}2425export class SilentConsole {26  // TODO: Remove, this is temporary: Filter unneeded API output27  // (Jaco promised it will be removed in the next version)28  consoleErr: any;29  consoleLog: any;30  consoleWarn: any;3132  constructor() {33    this.consoleErr = console.error;34    this.consoleLog = console.log;35    this.consoleWarn = console.warn;36  }3738  enable() {  39    const outFn = (printer: any) => (...args: any[]) => {40      for (const arg of args) {41        if (typeof arg !== 'string')42          continue;43        if (arg.includes('1000:: Normal connection closure') || arg.includes('Not decorating unknown runtime apis:') || arg.includes('RPC methods not decorated:') || arg === 'Normal connection closure')44          return;45      }46      printer(...args);47    };48  49    console.error = outFn(this.consoleErr.bind(console));50    console.log = outFn(this.consoleLog.bind(console));51    console.warn = outFn(this.consoleWarn.bind(console));52  }5354  disable() {55    console.error = this.consoleErr;56    console.log = this.consoleLog;57    console.warn = this.consoleWarn;58  }59}6061export class DevUniqueHelper extends UniqueHelper {62  /**63   * Arrange methods for tests64   */65  arrange: ArrangeGroup;66  wait: WaitGroup;67  admin: AdminGroup;68  testUtils: TestUtilGroup;6970  constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {71    options.helperBase = options.helperBase ?? DevUniqueHelper;7273    super(logger, options);74    this.arrange = new ArrangeGroup(this);75    this.wait = new WaitGroup(this);76    this.admin = new AdminGroup(this);77    this.testUtils = new TestUtilGroup(this);78  }7980  async connect(wsEndpoint: string, _listeners?: any): Promise<void> {81    const wsProvider = new WsProvider(wsEndpoint);82    this.api = new ApiPromise({83      provider: wsProvider,84      signedExtensions: {85        ContractHelpers: {86          extrinsic: {},87          payload: {},88        },89        CheckMaintenance: {90          extrinsic: {},91          payload: {},92        },93        FakeTransactionFinalizer: {94          extrinsic: {},95          payload: {},96        },97      },98      rpc: {99        unique: defs.unique.rpc,100        appPromotion: defs.appPromotion.rpc,101        rmrk: defs.rmrk.rpc,102        eth: {103          feeHistory: {104            description: 'Dummy',105            params: [],106            type: 'u8',107          },108          maxPriorityFeePerGas: {109            description: 'Dummy',110            params: [],111            type: 'u8',112          },113        },114      },115    });116    await this.api.isReadyOrError;117    this.network = await UniqueHelper.detectNetwork(this.api);118  }119}120121export class DevRelayHelper extends RelayHelper {}122123export class DevWestmintHelper extends WestmintHelper {124  wait: WaitGroup;125126  constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {127    options.helperBase = options.helperBase ?? DevWestmintHelper;128129    super(logger, options);130    this.wait = new WaitGroup(this);131  }132}133134export class DevMoonbeamHelper extends MoonbeamHelper {135  account: MoonbeamAccountGroup;136  wait: WaitGroup;137138  constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {139    options.helperBase = options.helperBase ?? DevMoonbeamHelper;140141    super(logger, options);142    this.account = new MoonbeamAccountGroup(this);143    this.wait = new WaitGroup(this);144  }145}146147export class DevMoonriverHelper extends DevMoonbeamHelper {}148149export class DevAcalaHelper extends AcalaHelper {150  wait: WaitGroup;151152  constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {153    options.helperBase = options.helperBase ?? DevAcalaHelper;154155    super(logger, options);156    this.wait = new WaitGroup(this);157  }158}159160export class DevKaruraHelper extends DevAcalaHelper {}161162class ArrangeGroup {163  helper: DevUniqueHelper;164165  scheduledIdSlider = 0;166167  constructor(helper: DevUniqueHelper) {168    this.helper = helper;169  }170171  /**172   * Generates accounts with the specified UNQ token balance 173   * @param balances balances for generated accounts. Each balance will be multiplied by the token nominal.174   * @param donor donor account for balances175   * @returns array of newly created accounts176   * @example const [acc1, acc2, acc3] = await createAccounts([0n, 10n, 20n], donor); 177   */178  createAccounts = async (balances: bigint[], donor: IKeyringPair): Promise<IKeyringPair[]> => {179    let nonce = await this.helper.chain.getNonce(donor.address);180    const wait = new WaitGroup(this.helper);181    const ss58Format = this.helper.chain.getChainProperties().ss58Format;182    const tokenNominal = this.helper.balance.getOneTokenNominal();183    const transactions = [];184    const accounts: IKeyringPair[] = [];185    for (const balance of balances) {186      const recipient = this.helper.util.fromSeed(mnemonicGenerate(), ss58Format);187      accounts.push(recipient);188      if (balance !== 0n) {189        const tx = this.helper.constructApiCall('api.tx.balances.transfer', [{Id: recipient.address}, balance * tokenNominal]);190        transactions.push(this.helper.signTransaction(donor, tx, {nonce}, 'account generation'));191        nonce++;192      }193    }194195    await Promise.all(transactions).catch(_e => {});196    197    //#region TODO remove this region, when nonce problem will be solved198    const checkBalances = async () => {199      let isSuccess = true;200      for (let i = 0; i < balances.length; i++) {201        const balance = await this.helper.balance.getSubstrate(accounts[i].address);202        if (balance !== balances[i] * tokenNominal) {203          isSuccess = false;204          break;205        }206      }207      return isSuccess;208    };209210    let accountsCreated = false;211    const maxBlocksChecked = await this.helper.arrange.isDevNode() ? 50 : 5;212    // checkBalances retry up to 5-50 blocks213    for (let index = 0; index < maxBlocksChecked; index++) {214      accountsCreated = await checkBalances();215      if(accountsCreated) break;216      await wait.newBlocks(1);217    }218219    if (!accountsCreated) throw Error('Accounts generation failed');220    //#endregion221222    return accounts;223  };224225  // TODO combine this method and createAccounts into one226  createCrowd = async (accountsToCreate: number, withBalance: bigint, donor: IKeyringPair): Promise<IKeyringPair[]> => {  227    const createAsManyAsCan = async () => {228      let transactions: any = [];229      const accounts: IKeyringPair[] = [];230      let nonce = await this.helper.chain.getNonce(donor.address);231      const tokenNominal = this.helper.balance.getOneTokenNominal();232      for (let i = 0; i < accountsToCreate; i++) {233        if (i === 500) { // if there are too many accounts to create234          await Promise.allSettled(transactions); // wait while first 500 (should be 100 for devnode) tx will be settled 235          transactions = []; //236          nonce = await this.helper.chain.getNonce(donor.address); // update nonce 237        }238        const recepient = this.helper.util.fromSeed(mnemonicGenerate());239        accounts.push(recepient);240        if (withBalance !== 0n) {241          const tx = this.helper.constructApiCall('api.tx.balances.transfer', [{Id: recepient.address}, withBalance * tokenNominal]);242          transactions.push(this.helper.signTransaction(donor, tx, {nonce}, 'account generation'));243          nonce++;244        }245      }246      247      const fullfilledAccounts = [];248      await Promise.allSettled(transactions);249      for (const account of accounts) {250        const accountBalance = await this.helper.balance.getSubstrate(account.address);251        if (accountBalance === withBalance * tokenNominal) {252          fullfilledAccounts.push(account);253        }254      }255      return fullfilledAccounts;256    };257258    259    const crowd: IKeyringPair[] = [];260    // do up to 5 retries261    for (let index = 0; index < 5 && accountsToCreate !== 0; index++) {262      const asManyAsCan = await createAsManyAsCan();263      crowd.push(...asManyAsCan);264      accountsToCreate -= asManyAsCan.length;265    }266267    if (accountsToCreate !== 0) throw Error(`Crowd generation failed: ${accountsToCreate} accounts left`);268269    return crowd;270  };271272  isDevNode = async () => {273    let blockNumber = (await this.helper.callRpc('api.query.system.number')).toJSON();274    if (blockNumber == 0) {275      await this.helper.wait.newBlocks(1); 276      blockNumber = (await this.helper.callRpc('api.query.system.number')).toJSON();277    }278    const block2 = await this.helper.callRpc('api.rpc.chain.getBlock', [await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockNumber])]);279    const block1 = await this.helper.callRpc('api.rpc.chain.getBlock', [await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockNumber - 1])]);280    const findCreationDate = (block: any) => {281      const humanBlock = block.toHuman();282      let date;283      humanBlock.block.extrinsics.forEach((ext: any) => {284        if(ext.method.section === 'timestamp') {285          date = Number(ext.method.args.now.replaceAll(',', ''));286        }287      });288      return date;289    };290    const block1date = await findCreationDate(block1);291    const block2date = await findCreationDate(block2);292    if(block2date! - block1date! < 9000) return true;293  };294  295  async calculcateFee(payer: ICrossAccountId, promise: () => Promise<any>): Promise<bigint> {296    const address = payer.Substrate ? payer.Substrate : await this.helper.address.ethToSubstrate(payer.Ethereum!);297    let balance = await this.helper.balance.getSubstrate(address); 298    299    await promise();300    301    balance -= await this.helper.balance.getSubstrate(address);302    303    return balance;304  }305306  calculatePalletAddress(palletId: any) {307    const address = stringToU8a(('modl' + palletId).padEnd(32, '\0'));308    return encodeAddress(address);309  }310311  makeScheduledIds(num: number): string[] {312    function makeId(slider: number) {313      const scheduledIdSize = 64;314      const hexId = slider.toString(16);315      const prefixSize = scheduledIdSize - hexId.length;316317      const scheduledId = '0x' + '0'.repeat(prefixSize) + hexId;318319      return scheduledId;  320    }321322    const ids = [];323    for (let i = 0; i < num; i++) {324      ids.push(makeId(this.scheduledIdSlider));325      this.scheduledIdSlider += 1;326    }327328    return ids;329  }330331  makeScheduledId(): string {332    return (this.makeScheduledIds(1))[0];333  }334335  async captureEvents(eventSection: string, eventMethod: string): Promise<EventCapture> {336    const capture = new EventCapture(this.helper, eventSection, eventMethod);337    await capture.startCapture();338339    return capture;340  }341}342343class MoonbeamAccountGroup {344  helper: MoonbeamHelper;345346  keyring: Keyring;347  _alithAccount: IKeyringPair;348  _baltatharAccount: IKeyringPair;349  _dorothyAccount: IKeyringPair;350351  constructor(helper: MoonbeamHelper) {352    this.helper = helper;353354    this.keyring = new Keyring({type: 'ethereum'});355    const alithPrivateKey = '0x5fb92d6e98884f76de468fa3f6278f8807c48bebc13595d45af5bdc4da702133';356    const baltatharPrivateKey = '0x8075991ce870b93a8870eca0c0f91913d12f47948ca0fd25b49c6fa7cdbeee8b';357    const dorothyPrivateKey = '0x39539ab1876910bbf3a223d84a29e28f1cb4e2e456503e7e91ed39b2e7223d68';358359    this._alithAccount = this.keyring.addFromUri(alithPrivateKey, undefined, 'ethereum');360    this._baltatharAccount = this.keyring.addFromUri(baltatharPrivateKey, undefined, 'ethereum');361    this._dorothyAccount = this.keyring.addFromUri(dorothyPrivateKey, undefined, 'ethereum');362  }363364  alithAccount() {365    return this._alithAccount;366  }367368  baltatharAccount() {369    return this._baltatharAccount;370  }371372  dorothyAccount() {373    return this._dorothyAccount;374  }375376  create() {377    return this.keyring.addFromUri(mnemonicGenerate());378  }379}380381class WaitGroup {382  helper: ChainHelperBase;383384  constructor(helper: ChainHelperBase) {385    this.helper = helper;386  }387388  sleep(milliseconds: number) {389    return new Promise((resolve) => setTimeout(resolve, milliseconds));390  }391392  private async waitWithTimeout(promise: Promise<any>, timeout: number) {393    let isBlock = false;394    promise.then(() => isBlock = true).catch(() => isBlock = true);395    let totalTime = 0;396    const step = 100;397    while(!isBlock) {398      await this.sleep(step);399      totalTime += step;400      if(totalTime >= timeout) throw Error('Blocks production failed');401    }402    return promise;403  }404405  /**406   * Wait for specified number of blocks407   * @param blocksCount number of blocks to wait408   * @returns 409   */410  async newBlocks(blocksCount = 1, timeout?: number): Promise<void> {411    timeout = timeout ?? blocksCount * 60_000;412    // eslint-disable-next-line no-async-promise-executor413    const promise = new Promise<void>(async (resolve) => {414      const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads(() => {415        if (blocksCount > 0) {416          blocksCount--;417        } else {418          unsubscribe();419          resolve();420        }421      });422    });423    await this.waitWithTimeout(promise, timeout);424    return promise;425  }426427  async forParachainBlockNumber(blockNumber: bigint | number, timeout?: number) {428    timeout = timeout ?? 30 * 60 * 1000;429    // eslint-disable-next-line no-async-promise-executor430    const promise = new Promise<void>(async (resolve) => {431      const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads((data: any) => {432        if (data.number.toNumber() >= blockNumber) {433          unsubscribe();434          resolve();435        }436      });437    });438    await this.waitWithTimeout(promise, timeout);439    return promise;440  }441  442  async forRelayBlockNumber(blockNumber: bigint | number, timeout?: number) {443    timeout = timeout ?? 30 * 60 * 1000;444    // eslint-disable-next-line no-async-promise-executor445    const promise = new Promise<void>(async (resolve) => {446      const unsubscribe = await this.helper.getApi().query.parachainSystem.validationData((data: any) => {447        if (data.value.relayParentNumber.toNumber() >= blockNumber) {448          // @ts-ignore449          unsubscribe();450          resolve();451        }452      });453    });454    await this.waitWithTimeout(promise, timeout);455    return promise;456  }457458  noScheduledTasks() {459    const api = this.helper.getApi();460    461    // eslint-disable-next-line no-async-promise-executor462    const promise = new Promise<void>(async resolve => {463      const unsubscribe = await api.rpc.chain.subscribeNewHeads(async () => {464        const areThereScheduledTasks = await api.query.scheduler.lookup.entries();465466        if(areThereScheduledTasks.length == 0) {467          unsubscribe();468          resolve();469        }470      }); 471    });472473    return promise;474  }475476  event(maxBlocksToWait: number, eventSection: string, eventMethod: string) {477    // eslint-disable-next-line no-async-promise-executor478    const promise = new Promise<EventRecord | null>(async (resolve) => {479      const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads(async header => {480        const blockNumber = header.number.toHuman();481        const blockHash = header.hash;482        const eventIdStr = `${eventSection}.${eventMethod}`;483        const waitLimitStr = `wait blocks remaining: ${maxBlocksToWait}`;484  485        this.helper.logger.log(`[Block #${blockNumber}] Waiting for event \`${eventIdStr}\` (${waitLimitStr})`);486  487        const apiAt = await this.helper.getApi().at(blockHash);488        const eventRecords = (await apiAt.query.system.events()) as any;489  490        const neededEvent = eventRecords.toArray().find((r: FrameSystemEventRecord) => {491          return r.event.section == eventSection && r.event.method == eventMethod;492        });493  494        if (neededEvent) {495          unsubscribe();496          resolve(neededEvent);497        } else if (maxBlocksToWait > 0) {498          maxBlocksToWait--;499        } else {500          this.helper.logger.log(`Event \`${eventIdStr}\` is NOT found`);501          unsubscribe();502          resolve(null);503        }504      });505    });506    return promise;507  }508}509510class TestUtilGroup {511  helper: DevUniqueHelper;512513  constructor(helper: DevUniqueHelper) {514    this.helper = helper;515  }516517  async enable() {518    if (this.helper.fetchMissingPalletNames([Pallets.TestUtils]).length != 0) {519      return;520    }521522    const signer = this.helper.util.fromSeed('//Alice');523    await this.helper.getSudo<DevUniqueHelper>().executeExtrinsic(signer, 'api.tx.testUtils.enable', [], true);524  }525526  async setTestValue(signer: TSigner, testVal: number) {527    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.setTestValue', [testVal], true);528  }529530  async incTestValue(signer: TSigner) {531    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.incTestValue', [], true);532  }533534  async setTestValueAndRollback(signer: TSigner, testVal: number) {535    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.setTestValueAndRollback', [testVal], true);536  }537538  async testValue(blockIdx?: number) {539    const api = blockIdx540      ? await this.helper.getApi().at(await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockIdx]))541      : this.helper.getApi();542543    return (await api.query.testUtils.testValue()).toJSON();544  }545546  async justTakeFee(signer: TSigner) {547    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.justTakeFee', [], true);548  }549550  async selfCancelingInc(signer: TSigner, scheduledId: string, maxTestVal: number) {551    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.selfCancelingInc', [scheduledId, maxTestVal], true);552  }553}554555class EventCapture {556  helper: DevUniqueHelper;557  eventSection: string;558  eventMethod: string;559  events: EventRecord[] = [];560  unsubscribe: VoidFn | null = null;561562  constructor(563    helper: DevUniqueHelper,564    eventSection: string,565    eventMethod: string,566  ) {567    this.helper = helper;568    this.eventSection = eventSection;569    this.eventMethod = eventMethod;570  }571572  async startCapture() {573    this.stopCapture();574    this.unsubscribe = (await this.helper.getApi().query.system.events((eventRecords: FrameSystemEventRecord[]) => {575      const newEvents = eventRecords.filter(r => {576        return r.event.section == this.eventSection && r.event.method == this.eventMethod;577      });578579      this.events.push(...newEvents);580    })) as any;581  }582583  stopCapture() {584    if (this.unsubscribe !== null) {585      this.unsubscribe();586    }587  }588589  extractCapturedEvents() {590    return this.events;591  }592}593594class AdminGroup {595  helper: UniqueHelper;596597  constructor(helper: UniqueHelper) {598    this.helper = helper;599  }600601  async payoutStakers(signer: IKeyringPair, stakersToPayout: number) {602    const payoutResult = await this.helper.executeExtrinsic(signer, 'api.tx.appPromotion.payoutStakers', [stakersToPayout], true);603    return payoutResult.result.events.filter(e => e.event.method === 'StakingRecalculation').map(e => {604      return {605        staker: e.event.data[0].toString(),606        stake: e.event.data[1].toBigInt(),607        payout: e.event.data[2].toBigInt(),608      };609    });610  }611}