1234import {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 27 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 636465 arrange: ArrangeGroup;66 wait: WaitGroup;67 admin: AdminGroup;68 session: SessionGroup;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 this.session = new SessionGroup(this);80 }8182 async connect(wsEndpoint: string, _listeners?: any): Promise<void> {83 const wsProvider = new WsProvider(wsEndpoint);84 this.api = new ApiPromise({85 provider: wsProvider,86 signedExtensions: {87 ContractHelpers: {88 extrinsic: {},89 payload: {},90 },91 CheckMaintenance: {92 extrinsic: {},93 payload: {},94 },95 FakeTransactionFinalizer: {96 extrinsic: {},97 payload: {},98 },99 },100 rpc: {101 unique: defs.unique.rpc,102 appPromotion: defs.appPromotion.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 {}124125export class DevWestmintHelper extends WestmintHelper {126 wait: WaitGroup;127128 constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {129 options.helperBase = options.helperBase ?? DevWestmintHelper;130131 super(logger, options);132 this.wait = new WaitGroup(this);133 }134}135136export class DevMoonbeamHelper extends MoonbeamHelper {137 account: MoonbeamAccountGroup;138 wait: WaitGroup;139140 constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {141 options.helperBase = options.helperBase ?? DevMoonbeamHelper;142143 super(logger, options);144 this.account = new MoonbeamAccountGroup(this);145 this.wait = new WaitGroup(this);146 }147}148149export class DevMoonriverHelper extends DevMoonbeamHelper {}150151export class DevAcalaHelper extends AcalaHelper {152 wait: WaitGroup;153154 constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {155 options.helperBase = options.helperBase ?? DevAcalaHelper;156157 super(logger, options);158 this.wait = new WaitGroup(this);159 }160}161162export class DevKaruraHelper extends DevAcalaHelper {}163164class ArrangeGroup {165 helper: DevUniqueHelper;166167 scheduledIdSlider = 0;168169 constructor(helper: DevUniqueHelper) {170 this.helper = helper;171 }172173 174175176177178179180 createAccounts = async (balances: bigint[], donor: IKeyringPair): Promise<IKeyringPair[]> => {181 let nonce = await this.helper.chain.getNonce(donor.address);182 const wait = new WaitGroup(this.helper);183 const ss58Format = this.helper.chain.getChainProperties().ss58Format;184 const tokenNominal = this.helper.balance.getOneTokenNominal();185 const transactions = [];186 const accounts: IKeyringPair[] = [];187 for (const balance of balances) {188 const recipient = this.helper.util.fromSeed(mnemonicGenerate(), ss58Format);189 accounts.push(recipient);190 if (balance !== 0n) {191 const tx = this.helper.constructApiCall('api.tx.balances.transfer', [{Id: recipient.address}, balance * tokenNominal]);192 transactions.push(this.helper.signTransaction(donor, tx, {nonce}, 'account generation'));193 nonce++;194 }195 }196197 await Promise.all(transactions).catch(_e => {});198 199 200 const checkBalances = async () => {201 let isSuccess = true;202 for (let i = 0; i < balances.length; i++) {203 const balance = await this.helper.balance.getSubstrate(accounts[i].address);204 if (balance !== balances[i] * tokenNominal) {205 isSuccess = false;206 break;207 }208 }209 return isSuccess;210 };211212 let accountsCreated = false;213 const maxBlocksChecked = await this.helper.arrange.isDevNode() ? 50 : 5;214 215 for (let index = 0; index < maxBlocksChecked; index++) {216 accountsCreated = await checkBalances();217 if(accountsCreated) break;218 await wait.newBlocks(1);219 }220221 if (!accountsCreated) throw Error('Accounts generation failed');222 223224 return accounts;225 };226227 228 createCrowd = async (accountsToCreate: number, withBalance: bigint, donor: IKeyringPair): Promise<IKeyringPair[]> => { 229 const createAsManyAsCan = async () => {230 let transactions: any = [];231 const accounts: IKeyringPair[] = [];232 let nonce = await this.helper.chain.getNonce(donor.address);233 const tokenNominal = this.helper.balance.getOneTokenNominal();234 for (let i = 0; i < accountsToCreate; i++) {235 if (i === 500) { 236 await Promise.allSettled(transactions); 237 transactions = []; 238 nonce = await this.helper.chain.getNonce(donor.address); 239 }240 const recepient = this.helper.util.fromSeed(mnemonicGenerate());241 accounts.push(recepient);242 if (withBalance !== 0n) {243 const tx = this.helper.constructApiCall('api.tx.balances.transfer', [{Id: recepient.address}, withBalance * tokenNominal]);244 transactions.push(this.helper.signTransaction(donor, tx, {nonce}, 'account generation'));245 nonce++;246 }247 }248 249 const fullfilledAccounts = [];250 await Promise.allSettled(transactions);251 for (const account of accounts) {252 const accountBalance = await this.helper.balance.getSubstrate(account.address);253 if (accountBalance === withBalance * tokenNominal) {254 fullfilledAccounts.push(account);255 }256 }257 return fullfilledAccounts;258 };259260 261 const crowd: IKeyringPair[] = [];262 263 for (let index = 0; index < 5 && accountsToCreate !== 0; index++) {264 const asManyAsCan = await createAsManyAsCan();265 crowd.push(...asManyAsCan);266 accountsToCreate -= asManyAsCan.length;267 }268269 if (accountsToCreate !== 0) throw Error(`Crowd generation failed: ${accountsToCreate} accounts left`);270271 return crowd;272 };273274 isDevNode = async () => {275 let blockNumber = (await this.helper.callRpc('api.query.system.number')).toJSON();276 if (blockNumber == 0) {277 await this.helper.wait.newBlocks(1); 278 blockNumber = (await this.helper.callRpc('api.query.system.number')).toJSON();279 }280 const block2 = await this.helper.callRpc('api.rpc.chain.getBlock', [await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockNumber])]);281 const block1 = await this.helper.callRpc('api.rpc.chain.getBlock', [await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockNumber - 1])]);282 const findCreationDate = (block: any) => {283 const humanBlock = block.toHuman();284 let date;285 humanBlock.block.extrinsics.forEach((ext: any) => {286 if(ext.method.section === 'timestamp') {287 date = Number(ext.method.args.now.replaceAll(',', ''));288 }289 });290 return date;291 };292 const block1date = await findCreationDate(block1);293 const block2date = await findCreationDate(block2);294 if(block2date! - block1date! < 9000) return true;295 };296 297 async calculcateFee(payer: ICrossAccountId, promise: () => Promise<any>): Promise<bigint> {298 const address = payer.Substrate ? payer.Substrate : await this.helper.address.ethToSubstrate(payer.Ethereum!);299 let balance = await this.helper.balance.getSubstrate(address); 300 301 await promise();302 303 balance -= await this.helper.balance.getSubstrate(address);304 305 return balance;306 }307308 calculatePalletAddress(palletId: any) {309 const address = stringToU8a(('modl' + palletId).padEnd(32, '\0'));310 return encodeAddress(address, this.helper.chain.getChainProperties().ss58Format);311 }312313 makeScheduledIds(num: number): string[] {314 function makeId(slider: number) {315 const scheduledIdSize = 64;316 const hexId = slider.toString(16);317 const prefixSize = scheduledIdSize - hexId.length;318319 const scheduledId = '0x' + '0'.repeat(prefixSize) + hexId;320321 return scheduledId; 322 }323324 const ids = [];325 for (let i = 0; i < num; i++) {326 ids.push(makeId(this.scheduledIdSlider));327 this.scheduledIdSlider += 1;328 }329330 return ids;331 }332333 makeScheduledId(): string {334 return (this.makeScheduledIds(1))[0];335 }336337 async captureEvents(eventSection: string, eventMethod: string): Promise<EventCapture> {338 const capture = new EventCapture(this.helper, eventSection, eventMethod);339 await capture.startCapture();340341 return capture;342 }343}344345class MoonbeamAccountGroup {346 helper: MoonbeamHelper;347348 keyring: Keyring;349 _alithAccount: IKeyringPair;350 _baltatharAccount: IKeyringPair;351 _dorothyAccount: IKeyringPair;352353 constructor(helper: MoonbeamHelper) {354 this.helper = helper;355356 this.keyring = new Keyring({type: 'ethereum'});357 const alithPrivateKey = '0x5fb92d6e98884f76de468fa3f6278f8807c48bebc13595d45af5bdc4da702133';358 const baltatharPrivateKey = '0x8075991ce870b93a8870eca0c0f91913d12f47948ca0fd25b49c6fa7cdbeee8b';359 const dorothyPrivateKey = '0x39539ab1876910bbf3a223d84a29e28f1cb4e2e456503e7e91ed39b2e7223d68';360361 this._alithAccount = this.keyring.addFromUri(alithPrivateKey, undefined, 'ethereum');362 this._baltatharAccount = this.keyring.addFromUri(baltatharPrivateKey, undefined, 'ethereum');363 this._dorothyAccount = this.keyring.addFromUri(dorothyPrivateKey, undefined, 'ethereum');364 }365366 alithAccount() {367 return this._alithAccount;368 }369370 baltatharAccount() {371 return this._baltatharAccount;372 }373374 dorothyAccount() {375 return this._dorothyAccount;376 }377378 create() {379 return this.keyring.addFromUri(mnemonicGenerate());380 }381}382383class WaitGroup {384 helper: ChainHelperBase;385386 constructor(helper: ChainHelperBase) {387 this.helper = helper;388 }389390 sleep(milliseconds: number) {391 return new Promise((resolve) => setTimeout(resolve, milliseconds));392 }393394 private async waitWithTimeout(promise: Promise<any>, timeout: number) {395 let isBlock = false;396 promise.then(() => isBlock = true).catch(() => isBlock = true);397 let totalTime = 0;398 const step = 100;399 while(!isBlock) {400 await this.sleep(step);401 totalTime += step;402 if(totalTime >= timeout) throw Error('Blocks production failed');403 }404 return promise;405 }406407 408409410411412413414 async withTimeout<T>(415 promise: Promise<T>,416 timeoutMS = 30000,417 timeoutError = 'The operation has timed out!',418 ): Promise<T> {419 const timeout = new Promise<never>((_, reject) => {420 setTimeout(() => {421 reject(new Error(timeoutError));422 }, timeoutMS);423 });424 425 return Promise.race<T>([promise, timeout]).catch(e => {throw new Error(e);});426 }427428 429430431432433 async newBlocks(blocksCount = 1, timeout?: number): Promise<void> {434 timeout = timeout ?? blocksCount * 60_000;435 436 const promise = new Promise<void>(async (resolve) => {437 const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads(() => {438 if (blocksCount > 0) {439 blocksCount--;440 } else {441 unsubscribe();442 resolve();443 }444 });445 });446 await this.waitWithTimeout(promise, timeout);447 return promise;448 }449450 451452453454455456457 async newSessions(sessionCount = 1, blockTimeout = 60000): Promise<void> {458 console.log(`Waiting for ${sessionCount} new session${sessionCount > 1 ? 's' : ''}.` 459 + ' This might take a while -- check SessionPeriod in pallet_session::Config for session time.');460461 const expectedSessionIndex = await (this.helper as DevUniqueHelper).session.getIndex() + sessionCount;462 let currentSessionIndex = -1;463464 while (currentSessionIndex < expectedSessionIndex) {465 466 currentSessionIndex = await this.withTimeout(new Promise(async (resolve) => {467 await this.newBlocks(1);468 const res = await (this.helper as DevUniqueHelper).session.getIndex();469 resolve(res);470 }), blockTimeout, 'The chain has stopped producing blocks!');471 }472 }473474 async forParachainBlockNumber(blockNumber: bigint | number, timeout?: number) {475 timeout = timeout ?? 30 * 60 * 1000;476 477 const promise = new Promise<void>(async (resolve) => {478 const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads((data: any) => {479 if (data.number.toNumber() >= blockNumber) {480 unsubscribe();481 resolve();482 }483 });484 });485 await this.waitWithTimeout(promise, timeout);486 return promise;487 }488 489 async forRelayBlockNumber(blockNumber: bigint | number, timeout?: number) {490 timeout = timeout ?? 30 * 60 * 1000;491 492 const promise = new Promise<void>(async (resolve) => {493 const unsubscribe = await this.helper.getApi().query.parachainSystem.validationData((data: any) => {494 if (data.value.relayParentNumber.toNumber() >= blockNumber) {495 496 unsubscribe();497 resolve();498 }499 });500 });501 await this.waitWithTimeout(promise, timeout);502 return promise;503 }504505 noScheduledTasks() {506 const api = this.helper.getApi();507 508 509 const promise = new Promise<void>(async resolve => {510 const unsubscribe = await api.rpc.chain.subscribeNewHeads(async () => {511 const areThereScheduledTasks = await api.query.scheduler.lookup.entries();512513 if(areThereScheduledTasks.length == 0) {514 unsubscribe();515 resolve();516 }517 }); 518 });519520 return promise;521 }522523 event(maxBlocksToWait: number, eventSection: string, eventMethod: string) {524 525 const promise = new Promise<EventRecord | null>(async (resolve) => {526 const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads(async header => {527 const blockNumber = header.number.toHuman();528 const blockHash = header.hash;529 const eventIdStr = `${eventSection}.${eventMethod}`;530 const waitLimitStr = `wait blocks remaining: ${maxBlocksToWait}`;531 532 this.helper.logger.log(`[Block #${blockNumber}] Waiting for event \`${eventIdStr}\` (${waitLimitStr})`);533 534 const apiAt = await this.helper.getApi().at(blockHash);535 const eventRecords = (await apiAt.query.system.events()) as any;536 537 const neededEvent = eventRecords.toArray().find((r: FrameSystemEventRecord) => {538 return r.event.section == eventSection && r.event.method == eventMethod;539 });540 541 if (neededEvent) {542 unsubscribe();543 resolve(neededEvent);544 } else if (maxBlocksToWait > 0) {545 maxBlocksToWait--;546 } else {547 this.helper.logger.log(`Event \`${eventIdStr}\` is NOT found`);548 unsubscribe();549 resolve(null);550 }551 });552 });553 return promise;554 }555}556557class SessionGroup {558 helper: ChainHelperBase;559560 constructor(helper: ChainHelperBase) {561 this.helper = helper;562 }563 564 565 async getIndex(): Promise<number> {566 return (await this.helper.callRpc('api.query.session.currentIndex')).toNumber();567 }568569 newSessions(sessionCount = 1, blockTimeout = 24000): Promise<void> {570 return (this.helper as DevUniqueHelper).wait.newSessions(sessionCount, blockTimeout);571 }572573 setOwnKeys(signer: TSigner, key: string) {574 return this.helper.executeExtrinsic(575 signer,576 'api.tx.session.setKeys', 577 [key, '0x0'],578 true,579 );580 }581582 setOwnKeysFromAddress(signer: TSigner) {583 return this.setOwnKeys(signer, '0x' + Buffer.from(signer.addressRaw).toString('hex'));584 }585}586587class TestUtilGroup {588 helper: DevUniqueHelper;589590 constructor(helper: DevUniqueHelper) {591 this.helper = helper;592 }593594 async enable() {595 if (this.helper.fetchMissingPalletNames([Pallets.TestUtils]).length != 0) {596 return;597 }598599 const signer = this.helper.util.fromSeed('//Alice');600 await this.helper.getSudo<DevUniqueHelper>().executeExtrinsic(signer, 'api.tx.testUtils.enable', [], true);601 }602603 async setTestValue(signer: TSigner, testVal: number) {604 await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.setTestValue', [testVal], true);605 }606607 async incTestValue(signer: TSigner) {608 await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.incTestValue', [], true);609 }610611 async setTestValueAndRollback(signer: TSigner, testVal: number) {612 await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.setTestValueAndRollback', [testVal], true);613 }614615 async testValue(blockIdx?: number) {616 const api = blockIdx617 ? await this.helper.getApi().at(await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockIdx]))618 : this.helper.getApi();619620 return (await api.query.testUtils.testValue()).toJSON();621 }622623 async justTakeFee(signer: TSigner) {624 await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.justTakeFee', [], true);625 }626627 async selfCancelingInc(signer: TSigner, scheduledId: string, maxTestVal: number) {628 await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.selfCancelingInc', [scheduledId, maxTestVal], true);629 }630}631632class EventCapture {633 helper: DevUniqueHelper;634 eventSection: string;635 eventMethod: string;636 events: EventRecord[] = [];637 unsubscribe: VoidFn | null = null;638639 constructor(640 helper: DevUniqueHelper,641 eventSection: string,642 eventMethod: string,643 ) {644 this.helper = helper;645 this.eventSection = eventSection;646 this.eventMethod = eventMethod;647 }648649 async startCapture() {650 this.stopCapture();651 this.unsubscribe = (await this.helper.getApi().query.system.events((eventRecords: FrameSystemEventRecord[]) => {652 const newEvents = eventRecords.filter(r => {653 return r.event.section == this.eventSection && r.event.method == this.eventMethod;654 });655656 this.events.push(...newEvents);657 })) as any;658 }659660 stopCapture() {661 if (this.unsubscribe !== null) {662 this.unsubscribe();663 }664 }665666 extractCapturedEvents() {667 return this.events;668 }669}670671class AdminGroup {672 helper: UniqueHelper;673674 constructor(helper: UniqueHelper) {675 this.helper = helper;676 }677678 async payoutStakers(signer: IKeyringPair, stakersToPayout: number) {679 const payoutResult = await this.helper.executeExtrinsic(signer, 'api.tx.appPromotion.payoutStakers', [stakersToPayout], true);680 return payoutResult.result.events.filter(e => e.event.method === 'StakingRecalculation').map(e => {681 return {682 staker: e.event.data[0].toString(),683 stake: e.event.data[1].toBigInt(),684 payout: e.event.data[2].toBigInt(),685 };686 });687 }688}