difftreelog
fix flaky tests: account generation with nonce
in: master
ignore errors wait for balances
1 file changed
tests/src/util/playgrounds/unique.dev.tsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// SPDX-License-Identifier: Apache-2.034import {mnemonicGenerate} from '@polkadot/util-crypto';5import {UniqueHelper} from './unique';6import {ApiPromise, WsProvider} from '@polkadot/api';7import * as defs from '../../interfaces/definitions';8import { TSigner } from './types';91011export class DevUniqueHelper extends UniqueHelper {12 /**13 * Arrange methods for tests14 */15 arrange: ArrangeGroup;1617 constructor(logger: { log: (msg: any, level: any) => void, level: any }) {18 super(logger);19 this.arrange = new ArrangeGroup(this);20 }2122 async connect(wsEndpoint: string, listeners?: any): Promise<void> {23 const wsProvider = new WsProvider(wsEndpoint);24 this.api = new ApiPromise({25 provider: wsProvider,26 signedExtensions: {27 ContractHelpers: {28 extrinsic: {},29 payload: {},30 },31 FakeTransactionFinalizer: {32 extrinsic: {},33 payload: {},34 },35 },36 rpc: {37 unique: defs.unique.rpc,38 rmrk: defs.rmrk.rpc,39 eth: {40 feeHistory: {41 description: 'Dummy',42 params: [],43 type: 'u8',44 },45 maxPriorityFeePerGas: {46 description: 'Dummy',47 params: [],48 type: 'u8',49 },50 },51 },52 });53 await this.api.isReadyOrError;54 this.network = await UniqueHelper.detectNetwork(this.api);55 }56}5758class ArrangeGroup {59 helper: UniqueHelper;6061 constructor(helper: UniqueHelper) {62 this.helper = helper;63 }6465 /**66 * Generates accounts with the specified UNQ token balance 67 * @param balances balances for generated accounts. Each balance will be multiplied by the token nominal.68 * @param donor donor account for balances69 * @returns array of newly created accounts70 * @example const [acc1, acc2, acc3] = await createAccounts([0n, 10n, 20n], donor); 71 */72 creteAccounts = async (balances: bigint[], donor: TSigner): Promise<TSigner[]> => {73 let nonce = await this.helper.chain.getNonce(donor.address);74 const tokenNominal = this.helper.balance.getOneTokenNominal();75 const transactions = [];76 const accounts = [];77 for (const balance of balances) {78 const recepient = this.helper.util.fromSeed(mnemonicGenerate());79 accounts.push(recepient);80 if (balance !== 0n) {81 const tx = this.helper.constructApiCall('api.tx.balances.transfer', [{Id: recepient.address}, balance * tokenNominal]);82 transactions.push(this.helper.signTransaction(donor, tx, 'account generation', {nonce}));83 nonce++;84 }85 }8687 await Promise.all(transactions);88 return accounts;89 };90}1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// SPDX-License-Identifier: Apache-2.034import {mnemonicGenerate} from '@polkadot/util-crypto';5import {UniqueHelper} from './unique';6import {ApiPromise, WsProvider} from '@polkadot/api';7import * as defs from '../../interfaces/definitions';8import {TSigner} from './types';9import {IKeyringPair} from '@polkadot/types/types';101112export class DevUniqueHelper extends UniqueHelper {13 /**14 * Arrange methods for tests15 */16 arrange: ArrangeGroup;1718 constructor(logger: { log: (msg: any, level: any) => void, level: any }) {19 super(logger);20 this.arrange = new ArrangeGroup(this);21 }2223 async connect(wsEndpoint: string, listeners?: any): Promise<void> {24 const wsProvider = new WsProvider(wsEndpoint);25 this.api = new ApiPromise({26 provider: wsProvider,27 signedExtensions: {28 ContractHelpers: {29 extrinsic: {},30 payload: {},31 },32 FakeTransactionFinalizer: {33 extrinsic: {},34 payload: {},35 },36 },37 rpc: {38 unique: defs.unique.rpc,39 rmrk: defs.rmrk.rpc,40 eth: {41 feeHistory: {42 description: 'Dummy',43 params: [],44 type: 'u8',45 },46 maxPriorityFeePerGas: {47 description: 'Dummy',48 params: [],49 type: 'u8',50 },51 },52 },53 });54 await this.api.isReadyOrError;55 this.network = await UniqueHelper.detectNetwork(this.api);56 }57}5859class ArrangeGroup {60 helper: UniqueHelper;6162 constructor(helper: UniqueHelper) {63 this.helper = helper;64 }6566 /**67 * Generates accounts with the specified UNQ token balance 68 * @param balances balances for generated accounts. Each balance will be multiplied by the token nominal.69 * @param donor donor account for balances70 * @returns array of newly created accounts71 * @example const [acc1, acc2, acc3] = await createAccounts([0n, 10n, 20n], donor); 72 */73 creteAccounts = async (balances: bigint[], donor: TSigner): Promise<TSigner[]> => {74 let nonce = await this.helper.chain.getNonce(donor.address);75 const tokenNominal = this.helper.balance.getOneTokenNominal();76 const transactions = [];77 const accounts: IKeyringPair[] = [];78 for (const balance of balances) {79 const recepient = this.helper.util.fromSeed(mnemonicGenerate());80 accounts.push(recepient);81 if (balance !== 0n) {82 const tx = this.helper.constructApiCall('api.tx.balances.transfer', [{Id: recepient.address}, balance * tokenNominal]);83 transactions.push(this.helper.signTransaction(donor, tx, 'account generation', {nonce}));84 nonce++;85 }86 }8788 await Promise.all(transactions).catch(e => {});89 90 //#region TODO remove this region, when nonce problem will be solved91 const checkBalances = async () => {92 let isSuccess = true;93 for (let i = 0; i < balances.length; i++) {94 const balance = await this.helper.balance.getSubstrate(accounts[i].address);95 if (balance !== balances[i] * tokenNominal) {96 isSuccess = false;97 break;98 }99 }100 return isSuccess;101 };102103 let accountsCreated = false;104 // waiting up to 1 minute with .25 sec retry105 for (let index = 0; index < 240; index++) {106 accountsCreated = await checkBalances();107 if(accountsCreated) break;108 await new Promise(resolve => setTimeout(resolve, 250));109 }110111 if (!accountsCreated) throw Error('Accounts generation failed');112 //#endregion113114 return accounts;115 };116}