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

chore: improve examples #1935

Merged
merged 35 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
96f276d
chore: update examples (added `Client#close`)
thenswan Aug 5, 2024
6b6fb63
chore: update examples (optimize exception handling)
thenswan Aug 5, 2024
d618e1c
chore: update examples (optimize num values readability)
thenswan Aug 5, 2024
2576d72
chore: update examples (improve clean up section)
thenswan Aug 6, 2024
e9609b8
Merge branch 'refs/heads/main' into 01631-improve-java-examples
thenswan Aug 7, 2024
6c6e7e6
fix: ExemptCustomFeesExample
thenswan Aug 7, 2024
ca14652
fix: ExemptCustomFeesExample
thenswan Aug 7, 2024
c3d41f0
chore: updated structure in example resources folder
thenswan Aug 7, 2024
ab82dff
chore: update example resources
thenswan Aug 7, 2024
367aad4
chore: update example (pass private keys as a parameter only if needed)
thenswan Aug 7, 2024
184b553
chore: update example (align by a template)
thenswan Aug 9, 2024
6b656b5
chore: update ConsensusPubSubWithSubmitKeyExample example (increase l…
thenswan Aug 11, 2024
e865a7e
chore: update examples (align the approach for handling null values)
thenswan Aug 11, 2024
df1f03e
revert: gradle.properties
thenswan Aug 11, 2024
f7f8ebe
chore: update examples (update error handling)
thenswan Aug 12, 2024
5ad4476
chore: update examples (implement logging)
thenswan Aug 13, 2024
1e00710
fix: ConsensusPubSubWithSubmitKeyExample
thenswan Aug 13, 2024
5045e36
chore: examples (align logging/printing approach)
thenswan Aug 14, 2024
40aace4
docs: update examples README.md
thenswan Aug 15, 2024
baee41b
chore: align examples documentation
thenswan Aug 15, 2024
3644837
chore: optimize wait time in examples
thenswan Aug 16, 2024
0dd72d2
chore: optimize tokens and gas spendings in examples
thenswan Aug 16, 2024
b06f702
chore: remove errorprone from examples
thenswan Aug 16, 2024
1c4c847
chore: revisit variable names in examples
thenswan Aug 16, 2024
edda3f6
chore: revisit Hedera objects memos, names, etc. in examples
thenswan Aug 16, 2024
38ae862
chore: revisited structure in example resources folder
thenswan Aug 16, 2024
cc75d44
chore: add contracts which used in examples to the resources folder
thenswan Aug 16, 2024
04be75d
chore: additional handling of null values in examples
thenswan Aug 20, 2024
3d8add9
chore: update from main
0xivanov Sep 9, 2024
eb19467
chore: fix TODOs
0xivanov Sep 9, 2024
e172a84
chore: fix solidity example
0xivanov Sep 9, 2024
595afaf
chore: refactor resources folder
0xivanov Sep 9, 2024
c543836
chore: revert gradle.kts changes
0xivanov Sep 9, 2024
5e9c585
chore: update from main
0xivanov Sep 9, 2024
3cae803
chore: fix typo
0xivanov Sep 9, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,10 @@
*/
package com.hedera.hashgraph.sdk.examples;

import com.hedera.hashgraph.sdk.AccountBalance;
import com.hedera.hashgraph.sdk.AccountBalanceQuery;
import com.hedera.hashgraph.sdk.AccountId;
import com.hedera.hashgraph.sdk.AccountInfo;
import com.hedera.hashgraph.sdk.AccountInfoQuery;
import com.hedera.hashgraph.sdk.Client;
import com.hedera.hashgraph.sdk.Hbar;
import com.hedera.hashgraph.sdk.PrecheckStatusException;
import com.hedera.hashgraph.sdk.PrivateKey;
import com.hedera.hashgraph.sdk.PublicKey;
import com.hedera.hashgraph.sdk.ReceiptStatusException;
import com.hedera.hashgraph.sdk.TransferTransaction;
import com.hedera.hashgraph.sdk.*;
import io.github.cdimascio.dotenv.Dotenv;

import java.util.Objects;
import java.util.concurrent.TimeoutException;

public class AccountAliasExample {

Expand All @@ -45,8 +33,7 @@ public class AccountAliasExample {
// HEDERA_NETWORK defaults to testnet if not specified in dotenv
private static final String HEDERA_NETWORK = Dotenv.load().get("HEDERA_NETWORK", "testnet");

public static void main(String[] args)
throws TimeoutException, PrecheckStatusException, ReceiptStatusException, InterruptedException {
public static void main(String[] args) throws Exception {
Client client = ClientHelper.forName(HEDERA_NETWORK);

// Defaults the operator account ID and key such that all generated transactions will be paid for
Expand Down Expand Up @@ -139,6 +126,16 @@ public static void main(String[] args)
System.out.println("The alias key: " + info.aliasKey);

System.out.println("Example complete!");

// Clean up
new AccountDeleteTransaction()
.setAccountId(info.accountId)
.setTransferAccountId(OPERATOR_ID)
.freezeWith(client)
.sign(privateKey)
.execute(client)
.getReceipt(client);

client.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,7 @@
*/
package com.hedera.hashgraph.sdk.examples;

import com.hedera.hashgraph.sdk.AccountAllowanceApproveTransaction;
import com.hedera.hashgraph.sdk.AccountBalanceQuery;
import com.hedera.hashgraph.sdk.AccountCreateTransaction;
import com.hedera.hashgraph.sdk.AccountDeleteTransaction;
import com.hedera.hashgraph.sdk.AccountId;
import com.hedera.hashgraph.sdk.Client;
import com.hedera.hashgraph.sdk.Hbar;
import com.hedera.hashgraph.sdk.PrecheckStatusException;
import com.hedera.hashgraph.sdk.PrivateKey;
import com.hedera.hashgraph.sdk.ReceiptStatusException;
import com.hedera.hashgraph.sdk.TransactionId;
import com.hedera.hashgraph.sdk.TransferTransaction;
import com.hedera.hashgraph.sdk.*;
import io.github.cdimascio.dotenv.Dotenv;

import java.util.Objects;
Expand All @@ -56,16 +45,14 @@ public class AccountAllowanceExample {
private final PrivateKey charlieKey;
private final AccountId charlieId;

public static void main(String[] args)
throws PrecheckStatusException, TimeoutException, ReceiptStatusException, InterruptedException {
public static void main(String[] args) throws Exception {
AccountAllowanceExample example = new AccountAllowanceExample();
example.demonstrateAllowances();
example.cleanUp();
System.out.println("End of example");
}

private AccountAllowanceExample()
throws PrecheckStatusException, TimeoutException, ReceiptStatusException, InterruptedException {
private AccountAllowanceExample() throws Exception {
thenswan marked this conversation as resolved.
Show resolved Hide resolved
client = ClientHelper.forName(HEDERA_NETWORK);

// Defaults the operator account ID and key such that all generated transactions will be paid for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import com.hedera.hashgraph.sdk.*;
import io.github.cdimascio.dotenv.Dotenv;

import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public final class AccountCreateWithHtsExample {

Expand Down Expand Up @@ -142,14 +144,14 @@ public static void main(String[] args) throws Exception {
System.out.println("Current owner account id: " + nftOwnerAccountId);

// Step 6 - Show the new account ID owns the NFT
String accountId = new AccountInfoQuery()
String accountIdString = new AccountInfoQuery()
.setAccountId(aliasAccountId)
.execute(client)
.accountId.toString();

System.out.println("The normal account ID of the given alias: " + accountId);
System.out.println("The normal account ID of the given alias: " + accountIdString);

if (nftOwnerAccountId.equals(accountId)) {
if (nftOwnerAccountId.equals(accountIdString)) {
System.out.println("The NFT owner accountId matches the accountId created with the HTS");
} else {
throw new Exception("The two account IDs does not match");
Expand All @@ -161,11 +163,13 @@ public static void main(String[] args) throws Exception {
TokenCreateTransaction tokenCreateTx = new TokenCreateTransaction()
.setTokenName("HIP-542 Token")
.setTokenSymbol("H542")
.setInitialSupply(10_000) // Total supply = 10000 / 10 ^ 2
.setDecimals(2)
.setTokenType(TokenType.FUNGIBLE_COMMON)
.setTreasuryAccountId(OPERATOR_ID)
.setInitialSupply(10000) // Total supply = 10000 / 10 ^ 2
.setDecimals(2)
.setAutoRenewAccountId(OPERATOR_ID)
.setAdminKey(OPERATOR_KEY)
.setWipeKey(wipeKey)
.freezeWith(client);

// Sign the transaction with the operator key
Expand Down Expand Up @@ -208,12 +212,12 @@ public static void main(String[] args) throws Exception {
tokenTransferSubmit.getReceipt(client);

// Step 4 - Return the new account ID in the child record
String accountId2 = new AccountInfoQuery()
String accountId2String = new AccountInfoQuery()
.setAccountId(aliasAccountId2)
.execute(client)
.accountId.toString();

System.out.println("The normal account ID of the given alias: " + accountId2);
System.out.println("The normal account ID of the given alias: " + accountId2String);

// Step 5 - Show the new account ID owns the fungible token
AccountBalance accountBalances = new AccountBalanceQuery()
Expand All @@ -226,6 +230,67 @@ public static void main(String[] args) throws Exception {
else
throw new Exception("Creating account with HTS using public key alias failed");

// Clean Up

var accountId1 = AccountId.fromString(accountIdString);

new TokenWipeTransaction()
.setTokenId(nftTokenId)
.addSerial(exampleNftId)
.setAccountId(accountId1)
.freezeWith(client)
.sign(wipeKey)
.execute(client)
.getReceipt(client);

var accountId2 = AccountId.fromString(accountId2String);

Map<TokenId, Long> accountId2TokensBeforeWipe = new AccountBalanceQuery()
.setAccountId(accountId2)
.execute(client)
.tokens;

System.out.println("Account Id 2 token balance (before wipe): " + accountId2TokensBeforeWipe.get(tokenId));

new TokenWipeTransaction()
.setTokenId(tokenId)
.setAmount(accountId2TokensBeforeWipe.get(tokenId))
.setAccountId(accountId2)
.freezeWith(client)
.sign(wipeKey)
.execute(client)
.getReceipt(client);

new AccountDeleteTransaction()
.setAccountId(accountId1)
.setTransferAccountId(OPERATOR_ID)
.freezeWith(client)
.sign(privateKey)
.execute(client)
.getReceipt(client);

new AccountDeleteTransaction()
.setAccountId(accountId2)
.setTransferAccountId(OPERATOR_ID)
.freezeWith(client)
.sign(privateKey2)
.execute(client)
.getReceipt(client);

new TokenDeleteTransaction()
.setTokenId(nftTokenId)
.freezeWith(client)
.sign(OPERATOR_KEY)
.execute(client)
.getReceipt(client);

new TokenDeleteTransaction()
.setTokenId(tokenId)
.freezeWith(client)
.sign(OPERATOR_KEY)
.execute(client)
.getReceipt(client);

client.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
import com.hedera.hashgraph.sdk.*;
import io.github.cdimascio.dotenv.Dotenv;

import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.TimeoutException;

public class AutoCreateAccountTransferTransactionExample {
// see `.env.sample` in the repository root for how to specify these values
Expand Down Expand Up @@ -141,6 +139,7 @@ public static void main(String[] args) throws Exception {
* - To enhance the hollow account to have a public key the hollow account needs to be specified as a transaction fee payer in a HAPI transaction
*/
TransactionReceipt receipt2 = new TopicCreateTransaction()
.setAdminKey(publicKey)
.setTransactionId(TransactionId.generate(newAccountId))
.setTopicMemo("Memo")
.freezeWith(client)
Expand All @@ -159,5 +158,23 @@ public static void main(String[] args) throws Exception {
.execute(client);

System.out.println("The public key of the newly created and now complete account: " + accountInfo2.key);

// Clean up
new AccountDeleteTransaction()
.setTransferAccountId(OPERATOR_ID)
.setAccountId(newAccountId)
.freezeWith(client)
.sign(privateKey)
.execute(client)
.getReceipt(client);

new TopicDeleteTransaction()
.setTopicId(receipt2.topicId)
.freezeWith(client)
.sign(privateKey)
.execute(client)
.getReceipt(client);

client.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,9 @@
*/
package com.hedera.hashgraph.sdk.examples;

import com.hedera.hashgraph.sdk.AccountId;
import com.hedera.hashgraph.sdk.Client;
import com.hedera.hashgraph.sdk.KeyList;
import com.hedera.hashgraph.sdk.PrivateKey;
import com.hedera.hashgraph.sdk.PublicKey;
import com.hedera.hashgraph.sdk.TokenCreateTransaction;
import com.hedera.hashgraph.sdk.TokenInfoQuery;
import com.hedera.hashgraph.sdk.TokenKeyValidation;
import com.hedera.hashgraph.sdk.TokenType;
import com.hedera.hashgraph.sdk.TokenUpdateTransaction;
import com.hedera.hashgraph.sdk.*;
import io.github.cdimascio.dotenv.Dotenv;

import java.util.Objects;

public class ChangeRemoveTokenKeys {
Expand Down Expand Up @@ -163,6 +155,9 @@ public static void main(String[] args) throws Exception {

System.out.println("Supply Key (after removal):" + supplyKeyAfterRemoval.toStringRaw());

// Clean up
// Can't delete a token as it is immutable

client.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,7 @@
package com.hedera.hashgraph.sdk.examples;

import com.google.errorprone.annotations.Var;
import com.google.protobuf.InvalidProtocolBufferException;
import com.hedera.hashgraph.sdk.AccountId;
import com.hedera.hashgraph.sdk.Client;
import com.hedera.hashgraph.sdk.PrecheckStatusException;
import com.hedera.hashgraph.sdk.PrivateKey;
import com.hedera.hashgraph.sdk.ReceiptStatusException;
import com.hedera.hashgraph.sdk.TopicCreateTransaction;
import com.hedera.hashgraph.sdk.TopicId;
import com.hedera.hashgraph.sdk.TopicMessageQuery;
import com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction;
import com.hedera.hashgraph.sdk.Transaction;
import com.hedera.hashgraph.sdk.*;
import io.github.cdimascio.dotenv.Dotenv;

import java.io.BufferedReader;
Expand All @@ -55,7 +45,7 @@ public final class ConsensusPubSubChunkedExample {
private ConsensusPubSubChunkedExample() {
}

public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException, InterruptedException, InvalidProtocolBufferException {
public static void main(String[] args) throws Exception {
Client client = ClientHelper.forName(HEDERA_NETWORK);

// Defaults the operator account ID and key such that all generated transactions will be paid for
Expand All @@ -68,6 +58,7 @@ public static void main(String[] args) throws TimeoutException, PrecheckStatusEx
// make a new topic ID to use
TopicId newTopicId = new TopicCreateTransaction()
.setTopicMemo("hedera-sdk-java/ConsensusPubSubChunkedExample")
.setAdminKey(OPERATOR_KEY.getPublicKey())
.setSubmitKey(submitKey)
.execute(client)
.getReceipt(client)
Expand All @@ -79,7 +70,7 @@ public static void main(String[] args) throws TimeoutException, PrecheckStatusEx

// Let's wait a bit
System.out.println("wait 10s to propagate to the mirror ...");
Thread.sleep(10000);
Thread.sleep(10_000);

// setup a mirror client to print out messages as we receive them
new TopicMessageQuery()
Expand All @@ -90,7 +81,7 @@ public static void main(String[] args) throws TimeoutException, PrecheckStatusEx
});

// get a large file to send
String bigContents = readResources("large_message.txt");
String bigContents = readResources("util/large_message.txt");

System.out.println("about to prepare a transaction to send a message of " + bigContents.length() + " bytes");

Expand Down Expand Up @@ -123,6 +114,16 @@ public static void main(String[] args) throws TimeoutException, PrecheckStatusEx
transaction.execute(client).getReceipt(client);

boolean largeMessageReceived = largeMessageLatch.await(60, TimeUnit.SECONDS);

// Clean up
new TopicDeleteTransaction()
.setTopicId(newTopicId)
.execute(client)
.getReceipt(client);

client.close();

// Edge case
if (!largeMessageReceived) {
throw new TimeoutException("Large topic message was not received!");
}
Expand Down
Loading
Loading