Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update UpdateNftsMetadataExample.java #1783

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions examples/src/main/java/UpdateNftsMetadataExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -98,36 +102,31 @@ 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)
.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;

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)
Expand All @@ -136,11 +135,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));
});
Expand Down