git.delta.rocks / unique-network / refs/commits / 2d180a9c85ae

difftreelog

source

tests/src/util/playgrounds/unique.dev.ts19.8 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 {122  wait: WaitGroup;123124  constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {125    options.helperBase = options.helperBase ?? DevRelayHelper;126127    super(logger, options);128    this.wait = new WaitGroup(this);129  }130}131132export class DevWestmintHelper extends WestmintHelper {133  wait: WaitGroup;134135  constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {136    options.helperBase = options.helperBase ?? DevWestmintHelper;137138    super(logger, options);139    this.wait = new WaitGroup(this);140  }141}142143export class DevStatemineHelper extends DevWestmintHelper {}144145export class DevStatemintHelper extends DevWestmintHelper {}146147export class DevMoonbeamHelper extends MoonbeamHelper {148  account: MoonbeamAccountGroup;149  wait: WaitGroup;150151  constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {152    options.helperBase = options.helperBase ?? DevMoonbeamHelper;153154    super(logger, options);155    this.account = new MoonbeamAccountGroup(this);156    this.wait = new WaitGroup(this);157  }158}159160export class DevMoonriverHelper extends DevMoonbeamHelper {}161162export class DevAcalaHelper extends AcalaHelper {163  wait: WaitGroup;164165  constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {166    options.helperBase = options.helperBase ?? DevAcalaHelper;167168    super(logger, options);169    this.wait = new WaitGroup(this);170  }171}172173export class DevKaruraHelper extends DevAcalaHelper {}174175class ArrangeGroup {176  helper: DevUniqueHelper;177178  scheduledIdSlider = 0;179180  constructor(helper: DevUniqueHelper) {181    this.helper = helper;182  }183184  /**185   * Generates accounts with the specified UNQ token balance 186   * @param balances balances for generated accounts. Each balance will be multiplied by the token nominal.187   * @param donor donor account for balances188   * @returns array of newly created accounts189   * @example const [acc1, acc2, acc3] = await createAccounts([0n, 10n, 20n], donor); 190   */191  createAccounts = async (balances: bigint[], donor: IKeyringPair): Promise<IKeyringPair[]> => {192    let nonce = await this.helper.chain.getNonce(donor.address);193    const wait = new WaitGroup(this.helper);194    const ss58Format = this.helper.chain.getChainProperties().ss58Format;195    const tokenNominal = this.helper.balance.getOneTokenNominal();196    const transactions = [];197    const accounts: IKeyringPair[] = [];198    for (const balance of balances) {199      const recipient = this.helper.util.fromSeed(mnemonicGenerate(), ss58Format);200      accounts.push(recipient);201      if (balance !== 0n) {202        const tx = this.helper.constructApiCall('api.tx.balances.transfer', [{Id: recipient.address}, balance * tokenNominal]);203        transactions.push(this.helper.signTransaction(donor, tx, {nonce}, 'account generation'));204        nonce++;205      }206    }207208    await Promise.all(transactions).catch(_e => {});209    210    //#region TODO remove this region, when nonce problem will be solved211    const checkBalances = async () => {212      let isSuccess = true;213      for (let i = 0; i < balances.length; i++) {214        const balance = await this.helper.balance.getSubstrate(accounts[i].address);215        if (balance !== balances[i] * tokenNominal) {216          isSuccess = false;217          break;218        }219      }220      return isSuccess;221    };222223    let accountsCreated = false;224    const maxBlocksChecked = await this.helper.arrange.isDevNode() ? 50 : 5;225    // checkBalances retry up to 5-50 blocks226    for (let index = 0; index < maxBlocksChecked; index++) {227      accountsCreated = await checkBalances();228      if(accountsCreated) break;229      await wait.newBlocks(1);230    }231232    if (!accountsCreated) throw Error('Accounts generation failed');233    //#endregion234235    return accounts;236  };237238  // TODO combine this method and createAccounts into one239  createCrowd = async (accountsToCreate: number, withBalance: bigint, donor: IKeyringPair): Promise<IKeyringPair[]> => {  240    const createAsManyAsCan = async () => {241      let transactions: any = [];242      const accounts: IKeyringPair[] = [];243      let nonce = await this.helper.chain.getNonce(donor.address);244      const tokenNominal = this.helper.balance.getOneTokenNominal();245      for (let i = 0; i < accountsToCreate; i++) {246        if (i === 500) { // if there are too many accounts to create247          await Promise.allSettled(transactions); // wait while first 500 (should be 100 for devnode) tx will be settled 248          transactions = []; //249          nonce = await this.helper.chain.getNonce(donor.address); // update nonce 250        }251        const recepient = this.helper.util.fromSeed(mnemonicGenerate());252        accounts.push(recepient);253        if (withBalance !== 0n) {254          const tx = this.helper.constructApiCall('api.tx.balances.transfer', [{Id: recepient.address}, withBalance * tokenNominal]);255          transactions.push(this.helper.signTransaction(donor, tx, {nonce}, 'account generation'));256          nonce++;257        }258      }259      260      const fullfilledAccounts = [];261      await Promise.allSettled(transactions);262      for (const account of accounts) {263        const accountBalance = await this.helper.balance.getSubstrate(account.address);264        if (accountBalance === withBalance * tokenNominal) {265          fullfilledAccounts.push(account);266        }267      }268      return fullfilledAccounts;269    };270271    272    const crowd: IKeyringPair[] = [];273    // do up to 5 retries274    for (let index = 0; index < 5 && accountsToCreate !== 0; index++) {275      const asManyAsCan = await createAsManyAsCan();276      crowd.push(...asManyAsCan);277      accountsToCreate -= asManyAsCan.length;278    }279280    if (accountsToCreate !== 0) throw Error(`Crowd generation failed: ${accountsToCreate} accounts left`);281282    return crowd;283  };284285  isDevNode = async () => {286    let blockNumber = (await this.helper.callRpc('api.query.system.number')).toJSON();287    if (blockNumber == 0) {288      await this.helper.wait.newBlocks(1); 289      blockNumber = (await this.helper.callRpc('api.query.system.number')).toJSON();290    }291    const block2 = await this.helper.callRpc('api.rpc.chain.getBlock', [await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockNumber])]);292    const block1 = await this.helper.callRpc('api.rpc.chain.getBlock', [await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockNumber - 1])]);293    const findCreationDate = (block: any) => {294      const humanBlock = block.toHuman();295      let date;296      humanBlock.block.extrinsics.forEach((ext: any) => {297        if(ext.method.section === 'timestamp') {298          date = Number(ext.method.args.now.replaceAll(',', ''));299        }300      });301      return date;302    };303    const block1date = await findCreationDate(block1);304    const block2date = await findCreationDate(block2);305    if(block2date! - block1date! < 9000) return true;306  };307  308  async calculcateFee(payer: ICrossAccountId, promise: () => Promise<any>): Promise<bigint> {309    const address = payer.Substrate ? payer.Substrate : await this.helper.address.ethToSubstrate(payer.Ethereum!);310    let balance = await this.helper.balance.getSubstrate(address); 311    312    await promise();313    314    balance -= await this.helper.balance.getSubstrate(address);315    316    return balance;317  }318319  calculatePalletAddress(palletId: any) {320    const address = stringToU8a(('modl' + palletId).padEnd(32, '\0'));321    return encodeAddress(address, this.helper.chain.getChainProperties().ss58Format);322  }323324  makeScheduledIds(num: number): string[] {325    function makeId(slider: number) {326      const scheduledIdSize = 64;327      const hexId = slider.toString(16);328      const prefixSize = scheduledIdSize - hexId.length;329330      const scheduledId = '0x' + '0'.repeat(prefixSize) + hexId;331332      return scheduledId;  333    }334335    const ids = [];336    for (let i = 0; i < num; i++) {337      ids.push(makeId(this.scheduledIdSlider));338      this.scheduledIdSlider += 1;339    }340341    return ids;342  }343344  makeScheduledId(): string {345    return (this.makeScheduledIds(1))[0];346  }347348  async captureEvents(eventSection: string, eventMethod: string): Promise<EventCapture> {349    const capture = new EventCapture(this.helper, eventSection, eventMethod);350    await capture.startCapture();351352    return capture;353  }354}355356class MoonbeamAccountGroup {357  helper: MoonbeamHelper;358359  keyring: Keyring;360  _alithAccount: IKeyringPair;361  _baltatharAccount: IKeyringPair;362  _dorothyAccount: IKeyringPair;363364  constructor(helper: MoonbeamHelper) {365    this.helper = helper;366367    this.keyring = new Keyring({type: 'ethereum'});368    const alithPrivateKey = '0x5fb92d6e98884f76de468fa3f6278f8807c48bebc13595d45af5bdc4da702133';369    const baltatharPrivateKey = '0x8075991ce870b93a8870eca0c0f91913d12f47948ca0fd25b49c6fa7cdbeee8b';370    const dorothyPrivateKey = '0x39539ab1876910bbf3a223d84a29e28f1cb4e2e456503e7e91ed39b2e7223d68';371372    this._alithAccount = this.keyring.addFromUri(alithPrivateKey, undefined, 'ethereum');373    this._baltatharAccount = this.keyring.addFromUri(baltatharPrivateKey, undefined, 'ethereum');374    this._dorothyAccount = this.keyring.addFromUri(dorothyPrivateKey, undefined, 'ethereum');375  }376377  alithAccount() {378    return this._alithAccount;379  }380381  baltatharAccount() {382    return this._baltatharAccount;383  }384385  dorothyAccount() {386    return this._dorothyAccount;387  }388389  create() {390    return this.keyring.addFromUri(mnemonicGenerate());391  }392}393394class WaitGroup {395  helper: ChainHelperBase;396397  constructor(helper: ChainHelperBase) {398    this.helper = helper;399  }400401  sleep(milliseconds: number) {402    return new Promise((resolve) => setTimeout(resolve, milliseconds));403  }404405  private async waitWithTimeout(promise: Promise<any>, timeout: number) {406    let isBlock = false;407    promise.then(() => isBlock = true).catch(() => isBlock = true);408    let totalTime = 0;409    const step = 100;410    while(!isBlock) {411      await this.sleep(step);412      totalTime += step;413      if(totalTime >= timeout) throw Error('Blocks production failed');414    }415    return promise;416  }417418  /**419   * Wait for specified number of blocks420   * @param blocksCount number of blocks to wait421   * @returns 422   */423  async newBlocks(blocksCount = 1, timeout?: number): Promise<void> {424    timeout = timeout ?? blocksCount * 60_000;425    // eslint-disable-next-line no-async-promise-executor426    const promise = new Promise<void>(async (resolve) => {427      const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads(() => {428        if (blocksCount > 0) {429          blocksCount--;430        } else {431          unsubscribe();432          resolve();433        }434      });435    });436    await this.waitWithTimeout(promise, timeout);437    return promise;438  }439440  async forParachainBlockNumber(blockNumber: bigint | number, timeout?: number) {441    timeout = timeout ?? 30 * 60 * 1000;442    // eslint-disable-next-line no-async-promise-executor443    const promise = new Promise<void>(async (resolve) => {444      const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads((data: any) => {445        if (data.number.toNumber() >= blockNumber) {446          unsubscribe();447          resolve();448        }449      });450    });451    await this.waitWithTimeout(promise, timeout);452    return promise;453  }454  455  async forRelayBlockNumber(blockNumber: bigint | number, timeout?: number) {456    timeout = timeout ?? 30 * 60 * 1000;457    // eslint-disable-next-line no-async-promise-executor458    const promise = new Promise<void>(async (resolve) => {459      const unsubscribe = await this.helper.getApi().query.parachainSystem.validationData((data: any) => {460        if (data.value.relayParentNumber.toNumber() >= blockNumber) {461          // @ts-ignore462          unsubscribe();463          resolve();464        }465      });466    });467    await this.waitWithTimeout(promise, timeout);468    return promise;469  }470471  noScheduledTasks() {472    const api = this.helper.getApi();473    474    // eslint-disable-next-line no-async-promise-executor475    const promise = new Promise<void>(async resolve => {476      const unsubscribe = await api.rpc.chain.subscribeNewHeads(async () => {477        const areThereScheduledTasks = await api.query.scheduler.lookup.entries();478479        if(areThereScheduledTasks.length == 0) {480          unsubscribe();481          resolve();482        }483      }); 484    });485486    return promise;487  }488489  event(maxBlocksToWait: number, eventSection: string, eventMethod: string) {490    // eslint-disable-next-line no-async-promise-executor491    const promise = new Promise<EventRecord | null>(async (resolve) => {492      const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads(async header => {493        const blockNumber = header.number.toHuman();494        const blockHash = header.hash;495        const eventIdStr = `${eventSection}.${eventMethod}`;496        const waitLimitStr = `wait blocks remaining: ${maxBlocksToWait}`;497  498        this.helper.logger.log(`[Block #${blockNumber}] Waiting for event \`${eventIdStr}\` (${waitLimitStr})`);499  500        const apiAt = await this.helper.getApi().at(blockHash);501        const eventRecords = (await apiAt.query.system.events()) as any;502  503        const neededEvent = eventRecords.toArray().find((r: FrameSystemEventRecord) => {504          return r.event.section == eventSection && r.event.method == eventMethod;505        });506  507        if (neededEvent) {508          unsubscribe();509          resolve(neededEvent);510        } else if (maxBlocksToWait > 0) {511          maxBlocksToWait--;512        } else {513          this.helper.logger.log(`Event \`${eventIdStr}\` is NOT found`);514          unsubscribe();515          resolve(null);516        }517      });518    });519    return promise;520  }521}522523class TestUtilGroup {524  helper: DevUniqueHelper;525526  constructor(helper: DevUniqueHelper) {527    this.helper = helper;528  }529530  async enable() {531    if (this.helper.fetchMissingPalletNames([Pallets.TestUtils]).length != 0) {532      return;533    }534535    const signer = this.helper.util.fromSeed('//Alice');536    await this.helper.getSudo<DevUniqueHelper>().executeExtrinsic(signer, 'api.tx.testUtils.enable', [], true);537  }538539  async setTestValue(signer: TSigner, testVal: number) {540    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.setTestValue', [testVal], true);541  }542543  async incTestValue(signer: TSigner) {544    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.incTestValue', [], true);545  }546547  async setTestValueAndRollback(signer: TSigner, testVal: number) {548    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.setTestValueAndRollback', [testVal], true);549  }550551  async testValue(blockIdx?: number) {552    const api = blockIdx553      ? await this.helper.getApi().at(await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockIdx]))554      : this.helper.getApi();555556    return (await api.query.testUtils.testValue()).toJSON();557  }558559  async justTakeFee(signer: TSigner) {560    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.justTakeFee', [], true);561  }562563  async selfCancelingInc(signer: TSigner, scheduledId: string, maxTestVal: number) {564    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.selfCancelingInc', [scheduledId, maxTestVal], true);565  }566}567568class EventCapture {569  helper: DevUniqueHelper;570  eventSection: string;571  eventMethod: string;572  events: EventRecord[] = [];573  unsubscribe: VoidFn | null = null;574575  constructor(576    helper: DevUniqueHelper,577    eventSection: string,578    eventMethod: string,579  ) {580    this.helper = helper;581    this.eventSection = eventSection;582    this.eventMethod = eventMethod;583  }584585  async startCapture() {586    this.stopCapture();587    this.unsubscribe = (await this.helper.getApi().query.system.events((eventRecords: FrameSystemEventRecord[]) => {588      const newEvents = eventRecords.filter(r => {589        return r.event.section == this.eventSection && r.event.method == this.eventMethod;590      });591592      this.events.push(...newEvents);593    })) as any;594  }595596  stopCapture() {597    if (this.unsubscribe !== null) {598      this.unsubscribe();599    }600  }601602  extractCapturedEvents() {603    return this.events;604  }605}606607class AdminGroup {608  helper: UniqueHelper;609610  constructor(helper: UniqueHelper) {611    this.helper = helper;612  }613614  async payoutStakers(signer: IKeyringPair, stakersToPayout: number) {615    const payoutResult = await this.helper.executeExtrinsic(signer, 'api.tx.appPromotion.payoutStakers', [stakersToPayout], true);616    return payoutResult.result.events.filter(e => e.event.method === 'StakingRecalculation').map(e => {617      return {618        staker: e.event.data[0].toString(),619        stake: e.event.data[1].toBigInt(),620        payout: e.event.data[2].toBigInt(),621      };622    });623  }624}