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
--- a/tests/src/util/playgrounds/unique.ts
+++ b/tests/src/util/playgrounds/unique.ts
@@ -534,25 +534,27 @@
   }
 
   async getPaymentInfo(signer: TSigner, tx: any, len: number | null) {
-    const signingInfo = await this.api!.derive.tx.signingInfo(signer.address);
+    const api = this.getApi();
+    const signingInfo = await api.derive.tx.signingInfo(signer.address);
 
     // We need to sign the tx because
     // unsigned transactions does not have an inclusion fee
     tx.sign(signer, {
-      blockHash: this.api!.genesisHash,
-      genesisHash: this.api!.genesisHash,
-      runtimeVersion: this.api!.runtimeVersion,
+      blockHash: api.genesisHash,
+      genesisHash: api.genesisHash,
+      runtimeVersion: api.runtimeVersion,
       nonce: signingInfo.nonce,
     });
 
     if (len === null) {
       return (await this.callRpc('api.rpc.payment.queryInfo', [tx.toHex()])) as RuntimeDispatchInfo;
     } else {
-      return (await this.api!.call.transactionPaymentApi.queryInfo(tx, len)) as RuntimeDispatchInfo;
+      return (await api.call.transactionPaymentApi.queryInfo(tx, len)) as RuntimeDispatchInfo;
     }
   }
 
   constructApiCall(apiCall: string, params: any[]) {
+    if(this.api === null) throw Error('API not initialized');
     if(!apiCall.startsWith('api.')) throw Error(`Invalid api call: ${apiCall}`);
     let call = this.getApi() as any;
     for(const part of apiCall.slice(4).split('.')) {
@@ -2274,6 +2276,25 @@
   async transferToSubstrate(signer: TSigner, address: TSubstrateAccount, amount: bigint | string): Promise<boolean> {
     return this.subBalanceGroup.transferToSubstrate(signer, address, amount);
   }
+
+  async forceTransferToSubstrate(signer: TSigner, from: TSubstrateAccount, to: TSubstrateAccount, amount: bigint | string): Promise<boolean> {
+    const result = await this.helper.executeExtrinsic(signer, 'api.tx.balances.forceTransfer', [from, to, amount], true);
+
+    let transfer = {from: null, to: null, amount: 0n} as any;
+    result.result.events.forEach(({event: {data, method, section}}) => {
+      if ((section === 'balances') && (method === 'Transfer')) {
+        transfer = {
+          from: this.helper.address.normalizeSubstrate(data[0]),
+          to: this.helper.address.normalizeSubstrate(data[1]),
+          amount: BigInt(data[2]),
+        };
+      }
+    });
+    let isSuccess = this.helper.address.normalizeSubstrate(from) === transfer.from;
+    isSuccess = isSuccess && this.helper.address.normalizeSubstrate(to) === transfer.to;
+    isSuccess = isSuccess && BigInt(amount) === transfer.amount;
+    return isSuccess;
+  }
 }
 
 class AddressGroup extends HelperGroup<ChainHelperBase> {
@@ -2418,52 +2439,6 @@
 }
 
 class SchedulerGroup extends HelperGroup<UniqueHelper> {
-  scheduledIdSlider = 0;
-
-  async waitNoScheduledTasks() {
-    const api = this.helper.api!;
-    
-    // eslint-disable-next-line no-async-promise-executor
-    const promise = new Promise<void>(async resolve => {
-      const unsubscribe = await api.rpc.chain.subscribeNewHeads(async () => {
-        const areThereScheduledTasks = await api.query.scheduler.lookup.entries();
-
-        if(areThereScheduledTasks.length == 0) {
-          unsubscribe();
-          resolve();
-        }
-      }); 
-    });
-
-    return promise;
-  }
-
-  async makeScheduledIds(num: number): Promise<string[]> {
-    await this.waitNoScheduledTasks();
-
-    function makeId(slider: number) {
-      const scheduledIdSize = 32;
-      const hexId = slider.toString(16);
-      const prefixSize = scheduledIdSize - hexId.length;
-
-      const scheduledId = '0x' + '0'.repeat(prefixSize) + hexId;
-
-      return scheduledId;  
-    }
-
-    const ids = [];
-    for (let i = 0; i < num; i++) {
-      ids.push(makeId(this.scheduledIdSlider));
-      this.scheduledIdSlider += 1;
-    }
-
-    return ids;
-  }
-
-  async makeScheduledId(): Promise<string> {
-    return (await this.makeScheduledIds(1))[0];
-  }
-
   async cancelScheduled(signer: TSigner, scheduledId: string) {
     return this.helper.executeExtrinsic(
       signer,
@@ -2990,6 +2965,10 @@
   getSudo<T extends UniqueHelper>() {
     return new UniqueBaseCollection(this.collectionId, this.helper.getSudo<T>());
   }
+
+  getSudo() {
+    return new UniqueCollectionBase(this.collectionId, this.helper.getSudo());
+  }
 }