git.delta.rocks / unique-network / refs/commits / 3329598f7e9f

difftreelog

feat add scheduler tx to helpers

Daniel Shiposha2022-09-13parent: #3808c3f.patch.diff
in: master

1 file changed

modifiedtests/src/util/playgrounds/unique.tsdiffbeforeafterboth
10import {encodeAddress, decodeAddress, keccakAsHex, evmToAddress, addressToEvm} from '@polkadot/util-crypto';10import {encodeAddress, decodeAddress, keccakAsHex, evmToAddress, addressToEvm} from '@polkadot/util-crypto';
11import {IKeyringPair} from '@polkadot/types/types';11import {IKeyringPair} from '@polkadot/types/types';
12import {IApiListeners, IBlock, IEvent, IChainProperties, ICollectionCreationOptions, ICollectionLimits, ICollectionPermissions, ICrossAccountId, ICrossAccountIdLower, ILogger, INestingPermissions, IProperty, IStakingInfo, ISchedulerOptions, ISubstrateBalance, IToken, ITokenPropertyPermission, ITransactionResult, IUniqueHelperLog, TApiAllowedListeners, TEthereumAccount, TSigner, TSubstrateAccount, IForeignAssetMetadata, TNetworks, MoonbeamAssetInfo, DemocracyStandardAccountVote, AcalaAssetMetadata} from './types';12import {IApiListeners, IBlock, IEvent, IChainProperties, ICollectionCreationOptions, ICollectionLimits, ICollectionPermissions, ICrossAccountId, ICrossAccountIdLower, ILogger, INestingPermissions, IProperty, IStakingInfo, ISchedulerOptions, ISubstrateBalance, IToken, ITokenPropertyPermission, ITransactionResult, IUniqueHelperLog, TApiAllowedListeners, TEthereumAccount, TSigner, TSubstrateAccount, IForeignAssetMetadata, TNetworks, MoonbeamAssetInfo, DemocracyStandardAccountVote, AcalaAssetMetadata} from './types';
13import {RuntimeDispatchInfo} from '@polkadot/types/interfaces';
1314
14export class CrossAccountId implements ICrossAccountId {15export class CrossAccountId implements ICrossAccountId {
15 Substrate?: TSubstrateAccount;16 Substrate?: TSubstrateAccount;
532 });533 });
533 }534 }
535
536 async getPaymentInfo(signer: TSigner, tx: any, len: number | null) {
537 const signingInfo = await this.api!.derive.tx.signingInfo(signer.address);
538
539 // We need to sign the tx because
540 // unsigned transactions does not have an inclusion fee
541 tx.sign(signer, {
542 blockHash: this.api!.genesisHash,
543 genesisHash: this.api!.genesisHash,
544 runtimeVersion: this.api!.runtimeVersion,
545 nonce: signingInfo.nonce,
546 });
547
548 if (len === null) {
549 return (await this.callRpc('api.rpc.payment.queryInfo', [tx.toHex()])) as RuntimeDispatchInfo;
550 } else {
551 return (await this.api!.call.transactionPaymentApi.queryInfo(tx, len)) as RuntimeDispatchInfo;
552 }
553 }
534554
535 constructApiCall(apiCall: string, params: any[]) {555 constructApiCall(apiCall: string, params: any[]) {
536 if(!apiCall.startsWith('api.')) throw Error(`Invalid api call: ${apiCall}`);556 if(!apiCall.startsWith('api.')) throw Error(`Invalid api call: ${apiCall}`);
1353 return this.getCollectionObject(this.helper.util.extractCollectionIdFromCreationResult(creationResult));1373 return this.getCollectionObject(this.helper.util.extractCollectionIdFromCreationResult(creationResult));
1354 }1374 }
1375
1376 async mintDefaultCollection(signer: TSigner, mode: 'NFT' | 'RFT'): Promise<UniqueCollectionBase> {
1377 const defaultCreateCollectionParams: ICollectionCreationOptions = {
1378 description: 'description',
1379 name: 'name',
1380 tokenPrefix: 'prfx',
1381 };
1382
1383 return this.mintCollection(signer, defaultCreateCollectionParams, mode);
1384 }
13551385
1356 getCollectionObject(_collectionId: number): any {1386 getCollectionObject(_collectionId: number): any {
1357 return null;1387 return null;
1537 return await super.mintCollection(signer, collectionOptions, 'NFT') as UniqueNFTCollection;1567 return await super.mintCollection(signer, collectionOptions, 'NFT') as UniqueNFTCollection;
1538 }1568 }
1569
1570 async mintDefaultCollection(signer: IKeyringPair): Promise<UniqueNFTCollection> {
1571 return await super.mintDefaultCollection(signer, 'NFT') as UniqueNFTCollection;
1572 }
15391573
1540 /**1574 /**
1541 * Mint new token1575 * Mint new token
1723 return await super.mintCollection(signer, collectionOptions, 'RFT') as UniqueRFTCollection;1757 return await super.mintCollection(signer, collectionOptions, 'RFT') as UniqueRFTCollection;
1724 }1758 }
1759
1760 async mintDefaultCollection(signer: IKeyringPair): Promise<UniqueRFTCollection> {
1761 return await super.mintDefaultCollection(signer, 'RFT') as UniqueRFTCollection;
1762 }
17251763
1726 /**1764 /**
1727 * Mint new token1765 * Mint new token
2398}2436}
23992437
2400class SchedulerGroup extends HelperGroup<UniqueHelper> {2438class SchedulerGroup extends HelperGroup<UniqueHelper> {
2439 scheduledIdSlider = 0;
2440
2441 async waitNoScheduledTasks() {
2442 const api = this.helper.api!;
2443
2444 // eslint-disable-next-line no-async-promise-executor
2445 const promise = new Promise<void>(async resolve => {
2446 const unsubscribe = await api.rpc.chain.subscribeNewHeads(async () => {
2447 const areThereScheduledTasks = await api.query.scheduler.lookup.entries();
2448
2449 if(areThereScheduledTasks.length == 0) {
2450 unsubscribe();
2451 resolve();
2452 }
2453 });
2454 });
2455
2456 return promise;
2457 }
2458
2401 constructor(helper: UniqueHelper) {2459 async makeScheduledIds(num: number): Promise<string[]> {
2402 super(helper);2460 await this.waitNoScheduledTasks();
2461
2462 function makeId(slider: number) {
2463 const scheduledIdSize = 32;
2464 const hexId = slider.toString(16);
2465 const prefixSize = scheduledIdSize - hexId.length;
2466
2467 const scheduledId = '0x' + '0'.repeat(prefixSize) + hexId;
2468
2469 return scheduledId;
2470 }
2471
2472 const ids = [];
2473 for (let i = 0; i < num; i++) {
2474 ids.push(makeId(this.scheduledIdSlider));
2475 this.scheduledIdSlider += 1;
2476 }
2477
2478 return ids;
2403 }2479 }
2480
2481 async makeScheduledId(): Promise<string> {
2482 return (await this.makeScheduledIds(1))[0];
2483 }
24042484
2405 async cancelScheduled(signer: TSigner, scheduledId: string) {2485 async cancelScheduled(signer: TSigner, scheduledId: string) {
2406 return this.helper.executeExtrinsic(2486 return this.helper.executeExtrinsic(