From 3329598f7e9f48224ac97c7f5d940f0447367e4e Mon Sep 17 00:00:00 2001 From: Daniel Shiposha Date: Tue, 13 Sep 2022 22:05:23 +0000 Subject: [PATCH] feat: add scheduler tx to helpers --- --- a/tests/src/util/playgrounds/unique.ts +++ b/tests/src/util/playgrounds/unique.ts @@ -10,6 +10,7 @@ import {encodeAddress, decodeAddress, keccakAsHex, evmToAddress, addressToEvm} from '@polkadot/util-crypto'; import {IKeyringPair} from '@polkadot/types/types'; import {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'; +import {RuntimeDispatchInfo} from '@polkadot/types/interfaces'; export class CrossAccountId implements ICrossAccountId { Substrate?: TSubstrateAccount; @@ -532,6 +533,25 @@ }); } + async getPaymentInfo(signer: TSigner, tx: any, len: number | null) { + const signingInfo = await this.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, + 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; + } + } + constructApiCall(apiCall: string, params: any[]) { if(!apiCall.startsWith('api.')) throw Error(`Invalid api call: ${apiCall}`); let call = this.getApi() as any; @@ -1353,6 +1373,16 @@ return this.getCollectionObject(this.helper.util.extractCollectionIdFromCreationResult(creationResult)); } + async mintDefaultCollection(signer: TSigner, mode: 'NFT' | 'RFT'): Promise { + const defaultCreateCollectionParams: ICollectionCreationOptions = { + description: 'description', + name: 'name', + tokenPrefix: 'prfx', + }; + + return this.mintCollection(signer, defaultCreateCollectionParams, mode); + } + getCollectionObject(_collectionId: number): any { return null; } @@ -1537,6 +1567,10 @@ return await super.mintCollection(signer, collectionOptions, 'NFT') as UniqueNFTCollection; } + async mintDefaultCollection(signer: IKeyringPair): Promise { + return await super.mintDefaultCollection(signer, 'NFT') as UniqueNFTCollection; + } + /** * Mint new token * @param signer keyring of signer @@ -1723,6 +1757,10 @@ return await super.mintCollection(signer, collectionOptions, 'RFT') as UniqueRFTCollection; } + async mintDefaultCollection(signer: IKeyringPair): Promise { + return await super.mintDefaultCollection(signer, 'RFT') as UniqueRFTCollection; + } + /** * Mint new token * @param signer keyring of signer @@ -2398,8 +2436,50 @@ } class SchedulerGroup extends HelperGroup { - constructor(helper: UniqueHelper) { - super(helper); + scheduledIdSlider = 0; + + async waitNoScheduledTasks() { + const api = this.helper.api!; + + // eslint-disable-next-line no-async-promise-executor + const promise = new Promise(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 { + 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 { + return (await this.makeScheduledIds(1))[0]; } async cancelScheduled(signer: TSigner, scheduledId: string) { -- gitstuff