From b30739fc3729f07305d0fb7e6e5f329660e58016 Mon Sep 17 00:00:00 2001 From: SimiHunjan Date: Fri, 19 Apr 2024 12:45:53 -0700 Subject: [PATCH 1/2] Update UpdateNftsMetadataExample.java Signed-off-by: SimiHunjan Signed-off-by: SimiHunjan --- .../main/java/UpdateNftsMetadataExample.java | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/examples/src/main/java/UpdateNftsMetadataExample.java b/examples/src/main/java/UpdateNftsMetadataExample.java index 6937c4133..c23d0fb8c 100644 --- a/examples/src/main/java/UpdateNftsMetadataExample.java +++ b/examples/src/main/java/UpdateNftsMetadataExample.java @@ -76,19 +76,23 @@ private UpdateNftsMetadataExample() throws Exception { } private void updateNftsMetadata(TokenCreateTransaction tokenCreateTransaction) throws Exception { + //Create a non-fungible token (NFT) with the metadata key field set var tokenCreateResponse = tokenCreateTransaction.sign(OPERATOR_KEY).execute(client); var tokenCreateReceipt = tokenCreateResponse.getReceipt(client); System.out.println("Status of token create transaction: " + tokenCreateReceipt.status); + //Get the token ID of the token that was created var tokenId = tokenCreateReceipt.tokenId; System.out.println("Token id: " + tokenId); + //Query for the token information stored in consensus node state to see that the metadata key is set var tokenInfo = new TokenInfoQuery() .setTokenId(tokenId) .execute(client); System.out.println("Token metadata key: " + tokenInfo.metadataKey); + //Mint the first NFT and set the initial metadata for the NFT var tokenMintTransaction = new TokenMintTransaction() .setMetadata(List.of(INITIAL_METADATA)) .setTokenId(tokenId); @@ -98,16 +102,18 @@ private void updateNftsMetadata(TokenCreateTransaction tokenCreateTransaction) t }); var tokenMintResponse = tokenMintTransaction.execute(client); + // Get receipt for mint token transaction var tokenMintReceipt = tokenMintResponse.getReceipt(client); System.out.println("Status of token mint transaction: " + tokenMintReceipt.status); var nftSerials = tokenMintReceipt.serials; - // Check that metadata was set correctly + // Check that metadata on the NFT was set correctly getMetadataList(client, tokenId, nftSerials).forEach(metadata -> { System.out.println("Metadata after mint: " + Arrays.toString(metadata)); }); + //Create an account to send the NFT to var accountCreateTransaction = new AccountCreateTransaction() .setKey(OPERATOR_KEY) .setMaxAutomaticTokenAssociations(10) @@ -115,19 +121,21 @@ private void updateNftsMetadata(TokenCreateTransaction tokenCreateTransaction) t var newAccountId = accountCreateTransaction.getReceipt(client).accountId; - new TokenAssociateTransaction() - .setAccountId(newAccountId) - .setTokenIds(Collections.singletonList(tokenId)) - .freezeWith(client) - .sign(OPERATOR_KEY) - .execute(client) - .getReceipt(client); + //Associate the NFT to the account only if the account does not have any automatic token association slots open. When we created the account in the previous step we set automatic token associations to 10 so I should have 10 slots open to receive a token + //new TokenAssociateTransaction() + //.setAccountId(newAccountId) + //.setTokenIds(Collections.singletonList(tokenId)) + //.freezeWith(client) + //.sign(OPERATOR_KEY) + //.execute(client) + //.getReceipt(client); + //Transfer the NFT to the new account new TransferTransaction() .addNftTransfer(tokenId.nft(nftSerials.get(0)), OPERATOR_ID, newAccountId) .execute(client); - // Update nfts metadata + // Update nft's metadata var tokenUpdateNftsTransaction = new TokenUpdateNftsTransaction() .setTokenId(tokenId) .setSerials(nftSerials) @@ -136,11 +144,12 @@ private void updateNftsMetadata(TokenCreateTransaction tokenCreateTransaction) t System.out.println("Updated metadata: " + Arrays.toString(tokenUpdateNftsTransaction.getMetadata())); var tokenUpdateNftsResponse = tokenUpdateNftsTransaction.sign(METADATA_KEY).execute(client); + // Get receipt for update nfts metadata transaction var tokenUpdateNftsReceipt = tokenUpdateNftsResponse.getReceipt(client); System.out.println("Status of token update nfts metadata transaction: " + tokenUpdateNftsReceipt.status); - // Check that metadata was updated correctly + // Check that metadata for the NFT was updated correctly getMetadataList(client, tokenId, nftSerials).forEach(metadata -> { System.out.println("Metadata after update: " + Arrays.toString(metadata)); }); From 3329155c0365631881006cdad6f36c324f1783b1 Mon Sep 17 00:00:00 2001 From: Nikita Lebedev Date: Tue, 23 Apr 2024 09:51:07 +0300 Subject: [PATCH 2/2] address PR comments Signed-off-by: Nikita Lebedev --- .../main/java/UpdateNftsMetadataExample.java | 27 +++++++------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/examples/src/main/java/UpdateNftsMetadataExample.java b/examples/src/main/java/UpdateNftsMetadataExample.java index c23d0fb8c..b23285887 100644 --- a/examples/src/main/java/UpdateNftsMetadataExample.java +++ b/examples/src/main/java/UpdateNftsMetadataExample.java @@ -76,23 +76,23 @@ private UpdateNftsMetadataExample() throws Exception { } private void updateNftsMetadata(TokenCreateTransaction tokenCreateTransaction) throws Exception { - //Create a non-fungible token (NFT) with the metadata key field set + // Create a non-fungible token (NFT) with the metadata key field set var tokenCreateResponse = tokenCreateTransaction.sign(OPERATOR_KEY).execute(client); var tokenCreateReceipt = tokenCreateResponse.getReceipt(client); System.out.println("Status of token create transaction: " + tokenCreateReceipt.status); - //Get the token ID of the token that was created + // Get the token ID of the token that was created var tokenId = tokenCreateReceipt.tokenId; System.out.println("Token id: " + tokenId); - //Query for the token information stored in consensus node state to see that the metadata key is set + // Query for the token information stored in consensus node state to see that the metadata key is set var tokenInfo = new TokenInfoQuery() .setTokenId(tokenId) .execute(client); System.out.println("Token metadata key: " + tokenInfo.metadataKey); - //Mint the first NFT and set the initial metadata for the NFT + // Mint the first NFT and set the initial metadata for the NFT var tokenMintTransaction = new TokenMintTransaction() .setMetadata(List.of(INITIAL_METADATA)) .setTokenId(tokenId); @@ -102,7 +102,7 @@ private void updateNftsMetadata(TokenCreateTransaction tokenCreateTransaction) t }); var tokenMintResponse = tokenMintTransaction.execute(client); - + // Get receipt for mint token transaction var tokenMintReceipt = tokenMintResponse.getReceipt(client); System.out.println("Status of token mint transaction: " + tokenMintReceipt.status); @@ -113,24 +113,15 @@ private void updateNftsMetadata(TokenCreateTransaction tokenCreateTransaction) t System.out.println("Metadata after mint: " + Arrays.toString(metadata)); }); - //Create an account to send the NFT to + // Create an account to send the NFT to var accountCreateTransaction = new AccountCreateTransaction() .setKey(OPERATOR_KEY) - .setMaxAutomaticTokenAssociations(10) + .setMaxAutomaticTokenAssociations(10) // If the account does not have any automatic token association slots open ONLY then associate the NFT to the account .execute(client); var newAccountId = accountCreateTransaction.getReceipt(client).accountId; - //Associate the NFT to the account only if the account does not have any automatic token association slots open. When we created the account in the previous step we set automatic token associations to 10 so I should have 10 slots open to receive a token - //new TokenAssociateTransaction() - //.setAccountId(newAccountId) - //.setTokenIds(Collections.singletonList(tokenId)) - //.freezeWith(client) - //.sign(OPERATOR_KEY) - //.execute(client) - //.getReceipt(client); - - //Transfer the NFT to the new account + // Transfer the NFT to the new account new TransferTransaction() .addNftTransfer(tokenId.nft(nftSerials.get(0)), OPERATOR_ID, newAccountId) .execute(client); @@ -144,7 +135,7 @@ private void updateNftsMetadata(TokenCreateTransaction tokenCreateTransaction) t System.out.println("Updated metadata: " + Arrays.toString(tokenUpdateNftsTransaction.getMetadata())); var tokenUpdateNftsResponse = tokenUpdateNftsTransaction.sign(METADATA_KEY).execute(client); - + // Get receipt for update nfts metadata transaction var tokenUpdateNftsReceipt = tokenUpdateNftsResponse.getReceipt(client); System.out.println("Status of token update nfts metadata transaction: " + tokenUpdateNftsReceipt.status);