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 = async (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 = 64;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 387388389390391 async newBlocks(blocksCount = 1): Promise<void> {392 393 const promise = new Promise<void>(async (resolve) => {394 const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads(() => {395 if (blocksCount > 0) {396 blocksCount--;397 } else {398 unsubscribe();399 resolve();400 }401 });402 });403 return promise;404 }405406 async forParachainBlockNumber(blockNumber: bigint) {407 408 return new Promise<void>(async (resolve) => {409 const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads(async (data: any) => {410 if (data.number.toNumber() >= blockNumber) {411 unsubscribe();412 resolve();413 }414 });415 });416 }417 418 async forRelayBlockNumber(blockNumber: bigint) {419 420 return new Promise<void>(async (resolve) => {421 const unsubscribe = await this.helper.getApi().query.parachainSystem.validationData(async (data: any) => {422 if (data.value.relayParentNumber.toNumber() >= blockNumber) {423 424 unsubscribe();425 resolve();426 }427 });428 });429 }430431 async noScheduledTasks() {432 const api = this.helper.getApi();433 434 435 const promise = new Promise<void>(async resolve => {436 const unsubscribe = await api.rpc.chain.subscribeNewHeads(async () => {437 const areThereScheduledTasks = await api.query.scheduler.lookup.entries();438439 if(areThereScheduledTasks.length == 0) {440 unsubscribe();441 resolve();442 }443 }); 444 });445446 return promise;447 }448449 async event(maxBlocksToWait: number, eventSection: string, eventMethod: string) {450 451 const promise = new Promise<EventRecord | null>(async (resolve) => {452 const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads(async header => {453 const blockNumber = header.number.toHuman();454 const blockHash = header.hash;455 const eventIdStr = `${eventSection}.${eventMethod}`;456 const waitLimitStr = `wait blocks remaining: ${maxBlocksToWait}`;457 458 this.helper.logger.log(`[Block #${blockNumber}] Waiting for event \`${eventIdStr}\` (${waitLimitStr})`);459 460 const apiAt = await this.helper.getApi().at(blockHash);461 const eventRecords = (await apiAt.query.system.events()) as any;462 463 const neededEvent = eventRecords.toArray().find((r: FrameSystemEventRecord) => {464 return r.event.section == eventSection && r.event.method == eventMethod;465 });466 467 if (neededEvent) {468 unsubscribe();469 resolve(neededEvent);470 } else if (maxBlocksToWait > 0) {471 maxBlocksToWait--;472 } else {473 this.helper.logger.log(`Event \`${eventIdStr}\` is NOT found`);474 unsubscribe();475 resolve(null);476 }477 });478 });479 return promise;480 }481}482483class TestUtilGroup {484 helper: DevUniqueHelper;485486 constructor(helper: DevUniqueHelper) {487 this.helper = helper;488 }489490 async enable() {491 if (this.helper.fetchMissingPalletNames([Pallets.TestUtils]).length != 0) {492 return;493 }494495 const signer = this.helper.util.fromSeed('//Alice');496 await this.helper.getSudo<DevUniqueHelper>().executeExtrinsic(signer, 'api.tx.testUtils.enable', [], true);497 }498499 async setTestValue(signer: TSigner, testVal: number) {500 await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.setTestValue', [testVal], true);501 }502503 async incTestValue(signer: TSigner) {504 await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.incTestValue', [], true);505 }506507 async setTestValueAndRollback(signer: TSigner, testVal: number) {508 await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.setTestValueAndRollback', [testVal], true);509 }510511 async testValue() {512 return (await this.helper.callRpc('api.query.testUtils.testValue', [])).toNumber();513 }514515 async justTakeFee(signer: TSigner) {516 await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.justTakeFee', [], true);517 }518519 async selfCancelingInc(signer: TSigner, scheduledId: string, maxTestVal: number) {520 await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.selfCancelingInc', [scheduledId, maxTestVal], true);521 }522}523524class EventCapture {525 helper: DevUniqueHelper;526 eventSection: string;527 eventMethod: string;528 events: EventRecord[] = [];529 unsubscribe: VoidFn | null = null;530531 constructor(532 helper: DevUniqueHelper,533 eventSection: string,534 eventMethod: string,535 ) {536 this.helper = helper;537 this.eventSection = eventSection;538 this.eventMethod = eventMethod;539 }540541 async startCapture() {542 this.stopCapture();543 this.unsubscribe = (await this.helper.getApi().query.system.events((eventRecords: FrameSystemEventRecord[]) => {544 const newEvents = eventRecords.filter(r => {545 return r.event.section == this.eventSection && r.event.method == this.eventMethod;546 });547548 this.events.push(...newEvents);549 })) as any;550 }551552 stopCapture() {553 if (this.unsubscribe !== null) {554 this.unsubscribe();555 }556 }557558 extractCapturedEvents() {559 return this.events;560 }561}562563class AdminGroup {564 helper: UniqueHelper;565566 constructor(helper: UniqueHelper) {567 this.helper = helper;568 }569570 async payoutStakers(signer: IKeyringPair, stakersToPayout: number) {571 const payoutResult = await this.helper.executeExtrinsic(signer, 'api.tx.appPromotion.payoutStakers', [stakersToPayout], true);572 return payoutResult.result.events.filter(e => e.event.method === 'StakingRecalculation').map(e => {573 return {574 staker: e.event.data[0].toString(),575 stake: e.event.data[1].toBigInt(),576 payout: e.event.data[2].toBigInt(),577 };578 });579 }580}