From cde495e758569d503be88f44bf7a4226db5cdd0b Mon Sep 17 00:00:00 2001 From: Max Andreev Date: Wed, 18 Jan 2023 14:47:42 +0000 Subject: [PATCH] Add common nesting helpers to NFTnRFT group --- --- a/tests/src/util/playgrounds/unique.ts +++ b/tests/src/util/playgrounds/unique.ts @@ -1392,6 +1392,80 @@ } /** + * Get token's owner + * @param collectionId ID of collection + * @param tokenId ID of token + * @param blockHashAt optionally query the data at the block with this hash + * @example getTokenOwner(10, 5); + * @returns Address in CrossAccountId format, e.g. {Substrate: "5DnSF6RRjwteE3BrCj..."} + */ + async getTokenOwner(collectionId: number, tokenId: number, blockHashAt?: string): Promise { + let owner; + if (typeof blockHashAt === 'undefined') { + owner = await this.helper.callRpc('api.rpc.unique.tokenOwner', [collectionId, tokenId]); + } else { + owner = await this.helper.callRpc('api.rpc.unique.tokenOwner', [collectionId, tokenId, blockHashAt]); + } + return CrossAccountId.fromLowerCaseKeys(owner.toJSON()); + } + + /** + * Recursively find the address that owns the token + * @param collectionId ID of collection + * @param tokenId ID of token + * @param blockHashAt + * @example getTokenTopmostOwner(10, 5); + * @returns address in CrossAccountId format, e.g. {Substrate: "5DyN4Y92vZCjv38fg..."} + */ + async getTokenTopmostOwner(collectionId: number, tokenId: number, blockHashAt?: string): Promise { + let owner; + if (typeof blockHashAt === 'undefined') { + owner = await this.helper.callRpc('api.rpc.unique.topmostTokenOwner', [collectionId, tokenId]); + } else { + owner = await this.helper.callRpc('api.rpc.unique.topmostTokenOwner', [collectionId, tokenId, blockHashAt]); + } + + if (owner === null) return null; + + return owner.toHuman(); + } + + /** + * Nest one token into another + * @param signer keyring of signer + * @param tokenObj token to be nested + * @param rootTokenObj token to be parent + * @example nestToken(aliceKeyring, {collectionId: 10, tokenId: 5}, {collectionId: 10, tokenId: 4}); + * @returns ```true``` if extrinsic success, otherwise ```false``` + */ + async nestToken(signer: TSigner, tokenObj: IToken, rootTokenObj: IToken): Promise { + const rootTokenAddress = this.helper.util.getTokenAccount(rootTokenObj); + const result = await this.transferToken(signer, tokenObj.collectionId, tokenObj.tokenId, rootTokenAddress); + if(!result) { + throw Error('Unable to nest token!'); + } + return result; + } + + /** + * Remove token from nested state + * @param signer keyring of signer + * @param tokenObj token to unnest + * @param rootTokenObj parent of a token + * @param toAddressObj address of a new token owner + * @example unnestToken(aliceKeyring, {collectionId: 10, tokenId: 5}, {collectionId: 10, tokenId: 4}, {Substrate: "5DyN4Y92vZCjv38fg..."}); + * @returns ```true``` if extrinsic success, otherwise ```false``` + */ + async unnestToken(signer: TSigner, tokenObj: IToken, rootTokenObj: IToken, toAddressObj: ICrossAccountId): Promise { + const rootTokenAddress = this.helper.util.getTokenAccount(rootTokenObj); + const result = await this.transferTokenFrom(signer, tokenObj.collectionId, tokenObj.tokenId, rootTokenAddress, toAddressObj); + if(!result) { + throw Error('Unable to unnest token!'); + } + return result; + } + + /** * Set permissions to change token properties * * @param signer keyring of signer @@ -1555,24 +1629,6 @@ */ getTokenObject(collectionId: number, tokenId: number): UniqueNFToken { return new UniqueNFToken(tokenId, this.getCollectionObject(collectionId)); - } - - /** - * Get token's owner - * @param collectionId ID of collection - * @param tokenId ID of token - * @param blockHashAt optionally query the data at the block with this hash - * @example getTokenOwner(10, 5); - * @returns Address in CrossAccountId format, e.g. {Substrate: "5DnSF6RRjwteE3BrCj..."} - */ - async getTokenOwner(collectionId: number, tokenId: number, blockHashAt?: string): Promise { - let owner; - if (typeof blockHashAt === 'undefined') { - owner = await this.helper.callRpc('api.rpc.unique.tokenOwner', [collectionId, tokenId]); - } else { - owner = await this.helper.callRpc('api.rpc.unique.tokenOwner', [collectionId, tokenId, blockHashAt]); - } - return CrossAccountId.fromLowerCaseKeys(owner.toJSON()); } /** @@ -1617,27 +1673,6 @@ } /** - * Recursively find the address that owns the token - * @param collectionId ID of collection - * @param tokenId ID of token - * @param blockHashAt - * @example getTokenTopmostOwner(10, 5); - * @returns address in CrossAccountId format, e.g. {Substrate: "5DyN4Y92vZCjv38fg..."} - */ - async getTokenTopmostOwner(collectionId: number, tokenId: number, blockHashAt?: string): Promise { - let owner; - if (typeof blockHashAt === 'undefined') { - owner = await this.helper.callRpc('api.rpc.unique.topmostTokenOwner', [collectionId, tokenId]); - } else { - owner = await this.helper.callRpc('api.rpc.unique.topmostTokenOwner', [collectionId, tokenId, blockHashAt]); - } - - if (owner === null) return null; - - return owner.toHuman(); - } - - /** * Get tokens nested in the provided token * @param collectionId ID of collection * @param tokenId ID of token @@ -1656,44 +1691,9 @@ return children.toJSON().map((x: any) => { return {collectionId: x.collection, tokenId: x.token}; }); - } - - /** - * Nest one token into another - * @param signer keyring of signer - * @param tokenObj token to be nested - * @param rootTokenObj token to be parent - * @example nestToken(aliceKeyring, {collectionId: 10, tokenId: 5}, {collectionId: 10, tokenId: 4}); - * @returns ```true``` if extrinsic success, otherwise ```false``` - */ - async nestToken(signer: TSigner, tokenObj: IToken, rootTokenObj: IToken): Promise { - const rootTokenAddress = this.helper.util.getTokenAccount(rootTokenObj); - const result = await this.transferToken(signer, tokenObj.collectionId, tokenObj.tokenId, rootTokenAddress); - if(!result) { - throw Error('Unable to nest token!'); - } - return result; } /** - * Remove token from nested state - * @param signer keyring of signer - * @param tokenObj token to unnest - * @param rootTokenObj parent of a token - * @param toAddressObj address of a new token owner - * @example unnestToken(aliceKeyring, {collectionId: 10, tokenId: 5}, {collectionId: 10, tokenId: 4}, {Substrate: "5DyN4Y92vZCjv38fg..."}); - * @returns ```true``` if extrinsic success, otherwise ```false``` - */ - async unnestToken(signer: TSigner, tokenObj: IToken, rootTokenObj: IToken, toAddressObj: ICrossAccountId): Promise { - const rootTokenAddress = this.helper.util.getTokenAccount(rootTokenObj); - const result = await this.transferTokenFrom(signer, tokenObj.collectionId, tokenObj.tokenId, rootTokenAddress, toAddressObj); - if(!result) { - throw Error('Unable to unnest token!'); - } - return result; - } - - /** * Mint new collection * @param signer keyring of signer * @param collectionOptions Collection options @@ -3531,6 +3531,10 @@ return await this.helper.rft.getToken(this.collectionId, tokenId, [], blockHashAt); } + async getTokenOwner(tokenId: number, blockHashAt?: string) { + return await this.helper.rft.getTokenOwner(this.collectionId, tokenId, blockHashAt); + } + async getTokensByAddress(addressObj: ICrossAccountId) { return await this.helper.rft.getTokensByAddress(this.collectionId, addressObj); } @@ -3539,6 +3543,10 @@ return await this.helper.rft.getTokenTop10Owners(this.collectionId, tokenId); } + async getTokenTopmostOwner(tokenId: number, blockHashAt?: string) { + return await this.helper.rft.getTokenTopmostOwner(this.collectionId, tokenId, blockHashAt); + } + async getTokenBalance(tokenId: number, addressObj: ICrossAccountId) { return await this.helper.rft.getTokenBalance(this.collectionId, tokenId, addressObj); } @@ -3582,7 +3590,7 @@ return await this.helper.rft.repartitionToken(signer, this.collectionId, tokenId, amount); } - async mintToken(signer: TSigner, pieces=1n, owner: ICrossAccountId = {Substrate: signer.address}, properties?: IProperty[]) { + async mintToken(signer: TSigner, pieces = 1n, owner: ICrossAccountId = {Substrate: signer.address}, properties?: IProperty[]) { return await this.helper.rft.mintToken(signer, {collectionId: this.collectionId, owner, pieces, properties}); } @@ -3610,6 +3618,14 @@ return await this.helper.rft.setTokenPropertyPermissions(signer, this.collectionId, permissions); } + async nestToken(signer: TSigner, tokenId: number, toTokenObj: IToken) { + return await this.helper.rft.nestToken(signer, {collectionId: this.collectionId, tokenId}, toTokenObj); + } + + async unnestToken(signer: TSigner, tokenId: number, fromTokenObj: IToken, toAddressObj: ICrossAccountId) { + return await this.helper.rft.unnestToken(signer, {collectionId: this.collectionId, tokenId}, fromTokenObj, toAddressObj); + } + scheduleAt( executionBlockNumber: number, options: ISchedulerOptions = {}, @@ -3849,10 +3865,26 @@ return await this.collection.getToken(this.tokenId, blockHashAt); } + async getOwner(blockHashAt?: string) { + return await this.collection.getTokenOwner(this.tokenId, blockHashAt); + } + async getTop10Owners() { return await this.collection.getTop10TokenOwners(this.tokenId); } + async getTopmostOwner(blockHashAt?: string) { + return await this.collection.getTokenTopmostOwner(this.tokenId, blockHashAt); + } + + async nest(signer: TSigner, toTokenObj: IToken) { + return await this.collection.nestToken(signer, this.tokenId, toTokenObj); + } + + async unnest(signer: TSigner, fromTokenObj: IToken, toAddressObj: ICrossAccountId) { + return await this.collection.unnestToken(signer, this.tokenId, fromTokenObj, toAddressObj); + } + async getBalance(addressObj: ICrossAccountId) { return await this.collection.getTokenBalance(this.tokenId, addressObj); } -- gitstuff