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 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 FakeTransactionFinalizer: {90 extrinsic: {},91 payload: {},92 },93 },94 rpc: {95 unique: defs.unique.rpc,96 appPromotion: defs.appPromotion.rpc,97 rmrk: defs.rmrk.rpc,98 eth: {99 feeHistory: {100 description: 'Dummy',101 params: [],102 type: 'u8',103 },104 maxPriorityFeePerGas: {105 description: 'Dummy',106 params: [],107 type: 'u8',108 },109 },110 },111 });112 await this.api.isReadyOrError;113 this.network = await UniqueHelper.detectNetwork(this.api);114 }115}116117export class DevRelayHelper extends RelayHelper {}118119export class DevWestmintHelper extends WestmintHelper {120 wait: WaitGroup;121122 constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {123 options.helperBase = options.helperBase ?? DevWestmintHelper;124125 super(logger, options);126 this.wait = new WaitGroup(this);127 }128}129130export class DevMoonbeamHelper extends MoonbeamHelper {131 account: MoonbeamAccountGroup;132 wait: WaitGroup;133134 constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {135 options.helperBase = options.helperBase ?? DevMoonbeamHelper;136137 super(logger, options);138 this.account = new MoonbeamAccountGroup(this);139 this.wait = new WaitGroup(this);140 }141}142143export class DevMoonriverHelper extends DevMoonbeamHelper {}144145export class DevAcalaHelper extends AcalaHelper {146 wait: WaitGroup;147148 constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {149 options.helperBase = options.helperBase ?? DevAcalaHelper;150151 super(logger, options);152 this.wait = new WaitGroup(this);153 }154}155156export class DevKaruraHelper extends DevAcalaHelper {}157158class ArrangeGroup {159 helper: DevUniqueHelper;160161 scheduledIdSlider = 0;162163 constructor(helper: DevUniqueHelper) {164 this.helper = helper;165 }166167 168169170171172173174 createAccounts = async (balances: bigint[], donor: IKeyringPair): Promise<IKeyringPair[]> => {175 let nonce = await this.helper.chain.getNonce(donor.address);176 const wait = new WaitGroup(this.helper);177 const ss58Format = this.helper.chain.getChainProperties().ss58Format;178 const tokenNominal = this.helper.balance.getOneTokenNominal();179 const transactions = [];180 const accounts: IKeyringPair[] = [];181 for (const balance of balances) {182 const recipient = this.helper.util.fromSeed(mnemonicGenerate(), ss58Format);183 accounts.push(recipient);184 if (balance !== 0n) {185 const tx = this.helper.constructApiCall('api.tx.balances.transfer', [{Id: recipient.address}, balance * tokenNominal]);186 transactions.push(this.helper.signTransaction(donor, tx, {nonce}, 'account generation'));187 nonce++;188 }189 }190191 await Promise.all(transactions).catch(_e => {});192 193 194 const checkBalances = async () => {195 let isSuccess = true;196 for (let i = 0; i < balances.length; i++) {197 const balance = await this.helper.balance.getSubstrate(accounts[i].address);198 if (balance !== balances[i] * tokenNominal) {199 isSuccess = false;200 break;201 }202 }203 return isSuccess;204 };205206 let accountsCreated = false;207 const maxBlocksChecked = await this.helper.arrange.isDevNode() ? 50 : 5;208 209 for (let index = 0; index < maxBlocksChecked; index++) {210 accountsCreated = await checkBalances();211 if(accountsCreated) break;212 await wait.newBlocks(1);213 }214215 if (!accountsCreated) throw Error('Accounts generation failed');216 217218 return accounts;219 };220221 222 createCrowd = async (accountsToCreate: number, withBalance: bigint, donor: IKeyringPair): Promise<IKeyringPair[]> => { 223 const createAsManyAsCan = async () => {224 let transactions: any = [];225 const accounts: IKeyringPair[] = [];226 let nonce = await this.helper.chain.getNonce(donor.address);227 const tokenNominal = this.helper.balance.getOneTokenNominal();228 for (let i = 0; i < accountsToCreate; i++) {229 if (i === 500) { 230 await Promise.allSettled(transactions); 231 transactions = []; 232 nonce = await this.helper.chain.getNonce(donor.address); 233 }234 const recepient = this.helper.util.fromSeed(mnemonicGenerate());235 accounts.push(recepient);236 if (withBalance !== 0n) {237 const tx = this.helper.constructApiCall('api.tx.balances.transfer', [{Id: recepient.address}, withBalance * tokenNominal]);238 transactions.push(this.helper.signTransaction(donor, tx, {nonce}, 'account generation'));239 nonce++;240 }241 }242 243 const fullfilledAccounts = [];244 await Promise.allSettled(transactions);245 for (const account of accounts) {246 const accountBalance = await this.helper.balance.getSubstrate(account.address);247 if (accountBalance === withBalance * tokenNominal) {248 fullfilledAccounts.push(account);249 }250 }251 return fullfilledAccounts;252 };253254 255 const crowd: IKeyringPair[] = [];256 257 for (let index = 0; index < 5 && accountsToCreate !== 0; index++) {258 const asManyAsCan = await createAsManyAsCan();259 crowd.push(...asManyAsCan);260 accountsToCreate -= asManyAsCan.length;261 }262263 if (accountsToCreate !== 0) throw Error(`Crowd generation failed: ${accountsToCreate} accounts left`);264265 return crowd;266 };267268 isDevNode = async () => {269 let blockNumber = (await this.helper.callRpc('api.query.system.number')).toJSON();270 if (blockNumber == 0) {271 await this.helper.wait.newBlocks(1); 272 blockNumber = (await this.helper.callRpc('api.query.system.number')).toJSON();273 }274 const block2 = await this.helper.callRpc('api.rpc.chain.getBlock', [await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockNumber])]);275 const block1 = await this.helper.callRpc('api.rpc.chain.getBlock', [await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockNumber - 1])]);276 const findCreationDate = (block: any) => {277 const humanBlock = block.toHuman();278 let date;279 humanBlock.block.extrinsics.forEach((ext: any) => {280 if(ext.method.section === 'timestamp') {281 date = Number(ext.method.args.now.replaceAll(',', ''));282 }283 });284 return date;285 };286 const block1date = await findCreationDate(block1);287 const block2date = await findCreationDate(block2);288 if(block2date! - block1date! < 9000) return true;289 };290 291 async calculcateFee(payer: ICrossAccountId, promise: () => Promise<any>): Promise<bigint> {292 const address = payer.Substrate ? payer.Substrate : await this.helper.address.ethToSubstrate(payer.Ethereum!);293 let balance = await this.helper.balance.getSubstrate(address); 294 295 await promise();296 297 balance -= await this.helper.balance.getSubstrate(address);298 299 return balance;300 }301302 calculatePalletAddress(palletId: any) {303 const address = stringToU8a(('modl' + palletId).padEnd(32, '\0'));304 return encodeAddress(address);305 }306307 async makeScheduledIds(num: number): Promise<string[]> {308 await this.helper.wait.noScheduledTasks();309310 function makeId(slider: number) {311 const scheduledIdSize = 32;312 const hexId = slider.toString(16);313 const prefixSize = scheduledIdSize - hexId.length;314315 const scheduledId = '0x' + '0'.repeat(prefixSize) + hexId;316317 return scheduledId; 318 }319320 const ids = [];321 for (let i = 0; i < num; i++) {322 ids.push(makeId(this.scheduledIdSlider));323 this.scheduledIdSlider += 1;324 }325326 return ids;327 }328329 async makeScheduledId(): Promise<string> {330 return (await this.makeScheduledIds(1))[0];331 }332333 async captureEvents(eventSection: string, eventMethod: string): Promise<EventCapture> {334 const capture = new EventCapture(this.helper, eventSection, eventMethod);335 await capture.startCapture();336337 return capture;338 }339}340341class MoonbeamAccountGroup {342 helper: MoonbeamHelper;343344 keyring: Keyring;345 _alithAccount: IKeyringPair;346 _baltatharAccount: IKeyringPair;347 _dorothyAccount: IKeyringPair;348349 constructor(helper: MoonbeamHelper) {350 this.helper = helper;351352 this.keyring = new Keyring({type: 'ethereum'});353 const alithPrivateKey = '0x5fb92d6e98884f76de468fa3f6278f8807c48bebc13595d45af5bdc4da702133';354 const baltatharPrivateKey = '0x8075991ce870b93a8870eca0c0f91913d12f47948ca0fd25b49c6fa7cdbeee8b';355 const dorothyPrivateKey = '0x39539ab1876910bbf3a223d84a29e28f1cb4e2e456503e7e91ed39b2e7223d68';356357 this._alithAccount = this.keyring.addFromUri(alithPrivateKey, undefined, 'ethereum');358 this._baltatharAccount = this.keyring.addFromUri(baltatharPrivateKey, undefined, 'ethereum');359 this._dorothyAccount = this.keyring.addFromUri(dorothyPrivateKey, undefined, 'ethereum');360 }361362 alithAccount() {363 return this._alithAccount;364 }365366 baltatharAccount() {367 return this._baltatharAccount;368 }369370 dorothyAccount() {371 return this._dorothyAccount;372 }373374 create() {375 return this.keyring.addFromUri(mnemonicGenerate());376 }377}378379class WaitGroup {380 helper: ChainHelperBase;381382 constructor(helper: ChainHelperBase) {383 this.helper = helper;384 }385386 sleep(milliseconds: number) {387 return new Promise((resolve) => setTimeout(resolve, milliseconds));388 }389390 private async waitWithTimeout(promise: Promise<any>, timeout: number) {391 let isBlock = false;392 promise.then(() => isBlock = true).catch(() => isBlock = true);393 let totalTime = 0;394 const step = 100;395 while(!isBlock) {396 await this.sleep(step);397 totalTime += step;398 if(totalTime >= timeout) throw Error('Blocks production failed');399 }400 return promise;401 }402403 404405406407408 async newBlocks(blocksCount = 1, timeout?: number): Promise<void> {409 timeout = timeout ?? blocksCount * 60_000;410 411 const promise = new Promise<void>(async (resolve) => {412 const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads(() => {413 if (blocksCount > 0) {414 blocksCount--;415 } else {416 unsubscribe();417 resolve();418 }419 });420 });421 await this.waitWithTimeout(promise, timeout);422 return promise;423 }424425 async forParachainBlockNumber(blockNumber: bigint, timeout?: number) {426 timeout = timeout ?? 300_000;427 428 const promise = new Promise<void>(async (resolve) => {429 const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads((data: any) => {430 if (data.number.toNumber() >= blockNumber) {431 unsubscribe();432 resolve();433 }434 });435 });436 await this.waitWithTimeout(promise, timeout);437 return promise;438 }439 440 async forRelayBlockNumber(blockNumber: bigint, timeout?: number) {441 timeout = timeout ?? 300_000;442 443 const promise = new Promise<void>(async (resolve) => {444 const unsubscribe = await this.helper.getApi().query.parachainSystem.validationData((data: any) => {445 if (data.value.relayParentNumber.toNumber() >= blockNumber) {446 447 unsubscribe();448 resolve();449 }450 });451 });452 await this.waitWithTimeout(promise, timeout);453 return promise;454 }455456 noScheduledTasks() {457 const api = this.helper.getApi();458 459 460 const promise = new Promise<void>(async resolve => {461 const unsubscribe = await api.rpc.chain.subscribeNewHeads(async () => {462 const areThereScheduledTasks = await api.query.scheduler.lookup.entries();463464 if(areThereScheduledTasks.length == 0) {465 unsubscribe();466 resolve();467 }468 }); 469 });470471 return promise;472 }473474 event(maxBlocksToWait: number, eventSection: string, eventMethod: string) {475 476 const promise = new Promise<EventRecord | null>(async (resolve) => {477 const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads(async header => {478 const blockNumber = header.number.toHuman();479 const blockHash = header.hash;480 const eventIdStr = `${eventSection}.${eventMethod}`;481 const waitLimitStr = `wait blocks remaining: ${maxBlocksToWait}`;482 483 this.helper.logger.log(`[Block #${blockNumber}] Waiting for event \`${eventIdStr}\` (${waitLimitStr})`);484 485 const apiAt = await this.helper.getApi().at(blockHash);486 const eventRecords = (await apiAt.query.system.events()) as any;487 488 const neededEvent = eventRecords.toArray().find((r: FrameSystemEventRecord) => {489 return r.event.section == eventSection && r.event.method == eventMethod;490 });491 492 if (neededEvent) {493 unsubscribe();494 resolve(neededEvent);495 } else if (maxBlocksToWait > 0) {496 maxBlocksToWait--;497 } else {498 this.helper.logger.log(`Event \`${eventIdStr}\` is NOT found`);499 unsubscribe();500 resolve(null);501 }502 });503 });504 return promise;505 }506}507508class TestUtilGroup {509 helper: DevUniqueHelper;510511 constructor(helper: DevUniqueHelper) {512 this.helper = helper;513 }514515 async enable() {516 if (this.helper.fetchMissingPalletNames([Pallets.TestUtils]).length != 0) {517 return;518 }519520 const signer = this.helper.util.fromSeed('//Alice');521 await this.helper.getSudo<DevUniqueHelper>().executeExtrinsic(signer, 'api.tx.testUtils.enable', [], true);522 }523524 async setTestValue(signer: TSigner, testVal: number) {525 await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.setTestValue', [testVal], true);526 }527528 async incTestValue(signer: TSigner) {529 await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.incTestValue', [], true);530 }531532 async setTestValueAndRollback(signer: TSigner, testVal: number) {533 await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.setTestValueAndRollback', [testVal], true);534 }535536 async testValue() {537 return (await this.helper.callRpc('api.query.testUtils.testValue', [])).toNumber();538 }539540 async justTakeFee(signer: TSigner) {541 await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.justTakeFee', [], true);542 }543544 async selfCancelingInc(signer: TSigner, scheduledId: string, maxTestVal: number) {545 await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.selfCancelingInc', [scheduledId, maxTestVal], true);546 }547}548549class EventCapture {550 helper: DevUniqueHelper;551 eventSection: string;552 eventMethod: string;553 events: EventRecord[] = [];554 unsubscribe: VoidFn | null = null;555556 constructor(557 helper: DevUniqueHelper,558 eventSection: string,559 eventMethod: string,560 ) {561 this.helper = helper;562 this.eventSection = eventSection;563 this.eventMethod = eventMethod;564 }565566 async startCapture() {567 this.stopCapture();568 this.unsubscribe = (await this.helper.getApi().query.system.events((eventRecords: FrameSystemEventRecord[]) => {569 const newEvents = eventRecords.filter(r => {570 return r.event.section == this.eventSection && r.event.method == this.eventMethod;571 });572573 this.events.push(...newEvents);574 })) as any;575 }576577 stopCapture() {578 if (this.unsubscribe !== null) {579 this.unsubscribe();580 }581 }582583 extractCapturedEvents() {584 return this.events;585 }586}587588class AdminGroup {589 helper: UniqueHelper;590591 constructor(helper: UniqueHelper) {592 this.helper = helper;593 }594595 async payoutStakers(signer: IKeyringPair, stakersToPayout: number) {596 const payoutResult = await this.helper.executeExtrinsic(signer, 'api.tx.appPromotion.payoutStakers', [stakersToPayout], true);597 return payoutResult.result.events.filter(e => e.event.method === 'StakingRecalculation').map(e => {598 return {599 staker: e.event.data[0].toString(),600 stake: e.event.data[1].toBigInt(),601 payout: e.event.data[2].toBigInt(),602 };603 });604 }605}