1234import {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 141516 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 67686970717273 createAccounts = async (balances: bigint[], donor: IKeyringPair): Promise<IKeyringPair[]> => {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 91 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 105 for (let index = 0; index < 5; index++) {106 accountsCreated = await checkBalances();107 if(accountsCreated) break;108 await this.waitNewBlocks(1);109 }110111 if (!accountsCreated) throw Error('Accounts generation failed');112 113114 return accounts;115 };116117 118119120121122 async waitNewBlocks(blocksCount = 1): Promise<void> {123 const promise = new Promise<void>(async (resolve) => {124 const unsubscribe = await this.helper.api!.rpc.chain.subscribeNewHeads(() => {125 if (blocksCount > 0) {126 blocksCount--;127 } else {128 unsubscribe();129 resolve();130 }131 });132 });133 return promise;134 }135}