git.delta.rocks / unique-network / refs/commits / 76975d1a4b93

difftreelog

add jsdoc

Maksandre2022-08-08parent: #47f1fe8.patch.diff
in: master

1 file changed

modifiedtests/src/util/playgrounds/unique.tsdiffbeforeafterboth
575575
576576
577class CollectionGroup extends HelperGroup {577class CollectionGroup extends HelperGroup {
578 /**578 /**
579 * Get number of blocks when sponsored transaction is available.579 * Get number of blocks when sponsored transaction is available.
580 *580 *
581 * @param collectionId ID of collection581 * @param collectionId ID of collection
582 * @param tokenId ID of token582 * @param tokenId ID of token
583 * @param addressObj 583 * @param addressObj address for which the sponsorship is checked
584 * @example await getTokenNextSponsored(1, 2, {Substrate: '5DfhbVfww7ThF8q6f3...'});
584 * @returns number of blocks or null if sponsorship hasn't been set585 * @returns number of blocks or null if sponsorship hasn't been set
585 */586 */
586 async getTokenNextSponsored(collectionId: number, tokenId: number, addressObj: ICrossAccountId): Promise<number | null> {587 async getTokenNextSponsored(collectionId: number, tokenId: number, addressObj: ICrossAccountId): Promise<number | null> {
587 return (await this.helper.callRpc('api.rpc.unique.nextSponsored', [collectionId, addressObj, tokenId])).toJSON();588 return (await this.helper.callRpc('api.rpc.unique.nextSponsored', [collectionId, addressObj, tokenId])).toJSON();
588 }589 }
589590
590 /**591 /**
591 * Get number of collection created on current chain.592 * Get the number of created collections.
592 * 593 *
593 * @returns number of created collections594 * @returns number of created collections
594 */595 */
595 async getTotalCount(): Promise<number> {596 async getTotalCount(): Promise<number> {
596 return (await this.helper.callRpc('api.rpc.unique.collectionStats')).created.toNumber();597 return (await this.helper.callRpc('api.rpc.unique.collectionStats')).created.toNumber();
597 }598 }
598599
599 /**600 /**
600 * Get information about the collection with additional data, including the number of tokens it contains, its administrators, the normalized address of the collection's owner, and decoded name and description.601 * Get information about the collection with additional data, including the number of tokens it contains, its administrators, the normalized address of the collection's owner, and decoded name and description.
601 * 602 *
602 * @param collectionId ID of collection603 * @param collectionId ID of collection
603 * @returns collection information object604 * @example await getData(2)
605 * @returns collection information object
604 */606 */
605 async getData(collectionId: number): Promise<{607 async getData(collectionId: number): Promise<{
606 id: number;608 id: number;
607 name: string;609 name: string;
630 return collectionData;632 return collectionData;
631 }633 }
632634
633 /**635 /**
634 * Get the normalized addresses of the collection's administrators.636 * Get the normalized addresses of the collection's administrators.
635 * 637 *
636 * @param collectionId ID of collection638 * @param collectionId ID of collection
637 * @returns array of administrators639 * @example await getAdmins(1)
640 * @returns array of administrators
638 */641 */
639 async getAdmins(collectionId: number): Promise<ICrossAccountId[]> {642 async getAdmins(collectionId: number): Promise<ICrossAccountId[]> {
640 const normalized = [];643 const normalized = [];
641 for(const admin of (await this.helper.callRpc('api.rpc.unique.adminlist', [collectionId])).toHuman()) {644 for(const admin of (await this.helper.callRpc('api.rpc.unique.adminlist', [collectionId])).toHuman()) {
645 return normalized;648 return normalized;
646 }649 }
647650
648 /**651 /**
649 * Get the effective limits of the collection instead of null for default values652 * Get the effective limits of the collection instead of null for default values
650 * 653 *
651 * @param collectionId ID of collection654 * @param collectionId ID of collection
652 * @returns object of collection limits655 * @example await getEffectiveLimits(2)
656 * @returns object of collection limits
653 */657 */
654 async getEffectiveLimits(collectionId: number): Promise<ICollectionLimits> {658 async getEffectiveLimits(collectionId: number): Promise<ICollectionLimits> {
655 return (await this.helper.callRpc('api.rpc.unique.effectiveCollectionLimits', [collectionId])).toJSON();659 return (await this.helper.callRpc('api.rpc.unique.effectiveCollectionLimits', [collectionId])).toJSON();
656 }660 }
657661
658 /**662 /**
659 * Burns the collection if the signer has sufficient permissions and collection is empty.663 * Burns the collection if the signer has sufficient permissions and collection is empty.
660 * 664 *
661 * @param signer keyring of signer665 * @param signer keyring of signer
662 * @param collectionId ID of collection666 * @param collectionId ID of collection
663 * @param label extra label for log667 * @param label extra label for log
664 * @returns bool true on success668 * @example await helper.collection.burn(aliceKeyring, 3);
669 * @returns bool true on success
665 */670 */
666 async burn(signer: TSigner, collectionId: number, label?: string): Promise<boolean> {671 async burn(signer: TSigner, collectionId: number, label?: string): Promise<boolean> {
667 if(typeof label === 'undefined') label = `collection #${collectionId}`;672 if(typeof label === 'undefined') label = `collection #${collectionId}`;
668 const result = await this.helper.executeExtrinsic(673 const result = await this.helper.executeExtrinsic(
674 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'CollectionDestroyed', label);679 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'CollectionDestroyed', label);
675 }680 }
676681
677 /**682 /**
678 * Sets the sponsor for the collection (Requires the Substrate address).683 * Sets the sponsor for the collection (Requires the Substrate address).
679 * 684 *
680 * @param signer keyring of signer685 * @param signer keyring of signer
681 * @param collectionId ID of collection686 * @param collectionId ID of collection
682 * @param sponsorAddress Sponsor substrate address687 * @param sponsorAddress Sponsor substrate address
683 * @param label extra label for log688 * @param label extra label for log
684 * @returns bool true on success689 * @example setSponsor(aliceKeyring, 10, "5DyN4Y92vZCjv38fg...")
690 * @returns bool true on success
685 */691 */
686 async setSponsor(signer: TSigner, collectionId: number, sponsorAddress: TSubstrateAccount, label?: string): Promise<boolean> {692 async setSponsor(signer: TSigner, collectionId: number, sponsorAddress: TSubstrateAccount, label?: string): Promise<boolean> {
687 if(typeof label === 'undefined') label = `collection #${collectionId}`;693 if(typeof label === 'undefined') label = `collection #${collectionId}`;
688 const result = await this.helper.executeExtrinsic(694 const result = await this.helper.executeExtrinsic(
694 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionSponsorSet', label);700 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionSponsorSet', label);
695 }701 }
696702
697 /**703 /**
698 * Confirms consent to sponsor the collection on behalf of the signer.704 * Confirms consent to sponsor the collection on behalf of the signer.
699 * 705 *
700 * @param signer keyring of signer706 * @param signer keyring of signer
701 * @param collectionId ID of collection707 * @param collectionId ID of collection
702 * @param label extra label for log708 * @param label extra label for log
703 * @returns bool true on success709 * @example confirmSponsorship(aliceKeyring, 10)
710 * @returns bool true on success
704 */711 */
705 async confirmSponsorship(signer: TSigner, collectionId: number, label?: string): Promise<boolean> {712 async confirmSponsorship(signer: TSigner, collectionId: number, label?: string): Promise<boolean> {
706 if(typeof label === 'undefined') label = `collection #${collectionId}`;713 if(typeof label === 'undefined') label = `collection #${collectionId}`;
707 const result = await this.helper.executeExtrinsic(714 const result = await this.helper.executeExtrinsic(
713 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'SponsorshipConfirmed', label);720 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'SponsorshipConfirmed', label);
714 }721 }
715722
716 /**723 /**
717 * Sets the limits of the collection. At least one limit must be specified for a correct call.724 * Sets the limits of the collection. At least one limit must be specified for a correct call.
718 * 725 *
719 * @param signer keyring of signer726 * @param signer keyring of signer
720 * @param collectionId ID of collection727 * @param collectionId ID of collection
721 * @param limits collection limits object728 * @param limits collection limits object
722 * @param label extra label for log729 * @param label extra label for log
723 * @returns bool true on success730 * @example
731 * await setLimits(
732 * aliceKeyring,
733 * 10,
734 * {
735 * sponsorTransferTimeout: 0,
736 * ownerCanDestroy: false
737 * }
738 * )
739 * @returns bool true on success
724 */740 */
725 async setLimits(signer: TSigner, collectionId: number, limits: ICollectionLimits, label?: string): Promise<boolean> {741 async setLimits(signer: TSigner, collectionId: number, limits: ICollectionLimits, label?: string): Promise<boolean> {
726 if(typeof label === 'undefined') label = `collection #${collectionId}`;742 if(typeof label === 'undefined') label = `collection #${collectionId}`;
727 const result = await this.helper.executeExtrinsic(743 const result = await this.helper.executeExtrinsic(
733 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionLimitSet', label);749 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionLimitSet', label);
734 }750 }
735751
736 /**752 /**
737 * Changes the owner of the collection to the new Substrate address.753 * Changes the owner of the collection to the new Substrate address.
738 * 754 *
739 * @param signer keyring of signer755 * @param signer keyring of signer
740 * @param collectionId ID of collection756 * @param collectionId ID of collection
741 * @param ownerAddress substrate address of new owner757 * @param ownerAddress substrate address of new owner
742 * @param label extra label for log758 * @param label extra label for log
743 * @returns bool true on success759 * @example changeOwner(aliceKeyring, 10, "5DyN4Y92vZCjv38fg...")
760 * @returns bool true on success
744 */761 */
745 async changeOwner(signer: TSigner, collectionId: number, ownerAddress: TSubstrateAccount, label?: string): Promise<boolean> {762 async changeOwner(signer: TSigner, collectionId: number, ownerAddress: TSubstrateAccount, label?: string): Promise<boolean> {
746 if(typeof label === 'undefined') label = `collection #${collectionId}`;763 if(typeof label === 'undefined') label = `collection #${collectionId}`;
747 const result = await this.helper.executeExtrinsic(764 const result = await this.helper.executeExtrinsic(
753 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionOwnedChanged', label);770 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionOwnedChanged', label);
754 }771 }
755772
756 /**773 /**
757 * Adds a collection administrator. 774 * Adds a collection administrator.
758 * 775 *
759 * @param signer keyring of signer776 * @param signer keyring of signer
760 * @param collectionId ID of collection777 * @param collectionId ID of collection
761 * @param adminAddressObj Administrator address (substrate or ethereum)778 * @param adminAddressObj Administrator address (substrate or ethereum)
762 * @param label extra label for log779 * @param label extra label for log
763 * @returns bool true on success780 * @example addAdmin(aliceKeyring, 10, {Substrate: "5DyN4Y92vZCjv38fg..."})
781 * @returns bool true on success
764 */782 */
765 async addAdmin(signer: TSigner, collectionId: number, adminAddressObj: ICrossAccountId, label?: string): Promise<boolean> {783 async addAdmin(signer: TSigner, collectionId: number, adminAddressObj: ICrossAccountId, label?: string): Promise<boolean> {
766 if(typeof label === 'undefined') label = `collection #${collectionId}`;784 if(typeof label === 'undefined') label = `collection #${collectionId}`;
767 const result = await this.helper.executeExtrinsic(785 const result = await this.helper.executeExtrinsic(
773 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionAdminAdded', label);791 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionAdminAdded', label);
774 }792 }
775793
776 /**794 /**
777 * Removes a collection administrator.795 * Removes a collection administrator.
778 * 796 *
779 * @param signer keyring of signer797 * @param signer keyring of signer
780 * @param collectionId ID of collection798 * @param collectionId ID of collection
781 * @param adminAddressObj Administrator address (substrate or ethereum)799 * @param adminAddressObj Administrator address (substrate or ethereum)
782 * @param label extra label for log800 * @param label extra label for log
783 * @returns bool true on success801 * @example removeAdmin(aliceKeyring, 10, {Substrate: "5DyN4Y92vZCjv38fg..."})
802 * @returns bool true on success
784 */803 */
785 async removeAdmin(signer: TSigner, collectionId: number, adminAddressObj: ICrossAccountId, label?: string): Promise<boolean> {804 async removeAdmin(signer: TSigner, collectionId: number, adminAddressObj: ICrossAccountId, label?: string): Promise<boolean> {
786 if(typeof label === 'undefined') label = `collection #${collectionId}`;805 if(typeof label === 'undefined') label = `collection #${collectionId}`;
787 const result = await this.helper.executeExtrinsic(806 const result = await this.helper.executeExtrinsic(
793 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionAdminRemoved', label);812 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionAdminRemoved', label);
794 }813 }
795814
796 /**815 /**
797 * Sets onchain permissions for selected collection.816 * Sets onchain permissions for selected collection.
798 * 817 *
799 * @param signer keyring of signer818 * @param signer keyring of signer
800 * @param collectionId ID of collection819 * @param collectionId ID of collection
801 * @param permissions collection permissions object820 * @param permissions collection permissions object
802 * @param label extra label for log821 * @param label extra label for log
803 * @returns bool true on success822 * @example setPermissions(aliceKeyring, 10, TODO);
823 * @returns bool true on success
804 */824 */
805 async setPermissions(signer: TSigner, collectionId: number, permissions: ICollectionPermissions, label?: string): Promise<boolean> {825 async setPermissions(signer: TSigner, collectionId: number, permissions: ICollectionPermissions, label?: string): Promise<boolean> {
806 if(typeof label === 'undefined') label = `collection #${collectionId}`;826 if(typeof label === 'undefined') label = `collection #${collectionId}`;
807 const result = await this.helper.executeExtrinsic(827 const result = await this.helper.executeExtrinsic(
813 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionPermissionSet', label);833 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionPermissionSet', label);
814 }834 }
815835
816 /**836 /**
817 * Enables nesting for selected collection. If `restricted` set, you can nest only tokens from specified collections.837 * Enables nesting for selected collection. If `restricted` set, you can nest only tokens from specified collections.
818 * 838 *
819 * @param signer keyring of signer839 * @param signer keyring of signer
820 * @param collectionId ID of collection840 * @param collectionId ID of collection
821 * @param permissions nesting permissions object841 * @param permissions nesting permissions object
822 * @param label extra label for log842 * @param label extra label for log
823 * @returns bool true on success843 * @example enableNesting(aliceKeyring, 10, TODO);
844 * @returns bool true on success
824 */845 */
825 async enableNesting(signer: TSigner, collectionId: number, permissions: INestingPermissions, label?: string): Promise<boolean> {846 async enableNesting(signer: TSigner, collectionId: number, permissions: INestingPermissions, label?: string): Promise<boolean> {
826 return await this.setPermissions(signer, collectionId, {nesting: permissions}, label);847 return await this.setPermissions(signer, collectionId, {nesting: permissions}, label);
827 }848 }
828849
829 /**850 /**
830 * Disables nesting for selected collection.851 * Disables nesting for selected collection.
831 * 852 *
832 * @param signer keyring of signer853 * @param signer keyring of signer
833 * @param collectionId ID of collection854 * @param collectionId ID of collection
834 * @param label extra label for log855 * @param label extra label for log
835 * @returns bool true on success856 * @example disableNesting(aliceKeyring, 10);
857 * @returns bool true on success
836 */858 */
837 async disableNesting(signer: TSigner, collectionId: number, label?: string): Promise<boolean> {859 async disableNesting(signer: TSigner, collectionId: number, label?: string): Promise<boolean> {
838 return await this.setPermissions(signer, collectionId, {nesting: {tokenOwner: false, collectionAdmin: false}}, label);860 return await this.setPermissions(signer, collectionId, {nesting: {tokenOwner: false, collectionAdmin: false}}, label);
839 }861 }
840862
841 /**863 /**
842 * Sets onchain properties to the collection.864 * Sets onchain properties to the collection.
843 * 865 *
844 * @param signer keyring of signer866 * @param signer keyring of signer
845 * @param collectionId ID of collection867 * @param collectionId ID of collection
846 * @param properties array of property objects868 * @param properties array of property objects
847 * @param label extra label for log869 * @param label extra label for log
848 * @returns bool true on success870 * @example
871 * setProperties(aliceKeyring, 10, TODO)
872 * @returns bool true on success
849 */873 */
850 async setProperties(signer: TSigner, collectionId: number, properties: IProperty[], label?: string): Promise<boolean> {874 async setProperties(signer: TSigner, collectionId: number, properties: IProperty[], label?: string): Promise<boolean> {
851 if(typeof label === 'undefined') label = `collection #${collectionId}`;875 if(typeof label === 'undefined') label = `collection #${collectionId}`;
852 const result = await this.helper.executeExtrinsic(876 const result = await this.helper.executeExtrinsic(
858 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'CollectionPropertySet', label);882 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'CollectionPropertySet', label);
859 }883 }
860884
861 /**885 /**
862 * Deletes onchain properties from the collection.886 * Deletes onchain properties from the collection.
863 * 887 *
864 * @param signer keyring of signer888 * @param signer keyring of signer
865 * @param collectionId ID of collection889 * @param collectionId ID of collection
866 * @param propertyKeys array of property keys to delete890 * @param propertyKeys array of property keys to delete
867 * @param label 891 * @param label
892 * @example deleteProperties(aliceKeyring, 10, TODO)
868 * @returns 893 * @returns
869 */894 */
870 async deleteProperties(signer: TSigner, collectionId: number, propertyKeys: string[], label?: string): Promise<boolean> {895 async deleteProperties(signer: TSigner, collectionId: number, propertyKeys: string[], label?: string): Promise<boolean> {
871 if(typeof label === 'undefined') label = `collection #${collectionId}`;896 if(typeof label === 'undefined') label = `collection #${collectionId}`;
872 const result = await this.helper.executeExtrinsic(897 const result = await this.helper.executeExtrinsic(
878 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'CollectionPropertyDeleted', label);903 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'CollectionPropertyDeleted', label);
879 }904 }
880905
906 /**
907 * Changes the owner of the token.
908 *
909 * @param signer keyring of signer
910 * @param collectionId ID of collection
911 * @param tokenId ID of token
912 * @param addressObj address of a new owner
913 * @param amount amount of tokens to be transfered. For NFT must be set to 1n
914 * @example transferToken(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."})
915 * @returns true if the token success, otherwise false
916 */
881 async transferToken(signer: TSigner, collectionId: number, tokenId: number, addressObj: ICrossAccountId, amount=1n): Promise<boolean> {917 async transferToken(signer: TSigner, collectionId: number, tokenId: number, addressObj: ICrossAccountId, amount=1n): Promise<boolean> {
882 const result = await this.helper.executeExtrinsic(918 const result = await this.helper.executeExtrinsic(
883 signer,919 signer,
888 return this.helper.util.isTokenTransferSuccess(result.result.events, collectionId, tokenId, {Substrate: typeof signer === 'string' ? signer : signer.address}, addressObj, amount);924 return this.helper.util.isTokenTransferSuccess(result.result.events, collectionId, tokenId, {Substrate: typeof signer === 'string' ? signer : signer.address}, addressObj, amount);
889 }925 }
890926
927 /**
928 *
929 * Change ownership of a NFT on behalf of the owner.
930 *
931 * @param signer keyring of signer
932 * @param collectionId ID of collection
933 * @param tokenId ID of token
934 * @param fromAddressObj address on behalf of which the token will be sent
935 * @param toAddressObj new token owner
936 * @param amount amount of tokens to be transfered. For NFT must be set to 1n
937 * @example transferTokenFrom(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg"}, {Ethereum: "0x9F0583DbB85..."})
938 * @returns true if the token success, otherwise false
939 */
891 async transferTokenFrom(signer: TSigner, collectionId: number, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount=1n): Promise<boolean> {940 async transferTokenFrom(signer: TSigner, collectionId: number, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount=1n): Promise<boolean> {
892 const result = await this.helper.executeExtrinsic(941 const result = await this.helper.executeExtrinsic(
893 signer,942 signer,
897 return this.helper.util.isTokenTransferSuccess(result.result.events, collectionId, tokenId, fromAddressObj, toAddressObj, amount);946 return this.helper.util.isTokenTransferSuccess(result.result.events, collectionId, tokenId, fromAddressObj, toAddressObj, amount);
898 }947 }
899948
949 /**
950 *
951 * Destroys a concrete instance of NFT.
952 *
953 * @param signer keyring of signer
954 * @param collectionId ID of collection
955 * @param tokenId ID of token
956 * @param label
957 * @param amount amount of tokens to be burned. For NFT must be set to 1n
958 * @example burnToken(aliceKeyring, 10, 5);
959 * @returns ```true``` and burnt token number is extrinsic success. Otherwise ```false``` and ```null```
960 */
900 async burnToken(signer: TSigner, collectionId: number, tokenId: number, label?: string, amount=1n): Promise<{961 async burnToken(signer: TSigner, collectionId: number, tokenId: number, label?: string, amount=1n): Promise<{
901 success: boolean,962 success: boolean,
902 token: number | null963 token: number | null
912 return {success: burnedTokens.success, token: burnedTokens.tokens.length > 0 ? burnedTokens.tokens[0] : null};973 return {success: burnedTokens.success, token: burnedTokens.tokens.length > 0 ? burnedTokens.tokens[0] : null};
913 }974 }
914975
976 /**
977 * Destroys a concrete instance of NFT on behalf of the owner
978 *
979 * @param signer keyring of signer
980 * @param collectionId ID of collection
981 * @param fromAddressObj address on behalf of which the token will be burnt
982 * @param tokenId ID of token
983 * @param label
984 * @param amount amount of tokens to be burned. For NFT must be set to 1n
985 * @example burnTokenFrom(aliceKeyring, 10, {Substrate: "5DyN4Y92vZCjv38fg..."}, 5, {Ethereum: "0x9F0583DbB85..."})
986 * @returns ```true``` if extrinsic success. Otherwise ```false```
987 */
915 async burnTokenFrom(signer: TSigner, collectionId: number, fromAddressObj: ICrossAccountId, tokenId: number, label?: string, amount=1n): Promise<boolean> {988 async burnTokenFrom(signer: TSigner, collectionId: number, fromAddressObj: ICrossAccountId, tokenId: number, label?: string, amount=1n): Promise<boolean> {
916 if(typeof label === 'undefined') label = `collection #${collectionId}`;989 if(typeof label === 'undefined') label = `collection #${collectionId}`;
917 const burnResult = await this.helper.executeExtrinsic(990 const burnResult = await this.helper.executeExtrinsic(
923 return burnedTokens.success && burnedTokens.tokens.length > 0;996 return burnedTokens.success && burnedTokens.tokens.length > 0;
924 }997 }
925998
999 /**
1000 * Set, change, or remove approved address to transfer the ownership of the NFT.
1001 *
1002 * @param signer keyring of signer
1003 * @param collectionId ID of collection
1004 * @param tokenId ID of token
1005 * @param toAddressObj
1006 * @param label
1007 * @param amount amount of token to be approved. For NFT must be set to 1n
1008 * @returns ```true``` if extrinsic success. Otherwise ```false```
1009 */
926 async approveToken(signer: IKeyringPair, collectionId: number, tokenId: number, toAddressObj: ICrossAccountId, label?: string, amount=1n) {1010 async approveToken(signer: IKeyringPair, collectionId: number, tokenId: number, toAddressObj: ICrossAccountId, label?: string, amount=1n) {
927 if(typeof label === 'undefined') label = `collection #${collectionId}`;1011 if(typeof label === 'undefined') label = `collection #${collectionId}`;
928 const approveResult = await this.helper.executeExtrinsic(1012 const approveResult = await this.helper.executeExtrinsic(
934 return this.helper.util.findCollectionInEvents(approveResult.result.events, collectionId, 'common', 'Approved', label);1018 return this.helper.util.findCollectionInEvents(approveResult.result.events, collectionId, 'common', 'Approved', label);
935 }1019 }
9361020
1021 /**
1022 * TODO
1023 * @param collectionId ID of collection
1024 * @param tokenId ID of token
1025 * @param toAccountObj
1026 * @param fromAccountObj
1027 * @example getTokenApprovedPieces(10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."}, {Substrate: "5ERZNF88Mm7UGfPP3mdG..."})
1028 * @returns number of approved to transfer pieces
1029 */
937 async getTokenApprovedPieces(collectionId: number, tokenId: number, toAccountObj: ICrossAccountId, fromAccountObj: ICrossAccountId): Promise<bigint> {1030 async getTokenApprovedPieces(collectionId: number, tokenId: number, toAccountObj: ICrossAccountId, fromAccountObj: ICrossAccountId): Promise<bigint> {
938 return (await this.helper.callRpc('api.rpc.unique.allowance', [collectionId, fromAccountObj, toAccountObj, tokenId])).toBigInt();1031 return (await this.helper.callRpc('api.rpc.unique.allowance', [collectionId, fromAccountObj, toAccountObj, tokenId])).toBigInt();
939 }1032 }
9401033
1034 /**
1035 * Get the last created token id
1036 * @param collectionId ID of collection
1037 * @example getLastTokenId(10);
1038 * @returns id of the last created token
1039 */
941 async getLastTokenId(collectionId: number): Promise<number> {1040 async getLastTokenId(collectionId: number): Promise<number> {
942 return (await this.helper.callRpc('api.rpc.unique.lastTokenId', [collectionId])).toNumber();1041 return (await this.helper.callRpc('api.rpc.unique.lastTokenId', [collectionId])).toNumber();
943 }1042 }
9441043
1044 /**
1045 * Check if token exists
1046 * @param collectionId ID of collection
1047 * @param tokenId ID of token
1048 * @example isTokenExists(10, 20);
1049 * @returns true if the token exists, otherwise false
1050 */
945 async isTokenExists(collectionId: number, tokenId: number): Promise<boolean> {1051 async isTokenExists(collectionId: number, tokenId: number): Promise<boolean> {
946 return (await this.helper.callRpc('api.rpc.unique.tokenExists', [collectionId, tokenId])).toJSON();1052 return (await this.helper.callRpc('api.rpc.unique.tokenExists', [collectionId, tokenId])).toJSON();
947 }1053 }
948}1054}
9491055
950class NFTnRFT extends CollectionGroup {1056class NFTnRFT extends CollectionGroup {
1057 /**
1058 * Get tokens owned by account
1059 *
1060 * @param collectionId ID of collection
1061 * @param addressObj tokens owner
1062 * @example getTokensByAddress(10, {Substrate: "5DyN4Y92vZCjv38fg..."})
1063 * @returns array of token ids owned by account TODO for RFT?
1064 */
951 async getTokensByAddress(collectionId: number, addressObj: ICrossAccountId): Promise<number[]> {1065 async getTokensByAddress(collectionId: number, addressObj: ICrossAccountId): Promise<number[]> {
952 return (await this.helper.callRpc('api.rpc.unique.accountTokens', [collectionId, addressObj])).toJSON();1066 return (await this.helper.callRpc('api.rpc.unique.accountTokens', [collectionId, addressObj])).toJSON();
953 }1067 }
9541068
1069 /**
1070 * Get token data
1071 * @param collectionId ID of collection
1072 * @param tokenId ID of token
1073 * @param blockHashAt
1074 * @param propertyKeys TODO
1075 * @example getToken(10, 5);
1076 * @returns human readable token data
1077 */
955 async getToken(collectionId: number, tokenId: number, blockHashAt?: string, propertyKeys?: string[]): Promise<{1078 async getToken(collectionId: number, tokenId: number, blockHashAt?: string, propertyKeys?: string[]): Promise<{
956 properties: IProperty[];1079 properties: IProperty[];
957 owner: ICrossAccountId;1080 owner: ICrossAccountId;
979 return tokenData;1102 return tokenData;
980 }1103 }
9811104
1105 /**
1106 * Set permissions to change token properties
1107 * @param signer keyring of signer
1108 * @param collectionId ID of collection
1109 * @param permissions permissions to change a property by the collection owner or admin
1110 * @param label
1111 * @example setTokenPropertyPermissions(
1112 * aliceKeyring, 10, [{key: "gender", permission: {tokenOwner: true, mutable: true, collectionAdmin: true}}]
1113 * )
1114 * @returns true if extrinsic success otherwise false
1115 */
982 async setTokenPropertyPermissions(signer: TSigner, collectionId: number, permissions: ITokenPropertyPermission[], label?: string): Promise<boolean> {1116 async setTokenPropertyPermissions(signer: TSigner, collectionId: number, permissions: ITokenPropertyPermission[], label?: string): Promise<boolean> {
983 if(typeof label === 'undefined') label = `collection #${collectionId}`;1117 if(typeof label === 'undefined') label = `collection #${collectionId}`;
984 const result = await this.helper.executeExtrinsic(1118 const result = await this.helper.executeExtrinsic(
990 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'PropertyPermissionSet', label);1124 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'PropertyPermissionSet', label);
991 }1125 }
9921126
1127 /**
1128 * Set token properties
1129 * @param signer keyring of signer
1130 * @param collectionId ID of collection
1131 * @param tokenId ID of token
1132 * @param properties
1133 * @param label
1134 * @example setTokenProperties(aliceKeyring, 10, 5, [{key: "gender", value: "female"}, {key: "age", value: "23"}])
1135 * @returns true if extrinsic success, otherwise false
1136 */
993 async setTokenProperties(signer: TSigner, collectionId: number, tokenId: number, properties: IProperty[], label?: string): Promise<boolean> {1137 async setTokenProperties(signer: TSigner, collectionId: number, tokenId: number, properties: IProperty[], label?: string): Promise<boolean> {
994 if(typeof label === 'undefined') label = `token #${tokenId} from collection #${collectionId}`;1138 if(typeof label === 'undefined') label = `token #${tokenId} from collection #${collectionId}`;
995 const result = await this.helper.executeExtrinsic(1139 const result = await this.helper.executeExtrinsic(
1001 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'TokenPropertySet', label);1145 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'TokenPropertySet', label);
1002 }1146 }
10031147
1148 /**
1149 * Delete the provided properties of a token
1150 * @param signer keyring of signer
1151 * @param collectionId ID of collection
1152 * @param tokenId ID of token
1153 * @param propertyKeys property keys to be deleted
1154 * @param label
1155 * @example deleteTokenProperties(aliceKeyring, 10, 5, ["gender", "age"])
1156 * @returns true if extrinsic success, otherwise false
1157 */
1004 async deleteTokenProperties(signer: TSigner, collectionId: number, tokenId: number, propertyKeys: string[], label?: string): Promise<boolean> {1158 async deleteTokenProperties(signer: TSigner, collectionId: number, tokenId: number, propertyKeys: string[], label?: string): Promise<boolean> {
1005 if(typeof label === 'undefined') label = `token #${tokenId} from collection #${collectionId}`;1159 if(typeof label === 'undefined') label = `token #${tokenId} from collection #${collectionId}`;
1006 const result = await this.helper.executeExtrinsic(1160 const result = await this.helper.executeExtrinsic(
1012 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'TokenPropertyDeleted', label);1166 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'TokenPropertyDeleted', label);
1013 }1167 }
10141168
1169 /**
1170 * Mint new collection
1171 * @param signer keyring of signer
1172 * @param collectionOptions TODO
1173 * @param mode NFT or RFT type of a collection
1174 * @param errorLabel
1175 * @example mintCollection(aliceKeyring, TODO, "NFT")
1176 * @returns object of the created collection
1177 */
1015 async mintCollection(signer: TSigner, collectionOptions: ICollectionCreationOptions, mode: 'NFT' | 'RFT', errorLabel = 'Unable to mint collection'): Promise<UniqueCollectionBase> {1178 async mintCollection(signer: TSigner, collectionOptions: ICollectionCreationOptions, mode: 'NFT' | 'RFT', errorLabel = 'Unable to mint collection'): Promise<UniqueCollectionBase> {
1016 collectionOptions = JSON.parse(JSON.stringify(collectionOptions)) as ICollectionCreationOptions; // Clone object1179 collectionOptions = JSON.parse(JSON.stringify(collectionOptions)) as ICollectionCreationOptions; // Clone object
1017 collectionOptions.mode = (mode === 'NFT') ? {nft: null} : {refungible: null};1180 collectionOptions.mode = (mode === 'NFT') ? {nft: null} : {refungible: null};
10371200
10381201
1039class NFTGroup extends NFTnRFT {1202class NFTGroup extends NFTnRFT {
1203 /**
1204 * Get collection object
1205 * @param collectionId ID of collection
1206 * @example getCollectionObject(2);
1207 * @returns instance of UniqueNFTCollection
1208 */
1040 getCollectionObject(collectionId: number): UniqueNFTCollection {1209 getCollectionObject(collectionId: number): UniqueNFTCollection {
1041 return new UniqueNFTCollection(collectionId, this.helper);1210 return new UniqueNFTCollection(collectionId, this.helper);
1042 }1211 }
10431212
1213 /**
1214 * Get token object
1215 * @param collectionId ID of collection
1216 * @param tokenId ID of token
1217 * @example getTokenObject(10, 5);
1218 * @returns instance of UniqueNFTToken
1219 */
1044 getTokenObject(collectionId: number, tokenId: number): UniqueNFTToken {1220 getTokenObject(collectionId: number, tokenId: number): UniqueNFTToken {
1045 return new UniqueNFTToken(tokenId, this.getCollectionObject(collectionId));1221 return new UniqueNFTToken(tokenId, this.getCollectionObject(collectionId));
1046 }1222 }
10471223
1224 /**
1225 * Get token's owner
1226 * @param collectionId ID of collection
1227 * @param tokenId ID of token
1228 * @param blockHashAt
1229 * @example getTokenOwner(10, 5);
1230 * @returns Address in CrossAccountId format, e.g. {Substrate: "5DnSF6RRjwteE3BrCj..."}
1231 */
1048 async getTokenOwner(collectionId: number, tokenId: number, blockHashAt?: string): Promise<ICrossAccountId> {1232 async getTokenOwner(collectionId: number, tokenId: number, blockHashAt?: string): Promise<ICrossAccountId> {
1049 let owner;1233 let owner;
1050 if (typeof blockHashAt === 'undefined') {1234 if (typeof blockHashAt === 'undefined') {
1055 return crossAccountIdFromLower(owner.toJSON());1239 return crossAccountIdFromLower(owner.toJSON());
1056 }1240 }
10571241
1242 /**
1243 * Is token approved to transfer
1244 * @param collectionId ID of collection
1245 * @param tokenId ID of token
1246 * @param toAccountObj TODO
1247 * @returns TODO
1248 */
1058 async isTokenApproved(collectionId: number, tokenId: number, toAccountObj: ICrossAccountId): Promise<boolean> {1249 async isTokenApproved(collectionId: number, tokenId: number, toAccountObj: ICrossAccountId): Promise<boolean> {
1059 return (await this.getTokenApprovedPieces(collectionId, tokenId, toAccountObj, await this.getTokenOwner(collectionId, tokenId))) === 1n;1250 return (await this.getTokenApprovedPieces(collectionId, tokenId, toAccountObj, await this.getTokenOwner(collectionId, tokenId))) === 1n;
1060 }1251 }
10611252
1253 /**
1254 * Changes the owner of the token.
1255 *
1256 * @param signer keyring of signer
1257 * @param collectionId ID of collection
1258 * @param tokenId ID of token
1259 * @param addressObj address of a new owner
1260 * @example transferToken(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."})
1261 * @returns true if extrinsic success, otherwise false
1262 */
1062 async transferToken(signer: TSigner, collectionId: number, tokenId: number, addressObj: ICrossAccountId): Promise<boolean> {1263 async transferToken(signer: TSigner, collectionId: number, tokenId: number, addressObj: ICrossAccountId): Promise<boolean> {
1063 return await super.transferToken(signer, collectionId, tokenId, addressObj, 1n);1264 return await super.transferToken(signer, collectionId, tokenId, addressObj, 1n);
1064 }1265 }
10651266
1267 /**
1268 *
1269 * Change ownership of a NFT on behalf of the owner.
1270 *
1271 * @param signer keyring of signer
1272 * @param collectionId ID of collection
1273 * @param tokenId ID of token
1274 * @param fromAddressObj address on behalf of which the token will be sent
1275 * @param toAddressObj new token owner
1276 * @example transferTokenFrom(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."}, {Ethereum: "0x9F0583DbB85..."})
1277 * @returns true if extrinsic success, otherwise false
1278 */
1066 async transferTokenFrom(signer: TSigner, collectionId: number, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId): Promise<boolean> {1279 async transferTokenFrom(signer: TSigner, collectionId: number, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId): Promise<boolean> {
1067 return await super.transferTokenFrom(signer, collectionId, tokenId, fromAddressObj, toAddressObj, 1n);1280 return await super.transferTokenFrom(signer, collectionId, tokenId, fromAddressObj, toAddressObj, 1n);
1068 }1281 }
10691282
1283 /**
1284 * Recursively find the address that owns the token
1285 * @param collectionId ID of collection
1286 * @param tokenId ID of token
1287 * @param blockHashAt
1288 * @example getTokenTopmostOwner(10, 5);
1289 * @returns address in CrossAccountId format, e.g. {Substrate: "5DyN4Y92vZCjv38fg..."}
1290 */
1070 async getTokenTopmostOwner(collectionId: number, tokenId: number, blockHashAt?: string): Promise<ICrossAccountId | null> {1291 async getTokenTopmostOwner(collectionId: number, tokenId: number, blockHashAt?: string): Promise<ICrossAccountId | null> {
1071 let owner;1292 let owner;
1072 if (typeof blockHashAt === 'undefined') {1293 if (typeof blockHashAt === 'undefined') {
1082 return owner.Substrate ? {Substrate: this.helper.address.normalizeSubstrate(owner.Substrate)} : owner;1303 return owner.Substrate ? {Substrate: this.helper.address.normalizeSubstrate(owner.Substrate)} : owner;
1083 }1304 }
10841305
1306 /**
1307 * Get tokens nested in the provided token
1308 * @param collectionId ID of collection
1309 * @param tokenId ID of token
1310 * @param blockHashAt
1311 * @example getTokenChildren(10, 5);
1312 * @returns tokens whose depth of nesting is <= 5
1313 */
1085 async getTokenChildren(collectionId: number, tokenId: number, blockHashAt?: string): Promise<IToken[]> {1314 async getTokenChildren(collectionId: number, tokenId: number, blockHashAt?: string): Promise<IToken[]> {
1086 let children;1315 let children;
1087 if(typeof blockHashAt === 'undefined') {1316 if(typeof blockHashAt === 'undefined') {
1095 });1324 });
1096 }1325 }
10971326
1327 /**
1328 * Nest one token into another
1329 * @param signer keyring of signer
1330 * @param tokenObj token to be nested
1331 * @param rootTokenObj token to be parent
1332 * @param label
1333 * @example nestToken(aliceKeyring, {collectionId: 10, tokenId: 5}, {collectionId: 10, tokenId: 4});
1334 * @returns true if extrinsic success, otherwise false
1335 */
1098 async nestToken(signer: TSigner, tokenObj: IToken, rootTokenObj: IToken, label='nest token'): Promise<boolean> {1336 async nestToken(signer: TSigner, tokenObj: IToken, rootTokenObj: IToken, label='nest token'): Promise<boolean> {
1099 const rootTokenAddress = {Ethereum: this.helper.util.getNestingTokenAddress(rootTokenObj.collectionId, rootTokenObj.tokenId)};1337 const rootTokenAddress = {Ethereum: this.helper.util.getNestingTokenAddress(rootTokenObj.collectionId, rootTokenObj.tokenId)};
1100 const result = await this.transferToken(signer, tokenObj.collectionId, tokenObj.tokenId, rootTokenAddress);1338 const result = await this.transferToken(signer, tokenObj.collectionId, tokenObj.tokenId, rootTokenAddress);
1104 return result;1342 return result;
1105 }1343 }
11061344
1345 /**
1346 * Remove token from nested state
1347 * @param signer keyring of signer
1348 * @param tokenObj token to unnest
1349 * @param rootTokenObj parent of a token
1350 * @param toAddressObj address of a new token owner
1351 * @param label
1352 * @example unnestToken(aliceKeyring, {collectionId: 10, tokenId: 5}, {collectionId: 10, tokenId: 4}, {Substrate: "5DyN4Y92vZCjv38fg..."});
1353 * @returns true if extrinsic success, otherwise false
1354 */
1107 async unnestToken(signer: TSigner, tokenObj: IToken, rootTokenObj: IToken, toAddressObj: ICrossAccountId, label='unnest token'): Promise<boolean> {1355 async unnestToken(signer: TSigner, tokenObj: IToken, rootTokenObj: IToken, toAddressObj: ICrossAccountId, label='unnest token'): Promise<boolean> {
1108 const rootTokenAddress = {Ethereum: this.helper.util.getNestingTokenAddress(rootTokenObj.collectionId, rootTokenObj.tokenId)};1356 const rootTokenAddress = {Ethereum: this.helper.util.getNestingTokenAddress(rootTokenObj.collectionId, rootTokenObj.tokenId)};
1109 const result = await this.transferTokenFrom(signer, tokenObj.collectionId, tokenObj.tokenId, rootTokenAddress, toAddressObj);1357 const result = await this.transferTokenFrom(signer, tokenObj.collectionId, tokenObj.tokenId, rootTokenAddress, toAddressObj);
1113 return result;1361 return result;
1114 }1362 }
11151363
1364 /**
1365 * Mint new collection
1366 * @param signer keyring of signer
1367 * @param collectionOptions Collection options
1368 * @param label
1369 * @example
1370 * mintCollection(aliceKeyring, {
1371 * name: 'New',
1372 * description: 'New collection',
1373 * tokenPrefix: 'NEW',
1374 * })
1375 * @returns object of the created collection
1376 */
1116 async mintCollection(signer: TSigner, collectionOptions: ICollectionCreationOptions, label = 'new collection'): Promise<UniqueNFTCollection> {1377 async mintCollection(signer: TSigner, collectionOptions: ICollectionCreationOptions, label = 'new collection'): Promise<UniqueNFTCollection> {
1117 return await super.mintCollection(signer, collectionOptions, 'NFT', `Unable to mint NFT collection for ${label}`) as UniqueNFTCollection;1378 return await super.mintCollection(signer, collectionOptions, 'NFT', `Unable to mint NFT collection for ${label}`) as UniqueNFTCollection;
1118 }1379 }
11191380
1381 /**
1382 * Mint new token
1383 * @param signer keyring of signer
1384 * @param data token data
1385 * @param label
1386 * @returns created token object
1387 */
1120 async mintToken(signer: TSigner, data: { collectionId: number; owner: ICrossAccountId | string; properties?: IProperty[]; }, label?: string): Promise<UniqueNFTToken> {1388 async mintToken(signer: TSigner, data: { collectionId: number; owner: ICrossAccountId | string; properties?: IProperty[]; }, label?: string): Promise<UniqueNFTToken> {
1121 if(typeof label === 'undefined') label = `collection #${data.collectionId}`;1389 if(typeof label === 'undefined') label = `collection #${data.collectionId}`;
1122 const creationResult = await this.helper.executeExtrinsic(1390 const creationResult = await this.helper.executeExtrinsic(
1134 return this.getTokenObject(data.collectionId, createdTokens.tokens[0].tokenId);1402 return this.getTokenObject(data.collectionId, createdTokens.tokens[0].tokenId);
1135 }1403 }
11361404
1405 /**
1406 * Mint multiple NFT tokens
1407 * @param signer keyring of signer
1408 * @param collectionId ID of collection
1409 * @param tokens array of tokens with owner and properties
1410 * @param label
1411 * @example
1412 * mintMultipleTokens(aliceKeyring, 10, [{
1413 * owner: {Substrate: "5DyN4Y92vZCjv38fg..."},
1414 * properties: [{key: "gender", value: "male"},{key: "age", value: "45"}],
1415 * },{
1416 * owner: {Ethereum: "0x9F0583DbB855d..."},
1417 * properties: [{key: "gender", value: "female"},{key: "age", value: "22"}],
1418 * }]);
1419 * @returns true if extrinsic success, otherwise false
1420 */
1137 async mintMultipleTokens(signer: TSigner, collectionId: number, tokens: {owner: ICrossAccountId, properties?: IProperty[]}[], label?: string): Promise<UniqueNFTToken[]> {1421 async mintMultipleTokens(signer: TSigner, collectionId: number, tokens: {owner: ICrossAccountId, properties?: IProperty[]}[], label?: string): Promise<UniqueNFTToken[]> {
1138 if(typeof label === 'undefined') label = `collection #${collectionId}`;1422 if(typeof label === 'undefined') label = `collection #${collectionId}`;
1139 const creationResult = await this.helper.executeExtrinsic(1423 const creationResult = await this.helper.executeExtrinsic(
1145 return this.helper.util.extractTokensFromCreationResult(creationResult, label).tokens.map((x: IToken) => collection.getTokenObject(x.tokenId));1429 return this.helper.util.extractTokensFromCreationResult(creationResult, label).tokens.map((x: IToken) => collection.getTokenObject(x.tokenId));
1146 }1430 }
11471431
1432 /**
1433 * Mint multiple NFT tokens with one owner
1434 * @param signer keyring of signer
1435 * @param collectionId ID of collection
1436 * @param owner tokens owner
1437 * @param tokens array of tokens with owner and properties
1438 * @param label
1439 * @example
1440 * mintMultipleTokensWithOneOwner(aliceKeyring, 10, "5DyN4Y92vZCjv38fg...", [{
1441 * properties: [{
1442 * key: "gender",
1443 * value: "female",
1444 * },{
1445 * key: "age",
1446 * value: "33",
1447 * }],
1448 * }]);
1449 * @returns array of newly created tokens
1450 */
1148 async mintMultipleTokensWithOneOwner(signer: TSigner, collectionId: number, owner: ICrossAccountId, tokens: {properties?: IProperty[]}[], label?: string): Promise<UniqueNFTToken[]> {1451 async mintMultipleTokensWithOneOwner(signer: TSigner, collectionId: number, owner: ICrossAccountId, tokens: {properties?: IProperty[]}[], label?: string): Promise<UniqueNFTToken[]> {
1149 if(typeof label === 'undefined') label = `collection #${collectionId}`;1452 if(typeof label === 'undefined') label = `collection #${collectionId}`;
1150 const rawTokens = [];1453 const rawTokens = [];
1161 return this.helper.util.extractTokensFromCreationResult(creationResult, label).tokens.map((x: IToken) => collection.getTokenObject(x.tokenId));1464 return this.helper.util.extractTokensFromCreationResult(creationResult, label).tokens.map((x: IToken) => collection.getTokenObject(x.tokenId));
1162 }1465 }
11631466
1467 /**
1468 * Destroys a concrete instance of NFT.
1469 * @param signer keyring of signer
1470 * @param collectionId ID of collection
1471 * @param tokenId ID of token
1472 * @param label
1473 * @example burnToken(aliceKeyring, 10, 5);
1474 * @returns ```true``` and burnt token number is extrinsic success. Otherwise ```false``` and ```null```
1475 */
1164 async burnToken(signer: IKeyringPair, collectionId: number, tokenId: number, label?: string): Promise<{ success: boolean; token: number | null; }> {1476 async burnToken(signer: IKeyringPair, collectionId: number, tokenId: number, label?: string): Promise<{ success: boolean; token: number | null; }> {
1165 return await super.burnToken(signer, collectionId, tokenId, label, 1n);1477 return await super.burnToken(signer, collectionId, tokenId, label, 1n);
1166 }1478 }
11671479
1480 /**
1481 * Set, change, or remove approved address to transfer the ownership of the NFT.
1482 *
1483 * @param signer keyring of signer
1484 * @param collectionId ID of collection
1485 * @param tokenId ID of token
1486 * @param toAddressObj address to approve
1487 * @param label
1488 * @example approveToken(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."})
1489 * @returns ```true``` if extrinsic success. Otherwise ```false```
1490 */
1168 async approveToken(signer: IKeyringPair, collectionId: number, tokenId: number, toAddressObj: ICrossAccountId, label?: string) {1491 async approveToken(signer: IKeyringPair, collectionId: number, tokenId: number, toAddressObj: ICrossAccountId, label?: string) {
1169 return super.approveToken(signer, collectionId, tokenId, toAddressObj, label, 1n);1492 return super.approveToken(signer, collectionId, tokenId, toAddressObj, label, 1n);
1170 }1493 }
1171}1494}
11721495
11731496
1174class RFTGroup extends NFTnRFT {1497class RFTGroup extends NFTnRFT {
1498 /**
1499 * Get collection object
1500 * @param collectionId ID of collection
1501 * @example getCollectionObject(2);
1502 * @returns instance of UniqueNFTCollection
1503 */
1175 getCollectionObject(collectionId: number): UniqueRFTCollection {1504 getCollectionObject(collectionId: number): UniqueRFTCollection {
1176 return new UniqueRFTCollection(collectionId, this.helper);1505 return new UniqueRFTCollection(collectionId, this.helper);
1177 }1506 }
11781507
1508 /**
1509 * Get token object
1510 * @param collectionId ID of collection
1511 * @param tokenId ID of token
1512 * @example getTokenObject(10, 5);
1513 * @returns instance of UniqueNFTToken
1514 */
1179 getTokenObject(collectionId: number, tokenId: number): UniqueRFTToken {1515 getTokenObject(collectionId: number, tokenId: number): UniqueRFTToken {
1180 return new UniqueRFTToken(tokenId, this.getCollectionObject(collectionId));1516 return new UniqueRFTToken(tokenId, this.getCollectionObject(collectionId));
1181 }1517 }
11821518
1519 /**
1520 * Get top 10 token owners with the largest number of pieces
1521 * @param collectionId ID of collection
1522 * @param tokenId ID of token
1523 * @example getTokenTop10Owners(10, 5);
1524 * @returns array of top 10 owners
1525 */
1183 async getTokenTop10Owners(collectionId: number, tokenId: number): Promise<ICrossAccountId[]> {1526 async getTokenTop10Owners(collectionId: number, tokenId: number): Promise<ICrossAccountId[]> {
1184 return (await this.helper.callRpc('api.rpc.unique.tokenOwners', [collectionId, tokenId])).toJSON().map(crossAccountIdFromLower);1527 return (await this.helper.callRpc('api.rpc.unique.tokenOwners', [collectionId, tokenId])).toJSON().map(crossAccountIdFromLower);
1185 }1528 }
11861529
1530 /**
1531 * Get number of pieces owned by address
1532 * @param collectionId ID of collection
1533 * @param tokenId ID of token
1534 * @param addressObj address token owner
1535 * @example getTokenBalance(10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."});
1536 * @returns number of pieces ownerd by address
1537 */
1187 async getTokenBalance(collectionId: number, tokenId: number, addressObj: ICrossAccountId): Promise<bigint> {1538 async getTokenBalance(collectionId: number, tokenId: number, addressObj: ICrossAccountId): Promise<bigint> {
1188 return (await this.helper.callRpc('api.rpc.unique.balance', [collectionId, addressObj, tokenId])).toBigInt();1539 return (await this.helper.callRpc('api.rpc.unique.balance', [collectionId, addressObj, tokenId])).toBigInt();
1189 }1540 }
11901541
1542 /**
1543 * Transfer pieces of token to another address
1544 * @param signer keyring of signer
1545 * @param collectionId ID of collection
1546 * @param tokenId ID of token
1547 * @param addressObj address of a new owner
1548 * @param amount number of pieces to be transfered
1549 * @example transferTokenFrom(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."}, 2000n)
1550 * @returns true if extrinsic success, otherwise false
1551 */
1191 async transferToken(signer: TSigner, collectionId: number, tokenId: number, addressObj: ICrossAccountId, amount=100n): Promise<boolean> {1552 async transferToken(signer: TSigner, collectionId: number, tokenId: number, addressObj: ICrossAccountId, amount=100n): Promise<boolean> {
1192 return await super.transferToken(signer, collectionId, tokenId, addressObj, amount);1553 return await super.transferToken(signer, collectionId, tokenId, addressObj, amount);
1193 }1554 }
11941555
1556 /**
1557 * Change ownership of some pieces of RFT on behalf of the owner.
1558 * @param signer keyring of signer
1559 * @param collectionId ID of collection
1560 * @param tokenId ID of token
1561 * @param fromAddressObj address on behalf of which the token will be sent
1562 * @param toAddressObj new token owner
1563 * @param amount number of pieces to be transfered
1564 * @example transferTokenFrom(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."}, {Substrate: "5DfhbVfww7ThF8q6f3i..."}, 2000n)
1565 * @returns true if extrinsic success, otherwise false
1566 */
1195 async transferTokenFrom(signer: TSigner, collectionId: number, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount=100n): Promise<boolean> {1567 async transferTokenFrom(signer: TSigner, collectionId: number, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount=100n): Promise<boolean> {
1196 return await super.transferTokenFrom(signer, collectionId, tokenId, fromAddressObj, toAddressObj, amount);1568 return await super.transferTokenFrom(signer, collectionId, tokenId, fromAddressObj, toAddressObj, amount);
1197 }1569 }
11981570
1571 /**
1572 * Mint new collection
1573 * @param signer keyring of signer
1574 * @param collectionOptions Collection options
1575 * @param label
1576 * @example
1577 * mintCollection(aliceKeyring, {
1578 * name: 'New',
1579 * description: 'New collection',
1580 * tokenPrefix: 'NEW',
1581 * })
1582 * @returns object of the created collection
1583 */
1199 async mintCollection(signer: TSigner, collectionOptions: ICollectionCreationOptions, label = 'new collection'): Promise<UniqueRFTCollection> {1584 async mintCollection(signer: TSigner, collectionOptions: ICollectionCreationOptions, label = 'new collection'): Promise<UniqueRFTCollection> {
1200 return await super.mintCollection(signer, collectionOptions, 'RFT', `Unable to mint RFT collection for ${label}`) as UniqueRFTCollection;1585 return await super.mintCollection(signer, collectionOptions, 'RFT', `Unable to mint RFT collection for ${label}`) as UniqueRFTCollection;
1201 }1586 }
12021587
1588 /**
1589 * Mint new token
1590 * @param signer keyring of signer
1591 * @param data token data
1592 * @param label
1593 * @example mintToken(aliceKeyring, {collectionId: 10, owner: {Substrate: '5GHoZe9c73RYbVzq...'}, pieces: 10000n});
1594 * @returns created token object
1595 */
1203 async mintToken(signer: TSigner, data: { collectionId: number; owner: ICrossAccountId | string; pieces: bigint; properties?: IProperty[]; }, label?: string): Promise<UniqueRFTToken> {1596 async mintToken(signer: TSigner, data: { collectionId: number; owner: ICrossAccountId | string; pieces: bigint; properties?: IProperty[]; }, label?: string): Promise<UniqueRFTToken> {
1204 if(typeof label === 'undefined') label = `collection #${data.collectionId}`;1597 if(typeof label === 'undefined') label = `collection #${data.collectionId}`;
1205 const creationResult = await this.helper.executeExtrinsic(1598 const creationResult = await this.helper.executeExtrinsic(
1230 return this.helper.util.extractTokensFromCreationResult(creationResult, label).tokens.map((x: IToken) => collection.getTokenObject(x.tokenId));1623 return this.helper.util.extractTokensFromCreationResult(creationResult, label).tokens.map((x: IToken) => collection.getTokenObject(x.tokenId));
1231 }1624 }
12321625
1626 /**
1627 * Mint multiple RFT tokens with one owner
1628 * @param signer keyring of signer
1629 * @param collectionId ID of collection
1630 * @param owner tokens owner
1631 * @param tokens array of tokens with properties and pieces
1632 * @param label
1633 * @example mintMultipleTokensWithOneOwner(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq..."}, [{pieces: 100000n, properties: [{key: "gender", value: "male"}]}]);
1634 * @returns array of newly created RFT tokens
1635 */
1233 async mintMultipleTokensWithOneOwner(signer: TSigner, collectionId: number, owner: ICrossAccountId, tokens: {pieces: bigint, properties?: IProperty[]}[], label?: string): Promise<UniqueRFTToken[]> {1636 async mintMultipleTokensWithOneOwner(signer: TSigner, collectionId: number, owner: ICrossAccountId, tokens: {pieces: bigint, properties?: IProperty[]}[], label?: string): Promise<UniqueRFTToken[]> {
1234 if(typeof label === 'undefined') label = `collection #${collectionId}`;1637 if(typeof label === 'undefined') label = `collection #${collectionId}`;
1235 const rawTokens = [];1638 const rawTokens = [];
1246 return this.helper.util.extractTokensFromCreationResult(creationResult, label).tokens.map((x: IToken) => collection.getTokenObject(x.tokenId));1649 return this.helper.util.extractTokensFromCreationResult(creationResult, label).tokens.map((x: IToken) => collection.getTokenObject(x.tokenId));
1247 }1650 }
12481651
1652 /**
1653 * Destroys a concrete instance of RFT.
1654 * @param signer keyring of signer
1655 * @param collectionId ID of collection
1656 * @param tokenId ID of token
1657 * @param label
1658 * @param amount number of pieces to be burnt
1659 * @example burnToken(aliceKeyring, 10, 5);
1660 * @returns ```true``` and burnt token number is extrinsic success. Otherwise ```false``` and ```null```
1661 */
1249 async burnToken(signer: IKeyringPair, collectionId: number, tokenId: number, label?: string, amount=100n): Promise<{ success: boolean; token: number | null; }> {1662 async burnToken(signer: IKeyringPair, collectionId: number, tokenId: number, label?: string, amount=100n): Promise<{ success: boolean; token: number | null; }> {
1250 return await super.burnToken(signer, collectionId, tokenId, label, amount);1663 return await super.burnToken(signer, collectionId, tokenId, label, amount);
1251 }1664 }
12521665
1666 /**
1667 * Set, change, or remove approved address to transfer the ownership of the RFT.
1668 *
1669 * @param signer keyring of signer
1670 * @param collectionId ID of collection
1671 * @param tokenId ID of token
1672 * @param toAddressObj address to approve
1673 * @param label
1674 * @param amount number of pieces to be approved
1675 * @example approveToken(aliceKeyring, 10, 5, {Substrate: "5GHoZe9c73RYbVzq..."}, "", 10000n);
1676 * @returns true if the token success, otherwise false
1677 */
1253 async approveToken(signer: IKeyringPair, collectionId: number, tokenId: number, toAddressObj: ICrossAccountId, label?: string, amount=100n) {1678 async approveToken(signer: IKeyringPair, collectionId: number, tokenId: number, toAddressObj: ICrossAccountId, label?: string, amount=100n) {
1254 return super.approveToken(signer, collectionId, tokenId, toAddressObj, label, amount);1679 return super.approveToken(signer, collectionId, tokenId, toAddressObj, label, amount);
1255 }1680 }
12561681
1682 /**
1683 * Get total number of pieces
1684 * @param collectionId ID of collection
1685 * @param tokenId ID of token
1686 * @example getTokenTotalPieces(10, 5);
1687 * @returns number of pieces
1688 */
1257 async getTokenTotalPieces(collectionId: number, tokenId: number): Promise<bigint> {1689 async getTokenTotalPieces(collectionId: number, tokenId: number): Promise<bigint> {
1258 return (await this.helper.callRpc('api.rpc.unique.totalPieces', [collectionId, tokenId])).unwrap().toBigInt();1690 return (await this.helper.callRpc('api.rpc.unique.totalPieces', [collectionId, tokenId])).unwrap().toBigInt();
1259 }1691 }
12601692
1693 /**
1694 * Change number of token pieces. Signer must be the owner of all token pieces.
1695 * @param signer keyring of signer
1696 * @param collectionId ID of collection
1697 * @param tokenId ID of token
1698 * @param amount new number of pieces
1699 * @param label
1700 * @example repartitionToken(aliceKeyring, 10, 5, 12345n);
1701 * @returns true if the repartion was success, otherwise false
1702 */
1261 async repartitionToken(signer: TSigner, collectionId: number, tokenId: number, amount: bigint, label?: string): Promise<boolean> {1703 async repartitionToken(signer: TSigner, collectionId: number, tokenId: number, amount: bigint, label?: string): Promise<boolean> {
1262 if(typeof label === 'undefined') label = `collection #${collectionId}`;1704 if(typeof label === 'undefined') label = `collection #${collectionId}`;
1263 const currentAmount = await this.getTokenTotalPieces(collectionId, tokenId);1705 const currentAmount = await this.getTokenTotalPieces(collectionId, tokenId);
12731715
12741716
1275class FTGroup extends CollectionGroup {1717class FTGroup extends CollectionGroup {
1718 /**
1719 * Get collection object
1720 * @param collectionId ID of collection
1721 * @example getCollectionObject(2);
1722 * @returns instance of UniqueNFTCollection
1723 */
1276 getCollectionObject(collectionId: number): UniqueFTCollection {1724 getCollectionObject(collectionId: number): UniqueFTCollection {
1277 return new UniqueFTCollection(collectionId, this.helper);1725 return new UniqueFTCollection(collectionId, this.helper);
1278 }1726 }
12791727
1728 /**
1729 * Mint new fungible collection
1730 * @param signer keyring of signer
1731 * @param collectionOptions Collection options
1732 * @param decimalPoints number of token decimals
1733 * @param errorLabel
1734 * @example
1735 * mintCollection(aliceKeyring, {
1736 * name: 'New',
1737 * description: 'New collection',
1738 * tokenPrefix: 'NEW',
1739 * }, 18)
1740 * @returns newly created fungible collection
1741 */
1280 async mintCollection(signer: TSigner, collectionOptions: ICollectionCreationOptions, decimalPoints = 0, errorLabel = 'Unable to mint collection'): Promise<UniqueFTCollection> {1742 async mintCollection(signer: TSigner, collectionOptions: ICollectionCreationOptions, decimalPoints = 0, errorLabel = 'Unable to mint collection'): Promise<UniqueFTCollection> {
1281 collectionOptions = JSON.parse(JSON.stringify(collectionOptions)) as ICollectionCreationOptions; // Clone object1743 collectionOptions = JSON.parse(JSON.stringify(collectionOptions)) as ICollectionCreationOptions; // Clone object
1282 if(collectionOptions.tokenPropertyPermissions) throw Error('Fungible collections has no tokenPropertyPermissions');1744 if(collectionOptions.tokenPropertyPermissions) throw Error('Fungible collections has no tokenPropertyPermissions');
1292 return this.getCollectionObject(this.helper.util.extractCollectionIdFromCreationResult(creationResult, errorLabel));1754 return this.getCollectionObject(this.helper.util.extractCollectionIdFromCreationResult(creationResult, errorLabel));
1293 }1755 }
12941756
1757 /**
1758 * Mint tokens
1759 * @param signer keyring of signer
1760 * @param collectionId ID of collection
1761 * @param owner address owner of new tokens
1762 * @param amount amount of tokens to be meanted
1763 * @param label
1764 * @example mintTokens(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq"}, 1000n);
1765 * @returns ```true``` if extrinsic success. Otherwise ```false```
1766 */
1295 async mintTokens(signer: TSigner, collectionId: number, owner: ICrossAccountId | string, amount: bigint, label?: string): Promise<boolean> {1767 async mintTokens(signer: TSigner, collectionId: number, owner: ICrossAccountId | string, amount: bigint, label?: string): Promise<boolean> {
1296 if(typeof label === 'undefined') label = `collection #${collectionId}`;1768 if(typeof label === 'undefined') label = `collection #${collectionId}`;
1297 const creationResult = await this.helper.executeExtrinsic(1769 const creationResult = await this.helper.executeExtrinsic(
1306 return this.helper.util.findCollectionInEvents(creationResult.result.events, collectionId, 'common', 'ItemCreated', label);1778 return this.helper.util.findCollectionInEvents(creationResult.result.events, collectionId, 'common', 'ItemCreated', label);
1307 }1779 }
13081780
1781 /**
1782 * Mint multiple RFT tokens with one owner
1783 * @param signer keyring of signer
1784 * @param collectionId ID of collection
1785 * @param owner tokens owner
1786 * @param tokens array of tokens with properties and pieces
1787 * @param label
1788 * @example mintMultipleTokensWithOneOwner(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq..."}, [{pieces: 100000n, properties: [{key: "gender", value: "male"}]}]);
1789 * @returns array of newly created RFT tokens
1790 */
1791
1792 /**
1793 * Mint multiple Fungible tokens with one owner TODO For what??
1794 * @param signer keyring of signer
1795 * @param collectionId ID of collection
1796 * @param owner tokens owner
1797 * @param tokens array of tokens with properties and pieces
1798 * @param label
1799 * @returns
1800 */
1309 async mintMultipleTokensWithOneOwner(signer: TSigner, collectionId: number, owner: ICrossAccountId, tokens: {value: bigint}[], label?: string): Promise<boolean> {1801 async mintMultipleTokensWithOneOwner(signer: TSigner, collectionId: number, owner: ICrossAccountId, tokens: {value: bigint}[], label?: string): Promise<boolean> {
1310 if(typeof label === 'undefined') label = `collection #${collectionId}`;1802 if(typeof label === 'undefined') label = `collection #${collectionId}`;
1311 const rawTokens = [];1803 const rawTokens = [];
1321 return this.helper.util.findCollectionInEvents(creationResult.result.events, collectionId, 'common', 'ItemCreated', label);1813 return this.helper.util.findCollectionInEvents(creationResult.result.events, collectionId, 'common', 'ItemCreated', label);
1322 }1814 }
13231815
1816 /**
1817 * Get top 10 token owners
1818 * @param collectionId ID of collection
1819 * @example getTop10Owners(10);
1820 * @returns array of ```ICrossAccountId```
1821 */
1324 async getTop10Owners(collectionId: number): Promise<ICrossAccountId[]> {1822 async getTop10Owners(collectionId: number): Promise<ICrossAccountId[]> {
1325 return (await this.helper.callRpc('api.rpc.unique.tokenOwners', [collectionId, 0])).toJSON().map(crossAccountIdFromLower);1823 return (await this.helper.callRpc('api.rpc.unique.tokenOwners', [collectionId, 0])).toJSON().map(crossAccountIdFromLower);
1326 }1824 }
13271825
1826 /**
1827 * Get account balance
1828 * @param collectionId ID of collection
1829 * @param addressObj address of owner
1830 * @example getBalance(10, {Substrate: "5GHoZe9c73RYbVzq..."})
1831 * @returns amount of fungible tokens owned by address
1832 */
1328 async getBalance(collectionId: number, addressObj: ICrossAccountId): Promise<bigint> {1833 async getBalance(collectionId: number, addressObj: ICrossAccountId): Promise<bigint> {
1329 return (await this.helper.callRpc('api.rpc.unique.balance', [collectionId, addressObj, 0])).toBigInt();1834 return (await this.helper.callRpc('api.rpc.unique.balance', [collectionId, addressObj, 0])).toBigInt();
1330 }1835 }
13311836
1837 /**
1838 * Transfer tokens to address
1839 * @param signer keyring of signer
1840 * @param collectionId ID of collection
1841 * @param toAddressObj address recepient
1842 * @param amount amount of tokens to be sent
1843 * @example transfer(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq..."}, 1000n);
1844 * @returns ```true``` if extrinsic success. Otherwise ```false```
1845 */
1332 async transfer(signer: TSigner, collectionId: number, toAddressObj: ICrossAccountId, amount: bigint) {1846 async transfer(signer: TSigner, collectionId: number, toAddressObj: ICrossAccountId, amount: bigint) {
1333 return await super.transferToken(signer, collectionId, 0, toAddressObj, amount);1847 return await super.transferToken(signer, collectionId, 0, toAddressObj, amount);
1334 }1848 }
13351849
1850 /**
1851 * Transfer some tokens on behalf of the owner.
1852 * @param signer keyring of signer
1853 * @param collectionId ID of collection
1854 * @param fromAddressObj address on behalf of which tokens will be sent
1855 * @param toAddressObj address where token to be sent
1856 * @param amount number of tokens to be sent
1857 * @example transferFrom(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq..."}, {Substrate: "5DfhbVfww7ThF8q6f3ij..."}, 10000n);
1858 * @returns ```true``` if extrinsic success. Otherwise ```false```
1859 */
1336 async transferFrom(signer: TSigner, collectionId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount: bigint) {1860 async transferFrom(signer: TSigner, collectionId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount: bigint) {
1337 return await super.transferTokenFrom(signer, collectionId, 0, fromAddressObj, toAddressObj, amount);1861 return await super.transferTokenFrom(signer, collectionId, 0, fromAddressObj, toAddressObj, amount);
1338 }1862 }
13391863
1864 /**
1865 * Destroy some amount of tokens
1866 * @param signer keyring of signer
1867 * @param collectionId ID of collection
1868 * @param amount amount of tokens to be destroyed
1869 * @param label
1870 * @example burnTokens(aliceKeyring, 10, 1000n);
1871 * @returns ```true``` if extrinsic success. Otherwise ```false```
1872 */
1340 async burnTokens(signer: IKeyringPair, collectionId: number, amount=100n, label?: string): Promise<boolean> {1873 async burnTokens(signer: IKeyringPair, collectionId: number, amount=100n, label?: string): Promise<boolean> {
1341 return (await super.burnToken(signer, collectionId, 0, label, amount)).success;1874 return (await super.burnToken(signer, collectionId, 0, label, amount)).success;
1342 }1875 }
13431876
1877 /**
1878 * Burn some tokens on behalf of the owner.
1879 * @param signer keyring of signer
1880 * @param collectionId ID of collection
1881 * @param fromAddressObj address on behalf of which tokens will be burnt
1882 * @param amount amount of tokens to be burnt
1883 * @param label
1884 * @example burnTokensFrom(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq..."}, 1000n);
1885 * @returns ```true``` if extrinsic success. Otherwise ```false```
1886 */
1344 async burnTokensFrom(signer: IKeyringPair, collectionId: number, fromAddressObj: ICrossAccountId, amount=100n, label?: string): Promise<boolean> {1887 async burnTokensFrom(signer: IKeyringPair, collectionId: number, fromAddressObj: ICrossAccountId, amount=100n, label?: string): Promise<boolean> {
1345 return await super.burnTokenFrom(signer, collectionId, fromAddressObj, 0, label, amount);1888 return await super.burnTokenFrom(signer, collectionId, fromAddressObj, 0, label, amount);
1346 }1889 }
13471890
1891 /**
1892 * TODO for what?
1893 * @param collectionId
1894 * @returns
1895 */
1348 async getTotalPieces(collectionId: number): Promise<bigint> {1896 async getTotalPieces(collectionId: number): Promise<bigint> {
1349 return (await this.helper.callRpc('api.rpc.unique.totalPieces', [collectionId, 0])).unwrap().toBigInt();1897 return (await this.helper.callRpc('api.rpc.unique.totalPieces', [collectionId, 0])).unwrap().toBigInt();
1350 }1898 }
13511899
1900 /**
1901 * Set, change, or remove approved address to transfer tokens.
1902 *
1903 * @param signer keyring of signer
1904 * @param collectionId ID of collection
1905 * @param toAddressObj address to be approved
1906 * @param amount amount of tokens to be approved
1907 * @param label
1908 * @example approveTokens(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq..."}, 1000n)
1909 * @returns ```true``` if extrinsic success. Otherwise ```false```
1910 */
1352 async approveTokens(signer: IKeyringPair, collectionId: number, toAddressObj: ICrossAccountId, amount=100n, label?: string) {1911 async approveTokens(signer: IKeyringPair, collectionId: number, toAddressObj: ICrossAccountId, amount=100n, label?: string) {
1353 return super.approveToken(signer, collectionId, 0, toAddressObj, label, amount);1912 return super.approveToken(signer, collectionId, 0, toAddressObj, label, amount);
1354 }1913 }
13551914
1915 /**
1916 * TODO why pieces??
1917 * @param collectionId
1918 * @param fromAddressObj
1919 * @param toAddressObj
1920 * @returns
1921 */
1356 async getApprovedTokens(collectionId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId) {1922 async getApprovedTokens(collectionId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId) {
1357 return super.getTokenApprovedPieces(collectionId, 0, toAddressObj, fromAddressObj);1923 return super.getTokenApprovedPieces(collectionId, 0, toAddressObj, fromAddressObj);
1358 }1924 }
1359}1925}
13601926
13611927
1362class ChainGroup extends HelperGroup {1928class ChainGroup extends HelperGroup {
1929 /**
1930 * Get system properties of a chain
1931 * @example getChainProperties();
1932 * @returns ss58Format, token decimals, and token symbol
1933 */
1363 getChainProperties(): IChainProperties {1934 getChainProperties(): IChainProperties {
1364 const properties = (this.helper.api as any).registry.getChainProperties().toJSON();1935 const properties = (this.helper.api as any).registry.getChainProperties().toJSON();
1365 return {1936 return {
1369 };1940 };
1370 }1941 }
13711942
1943 /**
1944 * Get chain header
1945 * @example getLatestBlockNumber();
1946 * @returns the number of the last block
1947 */
1372 async getLatestBlockNumber(): Promise<number> {1948 async getLatestBlockNumber(): Promise<number> {
1373 return (await this.helper.callRpc('api.rpc.chain.getHeader')).number.toNumber();1949 return (await this.helper.callRpc('api.rpc.chain.getHeader')).number.toNumber();
1374 }1950 }
13751951
1952 /**
1953 * Get block hash by block number
1954 * @param blockNumber number of block
1955 * @example getBlockHashByNumber(12345);
1956 * @returns hash of a block
1957 */
1376 async getBlockHashByNumber(blockNumber: number): Promise<string | null> {1958 async getBlockHashByNumber(blockNumber: number): Promise<string | null> {
1377 const blockHash = (await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockNumber])).toJSON();1959 const blockHash = (await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockNumber])).toJSON();
1378 if(blockHash === '0x0000000000000000000000000000000000000000000000000000000000000000') return null;1960 if(blockHash === '0x0000000000000000000000000000000000000000000000000000000000000000') return null;
1379 return blockHash;1961 return blockHash;
1380 }1962 }
13811963
1964 /**
1965 * Get account nonce
1966 * @param address substrate address
1967 * @example getNonce("5GrwvaEF5zXb26Fz...");
1968 * @returns number, account's nonce
1969 */
1382 async getNonce(address: TSubstrateAccount): Promise<number> {1970 async getNonce(address: TSubstrateAccount): Promise<number> {
1383 return (await (this.helper.api as any).query.system.account(address)).nonce.toNumber();1971 return (await (this.helper.api as any).query.system.account(address)).nonce.toNumber();
1384 }1972 }
1391 return 10n ** BigInt((chainProperties.tokenDecimals || [18])[0]);1979 return 10n ** BigInt((chainProperties.tokenDecimals || [18])[0]);
1392 }1980 }
13931981
1982 /**
1983 * Get substrate address balance
1984 * @param address substrate address
1985 * @example getSubstrate("5GrwvaEF5zXb26Fz...")
1986 * @returns amount of tokens on address
1987 */
1394 async getSubstrate(address: TSubstrateAccount): Promise<bigint> {1988 async getSubstrate(address: TSubstrateAccount): Promise<bigint> {
1395 return (await this.helper.callRpc('api.query.system.account', [address])).data.free.toBigInt();1989 return (await this.helper.callRpc('api.query.system.account', [address])).data.free.toBigInt();
1396 }1990 }
13971991
1992 /**
1993 * Get ethereum address balance
1994 * @param address ethereum address
1995 * @example getEthereum("0x9F0583DbB855d...")
1996 * @returns amount of tokens on address
1997 */
1398 async getEthereum(address: TEthereumAccount): Promise<bigint> {1998 async getEthereum(address: TEthereumAccount): Promise<bigint> {
1399 return (await this.helper.callRpc('api.rpc.eth.getBalance', [address])).toBigInt();1999 return (await this.helper.callRpc('api.rpc.eth.getBalance', [address])).toBigInt();
1400 }2000 }
14012001
2002 /**
2003 * Transfer tokens to substrate address
2004 * @param signer keyring of signer
2005 * @param address substrate address of a recepient
2006 * @param amount amount of tokens to be transfered
2007 * @example transferToSubstrate(aliceKeyring, "5GrwvaEF5zXb26Fz...", 100_000_000_000n);
2008 * @returns true if extrinsic success, otherwise false
2009 */
1402 async transferToSubstrate(signer: TSigner, address: TSubstrateAccount, amount: bigint | string): Promise<boolean> {2010 async transferToSubstrate(signer: TSigner, address: TSubstrateAccount, amount: bigint | string): Promise<boolean> {
1403 const result = await this.helper.executeExtrinsic(signer, 'api.tx.balances.transfer', [address, amount], true, `Unable to transfer balance from ${this.helper.getSignerAddress(signer)} to ${address}`);2011 const result = await this.helper.executeExtrinsic(signer, 'api.tx.balances.transfer', [address, amount], true, `Unable to transfer balance from ${this.helper.getSignerAddress(signer)} to ${address}`);
14042012
1425 return this.helper.util.normalizeSubstrateAddress(address, ss58Format);2033 return this.helper.util.normalizeSubstrateAddress(address, ss58Format);
1426 }2034 }
14272035
2036 /**
2037 * Get address in the connected chain format
2038 * @param address substrate address
2039 * @example normalizeSubstrateToChainFormat("5GrwvaEF5zXb26Fz...") // returns unjKJQJrRd238pkUZZ... for Unique Network
2040 * @returns address in chain format
2041 */
1428 async normalizeSubstrateToChainFormat(address: TSubstrateAccount): Promise<TSubstrateAccount> {2042 async normalizeSubstrateToChainFormat(address: TSubstrateAccount): Promise<TSubstrateAccount> {
1429 const info = this.helper.chain.getChainProperties();2043 const info = this.helper.chain.getChainProperties();
1430 return encodeAddress(decodeAddress(address), info.ss58Format);2044 return encodeAddress(decodeAddress(address), info.ss58Format);
1431 }2045 }
14322046
2047 /**
2048 * Get substrate mirror of an ethereum address
2049 * @param ethAddress ethereum address
2050 * @param toChainFormat false for normalized account
2051 * @example ethToSubstrate('0x9F0583DbB855d...')
2052 * @returns substrate mirror of a provided ethereum address
2053 */
1433 async ethToSubstrate(ethAddress: TEthereumAccount, toChainFormat=false): Promise<TSubstrateAccount> {2054 async ethToSubstrate(ethAddress: TEthereumAccount, toChainFormat=false): Promise<TSubstrateAccount> {
1434 if(!toChainFormat) return evmToAddress(ethAddress);2055 if(!toChainFormat) return evmToAddress(ethAddress);
1435 const info = this.helper.chain.getChainProperties();2056 const info = this.helper.chain.getChainProperties();
1436 return evmToAddress(ethAddress, info.ss58Format);2057 return evmToAddress(ethAddress, info.ss58Format);
1437 }2058 }
14382059
2060 /**
2061 * Get ethereum mirror of a substrate address
2062 * @param subAddress substrate account
2063 * @example substrateToEth("5DnSF6RRjwteE3BrC...")
2064 * @returns ethereum mirror of a provided substrate address
2065 */
1439 substrateToEth(subAddress: TSubstrateAccount): TEthereumAccount {2066 substrateToEth(subAddress: TSubstrateAccount): TEthereumAccount {
1440 return nesting.toChecksumAddress('0x' + Array.from(addressToEvm(subAddress), i => i.toString(16).padStart(2, '0')).join(''));2067 return nesting.toChecksumAddress('0x' + Array.from(addressToEvm(subAddress), i => i.toString(16).padStart(2, '0')).join(''));
1441 }2068 }