diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 378c458f4..8f8b80e33 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -89,3 +89,42 @@ jobs: - name: Stop the local node run: npx @hashgraph/hedera-local stop + + examples: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis + - name: Set up JDK 17 + uses: actions/setup-java@v2 + with: + java-version: '17' + distribution: 'adopt' + - name: Cache Gradle packages + uses: actions/cache@v2 + with: + path: | + ~/.gradle/caches + ~/.gradle/wrapper + key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} + restore-keys: | + ${{ runner.os }}-gradle- + - name: Compile SDK + run: ./gradlew compileJava + - name: Compile Examples + run: ./gradlew :examples:compileJava + + - name: Start the local node + run: npx @hashgraph/hedera-local@2.13.0 start -d --network local + + - name: Prepare .env for Examples + run: | + echo "OPERATOR_KEY=0xa608e2130a0a3cb34f86e757303c862bee353d9ab77ba4387ec084f881d420d4" > examples/.env + echo "OPERATOR_ID=0.0.1022" >> examples/.env + echo "HEDERA_NETWORK=localhost" >> examples/.env + + - name: Run Examples + run: | + ./gradlew :examples:runAllExamples diff --git a/examples/build.gradle b/examples/build.gradle index 04ef84045..aac93e88b 100644 --- a/examples/build.gradle +++ b/examples/build.gradle @@ -8,6 +8,39 @@ dependencies { implementation "com.google.errorprone:error_prone_core:2.21.1" } +task runAllExamples { + def exampleClasses = fileTree('src/main/java') + .toList() + .stream() + .filter { it.name.endsWith('Example.java') } + .map { it.name.replaceAll('\\.java$', '') } + .filter { !it.equals('ValidateChecksumExample') } // disabled this example, because it needs user input (but it WORKS) + .filter { !it.equals('SolidityPrecompileExample') } // doesn't work with hedera-local-node + .toList() + + exampleClasses.each { className -> + def multilineString = """ + + ---EXECUTING $className: + + """ + + doLast { + javaexec { + println multilineString + + classpath = sourceSets.main.runtimeClasspath + main = className + standardInput(System.in) + + // NOTE: Uncomment to enable trace logs in the SDK during the examples + // jvmArgs "-Dorg.slf4j.simpleLogger.log.com.hedera.hashgraph=trace" + } + } + } + +} + tasks.addRule("Pattern: run: Runs an example.") { String taskName -> if (taskName.startsWith("run")) { task(taskName, type: JavaExec) { diff --git a/examples/src/main/java/AccountAliasExample.java b/examples/src/main/java/AccountAliasExample.java index bc6c6a58c..4191c19e9 100644 --- a/examples/src/main/java/AccountAliasExample.java +++ b/examples/src/main/java/AccountAliasExample.java @@ -43,8 +43,9 @@ 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 { - Client client = Client.forName(HEDERA_NETWORK); + public static void main(String[] args) + throws TimeoutException, PrecheckStatusException, ReceiptStatusException, InterruptedException { + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/AccountAllowanceExample.java b/examples/src/main/java/AccountAllowanceExample.java index 779b20ff7..79ec36e0a 100644 --- a/examples/src/main/java/AccountAllowanceExample.java +++ b/examples/src/main/java/AccountAllowanceExample.java @@ -57,15 +57,17 @@ public class AccountAllowanceExample { private final PrivateKey charlieKey; private final AccountId charlieId; - public static void main(String[] args) throws PrecheckStatusException, TimeoutException, ReceiptStatusException { + public static void main(String[] args) + throws PrecheckStatusException, TimeoutException, ReceiptStatusException, InterruptedException { AccountAllowanceExample example = new AccountAllowanceExample(); example.demonstrateAllowances(); example.cleanUp(); System.out.println("End of example"); } - private AccountAllowanceExample() throws PrecheckStatusException, TimeoutException, ReceiptStatusException { - client = Client.forName(HEDERA_NETWORK); + private AccountAllowanceExample() + throws PrecheckStatusException, TimeoutException, ReceiptStatusException, InterruptedException { + client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/AccountCreateWithHtsExample.java b/examples/src/main/java/AccountCreateWithHtsExample.java index e3adb676e..c7dfe62a9 100644 --- a/examples/src/main/java/AccountCreateWithHtsExample.java +++ b/examples/src/main/java/AccountCreateWithHtsExample.java @@ -36,7 +36,7 @@ private AccountCreateWithHtsExample() { } public static void main(String[] args) throws NullPointerException, PrecheckStatusException, ReceiptStatusException, InterruptedException, TimeoutException { - Client client = Client.forName(HEDERA_NETWORK); + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/AutoCreateAccountTransferTransactionExample.java b/examples/src/main/java/AutoCreateAccountTransferTransactionExample.java index e1fda5131..bde6d4440 100644 --- a/examples/src/main/java/AutoCreateAccountTransferTransactionExample.java +++ b/examples/src/main/java/AutoCreateAccountTransferTransactionExample.java @@ -42,7 +42,7 @@ private AutoCreateAccountTransferTransactionExample() { - Get the `AccountInfo` for the account and return the public key on the account to show it is a complete account */ public static void main(String[] args) throws PrecheckStatusException, TimeoutException, ReceiptStatusException, InterruptedException, IOException { - Client client = Client.forName(HEDERA_NETWORK); + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/ClientHelper.java b/examples/src/main/java/ClientHelper.java new file mode 100644 index 000000000..3fb8d046f --- /dev/null +++ b/examples/src/main/java/ClientHelper.java @@ -0,0 +1,27 @@ +import com.hedera.hashgraph.sdk.AccountId; +import com.hedera.hashgraph.sdk.Client; +import java.util.HashMap; +import java.util.List; + +public class ClientHelper { + public static final String LOCAL_NETWORK_NAME = "localhost"; + private static final String DEFAULT_LOCAL_NODE_ADDRESS = "127.0.0.1:50211"; + private static final String DEFAULT_LOCAL_MIRROR_NODE_ADDRESS = "127.0.0.1:5600"; + + public static Client forName(String network) throws InterruptedException { + if (network.equals(LOCAL_NETWORK_NAME)) { + return forLocalNetwork(); + } else { + return Client.forName(network); + } + } + + public static Client forLocalNetwork() throws InterruptedException { + var network = new HashMap(); + network.put(DEFAULT_LOCAL_NODE_ADDRESS, new AccountId(3)); + + return Client + .forNetwork(network) + .setMirrorNetwork(List.of(DEFAULT_LOCAL_MIRROR_NODE_ADDRESS)); + } +} diff --git a/examples/src/main/java/ConsensusPubSubChunkedExample.java b/examples/src/main/java/ConsensusPubSubChunkedExample.java index 507f38ee7..13e6806a1 100644 --- a/examples/src/main/java/ConsensusPubSubChunkedExample.java +++ b/examples/src/main/java/ConsensusPubSubChunkedExample.java @@ -36,6 +36,8 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.util.Objects; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import static java.nio.charset.StandardCharsets.UTF_8; @@ -46,11 +48,13 @@ public final class ConsensusPubSubChunkedExample { // HEDERA_NETWORK defaults to testnet if not specified in dotenv private static final String HEDERA_NETWORK = Dotenv.load().get("HEDERA_NETWORK", "testnet"); + private static final CountDownLatch largeMessageLatch = new CountDownLatch(1); + private ConsensusPubSubChunkedExample() { } public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException, InterruptedException, InvalidProtocolBufferException { - Client client = Client.forName(HEDERA_NETWORK); + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key @@ -80,6 +84,7 @@ public static void main(String[] args) throws TimeoutException, PrecheckStatusEx .setTopicId(newTopicId) .subscribe(client, topicMessage -> { System.out.println("at " + topicMessage.consensusTimestamp + " ( seq = " + topicMessage.sequenceNumber + " ) received topic message of " + topicMessage.contents.length + " bytes"); + largeMessageLatch.countDown(); }); // get a large file to send @@ -115,12 +120,9 @@ public static void main(String[] args) throws TimeoutException, PrecheckStatusEx // get the receipt to ensure there were no errors transaction.execute(client).getReceipt(client); - // noinspection InfiniteLoopStatement - while (true) { - System.out.println("waiting ..."); - - // noinspection BusyWait - Thread.sleep(2500); + boolean largeMessageReceived = largeMessageLatch.await(30, TimeUnit.SECONDS); + if (!largeMessageReceived) { + throw new TimeoutException("Large topic message was not received!"); } } diff --git a/examples/src/main/java/ConsensusPubSubExample.java b/examples/src/main/java/ConsensusPubSubExample.java index 2e47365b0..9403fa898 100644 --- a/examples/src/main/java/ConsensusPubSubExample.java +++ b/examples/src/main/java/ConsensusPubSubExample.java @@ -32,6 +32,8 @@ import java.nio.charset.StandardCharsets; import java.util.Objects; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -41,11 +43,14 @@ class ConsensusPubSubExample { // HEDERA_NETWORK defaults to testnet if not specified in dotenv private static final String HEDERA_NETWORK = Dotenv.load().get("HEDERA_NETWORK", "testnet"); + private static final int TOTAL_MESSAGES = 5; + private static final CountDownLatch messagesLatch = new CountDownLatch(TOTAL_MESSAGES); + private ConsensusPubSubExample() { } public static void main(String[] args) throws TimeoutException, InterruptedException, PrecheckStatusException, ReceiptStatusException { - Client client = Client.forName(HEDERA_NETWORK); + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key @@ -68,11 +73,10 @@ public static void main(String[] args) throws TimeoutException, InterruptedExcep String messageAsString = new String(resp.contents, StandardCharsets.UTF_8); System.out.println(resp.consensusTimestamp + " received topic message: " + messageAsString); + messagesLatch.countDown(); }); - // keep the main thread from exiting because the listeners run on daemon threads - // noinspection InfiniteLoopStatement - for (int i = 0; ; i++) { + for (int i = 0; i <= TOTAL_MESSAGES; i++) { new TopicMessageSubmitTransaction() .setTopicId(topicId) .setMessage("hello, HCS! " + i) @@ -81,5 +85,10 @@ public static void main(String[] args) throws TimeoutException, InterruptedExcep Thread.sleep(2500); } + + boolean allMessagesReceived = messagesLatch.await(30, TimeUnit.SECONDS); + if (!allMessagesReceived) { + throw new TimeoutException("Not all topic messages were received!"); + } } } diff --git a/examples/src/main/java/ConsensusPubSubWithSubmitKeyExample.java b/examples/src/main/java/ConsensusPubSubWithSubmitKeyExample.java index 56b235a18..3e99b4c0b 100644 --- a/examples/src/main/java/ConsensusPubSubWithSubmitKeyExample.java +++ b/examples/src/main/java/ConsensusPubSubWithSubmitKeyExample.java @@ -58,7 +58,8 @@ public class ConsensusPubSubWithSubmitKeyExample { private TopicId topicId; private PrivateKey submitKey; - public ConsensusPubSubWithSubmitKeyExample(int messagesToPublish, int millisBetweenMessages) { + public ConsensusPubSubWithSubmitKeyExample(int messagesToPublish, int millisBetweenMessages) + throws InterruptedException { this.messagesToPublish = messagesToPublish; this.millisBetweenMessages = millisBetweenMessages; setupClient(); @@ -77,8 +78,8 @@ public void execute() throws TimeoutException, InterruptedException, PrecheckSta publishMessagesToTopic(); } - private void setupClient() { - client = Client.forName(HEDERA_NETWORK); + private void setupClient() throws InterruptedException { + client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for by this // account and be signed by this key diff --git a/examples/src/main/java/ConstructClientExample.java b/examples/src/main/java/ConstructClientExample.java index 622fcd5a3..2aaacad4d 100644 --- a/examples/src/main/java/ConstructClientExample.java +++ b/examples/src/main/java/ConstructClientExample.java @@ -33,8 +33,7 @@ public class ConstructClientExample { // or set environment variables with the same names @Nullable private static final String CONFIG_FILE = Dotenv.load().get("CONFIG_FILE"); - // HEDERA_NETWORK defaults to testnet if not specified in dotenv - private static final String HEDERA_NETWORK = Dotenv.load().get("HEDERA_NETWORK", "testnet"); + private static final String HEDERA_NETWORK = "testnet"; ConstructClientExample() { } diff --git a/examples/src/main/java/ContractNoncesExample.java b/examples/src/main/java/ContractNoncesExample.java index e91caa67f..19a00e102 100644 --- a/examples/src/main/java/ContractNoncesExample.java +++ b/examples/src/main/java/ContractNoncesExample.java @@ -53,8 +53,9 @@ public final class ContractNoncesExample { private ContractNoncesExample() { } - public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException { - Client client = Client.forName(HEDERA_NETWORK); + public static void main(String[] args) + throws TimeoutException, PrecheckStatusException, ReceiptStatusException, InterruptedException { + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/CreateAccountExample.java b/examples/src/main/java/CreateAccountExample.java index c80b4019b..fe88c195a 100644 --- a/examples/src/main/java/CreateAccountExample.java +++ b/examples/src/main/java/CreateAccountExample.java @@ -44,8 +44,9 @@ public final class CreateAccountExample { private CreateAccountExample() { } - public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException { - Client client = Client.forName(HEDERA_NETWORK); + public static void main(String[] args) + throws TimeoutException, PrecheckStatusException, ReceiptStatusException, InterruptedException { + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/CreateAccountThresholdKeyExample.java b/examples/src/main/java/CreateAccountThresholdKeyExample.java index a8884ed28..cbddfef45 100644 --- a/examples/src/main/java/CreateAccountThresholdKeyExample.java +++ b/examples/src/main/java/CreateAccountThresholdKeyExample.java @@ -48,8 +48,9 @@ public final class CreateAccountThresholdKeyExample { private CreateAccountThresholdKeyExample() { } - public static void main(String[] args) throws PrecheckStatusException, TimeoutException, ReceiptStatusException { - Client client = Client.forName(HEDERA_NETWORK); + public static void main(String[] args) + throws PrecheckStatusException, TimeoutException, ReceiptStatusException, InterruptedException { + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/CreateAccountWithAliasAndReceiverSignatureRequiredExample.java b/examples/src/main/java/CreateAccountWithAliasAndReceiverSignatureRequiredExample.java index 17da4e18a..3c999be59 100644 --- a/examples/src/main/java/CreateAccountWithAliasAndReceiverSignatureRequiredExample.java +++ b/examples/src/main/java/CreateAccountWithAliasAndReceiverSignatureRequiredExample.java @@ -24,8 +24,9 @@ private CreateAccountWithAliasAndReceiverSignatureRequiredExample() { - Sign the `AccountCreateTransaction` transaction with both the new private key and the admin key - Get the `AccountInfo` and show that the account has contractAccountId */ - public static void main(String[] args) throws PrecheckStatusException, TimeoutException, ReceiptStatusException { - Client client = Client.forName(HEDERA_NETWORK); + public static void main(String[] args) + throws PrecheckStatusException, TimeoutException, ReceiptStatusException, InterruptedException { + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/CreateAccountWithAliasExample.java b/examples/src/main/java/CreateAccountWithAliasExample.java index 965906cab..af7f3fb8e 100644 --- a/examples/src/main/java/CreateAccountWithAliasExample.java +++ b/examples/src/main/java/CreateAccountWithAliasExample.java @@ -25,7 +25,7 @@ private CreateAccountWithAliasExample() { - Get the `AccountInfo` and show that the account has contractAccountId */ public static void main(String[] args) throws PrecheckStatusException, TimeoutException, InterruptedException { - Client client = Client.forName(HEDERA_NETWORK); + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/CreateFileExample.java b/examples/src/main/java/CreateFileExample.java index 1f9bd3c90..3fbd39f0d 100644 --- a/examples/src/main/java/CreateFileExample.java +++ b/examples/src/main/java/CreateFileExample.java @@ -44,8 +44,9 @@ public final class CreateFileExample { private CreateFileExample() { } - public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException { - Client client = Client.forName(HEDERA_NETWORK); + public static void main(String[] args) + throws TimeoutException, PrecheckStatusException, ReceiptStatusException, InterruptedException { + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/CreateSimpleContractExample.java b/examples/src/main/java/CreateSimpleContractExample.java index ab978cfa6..81ab1a144 100644 --- a/examples/src/main/java/CreateSimpleContractExample.java +++ b/examples/src/main/java/CreateSimpleContractExample.java @@ -56,11 +56,11 @@ public final class CreateSimpleContractExample { private CreateSimpleContractExample() { } - public static void main(String[] args) throws PrecheckStatusException, IOException, TimeoutException, ReceiptStatusException { + public static void main(String[] args) + throws PrecheckStatusException, IOException, TimeoutException, ReceiptStatusException, InterruptedException { String byteCodeHex = ContractHelper.getBytecodeHex("hello_world.json"); - - Client client = Client.forName(HEDERA_NETWORK); + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/CreateStatefulContractExample.java b/examples/src/main/java/CreateStatefulContractExample.java index 58e575240..5272bc5a5 100644 --- a/examples/src/main/java/CreateStatefulContractExample.java +++ b/examples/src/main/java/CreateStatefulContractExample.java @@ -56,10 +56,11 @@ public final class CreateStatefulContractExample { private CreateStatefulContractExample() { } - public static void main(String[] args) throws PrecheckStatusException, TimeoutException, IOException, ReceiptStatusException { + public static void main(String[] args) + throws PrecheckStatusException, TimeoutException, IOException, ReceiptStatusException, InterruptedException { String byteCodeHex = ContractHelper.getBytecodeHex("stateful.json"); - Client client = Client.forName(HEDERA_NETWORK); + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/CreateTopicExample.java b/examples/src/main/java/CreateTopicExample.java index 876617c71..44267356e 100644 --- a/examples/src/main/java/CreateTopicExample.java +++ b/examples/src/main/java/CreateTopicExample.java @@ -43,8 +43,9 @@ public final class CreateTopicExample { private CreateTopicExample() { } - public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException { - Client client = Client.forName(HEDERA_NETWORK); + public static void main(String[] args) + throws TimeoutException, PrecheckStatusException, ReceiptStatusException, InterruptedException { + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/CustomFeesExample.java b/examples/src/main/java/CustomFeesExample.java index 077f4939c..ce7d6ec97 100644 --- a/examples/src/main/java/CustomFeesExample.java +++ b/examples/src/main/java/CustomFeesExample.java @@ -32,10 +32,13 @@ import com.hedera.hashgraph.sdk.TokenAssociateTransaction; import com.hedera.hashgraph.sdk.TokenCreateTransaction; import com.hedera.hashgraph.sdk.TokenDeleteTransaction; +import com.hedera.hashgraph.sdk.TokenDissociateTransaction; import com.hedera.hashgraph.sdk.TokenFeeScheduleUpdateTransaction; import com.hedera.hashgraph.sdk.TokenId; import com.hedera.hashgraph.sdk.TokenInfo; import com.hedera.hashgraph.sdk.TokenInfoQuery; +import com.hedera.hashgraph.sdk.TokenUpdateTransaction; +import com.hedera.hashgraph.sdk.TokenWipeTransaction; import com.hedera.hashgraph.sdk.TransactionRecord; import com.hedera.hashgraph.sdk.TransferTransaction; import io.github.cdimascio.dotenv.Dotenv; @@ -58,8 +61,9 @@ public final class CustomFeesExample { private CustomFeesExample() { } - public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException { - Client client = Client.forName(HEDERA_NETWORK); + public static void main(String[] args) + throws TimeoutException, PrecheckStatusException, ReceiptStatusException, InterruptedException { + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key @@ -129,6 +133,7 @@ public static void main(String[] args) throws TimeoutException, PrecheckStatusEx .setAdminKey(aliceKey) .setSupplyKey(aliceKey) .setFeeScheduleKey(aliceKey) + .setWipeKey(aliceKey) .setTreasuryAccountId(aliceId) .setCustomFees(hbarFeeList) .setInitialSupply(100) @@ -259,13 +264,74 @@ public static void main(String[] args) throws TimeoutException, PrecheckStatusEx // clean up - new TokenDeleteTransaction() + // move token to operator account + new TokenAssociateTransaction() + .setAccountId(client.getOperatorAccountId()) + .setTokenIds(Collections.singletonList(tokenId)) + .freezeWith(client) + .sign(OPERATOR_KEY) + .execute(client) + .getReceipt(client); + + new TokenUpdateTransaction() .setTokenId(tokenId) + .setAdminKey(OPERATOR_KEY) + .setSupplyKey(OPERATOR_KEY) + .setFeeScheduleKey(OPERATOR_KEY) + .setWipeKey(OPERATOR_KEY) + .setTreasuryAccountId(client.getOperatorAccountId()) .freezeWith(client) .sign(aliceKey) .execute(client) .getReceipt(client); + // wipe token on created accounts + Map charlieTokensBeforeWipe = new AccountBalanceQuery() + .setAccountId(charlieId) + .execute(client) + .tokens; + System.out.println("Charlie's token balance (before wipe): " + charlieTokensBeforeWipe.get(tokenId)); + + new TokenWipeTransaction() + .setTokenId(tokenId) + .setAmount(charlieTokensBeforeWipe.get(tokenId)) + .setAccountId(charlieId) + .freezeWith(client) + .sign(OPERATOR_KEY) + .execute(client) + .getReceipt(client); + + Map bobTokensBeforeWipe = new AccountBalanceQuery() + .setAccountId(bobId) + .execute(client) + .tokens; + System.out.println("Bob's token balance (before wipe): " + bobTokensBeforeWipe.get(tokenId)); + + new TokenWipeTransaction() + .setTokenId(tokenId) + .setAmount(bobTokensBeforeWipe.get(tokenId)) + .setAccountId(bobId) + .freezeWith(client) + .sign(OPERATOR_KEY) + .execute(client) + .getReceipt(client); + + Map aliceTokensBeforeWipe = new AccountBalanceQuery() + .setAccountId(aliceId) + .execute(client) + .tokens; + System.out.println("Alice's token balance (before wipe): " + aliceTokensBeforeWipe.get(tokenId)); + + new TokenWipeTransaction() + .setTokenId(tokenId) + .setAmount(aliceTokensBeforeWipe.get(tokenId)) + .setAccountId(aliceId) + .freezeWith(client) + .sign(OPERATOR_KEY) + .execute(client) + .getReceipt(client); + + // delete accounts and token new AccountDeleteTransaction() .setAccountId(charlieId) .setTransferAccountId(client.getOperatorAccountId()) @@ -290,6 +356,13 @@ public static void main(String[] args) throws TimeoutException, PrecheckStatusEx .execute(client) .getReceipt(client); + new TokenDeleteTransaction() + .setTokenId(tokenId) + .freezeWith(client) + .sign(OPERATOR_KEY) + .execute(client) + .getReceipt(client); + client.close(); } } diff --git a/examples/src/main/java/DeleteAccountExample.java b/examples/src/main/java/DeleteAccountExample.java index f2d5c630c..ea8e5ecaf 100644 --- a/examples/src/main/java/DeleteAccountExample.java +++ b/examples/src/main/java/DeleteAccountExample.java @@ -46,8 +46,9 @@ public final class DeleteAccountExample { private DeleteAccountExample() { } - public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException { - Client client = Client.forName(HEDERA_NETWORK); + public static void main(String[] args) + throws TimeoutException, PrecheckStatusException, ReceiptStatusException, InterruptedException { + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/DeleteFileExample.java b/examples/src/main/java/DeleteFileExample.java index 440ee900a..d0a379db3 100644 --- a/examples/src/main/java/DeleteFileExample.java +++ b/examples/src/main/java/DeleteFileExample.java @@ -46,8 +46,9 @@ public final class DeleteFileExample { private DeleteFileExample() { } - public static void main(String[] args) throws PrecheckStatusException, TimeoutException, ReceiptStatusException { - Client client = Client.forName(HEDERA_NETWORK); + public static void main(String[] args) + throws PrecheckStatusException, TimeoutException, ReceiptStatusException, InterruptedException { + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/ExemptCustomFeesExample.java b/examples/src/main/java/ExemptCustomFeesExample.java index d8e3bed62..17d99728a 100644 --- a/examples/src/main/java/ExemptCustomFeesExample.java +++ b/examples/src/main/java/ExemptCustomFeesExample.java @@ -48,7 +48,7 @@ private ExemptCustomFeesExample() { } public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException, InterruptedException { - Client client = Client.forName(HEDERA_NETWORK); + Client client = ClientHelper.forName(HEDERA_NETWORK); client.setOperator(OPERATOR_ID, OPERATOR_KEY); /* diff --git a/examples/src/main/java/FileAppendChunkedExample.java b/examples/src/main/java/FileAppendChunkedExample.java index aab6c7e6b..98f15c083 100644 --- a/examples/src/main/java/FileAppendChunkedExample.java +++ b/examples/src/main/java/FileAppendChunkedExample.java @@ -48,8 +48,9 @@ public class FileAppendChunkedExample { private FileAppendChunkedExample() { } - public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException { - Client client = Client.forName(HEDERA_NETWORK); + public static void main(String[] args) + throws TimeoutException, PrecheckStatusException, ReceiptStatusException, InterruptedException { + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/GetAccountBalanceExample.java b/examples/src/main/java/GetAccountBalanceExample.java index 7f53ea75e..17a724311 100644 --- a/examples/src/main/java/GetAccountBalanceExample.java +++ b/examples/src/main/java/GetAccountBalanceExample.java @@ -38,8 +38,8 @@ public final class GetAccountBalanceExample { private GetAccountBalanceExample() { } - public static void main(String[] args) throws PrecheckStatusException, TimeoutException { - Client client = Client.forName(HEDERA_NETWORK); + public static void main(String[] args) throws PrecheckStatusException, TimeoutException, InterruptedException { + Client client = ClientHelper.forName(HEDERA_NETWORK); // Because AccountBalanceQuery is a free query, we can make it without setting an operator on the client. diff --git a/examples/src/main/java/GetAccountInfoExample.java b/examples/src/main/java/GetAccountInfoExample.java index bc6b3add0..7d2ea163e 100644 --- a/examples/src/main/java/GetAccountInfoExample.java +++ b/examples/src/main/java/GetAccountInfoExample.java @@ -41,8 +41,8 @@ public final class GetAccountInfoExample { private GetAccountInfoExample() { } - public static void main(String[] args) throws PrecheckStatusException, TimeoutException { - Client client = Client.forName(HEDERA_NETWORK); + public static void main(String[] args) throws PrecheckStatusException, TimeoutException, InterruptedException { + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/GetAddressBookExample.java b/examples/src/main/java/GetAddressBookExample.java index 7fc36c364..2c0af5f0b 100644 --- a/examples/src/main/java/GetAddressBookExample.java +++ b/examples/src/main/java/GetAddressBookExample.java @@ -48,7 +48,7 @@ public static void main(String[] args) throws InterruptedException, IOException System.out.println("Getting address book for " + HEDERA_NETWORK); - Client client = Client.forName(HEDERA_NETWORK); + Client client = ClientHelper.forName(HEDERA_NETWORK); NodeAddressBook addressBook = new AddressBookQuery() .setFileId(FileId.ADDRESS_BOOK) diff --git a/examples/src/main/java/GetExchangeRatesExample.java b/examples/src/main/java/GetExchangeRatesExample.java index f5604fc0b..956a61430 100644 --- a/examples/src/main/java/GetExchangeRatesExample.java +++ b/examples/src/main/java/GetExchangeRatesExample.java @@ -43,8 +43,9 @@ public final class GetExchangeRatesExample { private GetExchangeRatesExample() { } - public static void main(String[] args) throws ReceiptStatusException, TimeoutException, PrecheckStatusException, InvalidProtocolBufferException { - Client client = Client.forName(HEDERA_NETWORK); + public static void main(String[] args) + throws ReceiptStatusException, TimeoutException, PrecheckStatusException, InvalidProtocolBufferException, InterruptedException { + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/GetFileContentsExample.java b/examples/src/main/java/GetFileContentsExample.java index eb0a6d3bd..487e66763 100644 --- a/examples/src/main/java/GetFileContentsExample.java +++ b/examples/src/main/java/GetFileContentsExample.java @@ -45,8 +45,9 @@ public final class GetFileContentsExample { private GetFileContentsExample() { } - public static void main(String[] args) throws ReceiptStatusException, TimeoutException, PrecheckStatusException { - Client client = Client.forName(HEDERA_NETWORK); + public static void main(String[] args) + throws ReceiptStatusException, TimeoutException, PrecheckStatusException, InterruptedException { + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/LoggerFunctionalitiesExample.java b/examples/src/main/java/LoggerFunctionalitiesExample.java index 0e0ffad15..658f22f03 100644 --- a/examples/src/main/java/LoggerFunctionalitiesExample.java +++ b/examples/src/main/java/LoggerFunctionalitiesExample.java @@ -13,14 +13,14 @@ public class LoggerFunctionalitiesExample { // 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 PrecheckStatusException, TimeoutException { + public static void main(String[] args) throws PrecheckStatusException, TimeoutException, InterruptedException { var debugLogger = new Logger(LogLevel.DEBUG); var infoLogger = new Logger(LogLevel.INFO); - Client client = Client - .forName(HEDERA_NETWORK) - .setLogger(debugLogger) - .setOperator(OPERATOR_ID, OPERATOR_KEY); + Client client = ClientHelper.forName(HEDERA_NETWORK); + + client.setLogger(debugLogger); + client.setOperator(OPERATOR_ID, OPERATOR_KEY); var privateKey = PrivateKey.generateED25519(); var publicKey = privateKey.getPublicKey(); diff --git a/examples/src/main/java/MultiAppTransferExample.java b/examples/src/main/java/MultiAppTransferExample.java index 16052ca13..072ea95e5 100644 --- a/examples/src/main/java/MultiAppTransferExample.java +++ b/examples/src/main/java/MultiAppTransferExample.java @@ -49,7 +49,11 @@ public final class MultiAppTransferExample { private static final Client client; static { - client = Client.forName(HEDERA_NETWORK); + try { + client = ClientHelper.forName(HEDERA_NETWORK); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/MultiSigOfflineExample.java b/examples/src/main/java/MultiSigOfflineExample.java index e5ff0a063..ca1b2e7dd 100644 --- a/examples/src/main/java/MultiSigOfflineExample.java +++ b/examples/src/main/java/MultiSigOfflineExample.java @@ -49,9 +49,9 @@ public final class MultiSigOfflineExample { private MultiSigOfflineExample() { } - public static void main(String[] args) throws PrecheckStatusException, TimeoutException, ReceiptStatusException, InvalidProtocolBufferException { - - Client client = Client.forName(HEDERA_NETWORK); + public static void main(String[] args) + throws PrecheckStatusException, TimeoutException, ReceiptStatusException, InvalidProtocolBufferException, InterruptedException { + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/NftAddRemoveAllowancesExample.java b/examples/src/main/java/NftAddRemoveAllowancesExample.java index 73a6c369e..5a9d6f0f7 100644 --- a/examples/src/main/java/NftAddRemoveAllowancesExample.java +++ b/examples/src/main/java/NftAddRemoveAllowancesExample.java @@ -50,8 +50,9 @@ public final class NftAddRemoveAllowancesExample { private NftAddRemoveAllowancesExample() { } - public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException { - Client client = Client.forName(HEDERA_NETWORK); + public static void main(String[] args) + throws TimeoutException, PrecheckStatusException, ReceiptStatusException, InterruptedException { + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/PrngExample.java b/examples/src/main/java/PrngExample.java index 1b9777a95..76461689d 100644 --- a/examples/src/main/java/PrngExample.java +++ b/examples/src/main/java/PrngExample.java @@ -35,8 +35,9 @@ public final class PrngExample { private PrngExample() { } - public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException { - Client client = Client.forName(HEDERA_NETWORK); + public static void main(String[] args) + throws TimeoutException, PrecheckStatusException, ReceiptStatusException, InterruptedException { + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/ScheduleExample.java b/examples/src/main/java/ScheduleExample.java index 962f18920..75342ddd8 100644 --- a/examples/src/main/java/ScheduleExample.java +++ b/examples/src/main/java/ScheduleExample.java @@ -39,8 +39,9 @@ public final class ScheduleExample { private ScheduleExample() { } - public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException { - Client client = Client.forName(HEDERA_NETWORK); + public static void main(String[] args) + throws TimeoutException, PrecheckStatusException, ReceiptStatusException, InterruptedException { + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/ScheduleIdenticalTransactionExample.java b/examples/src/main/java/ScheduleIdenticalTransactionExample.java index 90c3f160c..5dee05d1b 100644 --- a/examples/src/main/java/ScheduleIdenticalTransactionExample.java +++ b/examples/src/main/java/ScheduleIdenticalTransactionExample.java @@ -55,9 +55,9 @@ public class ScheduleIdenticalTransactionExample { private ScheduleIdenticalTransactionExample() { } - public static void main(String[] args) throws PrecheckStatusException, TimeoutException, ReceiptStatusException { - - Client client = Client.forName(HEDERA_NETWORK); + public static void main(String[] args) + throws PrecheckStatusException, TimeoutException, ReceiptStatusException, InterruptedException { + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key @@ -92,7 +92,7 @@ public static void main(String[] args) throws PrecheckStatusException, TimeoutEx // Make sure the transaction succeeded TransactionReceipt transactionReceipt = createResponse.getReceipt(client); - Client newClient = Client.forName(HEDERA_NETWORK); + Client newClient = ClientHelper.forName(HEDERA_NETWORK); newClient.setOperator(Objects.requireNonNull(transactionReceipt.accountId), newKey); clients[i] = newClient; accounts[i] = transactionReceipt.accountId; diff --git a/examples/src/main/java/ScheduleMultiSigTransactionExample.java b/examples/src/main/java/ScheduleMultiSigTransactionExample.java index 4331c8a45..70aca2753 100644 --- a/examples/src/main/java/ScheduleMultiSigTransactionExample.java +++ b/examples/src/main/java/ScheduleMultiSigTransactionExample.java @@ -52,7 +52,7 @@ public class ScheduleMultiSigTransactionExample { } public static void main(String[] args) throws Exception { - Client client = Client.forName(HEDERA_NETWORK); + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/ScheduledTransactionMultiSigThresholdExample.java b/examples/src/main/java/ScheduledTransactionMultiSigThresholdExample.java index 06b9ad1ba..34a980e96 100644 --- a/examples/src/main/java/ScheduledTransactionMultiSigThresholdExample.java +++ b/examples/src/main/java/ScheduledTransactionMultiSigThresholdExample.java @@ -56,10 +56,9 @@ public class ScheduledTransactionMultiSigThresholdExample { private ScheduledTransactionMultiSigThresholdExample() { } -// public static void main(String[] args) throws PrecheckStatusException, IOException, TimeoutException, ReceiptStatusException { - public static void main(String[] args) throws PrecheckStatusException, TimeoutException, ReceiptStatusException { - - Client client = Client.forName(HEDERA_NETWORK); + public static void main(String[] args) + throws PrecheckStatusException, TimeoutException, ReceiptStatusException, InterruptedException { + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/ScheduledTransferExample.java b/examples/src/main/java/ScheduledTransferExample.java index 2e8a5397b..0386fb629 100644 --- a/examples/src/main/java/ScheduledTransferExample.java +++ b/examples/src/main/java/ScheduledTransferExample.java @@ -51,8 +51,9 @@ public final class ScheduledTransferExample { private ScheduledTransferExample() { } - public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException { - Client client = Client.forName(HEDERA_NETWORK); + public static void main(String[] args) + throws TimeoutException, PrecheckStatusException, ReceiptStatusException, InterruptedException { + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key @@ -97,8 +98,8 @@ public static void main(String[] args) throws TimeoutException, PrecheckStatusEx .accountId; Objects.requireNonNull(bobsId); - System.out.println("Alice's ID: " + client.getOperatorAccountId().toStringWithChecksum(client)); - System.out.println("Bob's ID: " + bobsId.toStringWithChecksum(client)); + System.out.println("Alice's ID: " + client.getOperatorAccountId()); + System.out.println("Bob's ID: " + bobsId); AccountBalance bobsInitialBalance = new AccountBalanceQuery() .setAccountId(bobsId) @@ -134,7 +135,7 @@ public static void main(String[] args) throws TimeoutException, PrecheckStatusEx .getReceipt(client) .scheduleId; Objects.requireNonNull(scheduleId); - System.out.println("The scheduleId is: " + scheduleId.toStringWithChecksum(client)); + System.out.println("The scheduleId is: " + scheduleId); /* * Bob's balance should be unchanged. The transfer has been scheduled, but it hasn't been executed yet diff --git a/examples/src/main/java/SignTransactionExample.java b/examples/src/main/java/SignTransactionExample.java index 31971ca77..0dea8fc35 100644 --- a/examples/src/main/java/SignTransactionExample.java +++ b/examples/src/main/java/SignTransactionExample.java @@ -47,9 +47,9 @@ public class SignTransactionExample { private SignTransactionExample() { } - public static void main(String[] args) throws PrecheckStatusException, TimeoutException, ReceiptStatusException { - - Client client = Client.forName(HEDERA_NETWORK); + public static void main(String[] args) + throws PrecheckStatusException, TimeoutException, ReceiptStatusException, InterruptedException { + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/SolidityPrecompileExample.java b/examples/src/main/java/SolidityPrecompileExample.java index 452fcf877..da7c152f9 100644 --- a/examples/src/main/java/SolidityPrecompileExample.java +++ b/examples/src/main/java/SolidityPrecompileExample.java @@ -39,86 +39,80 @@ public class SolidityPrecompileExample { private SolidityPrecompileExample() { } - public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException, IOException { - try (Client client = Client.forName(HEDERA_NETWORK)) { - - // Defaults the operator account ID and key such that all generated transactions will be paid for - // by this account and be signed by this key - client.setOperator(OPERATOR_ID, OPERATOR_KEY); - - // We need a new account for the contract to interact with in some of its steps - - PrivateKey alicePrivateKey = PrivateKey.generateED25519(); - PublicKey alicePublicKey = alicePrivateKey.getPublicKey(); - AccountId aliceAccountId = Objects.requireNonNull(new AccountCreateTransaction() - .setKey(alicePublicKey) - .setInitialBalance(Hbar.fromTinybars(1000)) - .execute(client) - .getReceipt(client) - .accountId - ); - - // Instantiate ContractHelper - - ContractHelper contractHelper = new ContractHelper( - "precompile-example/PrecompileExample.json", - new ContractFunctionParameters() - .addAddress(OPERATOR_ID.toSolidityAddress()) - .addAddress(aliceAccountId.toSolidityAddress()), - client - ); - - // Configure steps in ContracHelper - - contractHelper - .setResultValidatorForStep(0, contractFunctionResult -> { - System.out.println("getPseudoRandomSeed() returned " + Arrays.toString(contractFunctionResult.getBytes32(0))); - return true; - }).setPayableAmountForStep(1, Hbar.from(20)) - // step 3 associates Alice with the token, which requires Alice's signature - .addSignerForStep(3, alicePrivateKey) - .addSignerForStep(5, alicePrivateKey) - .setParameterSupplierForStep(11, () -> { - return new ContractFunctionParameters() - // when contracts work with a public key, they handle the raw bytes of the public key - .addBytes(alicePublicKey.toBytesRaw()); - }).setPayableAmountForStep(11, Hbar.from(40)) - // Because we're setting the adminKey for the created NFT token to Alice's key, - // Alice must sign the ContractExecuteTransaction. - .addSignerForStep(11, alicePrivateKey) - // and Alice must sign for minting because her key is the supply key. - .addSignerForStep(12, alicePrivateKey) - .setParameterSupplierForStep(12, () -> { - return new ContractFunctionParameters() - // add three metadatas - .addBytesArray(new byte[][]{new byte[]{0x01b}, new byte[]{0x02b}, new byte[]{0x03b}}); - }) // and alice must sign to become associated with the token. - .addSignerForStep(13, alicePrivateKey) - // Alice must sign to burn the token because her key is the supply key - .addSignerForStep(16, alicePrivateKey); - - // step 0 tests pseudo random number generator (PRNG) - // step 1 creates a fungible token - // step 2 mints it - // step 3 associates Alice with it - // step 4 transfers it to Alice. - // step 5 approves an allowance of the fungible token with operator as the owner and Alice as the spender [NOT WORKING] - // steps 6 - 10 test misc functions on the fungible token (see PrecompileExample.sol for details). - // step 11 creates an NFT token with a custom fee, and with the admin and supply set to Alice's key - // step 12 mints some NFTs - // step 13 associates Alice with the NFT token - // step 14 transfers some NFTs to Alice - // step 15 approves an NFT allowance with operator as the owner and Alice as the spender [NOT WORKING] - // step 16 burn some NFTs - - contractHelper - .executeSteps(/* from step */ 0, /* to step */ 16, client) - ; - - System.out.println("All steps completed with valid results."); - - } catch (Exception e) { - e.printStackTrace(); - } + 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 + // by this account and be signed by this key + client.setOperator(OPERATOR_ID, OPERATOR_KEY); + + // We need a new account for the contract to interact with in some of its steps + + PrivateKey alicePrivateKey = PrivateKey.generateED25519(); + PublicKey alicePublicKey = alicePrivateKey.getPublicKey(); + AccountId aliceAccountId = Objects.requireNonNull(new AccountCreateTransaction() + .setKey(alicePublicKey) + .setInitialBalance(Hbar.fromTinybars(1000)) + .execute(client) + .getReceipt(client) + .accountId + ); + + // Instantiate ContractHelper + + ContractHelper contractHelper = new ContractHelper( + "precompile-example/PrecompileExample.json", + new ContractFunctionParameters() + .addAddress(OPERATOR_ID.toSolidityAddress()) + .addAddress(aliceAccountId.toSolidityAddress()), + client + ); + + // Configure steps in ContracHelper + + contractHelper + .setResultValidatorForStep(0, contractFunctionResult -> { + System.out.println("getPseudoRandomSeed() returned " + Arrays.toString(contractFunctionResult.getBytes32(0))); + return true; + }).setPayableAmountForStep(1, Hbar.from(20)) + // step 3 associates Alice with the token, which requires Alice's signature + .addSignerForStep(3, alicePrivateKey) + .addSignerForStep(5, alicePrivateKey) + .setParameterSupplierForStep(11, () -> { + return new ContractFunctionParameters() + // when contracts work with a public key, they handle the raw bytes of the public key + .addBytes(alicePublicKey.toBytesRaw()); + }).setPayableAmountForStep(11, Hbar.from(40)) + // Because we're setting the adminKey for the created NFT token to Alice's key, + // Alice must sign the ContractExecuteTransaction. + .addSignerForStep(11, alicePrivateKey) + // and Alice must sign for minting because her key is the supply key. + .addSignerForStep(12, alicePrivateKey) + .setParameterSupplierForStep(12, () -> { + return new ContractFunctionParameters() + // add three metadatas + .addBytesArray(new byte[][]{new byte[]{0x01b}, new byte[]{0x02b}, new byte[]{0x03b}}); + }) // and alice must sign to become associated with the token. + .addSignerForStep(13, alicePrivateKey) + // Alice must sign to burn the token because her key is the supply key + .addSignerForStep(16, alicePrivateKey); + + // step 0 tests pseudo random number generator (PRNG) + // step 1 creates a fungible token + // step 2 mints it + // step 3 associates Alice with it + // step 4 transfers it to Alice. + // step 5 approves an allowance of the fungible token with operator as the owner and Alice as the spender [NOT WORKING] + // steps 6 - 10 test misc functions on the fungible token (see PrecompileExample.sol for details). + // step 11 creates an NFT token with a custom fee, and with the admin and supply set to Alice's key + // step 12 mints some NFTs + // step 13 associates Alice with the NFT token + // step 14 transfers some NFTs to Alice + // step 15 approves an NFT allowance with operator as the owner and Alice as the spender [NOT WORKING] + // step 16 burn some NFTs + + contractHelper.executeSteps(/* from step */ 0, /* to step */ 16, client); + + System.out.println("All steps completed with valid results."); } } diff --git a/examples/src/main/java/StakingExample.java b/examples/src/main/java/StakingExample.java index 91ae1f5b9..c6e7825e4 100644 --- a/examples/src/main/java/StakingExample.java +++ b/examples/src/main/java/StakingExample.java @@ -44,8 +44,9 @@ public class StakingExample { private StakingExample() { } - public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException { - Client client = Client.forName(HEDERA_NETWORK); + public static void main(String[] args) + throws TimeoutException, PrecheckStatusException, ReceiptStatusException, InterruptedException { + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/StakingWithUpdateExample.java b/examples/src/main/java/StakingWithUpdateExample.java index 7058e6343..9023052e6 100644 --- a/examples/src/main/java/StakingWithUpdateExample.java +++ b/examples/src/main/java/StakingWithUpdateExample.java @@ -45,8 +45,9 @@ public class StakingWithUpdateExample { private StakingWithUpdateExample() { } - public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException { - Client client = Client.forName(HEDERA_NETWORK); + public static void main(String[] args) + throws TimeoutException, PrecheckStatusException, ReceiptStatusException, InterruptedException { + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/TopicWithAdminKeyExample.java b/examples/src/main/java/TopicWithAdminKeyExample.java index 1283a92af..67a703f4a 100644 --- a/examples/src/main/java/TopicWithAdminKeyExample.java +++ b/examples/src/main/java/TopicWithAdminKeyExample.java @@ -61,11 +61,12 @@ class TopicWithAdminKeyExample { private PrivateKey[] initialAdminKeys; - private TopicWithAdminKeyExample() { + private TopicWithAdminKeyExample() throws InterruptedException { setupHapiClient(); } - public static void main(String[] args) throws ReceiptStatusException, TimeoutException, PrecheckStatusException { + public static void main(String[] args) + throws ReceiptStatusException, TimeoutException, PrecheckStatusException, InterruptedException { new TopicWithAdminKeyExample().execute(); } @@ -75,8 +76,8 @@ public void execute() throws ReceiptStatusException, TimeoutException, PrecheckS updateTopicAdminKeyAndMemo(); } - private void setupHapiClient() { - hapiClient = Client.forName(HEDERA_NETWORK); + private void setupHapiClient() throws InterruptedException { + hapiClient = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for by this // account and be signed by this key diff --git a/examples/src/main/java/TransferCryptoExample.java b/examples/src/main/java/TransferCryptoExample.java index b1088f80d..d6401d01c 100644 --- a/examples/src/main/java/TransferCryptoExample.java +++ b/examples/src/main/java/TransferCryptoExample.java @@ -44,8 +44,9 @@ public final class TransferCryptoExample { private TransferCryptoExample() { } - public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException { - Client client = Client.forName(HEDERA_NETWORK); + public static void main(String[] args) + throws TimeoutException, PrecheckStatusException, ReceiptStatusException, InterruptedException { + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/TransferTokensExample.java b/examples/src/main/java/TransferTokensExample.java index 19ce20e3b..57602ba24 100644 --- a/examples/src/main/java/TransferTokensExample.java +++ b/examples/src/main/java/TransferTokensExample.java @@ -51,7 +51,7 @@ private TransferTokensExample() { } public static void main(String[] args) throws Exception { - Client client = Client.forName(HEDERA_NETWORK); + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/TransferUsingEvmAddressExample.java b/examples/src/main/java/TransferUsingEvmAddressExample.java index 47d21cdfa..20d50bf14 100644 --- a/examples/src/main/java/TransferUsingEvmAddressExample.java +++ b/examples/src/main/java/TransferUsingEvmAddressExample.java @@ -36,7 +36,7 @@ private TransferUsingEvmAddressExample() { - Get the `AccountInfo` of the account and show the account is now a complete account by returning the public key on the account */ public static void main(String[] args) throws PrecheckStatusException, TimeoutException, ReceiptStatusException, InterruptedException, IOException { - Client client = Client.forName(HEDERA_NETWORK); + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for by this account and be signed by this key client.setOperator(OPERATOR_ID, OPERATOR_KEY); diff --git a/examples/src/main/java/UpdateAccountPublicKeyExample.java b/examples/src/main/java/UpdateAccountPublicKeyExample.java index 307a4d2e2..c6e1b6ca7 100644 --- a/examples/src/main/java/UpdateAccountPublicKeyExample.java +++ b/examples/src/main/java/UpdateAccountPublicKeyExample.java @@ -45,8 +45,9 @@ public final class UpdateAccountPublicKeyExample { private UpdateAccountPublicKeyExample() { } - public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException { - Client client = Client.forName(HEDERA_NETWORK); + public static void main(String[] args) + throws TimeoutException, PrecheckStatusException, ReceiptStatusException, InterruptedException { + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/ValidateChecksumExample.java b/examples/src/main/java/ValidateChecksumExample.java index f23ccf1cc..3e30f8f8f 100644 --- a/examples/src/main/java/ValidateChecksumExample.java +++ b/examples/src/main/java/ValidateChecksumExample.java @@ -45,8 +45,8 @@ public final class ValidateChecksumExample { private ValidateChecksumExample() { } - public static void main(String[] args) throws TimeoutException, PrecheckStatusException { - Client client = Client.forName(HEDERA_NETWORK); + public static void main(String[] args) throws TimeoutException, PrecheckStatusException, InterruptedException { + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key diff --git a/examples/src/main/java/ZeroTokenOperationsExample.java b/examples/src/main/java/ZeroTokenOperationsExample.java index c720b7f96..a5a22a060 100644 --- a/examples/src/main/java/ZeroTokenOperationsExample.java +++ b/examples/src/main/java/ZeroTokenOperationsExample.java @@ -36,7 +36,7 @@ private ZeroTokenOperationsExample() { } public static void main(String[] args) throws Exception { - Client client = Client.forName(HEDERA_NETWORK); + Client client = ClientHelper.forName(HEDERA_NETWORK); // Defaults the operator account ID and key such that all generated transactions will be paid for // by this account and be signed by this key