git.delta.rocks / unique-network / refs/commits / fcb2b324d1ba

difftreelog

feat add sudo/scheduler support to playgrounds, several minor additions

Daniel Shiposha2022-09-16parent: #b36ccba.patch.diff
in: master

2 files changed

modifiedtests/src/util/playgrounds/unique.dev.tsdiffbeforeafterboth
10import {EventRecord} from '@polkadot/types/interfaces';10import {EventRecord} from '@polkadot/types/interfaces';
11import {ICrossAccountId} from './types';11import {ICrossAccountId} from './types';
12import {FrameSystemEventRecord} from '@polkadot/types/lookup';12import {FrameSystemEventRecord} from '@polkadot/types/lookup';
13import {VoidFn} from '@polkadot/api/types';
1314
14export class SilentLogger {15export class SilentLogger {
15 log(_msg: any, _level: any): void { }16 log(_msg: any, _level: any): void { }
154class ArrangeGroup {155class ArrangeGroup {
155 helper: DevUniqueHelper;156 helper: DevUniqueHelper;
157
158 scheduledIdSlider = 0;
156159
157 constructor(helper: DevUniqueHelper) {160 constructor(helper: DevUniqueHelper) {
158 this.helper = helper;161 this.helper = helper;
298 return encodeAddress(address);301 return encodeAddress(address);
299 }302 }
303
304 async makeScheduledIds(num: number): Promise<string[]> {
305 await this.helper.wait.noScheduledTasks();
306
307 function makeId(slider: number) {
308 const scheduledIdSize = 32;
309 const hexId = slider.toString(16);
310 const prefixSize = scheduledIdSize - hexId.length;
311
312 const scheduledId = '0x' + '0'.repeat(prefixSize) + hexId;
313
314 return scheduledId;
315 }
316
317 const ids = [];
318 for (let i = 0; i < num; i++) {
319 ids.push(makeId(this.scheduledIdSlider));
320 this.scheduledIdSlider += 1;
321 }
322
323 return ids;
324 }
325
326 async makeScheduledId(): Promise<string> {
327 return (await this.makeScheduledIds(1))[0];
328 }
329
330 async captureEvents(eventSection: string, eventMethod: string): Promise<EventCapture> {
331 const capture = new EventCapture(this.helper, eventSection, eventMethod);
332 await capture.startCapture();
333
334 return capture;
335 }
300}336}
301337
302class MoonbeamAccountGroup {338class MoonbeamAccountGroup {
389 });425 });
390 }426 }
427
428 async noScheduledTasks() {
429 const api = this.helper.getApi();
430
431 // eslint-disable-next-line no-async-promise-executor
432 const promise = new Promise<void>(async resolve => {
433 const unsubscribe = await api.rpc.chain.subscribeNewHeads(async () => {
434 const areThereScheduledTasks = await api.query.scheduler.lookup.entries();
435
436 if(areThereScheduledTasks.length == 0) {
437 unsubscribe();
438 resolve();
439 }
440 });
441 });
442
443 return promise;
444 }
391445
392 async event(maxBlocksToWait: number, eventSection: string, eventMethod: string) {446 async event(maxBlocksToWait: number, eventSection: string, eventMethod: string) {
393 // eslint-disable-next-line no-async-promise-executor447 // eslint-disable-next-line no-async-promise-executor
424 }477 }
425}478}
479
480class EventCapture {
481 helper: DevUniqueHelper;
482 eventSection: string;
483 eventMethod: string;
484 events: EventRecord[] = [];
485 unsubscribe: VoidFn | null = null;
486
487 constructor(
488 helper: DevUniqueHelper,
489 eventSection: string,
490 eventMethod: string,
491 ) {
492 this.helper = helper;
493 this.eventSection = eventSection;
494 this.eventMethod = eventMethod;
495 }
496
497 async startCapture() {
498 this.stopCapture();
499 this.unsubscribe = await this.helper.getApi().query.system.events(eventRecords => {
500 const newEvents = eventRecords.filter(r => {
501 return r.event.section == this.eventSection && r.event.method == this.eventMethod;
502 });
503
504 this.events.push(...newEvents);
505 });
506 }
507
508 stopCapture() {
509 if (this.unsubscribe !== null) {
510 this.unsubscribe();
511 }
512 }
513
514 extractCapturedEvents() {
515 return this.events;
516 }
517}
426518
427class AdminGroup {519class AdminGroup {
428 helper: UniqueHelper;520 helper: UniqueHelper;
modifiedtests/src/util/playgrounds/unique.tsdiffbeforeafterboth
534 }534 }
535535
536 async getPaymentInfo(signer: TSigner, tx: any, len: number | null) {536 async getPaymentInfo(signer: TSigner, tx: any, len: number | null) {
537 const api = this.getApi();
537 const signingInfo = await this.api!.derive.tx.signingInfo(signer.address);538 const signingInfo = await api.derive.tx.signingInfo(signer.address);
538539
539 // We need to sign the tx because540 // We need to sign the tx because
540 // unsigned transactions does not have an inclusion fee541 // unsigned transactions does not have an inclusion fee
541 tx.sign(signer, {542 tx.sign(signer, {
542 blockHash: this.api!.genesisHash,543 blockHash: api.genesisHash,
543 genesisHash: this.api!.genesisHash,544 genesisHash: api.genesisHash,
544 runtimeVersion: this.api!.runtimeVersion,545 runtimeVersion: api.runtimeVersion,
545 nonce: signingInfo.nonce,546 nonce: signingInfo.nonce,
546 });547 });
547548
548 if (len === null) {549 if (len === null) {
549 return (await this.callRpc('api.rpc.payment.queryInfo', [tx.toHex()])) as RuntimeDispatchInfo;550 return (await this.callRpc('api.rpc.payment.queryInfo', [tx.toHex()])) as RuntimeDispatchInfo;
550 } else {551 } else {
551 return (await this.api!.call.transactionPaymentApi.queryInfo(tx, len)) as RuntimeDispatchInfo;552 return (await api.call.transactionPaymentApi.queryInfo(tx, len)) as RuntimeDispatchInfo;
552 }553 }
553 }554 }
554555
555 constructApiCall(apiCall: string, params: any[]) {556 constructApiCall(apiCall: string, params: any[]) {
557 if(this.api === null) throw Error('API not initialized');
556 if(!apiCall.startsWith('api.')) throw Error(`Invalid api call: ${apiCall}`);558 if(!apiCall.startsWith('api.')) throw Error(`Invalid api call: ${apiCall}`);
557 let call = this.getApi() as any;559 let call = this.getApi() as any;
558 for(const part of apiCall.slice(4).split('.')) {560 for(const part of apiCall.slice(4).split('.')) {
2275 return this.subBalanceGroup.transferToSubstrate(signer, address, amount);2277 return this.subBalanceGroup.transferToSubstrate(signer, address, amount);
2276 }2278 }
2279
2280 async forceTransferToSubstrate(signer: TSigner, from: TSubstrateAccount, to: TSubstrateAccount, amount: bigint | string): Promise<boolean> {
2281 const result = await this.helper.executeExtrinsic(signer, 'api.tx.balances.forceTransfer', [from, to, amount], true);
2282
2283 let transfer = {from: null, to: null, amount: 0n} as any;
2284 result.result.events.forEach(({event: {data, method, section}}) => {
2285 if ((section === 'balances') && (method === 'Transfer')) {
2286 transfer = {
2287 from: this.helper.address.normalizeSubstrate(data[0]),
2288 to: this.helper.address.normalizeSubstrate(data[1]),
2289 amount: BigInt(data[2]),
2290 };
2291 }
2292 });
2293 let isSuccess = this.helper.address.normalizeSubstrate(from) === transfer.from;
2294 isSuccess = isSuccess && this.helper.address.normalizeSubstrate(to) === transfer.to;
2295 isSuccess = isSuccess && BigInt(amount) === transfer.amount;
2296 return isSuccess;
2297 }
2277}2298}
22782299
2279class AddressGroup extends HelperGroup<ChainHelperBase> {2300class AddressGroup extends HelperGroup<ChainHelperBase> {
2418}2439}
24192440
2420class SchedulerGroup extends HelperGroup<UniqueHelper> {2441class SchedulerGroup extends HelperGroup<UniqueHelper> {
2421 scheduledIdSlider = 0;
2422
2423 async waitNoScheduledTasks() {
2424 const api = this.helper.api!;
2425
2426 // eslint-disable-next-line no-async-promise-executor
2427 const promise = new Promise<void>(async resolve => {
2428 const unsubscribe = await api.rpc.chain.subscribeNewHeads(async () => {
2429 const areThereScheduledTasks = await api.query.scheduler.lookup.entries();
2430
2431 if(areThereScheduledTasks.length == 0) {
2432 unsubscribe();
2433 resolve();
2434 }
2435 });
2436 });
2437
2438 return promise;
2439 }
2440
2441 async makeScheduledIds(num: number): Promise<string[]> {
2442 await this.waitNoScheduledTasks();
2443
2444 function makeId(slider: number) {
2445 const scheduledIdSize = 32;
2446 const hexId = slider.toString(16);
2447 const prefixSize = scheduledIdSize - hexId.length;
2448
2449 const scheduledId = '0x' + '0'.repeat(prefixSize) + hexId;
2450
2451 return scheduledId;
2452 }
2453
2454 const ids = [];
2455 for (let i = 0; i < num; i++) {
2456 ids.push(makeId(this.scheduledIdSlider));
2457 this.scheduledIdSlider += 1;
2458 }
2459
2460 return ids;
2461 }
2462
2463 async makeScheduledId(): Promise<string> {
2464 return (await this.makeScheduledIds(1))[0];
2465 }
2466
2467 async cancelScheduled(signer: TSigner, scheduledId: string) {2442 async cancelScheduled(signer: TSigner, scheduledId: string) {
2468 return this.helper.executeExtrinsic(2443 return this.helper.executeExtrinsic(
2991 return new UniqueBaseCollection(this.collectionId, this.helper.getSudo<T>());2966 return new UniqueBaseCollection(this.collectionId, this.helper.getSudo<T>());
2992 }2967 }
2968
2969 getSudo() {
2970 return new UniqueCollectionBase(this.collectionId, this.helper.getSudo());
2971 }
2993}2972}
29942973
29952974