difftreelog
Add common nesting helpers to NFTnRFT group
in: master
1 file changed
tests/src/util/playgrounds/unique.tsdiffbeforeafterboth1391 return tokenData;1391 return tokenData;1392 }1392 }13931394 /**1395 * Get token's owner1396 * @param collectionId ID of collection1397 * @param tokenId ID of token1398 * @param blockHashAt optionally query the data at the block with this hash1399 * @example getTokenOwner(10, 5);1400 * @returns Address in CrossAccountId format, e.g. {Substrate: "5DnSF6RRjwteE3BrCj..."}1401 */1402 async getTokenOwner(collectionId: number, tokenId: number, blockHashAt?: string): Promise<CrossAccountId> {1403 let owner;1404 if (typeof blockHashAt === 'undefined') {1405 owner = await this.helper.callRpc('api.rpc.unique.tokenOwner', [collectionId, tokenId]);1406 } else {1407 owner = await this.helper.callRpc('api.rpc.unique.tokenOwner', [collectionId, tokenId, blockHashAt]);1408 }1409 return CrossAccountId.fromLowerCaseKeys(owner.toJSON());1410 }14111412 /**1413 * Recursively find the address that owns the token1414 * @param collectionId ID of collection1415 * @param tokenId ID of token1416 * @param blockHashAt1417 * @example getTokenTopmostOwner(10, 5);1418 * @returns address in CrossAccountId format, e.g. {Substrate: "5DyN4Y92vZCjv38fg..."}1419 */1420 async getTokenTopmostOwner(collectionId: number, tokenId: number, blockHashAt?: string): Promise<CrossAccountId | null> {1421 let owner;1422 if (typeof blockHashAt === 'undefined') {1423 owner = await this.helper.callRpc('api.rpc.unique.topmostTokenOwner', [collectionId, tokenId]);1424 } else {1425 owner = await this.helper.callRpc('api.rpc.unique.topmostTokenOwner', [collectionId, tokenId, blockHashAt]);1426 }14271428 if (owner === null) return null;14291430 return owner.toHuman();1431 }14321433 /**1434 * Nest one token into another1435 * @param signer keyring of signer1436 * @param tokenObj token to be nested1437 * @param rootTokenObj token to be parent1438 * @example nestToken(aliceKeyring, {collectionId: 10, tokenId: 5}, {collectionId: 10, tokenId: 4});1439 * @returns ```true``` if extrinsic success, otherwise ```false```1440 */1441 async nestToken(signer: TSigner, tokenObj: IToken, rootTokenObj: IToken): Promise<boolean> {1442 const rootTokenAddress = this.helper.util.getTokenAccount(rootTokenObj);1443 const result = await this.transferToken(signer, tokenObj.collectionId, tokenObj.tokenId, rootTokenAddress);1444 if(!result) {1445 throw Error('Unable to nest token!');1446 }1447 return result;1448 }14491450 /**1451 * Remove token from nested state1452 * @param signer keyring of signer1453 * @param tokenObj token to unnest1454 * @param rootTokenObj parent of a token1455 * @param toAddressObj address of a new token owner1456 * @example unnestToken(aliceKeyring, {collectionId: 10, tokenId: 5}, {collectionId: 10, tokenId: 4}, {Substrate: "5DyN4Y92vZCjv38fg..."});1457 * @returns ```true``` if extrinsic success, otherwise ```false```1458 */1459 async unnestToken(signer: TSigner, tokenObj: IToken, rootTokenObj: IToken, toAddressObj: ICrossAccountId): Promise<boolean> {1460 const rootTokenAddress = this.helper.util.getTokenAccount(rootTokenObj);1461 const result = await this.transferTokenFrom(signer, tokenObj.collectionId, tokenObj.tokenId, rootTokenAddress, toAddressObj);1462 if(!result) {1463 throw Error('Unable to unnest token!');1464 }1465 return result;1466 }139314671394 /**1468 /**1395 * Set permissions to change token properties1469 * Set permissions to change token properties1557 return new UniqueNFToken(tokenId, this.getCollectionObject(collectionId));1631 return new UniqueNFToken(tokenId, this.getCollectionObject(collectionId));1558 }1632 }15591560 /**1561 * Get token's owner1562 * @param collectionId ID of collection1563 * @param tokenId ID of token1564 * @param blockHashAt optionally query the data at the block with this hash1565 * @example getTokenOwner(10, 5);1566 * @returns Address in CrossAccountId format, e.g. {Substrate: "5DnSF6RRjwteE3BrCj..."}1567 */1568 async getTokenOwner(collectionId: number, tokenId: number, blockHashAt?: string): Promise<CrossAccountId> {1569 let owner;1570 if (typeof blockHashAt === 'undefined') {1571 owner = await this.helper.callRpc('api.rpc.unique.tokenOwner', [collectionId, tokenId]);1572 } else {1573 owner = await this.helper.callRpc('api.rpc.unique.tokenOwner', [collectionId, tokenId, blockHashAt]);1574 }1575 return CrossAccountId.fromLowerCaseKeys(owner.toJSON());1576 }157716331578 /**1634 /**1579 * Is token approved to transfer1635 * Is token approved to transfer1616 return await super.transferTokenFrom(signer, collectionId, tokenId, fromAddressObj, toAddressObj, 1n);1672 return await super.transferTokenFrom(signer, collectionId, tokenId, fromAddressObj, toAddressObj, 1n);1617 }1673 }16181619 /**1620 * Recursively find the address that owns the token1621 * @param collectionId ID of collection1622 * @param tokenId ID of token1623 * @param blockHashAt1624 * @example getTokenTopmostOwner(10, 5);1625 * @returns address in CrossAccountId format, e.g. {Substrate: "5DyN4Y92vZCjv38fg..."}1626 */1627 async getTokenTopmostOwner(collectionId: number, tokenId: number, blockHashAt?: string): Promise<CrossAccountId | null> {1628 let owner;1629 if (typeof blockHashAt === 'undefined') {1630 owner = await this.helper.callRpc('api.rpc.unique.topmostTokenOwner', [collectionId, tokenId]);1631 } else {1632 owner = await this.helper.callRpc('api.rpc.unique.topmostTokenOwner', [collectionId, tokenId, blockHashAt]);1633 }16341635 if (owner === null) return null;16361637 return owner.toHuman();1638 }163916741640 /**1675 /**1641 * Get tokens nested in the provided token1676 * Get tokens nested in the provided token1658 });1693 });1659 }1694 }16601661 /**1662 * Nest one token into another1663 * @param signer keyring of signer1664 * @param tokenObj token to be nested1665 * @param rootTokenObj token to be parent1666 * @example nestToken(aliceKeyring, {collectionId: 10, tokenId: 5}, {collectionId: 10, tokenId: 4});1667 * @returns ```true``` if extrinsic success, otherwise ```false```1668 */1669 async nestToken(signer: TSigner, tokenObj: IToken, rootTokenObj: IToken): Promise<boolean> {1670 const rootTokenAddress = this.helper.util.getTokenAccount(rootTokenObj);1671 const result = await this.transferToken(signer, tokenObj.collectionId, tokenObj.tokenId, rootTokenAddress);1672 if(!result) {1673 throw Error('Unable to nest token!');1674 }1675 return result;1676 }16771678 /**1679 * Remove token from nested state1680 * @param signer keyring of signer1681 * @param tokenObj token to unnest1682 * @param rootTokenObj parent of a token1683 * @param toAddressObj address of a new token owner1684 * @example unnestToken(aliceKeyring, {collectionId: 10, tokenId: 5}, {collectionId: 10, tokenId: 4}, {Substrate: "5DyN4Y92vZCjv38fg..."});1685 * @returns ```true``` if extrinsic success, otherwise ```false```1686 */1687 async unnestToken(signer: TSigner, tokenObj: IToken, rootTokenObj: IToken, toAddressObj: ICrossAccountId): Promise<boolean> {1688 const rootTokenAddress = this.helper.util.getTokenAccount(rootTokenObj);1689 const result = await this.transferTokenFrom(signer, tokenObj.collectionId, tokenObj.tokenId, rootTokenAddress, toAddressObj);1690 if(!result) {1691 throw Error('Unable to unnest token!');1692 }1693 return result;1694 }169516951696 /**1696 /**1697 * Mint new collection1697 * Mint new collection3531 return await this.helper.rft.getToken(this.collectionId, tokenId, [], blockHashAt);3531 return await this.helper.rft.getToken(this.collectionId, tokenId, [], blockHashAt);3532 }3532 }35333534 async getTokenOwner(tokenId: number, blockHashAt?: string) {3535 return await this.helper.rft.getTokenOwner(this.collectionId, tokenId, blockHashAt);3536 }353335373534 async getTokensByAddress(addressObj: ICrossAccountId) {3538 async getTokensByAddress(addressObj: ICrossAccountId) {3535 return await this.helper.rft.getTokensByAddress(this.collectionId, addressObj);3539 return await this.helper.rft.getTokensByAddress(this.collectionId, addressObj);3539 return await this.helper.rft.getTokenTop10Owners(this.collectionId, tokenId);3543 return await this.helper.rft.getTokenTop10Owners(this.collectionId, tokenId);3540 }3544 }35453546 async getTokenTopmostOwner(tokenId: number, blockHashAt?: string) {3547 return await this.helper.rft.getTokenTopmostOwner(this.collectionId, tokenId, blockHashAt);3548 }354135493542 async getTokenBalance(tokenId: number, addressObj: ICrossAccountId) {3550 async getTokenBalance(tokenId: number, addressObj: ICrossAccountId) {3543 return await this.helper.rft.getTokenBalance(this.collectionId, tokenId, addressObj);3551 return await this.helper.rft.getTokenBalance(this.collectionId, tokenId, addressObj);3610 return await this.helper.rft.setTokenPropertyPermissions(signer, this.collectionId, permissions);3618 return await this.helper.rft.setTokenPropertyPermissions(signer, this.collectionId, permissions);3611 }3619 }36203621 async nestToken(signer: TSigner, tokenId: number, toTokenObj: IToken) {3622 return await this.helper.rft.nestToken(signer, {collectionId: this.collectionId, tokenId}, toTokenObj);3623 }36243625 async unnestToken(signer: TSigner, tokenId: number, fromTokenObj: IToken, toAddressObj: ICrossAccountId) {3626 return await this.helper.rft.unnestToken(signer, {collectionId: this.collectionId, tokenId}, fromTokenObj, toAddressObj);3627 }361236283613 scheduleAt<T extends UniqueHelper>(3629 scheduleAt<T extends UniqueHelper>(3614 executionBlockNumber: number,3630 executionBlockNumber: number,3849 return await this.collection.getToken(this.tokenId, blockHashAt);3865 return await this.collection.getToken(this.tokenId, blockHashAt);3850 }3866 }38673868 async getOwner(blockHashAt?: string) {3869 return await this.collection.getTokenOwner(this.tokenId, blockHashAt);3870 }385138713852 async getTop10Owners() {3872 async getTop10Owners() {3853 return await this.collection.getTop10TokenOwners(this.tokenId);3873 return await this.collection.getTop10TokenOwners(this.tokenId);3854 }3874 }38753876 async getTopmostOwner(blockHashAt?: string) {3877 return await this.collection.getTokenTopmostOwner(this.tokenId, blockHashAt);3878 }38793880 async nest(signer: TSigner, toTokenObj: IToken) {3881 return await this.collection.nestToken(signer, this.tokenId, toTokenObj);3882 }38833884 async unnest(signer: TSigner, fromTokenObj: IToken, toAddressObj: ICrossAccountId) {3885 return await this.collection.unnestToken(signer, this.tokenId, fromTokenObj, toAddressObj);3886 }385538873856 async getBalance(addressObj: ICrossAccountId) {3888 async getBalance(addressObj: ICrossAccountId) {3857 return await this.collection.getTokenBalance(this.tokenId, addressObj);3889 return await this.collection.getTokenBalance(this.tokenId, addressObj);