difftreelog
add jsdoc
in: master
1 file changed
tests/src/util/playgrounds/unique.tsdiffbeforeafterboth575575576576577class 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 collection582 * @param tokenId ID of token582 * @param tokenId ID of token583 * @param addressObj 583 * @param addressObj address for which the sponsorship is checked584 * @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 set585 */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 }589590590 /**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 collections594 */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 }598599599 /**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 collection603 * @returns collection information object604 * @example await getData(2)605 * @returns collection information object604 */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 }632634633 /**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 collection637 * @returns array of administrators639 * @example await getAdmins(1)640 * @returns array of administrators638 */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 }647650648 /**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 values650 * 653 * 651 * @param collectionId ID of collection654 * @param collectionId ID of collection652 * @returns object of collection limits655 * @example await getEffectiveLimits(2)656 * @returns object of collection limits653 */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 }657661658 /**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 signer662 * @param collectionId ID of collection666 * @param collectionId ID of collection663 * @param label extra label for log667 * @param label extra label for log664 * @returns bool true on success668 * @example await helper.collection.burn(aliceKeyring, 3);669 * @returns bool true on success665 */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 }676681677 /**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 signer681 * @param collectionId ID of collection686 * @param collectionId ID of collection682 * @param sponsorAddress Sponsor substrate address687 * @param sponsorAddress Sponsor substrate address683 * @param label extra label for log688 * @param label extra label for log684 * @returns bool true on success689 * @example setSponsor(aliceKeyring, 10, "5DyN4Y92vZCjv38fg...")690 * @returns bool true on success685 */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 }696702697 /**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 signer701 * @param collectionId ID of collection707 * @param collectionId ID of collection702 * @param label extra label for log708 * @param label extra label for log703 * @returns bool true on success709 * @example confirmSponsorship(aliceKeyring, 10)710 * @returns bool true on success704 */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 }715722716 /**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 signer720 * @param collectionId ID of collection727 * @param collectionId ID of collection721 * @param limits collection limits object728 * @param limits collection limits object722 * @param label extra label for log729 * @param label extra label for log723 * @returns bool true on success730 * @example731 * await setLimits(732 * aliceKeyring,733 * 10,734 * {735 * sponsorTransferTimeout: 0,736 * ownerCanDestroy: false737 * }738 * )739 * @returns bool true on success724 */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 }735751736 /**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 signer740 * @param collectionId ID of collection756 * @param collectionId ID of collection741 * @param ownerAddress substrate address of new owner757 * @param ownerAddress substrate address of new owner742 * @param label extra label for log758 * @param label extra label for log743 * @returns bool true on success759 * @example changeOwner(aliceKeyring, 10, "5DyN4Y92vZCjv38fg...")760 * @returns bool true on success744 */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 }755772756 /**773 /**757 * Adds a collection administrator. 774 * Adds a collection administrator. 758 * 775 * 759 * @param signer keyring of signer776 * @param signer keyring of signer760 * @param collectionId ID of collection777 * @param collectionId ID of collection761 * @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 log763 * @returns bool true on success780 * @example addAdmin(aliceKeyring, 10, {Substrate: "5DyN4Y92vZCjv38fg..."})781 * @returns bool true on success764 */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 }775793776 /**794 /**777 * Removes a collection administrator.795 * Removes a collection administrator.778 * 796 * 779 * @param signer keyring of signer797 * @param signer keyring of signer780 * @param collectionId ID of collection798 * @param collectionId ID of collection781 * @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 log783 * @returns bool true on success801 * @example removeAdmin(aliceKeyring, 10, {Substrate: "5DyN4Y92vZCjv38fg..."})802 * @returns bool true on success784 */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 }795814796 /**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 signer800 * @param collectionId ID of collection819 * @param collectionId ID of collection801 * @param permissions collection permissions object820 * @param permissions collection permissions object802 * @param label extra label for log821 * @param label extra label for log803 * @returns bool true on success822 * @example setPermissions(aliceKeyring, 10, TODO);823 * @returns bool true on success804 */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 }815835816 /**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 signer820 * @param collectionId ID of collection840 * @param collectionId ID of collection821 * @param permissions nesting permissions object841 * @param permissions nesting permissions object822 * @param label extra label for log842 * @param label extra label for log823 * @returns bool true on success843 * @example enableNesting(aliceKeyring, 10, TODO);844 * @returns bool true on success824 */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 }828849829 /**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 signer833 * @param collectionId ID of collection854 * @param collectionId ID of collection834 * @param label extra label for log855 * @param label extra label for log835 * @returns bool true on success856 * @example disableNesting(aliceKeyring, 10);857 * @returns bool true on success836 */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 }840862841 /**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 signer845 * @param collectionId ID of collection867 * @param collectionId ID of collection846 * @param properties array of property objects868 * @param properties array of property objects847 * @param label extra label for log869 * @param label extra label for log848 * @returns bool true on success870 * @example871 * setProperties(aliceKeyring, 10, TODO)872 * @returns bool true on success849 */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 }860884861 /**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 signer865 * @param collectionId ID of collection889 * @param collectionId ID of collection866 * @param propertyKeys array of property keys to delete890 * @param propertyKeys array of property keys to delete867 * @param label 891 * @param label892 * @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 }880905906 /**907 * Changes the owner of the token.908 * 909 * @param signer keyring of signer910 * @param collectionId ID of collection911 * @param tokenId ID of token912 * @param addressObj address of a new owner913 * @param amount amount of tokens to be transfered. For NFT must be set to 1n914 * @example transferToken(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."})915 * @returns true if the token success, otherwise false916 */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 }890926927 /**928 * 929 * Change ownership of a NFT on behalf of the owner. 930 * 931 * @param signer keyring of signer932 * @param collectionId ID of collection933 * @param tokenId ID of token934 * @param fromAddressObj address on behalf of which the token will be sent935 * @param toAddressObj new token owner936 * @param amount amount of tokens to be transfered. For NFT must be set to 1n937 * @example transferTokenFrom(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg"}, {Ethereum: "0x9F0583DbB85..."})938 * @returns true if the token success, otherwise false939 */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 }899948949 /**950 * 951 * Destroys a concrete instance of NFT.952 * 953 * @param signer keyring of signer954 * @param collectionId ID of collection955 * @param tokenId ID of token956 * @param label 957 * @param amount amount of tokens to be burned. For NFT must be set to 1n958 * @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 | null912 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 }914975976 /**977 * Destroys a concrete instance of NFT on behalf of the owner978 * 979 * @param signer keyring of signer980 * @param collectionId ID of collection981 * @param fromAddressObj address on behalf of which the token will be burnt982 * @param tokenId ID of token983 * @param label 984 * @param amount amount of tokens to be burned. For NFT must be set to 1n985 * @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 }925998999 /**1000 * Set, change, or remove approved address to transfer the ownership of the NFT.1001 * 1002 * @param signer keyring of signer1003 * @param collectionId ID of collection1004 * @param tokenId ID of token1005 * @param toAddressObj 1006 * @param label 1007 * @param amount amount of token to be approved. For NFT must be set to 1n1008 * @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 }93610201021 /**1022 * TODO1023 * @param collectionId ID of collection1024 * @param tokenId ID of token1025 * @param toAccountObj 1026 * @param fromAccountObj1027 * @example getTokenApprovedPieces(10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."}, {Substrate: "5ERZNF88Mm7UGfPP3mdG..."})1028 * @returns number of approved to transfer pieces1029 */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 }94010331034 /**1035 * Get the last created token id1036 * @param collectionId ID of collection1037 * @example getLastTokenId(10);1038 * @returns id of the last created token1039 */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 }94410431044 /**1045 * Check if token exists1046 * @param collectionId ID of collection1047 * @param tokenId ID of token1048 * @example isTokenExists(10, 20);1049 * @returns true if the token exists, otherwise false1050 */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}9491055950class NFTnRFT extends CollectionGroup {1056class NFTnRFT extends CollectionGroup {1057 /**1058 * Get tokens owned by account1059 * 1060 * @param collectionId ID of collection1061 * @param addressObj tokens owner1062 * @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 }95410681069 /**1070 * Get token data1071 * @param collectionId ID of collection1072 * @param tokenId ID of token1073 * @param blockHashAt 1074 * @param propertyKeys TODO1075 * @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 }98111041105 /**1106 * Set permissions to change token properties1107 * @param signer keyring of signer1108 * @param collectionId ID of collection1109 * @param permissions permissions to change a property by the collection owner or admin1110 * @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 false1115 */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 }99211261127 /**1128 * Set token properties1129 * @param signer keyring of signer1130 * @param collectionId ID of collection1131 * @param tokenId ID of token1132 * @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 false1136 */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 }100311471148 /**1149 * Delete the provided properties of a token1150 * @param signer keyring of signer1151 * @param collectionId ID of collection1152 * @param tokenId ID of token1153 * @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 false1157 */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 }101411681169 /**1170 * Mint new collection1171 * @param signer keyring of signer1172 * @param collectionOptions TODO1173 * @param mode NFT or RFT type of a collection1174 * @param errorLabel 1175 * @example mintCollection(aliceKeyring, TODO, "NFT")1176 * @returns object of the created collection1177 */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 object1017 collectionOptions.mode = (mode === 'NFT') ? {nft: null} : {refungible: null};1180 collectionOptions.mode = (mode === 'NFT') ? {nft: null} : {refungible: null};10371200103812011039class NFTGroup extends NFTnRFT {1202class NFTGroup extends NFTnRFT {1203 /**1204 * Get collection object1205 * @param collectionId ID of collection1206 * @example getCollectionObject(2);1207 * @returns instance of UniqueNFTCollection1208 */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 }104312121213 /**1214 * Get token object1215 * @param collectionId ID of collection1216 * @param tokenId ID of token1217 * @example getTokenObject(10, 5);1218 * @returns instance of UniqueNFTToken1219 */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 }104712231224 /**1225 * Get token's owner1226 * @param collectionId ID of collection1227 * @param tokenId ID of token1228 * @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 }105712411242 /**1243 * Is token approved to transfer1244 * @param collectionId ID of collection1245 * @param tokenId ID of token1246 * @param toAccountObj TODO1247 * @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 }106112521253 /**1254 * Changes the owner of the token.1255 * 1256 * @param signer keyring of signer1257 * @param collectionId ID of collection1258 * @param tokenId ID of token1259 * @param addressObj address of a new owner1260 * @example transferToken(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."})1261 * @returns true if extrinsic success, otherwise false1262 */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 }106512661267 /**1268 * 1269 * Change ownership of a NFT on behalf of the owner. 1270 * 1271 * @param signer keyring of signer1272 * @param collectionId ID of collection1273 * @param tokenId ID of token1274 * @param fromAddressObj address on behalf of which the token will be sent1275 * @param toAddressObj new token owner1276 * @example transferTokenFrom(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."}, {Ethereum: "0x9F0583DbB85..."})1277 * @returns true if extrinsic success, otherwise false1278 */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 }106912821283 /**1284 * Recursively find the address that owns the token1285 * @param collectionId ID of collection1286 * @param tokenId ID of token1287 * @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 }108413051306 /**1307 * Get tokens nested in the provided token1308 * @param collectionId ID of collection1309 * @param tokenId ID of token1310 * @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 }109713261327 /**1328 * Nest one token into another1329 * @param signer keyring of signer1330 * @param tokenObj token to be nested1331 * @param rootTokenObj token to be parent1332 * @param label 1333 * @example nestToken(aliceKeyring, {collectionId: 10, tokenId: 5}, {collectionId: 10, tokenId: 4});1334 * @returns true if extrinsic success, otherwise false1335 */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 }110613441345 /**1346 * Remove token from nested state1347 * @param signer keyring of signer1348 * @param tokenObj token to unnest1349 * @param rootTokenObj parent of a token1350 * @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 false1354 */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 }111513631364 /**1365 * Mint new collection1366 * @param signer keyring of signer1367 * @param collectionOptions Collection options1368 * @param label 1369 * @example 1370 * mintCollection(aliceKeyring, {1371 * name: 'New',1372 * description: 'New collection',1373 * tokenPrefix: 'NEW',1374 * })1375 * @returns object of the created collection1376 */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 }111913801381 /**1382 * Mint new token1383 * @param signer keyring of signer1384 * @param data token data1385 * @param label 1386 * @returns created token object1387 */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 }113614041405 /**1406 * Mint multiple NFT tokens1407 * @param signer keyring of signer1408 * @param collectionId ID of collection1409 * @param tokens array of tokens with owner and properties1410 * @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 false1420 */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 }114714311432 /**1433 * Mint multiple NFT tokens with one owner1434 * @param signer keyring of signer1435 * @param collectionId ID of collection1436 * @param owner tokens owner1437 * @param tokens array of tokens with owner and properties1438 * @param label 1439 * @example1440 * 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 tokens1450 */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 }116314661467 /**1468 * Destroys a concrete instance of NFT.1469 * @param signer keyring of signer1470 * @param collectionId ID of collection1471 * @param tokenId ID of token1472 * @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 }116714791480 /**1481 * Set, change, or remove approved address to transfer the ownership of the NFT.1482 * 1483 * @param signer keyring of signer1484 * @param collectionId ID of collection1485 * @param tokenId ID of token1486 * @param toAddressObj address to approve1487 * @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}11721495117314961174class RFTGroup extends NFTnRFT {1497class RFTGroup extends NFTnRFT {1498 /**1499 * Get collection object1500 * @param collectionId ID of collection1501 * @example getCollectionObject(2);1502 * @returns instance of UniqueNFTCollection1503 */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 }117815071508 /**1509 * Get token object1510 * @param collectionId ID of collection1511 * @param tokenId ID of token1512 * @example getTokenObject(10, 5);1513 * @returns instance of UniqueNFTToken1514 */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 }118215181519 /**1520 * Get top 10 token owners with the largest number of pieces 1521 * @param collectionId ID of collection1522 * @param tokenId ID of token1523 * @example getTokenTop10Owners(10, 5);1524 * @returns array of top 10 owners1525 */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 }118615291530 /**1531 * Get number of pieces owned by address1532 * @param collectionId ID of collection1533 * @param tokenId ID of token1534 * @param addressObj address token owner1535 * @example getTokenBalance(10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."});1536 * @returns number of pieces ownerd by address1537 */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 }119015411542 /**1543 * Transfer pieces of token to another address1544 * @param signer keyring of signer1545 * @param collectionId ID of collection1546 * @param tokenId ID of token1547 * @param addressObj address of a new owner1548 * @param amount number of pieces to be transfered1549 * @example transferTokenFrom(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."}, 2000n)1550 * @returns true if extrinsic success, otherwise false1551 */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 }119415551556 /**1557 * Change ownership of some pieces of RFT on behalf of the owner. 1558 * @param signer keyring of signer1559 * @param collectionId ID of collection1560 * @param tokenId ID of token1561 * @param fromAddressObj address on behalf of which the token will be sent1562 * @param toAddressObj new token owner1563 * @param amount number of pieces to be transfered1564 * @example transferTokenFrom(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."}, {Substrate: "5DfhbVfww7ThF8q6f3i..."}, 2000n)1565 * @returns true if extrinsic success, otherwise false1566 */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 }119815701571 /**1572 * Mint new collection1573 * @param signer keyring of signer1574 * @param collectionOptions Collection options1575 * @param label 1576 * @example1577 * mintCollection(aliceKeyring, {1578 * name: 'New',1579 * description: 'New collection',1580 * tokenPrefix: 'NEW',1581 * })1582 * @returns object of the created collection1583 */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 }120215871588 /**1589 * Mint new token1590 * @param signer keyring of signer1591 * @param data token data1592 * @param label 1593 * @example mintToken(aliceKeyring, {collectionId: 10, owner: {Substrate: '5GHoZe9c73RYbVzq...'}, pieces: 10000n});1594 * @returns created token object1595 */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 }123216251626 /**1627 * Mint multiple RFT tokens with one owner1628 * @param signer keyring of signer1629 * @param collectionId ID of collection1630 * @param owner tokens owner1631 * @param tokens array of tokens with properties and pieces1632 * @param label 1633 * @example mintMultipleTokensWithOneOwner(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq..."}, [{pieces: 100000n, properties: [{key: "gender", value: "male"}]}]);1634 * @returns array of newly created RFT tokens1635 */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 }124816511652 /**1653 * Destroys a concrete instance of RFT.1654 * @param signer keyring of signer1655 * @param collectionId ID of collection1656 * @param tokenId ID of token1657 * @param label 1658 * @param amount number of pieces to be burnt1659 * @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 }125216651666 /**1667 * Set, change, or remove approved address to transfer the ownership of the RFT.1668 * 1669 * @param signer keyring of signer1670 * @param collectionId ID of collection1671 * @param tokenId ID of token1672 * @param toAddressObj address to approve1673 * @param label 1674 * @param amount number of pieces to be approved1675 * @example approveToken(aliceKeyring, 10, 5, {Substrate: "5GHoZe9c73RYbVzq..."}, "", 10000n);1676 * @returns true if the token success, otherwise false1677 */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 }125616811682 /**1683 * Get total number of pieces1684 * @param collectionId ID of collection1685 * @param tokenId ID of token1686 * @example getTokenTotalPieces(10, 5);1687 * @returns number of pieces1688 */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 }126016921693 /**1694 * Change number of token pieces. Signer must be the owner of all token pieces.1695 * @param signer keyring of signer1696 * @param collectionId ID of collection1697 * @param tokenId ID of token1698 * @param amount new number of pieces1699 * @param label 1700 * @example repartitionToken(aliceKeyring, 10, 5, 12345n);1701 * @returns true if the repartion was success, otherwise false1702 */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);12731715127417161275class FTGroup extends CollectionGroup {1717class FTGroup extends CollectionGroup {1718 /**1719 * Get collection object1720 * @param collectionId ID of collection1721 * @example getCollectionObject(2);1722 * @returns instance of UniqueNFTCollection1723 */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 }127917271728 /**1729 * Mint new fungible collection1730 * @param signer keyring of signer1731 * @param collectionOptions Collection options1732 * @param decimalPoints number of token decimals 1733 * @param errorLabel 1734 * @example1735 * mintCollection(aliceKeyring, {1736 * name: 'New',1737 * description: 'New collection',1738 * tokenPrefix: 'NEW',1739 * }, 18)1740 * @returns newly created fungible collection1741 */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 object1282 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 }129417561757 /**1758 * Mint tokens1759 * @param signer keyring of signer1760 * @param collectionId ID of collection1761 * @param owner address owner of new tokens1762 * @param amount amount of tokens to be meanted1763 * @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 }130817801781 /**1782 * Mint multiple RFT tokens with one owner1783 * @param signer keyring of signer1784 * @param collectionId ID of collection1785 * @param owner tokens owner1786 * @param tokens array of tokens with properties and pieces1787 * @param label 1788 * @example mintMultipleTokensWithOneOwner(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq..."}, [{pieces: 100000n, properties: [{key: "gender", value: "male"}]}]);1789 * @returns array of newly created RFT tokens1790 */17911792 /**1793 * Mint multiple Fungible tokens with one owner TODO For what??1794 * @param signer keyring of signer1795 * @param collectionId ID of collection1796 * @param owner tokens owner1797 * @param tokens array of tokens with properties and pieces1798 * @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 }132318151816 /**1817 * Get top 10 token owners1818 * @param collectionId ID of collection1819 * @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 }132718251826 /**1827 * Get account balance1828 * @param collectionId ID of collection1829 * @param addressObj address of owner1830 * @example getBalance(10, {Substrate: "5GHoZe9c73RYbVzq..."})1831 * @returns amount of fungible tokens owned by address1832 */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 }133118361837 /**1838 * Transfer tokens to address1839 * @param signer keyring of signer1840 * @param collectionId ID of collection1841 * @param toAddressObj address recepient1842 * @param amount amount of tokens to be sent1843 * @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 }133518491850 /**1851 * Transfer some tokens on behalf of the owner.1852 * @param signer keyring of signer1853 * @param collectionId ID of collection1854 * @param fromAddressObj address on behalf of which tokens will be sent1855 * @param toAddressObj address where token to be sent1856 * @param amount number of tokens to be sent1857 * @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 }133918631864 /**1865 * Destroy some amount of tokens1866 * @param signer keyring of signer1867 * @param collectionId ID of collection1868 * @param amount amount of tokens to be destroyed1869 * @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 }134318761877 /**1878 * Burn some tokens on behalf of the owner.1879 * @param signer keyring of signer1880 * @param collectionId ID of collection1881 * @param fromAddressObj address on behalf of which tokens will be burnt1882 * @param amount amount of tokens to be burnt1883 * @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 }134718901891 /**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 }135118991900 /**1901 * Set, change, or remove approved address to transfer tokens.1902 * 1903 * @param signer keyring of signer1904 * @param collectionId ID of collection1905 * @param toAddressObj address to be approved1906 * @param amount amount of tokens to be approved1907 * @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 }135519141915 /**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}13601926136119271362class ChainGroup extends HelperGroup {1928class ChainGroup extends HelperGroup {1929 /**1930 * Get system properties of a chain1931 * @example getChainProperties();1932 * @returns ss58Format, token decimals, and token symbol1933 */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 }137119421943 /**1944 * Get chain header1945 * @example getLatestBlockNumber();1946 * @returns the number of the last block1947 */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 }137519511952 /**1953 * Get block hash by block number1954 * @param blockNumber number of block1955 * @example getBlockHashByNumber(12345);1956 * @returns hash of a block1957 */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 }138119631964 /**1965 * Get account nonce1966 * @param address substrate address1967 * @example getNonce("5GrwvaEF5zXb26Fz...");1968 * @returns number, account's nonce1969 */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 }139319811982 /**1983 * Get substrate address balance1984 * @param address substrate address1985 * @example getSubstrate("5GrwvaEF5zXb26Fz...")1986 * @returns amount of tokens on address1987 */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 }139719911992 /**1993 * Get ethereum address balance1994 * @param address ethereum address1995 * @example getEthereum("0x9F0583DbB855d...")1996 * @returns amount of tokens on address1997 */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 }140120012002 /**2003 * Transfer tokens to substrate address2004 * @param signer keyring of signer2005 * @param address substrate address of a recepient2006 * @param amount amount of tokens to be transfered2007 * @example transferToSubstrate(aliceKeyring, "5GrwvaEF5zXb26Fz...", 100_000_000_000n);2008 * @returns true if extrinsic success, otherwise false2009 */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}`);140420121425 return this.helper.util.normalizeSubstrateAddress(address, ss58Format);2033 return this.helper.util.normalizeSubstrateAddress(address, ss58Format);1426 }2034 }142720352036 /**2037 * Get address in the connected chain format2038 * @param address substrate address2039 * @example normalizeSubstrateToChainFormat("5GrwvaEF5zXb26Fz...") // returns unjKJQJrRd238pkUZZ... for Unique Network2040 * @returns address in chain format2041 */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 }143220462047 /**2048 * Get substrate mirror of an ethereum address2049 * @param ethAddress ethereum address2050 * @param toChainFormat false for normalized account2051 * @example ethToSubstrate('0x9F0583DbB855d...')2052 * @returns substrate mirror of a provided ethereum address2053 */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 }143820592060 /**2061 * Get ethereum mirror of a substrate address2062 * @param subAddress substrate account2063 * @example substrateToEth("5DnSF6RRjwteE3BrC...")2064 * @returns ethereum mirror of a provided substrate address2065 */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 }