git.delta.rocks / unique-network / refs/commits / cde495e75856

difftreelog

Add common nesting helpers to NFTnRFT group

Max Andreev2023-01-18parent: #7a935af.patch.diff
in: master

1 file changed

modifiedtests/src/util/playgrounds/unique.tsdiffbeforeafterboth
1391 return tokenData;1391 return tokenData;
1392 }1392 }
1393
1394 /**
1395 * Get token's owner
1396 * @param collectionId ID of collection
1397 * @param tokenId ID of token
1398 * @param blockHashAt optionally query the data at the block with this hash
1399 * @example getTokenOwner(10, 5);
1400 * @returns Address in CrossAccountId format, e.g. {Substrate: "5DnSF6RRjwteE3BrCj..."}
1401 */
1402 async getTokenOwner(collectionId: number, tokenId: number, blockHashAt?: string): Promise<CrossAccountId> {
1403 let owner;
1404 if (typeof blockHashAt === 'undefined') {
1405 owner = await this.helper.callRpc('api.rpc.unique.tokenOwner', [collectionId, tokenId]);
1406 } else {
1407 owner = await this.helper.callRpc('api.rpc.unique.tokenOwner', [collectionId, tokenId, blockHashAt]);
1408 }
1409 return CrossAccountId.fromLowerCaseKeys(owner.toJSON());
1410 }
1411
1412 /**
1413 * Recursively find the address that owns the token
1414 * @param collectionId ID of collection
1415 * @param tokenId ID of token
1416 * @param blockHashAt
1417 * @example getTokenTopmostOwner(10, 5);
1418 * @returns address in CrossAccountId format, e.g. {Substrate: "5DyN4Y92vZCjv38fg..."}
1419 */
1420 async getTokenTopmostOwner(collectionId: number, tokenId: number, blockHashAt?: string): Promise<CrossAccountId | null> {
1421 let owner;
1422 if (typeof blockHashAt === 'undefined') {
1423 owner = await this.helper.callRpc('api.rpc.unique.topmostTokenOwner', [collectionId, tokenId]);
1424 } else {
1425 owner = await this.helper.callRpc('api.rpc.unique.topmostTokenOwner', [collectionId, tokenId, blockHashAt]);
1426 }
1427
1428 if (owner === null) return null;
1429
1430 return owner.toHuman();
1431 }
1432
1433 /**
1434 * Nest one token into another
1435 * @param signer keyring of signer
1436 * @param tokenObj token to be nested
1437 * @param rootTokenObj token to be parent
1438 * @example nestToken(aliceKeyring, {collectionId: 10, tokenId: 5}, {collectionId: 10, tokenId: 4});
1439 * @returns ```true``` if extrinsic success, otherwise ```false```
1440 */
1441 async nestToken(signer: TSigner, tokenObj: IToken, rootTokenObj: IToken): Promise<boolean> {
1442 const rootTokenAddress = this.helper.util.getTokenAccount(rootTokenObj);
1443 const result = await this.transferToken(signer, tokenObj.collectionId, tokenObj.tokenId, rootTokenAddress);
1444 if(!result) {
1445 throw Error('Unable to nest token!');
1446 }
1447 return result;
1448 }
1449
1450 /**
1451 * Remove token from nested state
1452 * @param signer keyring of signer
1453 * @param tokenObj token to unnest
1454 * @param rootTokenObj parent of a token
1455 * @param toAddressObj address of a new token owner
1456 * @example unnestToken(aliceKeyring, {collectionId: 10, tokenId: 5}, {collectionId: 10, tokenId: 4}, {Substrate: "5DyN4Y92vZCjv38fg..."});
1457 * @returns ```true``` if extrinsic success, otherwise ```false```
1458 */
1459 async unnestToken(signer: TSigner, tokenObj: IToken, rootTokenObj: IToken, toAddressObj: ICrossAccountId): Promise<boolean> {
1460 const rootTokenAddress = this.helper.util.getTokenAccount(rootTokenObj);
1461 const result = await this.transferTokenFrom(signer, tokenObj.collectionId, tokenObj.tokenId, rootTokenAddress, toAddressObj);
1462 if(!result) {
1463 throw Error('Unable to unnest token!');
1464 }
1465 return result;
1466 }
13931467
1394 /**1468 /**
1395 * Set permissions to change token properties1469 * Set permissions to change token properties
1557 return new UniqueNFToken(tokenId, this.getCollectionObject(collectionId));1631 return new UniqueNFToken(tokenId, this.getCollectionObject(collectionId));
1558 }1632 }
1559
1560 /**
1561 * Get token's owner
1562 * @param collectionId ID of collection
1563 * @param tokenId ID of token
1564 * @param blockHashAt optionally query the data at the block with this hash
1565 * @example getTokenOwner(10, 5);
1566 * @returns Address in CrossAccountId format, e.g. {Substrate: "5DnSF6RRjwteE3BrCj..."}
1567 */
1568 async getTokenOwner(collectionId: number, tokenId: number, blockHashAt?: string): Promise<CrossAccountId> {
1569 let owner;
1570 if (typeof blockHashAt === 'undefined') {
1571 owner = await this.helper.callRpc('api.rpc.unique.tokenOwner', [collectionId, tokenId]);
1572 } else {
1573 owner = await this.helper.callRpc('api.rpc.unique.tokenOwner', [collectionId, tokenId, blockHashAt]);
1574 }
1575 return CrossAccountId.fromLowerCaseKeys(owner.toJSON());
1576 }
15771633
1578 /**1634 /**
1579 * Is token approved to transfer1635 * Is token approved to transfer
1616 return await super.transferTokenFrom(signer, collectionId, tokenId, fromAddressObj, toAddressObj, 1n);1672 return await super.transferTokenFrom(signer, collectionId, tokenId, fromAddressObj, toAddressObj, 1n);
1617 }1673 }
1618
1619 /**
1620 * Recursively find the address that owns the token
1621 * @param collectionId ID of collection
1622 * @param tokenId ID of token
1623 * @param blockHashAt
1624 * @example getTokenTopmostOwner(10, 5);
1625 * @returns address in CrossAccountId format, e.g. {Substrate: "5DyN4Y92vZCjv38fg..."}
1626 */
1627 async getTokenTopmostOwner(collectionId: number, tokenId: number, blockHashAt?: string): Promise<CrossAccountId | null> {
1628 let owner;
1629 if (typeof blockHashAt === 'undefined') {
1630 owner = await this.helper.callRpc('api.rpc.unique.topmostTokenOwner', [collectionId, tokenId]);
1631 } else {
1632 owner = await this.helper.callRpc('api.rpc.unique.topmostTokenOwner', [collectionId, tokenId, blockHashAt]);
1633 }
1634
1635 if (owner === null) return null;
1636
1637 return owner.toHuman();
1638 }
16391674
1640 /**1675 /**
1641 * Get tokens nested in the provided token1676 * Get tokens nested in the provided token
1658 });1693 });
1659 }1694 }
1660
1661 /**
1662 * Nest one token into another
1663 * @param signer keyring of signer
1664 * @param tokenObj token to be nested
1665 * @param rootTokenObj token to be parent
1666 * @example nestToken(aliceKeyring, {collectionId: 10, tokenId: 5}, {collectionId: 10, tokenId: 4});
1667 * @returns ```true``` if extrinsic success, otherwise ```false```
1668 */
1669 async nestToken(signer: TSigner, tokenObj: IToken, rootTokenObj: IToken): Promise<boolean> {
1670 const rootTokenAddress = this.helper.util.getTokenAccount(rootTokenObj);
1671 const result = await this.transferToken(signer, tokenObj.collectionId, tokenObj.tokenId, rootTokenAddress);
1672 if(!result) {
1673 throw Error('Unable to nest token!');
1674 }
1675 return result;
1676 }
1677
1678 /**
1679 * Remove token from nested state
1680 * @param signer keyring of signer
1681 * @param tokenObj token to unnest
1682 * @param rootTokenObj parent of a token
1683 * @param toAddressObj address of a new token owner
1684 * @example unnestToken(aliceKeyring, {collectionId: 10, tokenId: 5}, {collectionId: 10, tokenId: 4}, {Substrate: "5DyN4Y92vZCjv38fg..."});
1685 * @returns ```true``` if extrinsic success, otherwise ```false```
1686 */
1687 async unnestToken(signer: TSigner, tokenObj: IToken, rootTokenObj: IToken, toAddressObj: ICrossAccountId): Promise<boolean> {
1688 const rootTokenAddress = this.helper.util.getTokenAccount(rootTokenObj);
1689 const result = await this.transferTokenFrom(signer, tokenObj.collectionId, tokenObj.tokenId, rootTokenAddress, toAddressObj);
1690 if(!result) {
1691 throw Error('Unable to unnest token!');
1692 }
1693 return result;
1694 }
16951695
1696 /**1696 /**
1697 * Mint new collection1697 * Mint new collection
3531 return await this.helper.rft.getToken(this.collectionId, tokenId, [], blockHashAt);3531 return await this.helper.rft.getToken(this.collectionId, tokenId, [], blockHashAt);
3532 }3532 }
3533
3534 async getTokenOwner(tokenId: number, blockHashAt?: string) {
3535 return await this.helper.rft.getTokenOwner(this.collectionId, tokenId, blockHashAt);
3536 }
35333537
3534 async getTokensByAddress(addressObj: ICrossAccountId) {3538 async getTokensByAddress(addressObj: ICrossAccountId) {
3535 return await this.helper.rft.getTokensByAddress(this.collectionId, addressObj);3539 return await this.helper.rft.getTokensByAddress(this.collectionId, addressObj);
3539 return await this.helper.rft.getTokenTop10Owners(this.collectionId, tokenId);3543 return await this.helper.rft.getTokenTop10Owners(this.collectionId, tokenId);
3540 }3544 }
3545
3546 async getTokenTopmostOwner(tokenId: number, blockHashAt?: string) {
3547 return await this.helper.rft.getTokenTopmostOwner(this.collectionId, tokenId, blockHashAt);
3548 }
35413549
3542 async getTokenBalance(tokenId: number, addressObj: ICrossAccountId) {3550 async getTokenBalance(tokenId: number, addressObj: ICrossAccountId) {
3543 return await this.helper.rft.getTokenBalance(this.collectionId, tokenId, addressObj);3551 return await this.helper.rft.getTokenBalance(this.collectionId, tokenId, addressObj);
3610 return await this.helper.rft.setTokenPropertyPermissions(signer, this.collectionId, permissions);3618 return await this.helper.rft.setTokenPropertyPermissions(signer, this.collectionId, permissions);
3611 }3619 }
3620
3621 async nestToken(signer: TSigner, tokenId: number, toTokenObj: IToken) {
3622 return await this.helper.rft.nestToken(signer, {collectionId: this.collectionId, tokenId}, toTokenObj);
3623 }
3624
3625 async unnestToken(signer: TSigner, tokenId: number, fromTokenObj: IToken, toAddressObj: ICrossAccountId) {
3626 return await this.helper.rft.unnestToken(signer, {collectionId: this.collectionId, tokenId}, fromTokenObj, toAddressObj);
3627 }
36123628
3613 scheduleAt<T extends UniqueHelper>(3629 scheduleAt<T extends UniqueHelper>(
3614 executionBlockNumber: number,3630 executionBlockNumber: number,
3849 return await this.collection.getToken(this.tokenId, blockHashAt);3865 return await this.collection.getToken(this.tokenId, blockHashAt);
3850 }3866 }
3867
3868 async getOwner(blockHashAt?: string) {
3869 return await this.collection.getTokenOwner(this.tokenId, blockHashAt);
3870 }
38513871
3852 async getTop10Owners() {3872 async getTop10Owners() {
3853 return await this.collection.getTop10TokenOwners(this.tokenId);3873 return await this.collection.getTop10TokenOwners(this.tokenId);
3854 }3874 }
3875
3876 async getTopmostOwner(blockHashAt?: string) {
3877 return await this.collection.getTokenTopmostOwner(this.tokenId, blockHashAt);
3878 }
3879
3880 async nest(signer: TSigner, toTokenObj: IToken) {
3881 return await this.collection.nestToken(signer, this.tokenId, toTokenObj);
3882 }
3883
3884 async unnest(signer: TSigner, fromTokenObj: IToken, toAddressObj: ICrossAccountId) {
3885 return await this.collection.unnestToken(signer, this.tokenId, fromTokenObj, toAddressObj);
3886 }
38553887
3856 async getBalance(addressObj: ICrossAccountId) {3888 async getBalance(addressObj: ICrossAccountId) {
3857 return await this.collection.getTokenBalance(this.tokenId, addressObj);3889 return await this.collection.getTokenBalance(this.tokenId, addressObj);