|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +package com.hedera.hashgraph.sdk.examples; |
| 3 | + |
| 4 | +import com.hedera.hashgraph.sdk.*; |
| 5 | +import com.hedera.hashgraph.sdk.logger.LogLevel; |
| 6 | +import com.hedera.hashgraph.sdk.logger.Logger; |
| 7 | +import io.github.cdimascio.dotenv.Dotenv; |
| 8 | +import java.util.Objects; |
| 9 | + |
| 10 | +/** |
| 11 | + * Create a Hedera account using high-volume throttles. |
| 12 | + */ |
| 13 | +class HighVolumeAccountCreateExample { |
| 14 | + |
| 15 | + /* |
| 16 | + * See .env.sample in the examples folder root for how to specify values below |
| 17 | + * or set environment variables with the same names. |
| 18 | + */ |
| 19 | + |
| 20 | + /** |
| 21 | + * Operator's account ID. |
| 22 | + * Used to sign and pay for operations on Hedera. |
| 23 | + */ |
| 24 | + private static final AccountId OPERATOR_ID = |
| 25 | + AccountId.fromString(Objects.requireNonNull(Dotenv.load().get("OPERATOR_ID"))); |
| 26 | + |
| 27 | + /** |
| 28 | + * Operator's private key. |
| 29 | + */ |
| 30 | + private static final PrivateKey OPERATOR_KEY = |
| 31 | + PrivateKey.fromString(Objects.requireNonNull(Dotenv.load().get("OPERATOR_KEY"))); |
| 32 | + |
| 33 | + /** |
| 34 | + * HEDERA_NETWORK defaults to testnet if not specified in dotenv file. |
| 35 | + * Network can be: localhost, testnet, previewnet or mainnet. |
| 36 | + */ |
| 37 | + private static final String HEDERA_NETWORK = Dotenv.load().get("HEDERA_NETWORK", "testnet"); |
| 38 | + |
| 39 | + /** |
| 40 | + * SDK_LOG_LEVEL defaults to SILENT if not specified in dotenv file. |
| 41 | + * Log levels can be: TRACE, DEBUG, INFO, WARN, ERROR, SILENT. |
| 42 | + * <p> |
| 43 | + * Important pre-requisite: set simple logger log level to same level as the SDK_LOG_LEVEL, |
| 44 | + * for example via VM options: -Dorg.slf4j.simpleLogger.log.org.hiero=trace |
| 45 | + */ |
| 46 | + private static final String SDK_LOG_LEVEL = Dotenv.load().get("SDK_LOG_LEVEL", "SILENT"); |
| 47 | + |
| 48 | + public static void main(String[] args) throws Exception { |
| 49 | + System.out.println("High-Volume Account Create Example Start!"); |
| 50 | + |
| 51 | + /* |
| 52 | + * Step 0: |
| 53 | + * Create and configure the SDK Client. |
| 54 | + */ |
| 55 | + Client client = ClientHelper.forName(HEDERA_NETWORK); |
| 56 | + // All generated transactions will be paid by this account and signed by this key. |
| 57 | + client.setOperator(OPERATOR_ID, OPERATOR_KEY); |
| 58 | + // Attach logger to the SDK Client. |
| 59 | + client.setLogger(new Logger(LogLevel.valueOf(SDK_LOG_LEVEL))); |
| 60 | + |
| 61 | + /* |
| 62 | + * Step 1: |
| 63 | + * Generate ED25519 private and public key pair for the account. |
| 64 | + */ |
| 65 | + PrivateKey privateKey = PrivateKey.generateED25519(); |
| 66 | + PublicKey publicKey = privateKey.getPublicKey(); |
| 67 | + System.out.println("Future account private key: " + privateKey); |
| 68 | + System.out.println("Future account public key: " + publicKey); |
| 69 | + |
| 70 | + /* |
| 71 | + * Step 2: |
| 72 | + * Create a new account using high-volume throttles and set a fee limit. |
| 73 | + */ |
| 74 | + System.out.println("Creating new account with high-volume throttles..."); |
| 75 | + TransactionResponse accountCreateTxResponse = new AccountCreateTransaction() |
| 76 | + .setKeyWithoutAlias(publicKey) |
| 77 | + .setInitialBalance(Hbar.from(1)) |
| 78 | + .setHighVolume(true) |
| 79 | + .setMaxTransactionFee(Hbar.from(5)) |
| 80 | + .execute(client); |
| 81 | + |
| 82 | + // This will wait for the receipt to become available. |
| 83 | + TransactionReceipt accountCreateTxReceipt = accountCreateTxResponse.getReceipt(client); |
| 84 | + AccountId newAccountId = accountCreateTxReceipt.accountId; |
| 85 | + Objects.requireNonNull(newAccountId); |
| 86 | + System.out.println("Created account with ID: " + newAccountId); |
| 87 | + |
| 88 | + /* |
| 89 | + * Clean up: |
| 90 | + * Delete created account. |
| 91 | + */ |
| 92 | + new AccountDeleteTransaction() |
| 93 | + .setTransferAccountId(OPERATOR_ID) |
| 94 | + .setAccountId(newAccountId) |
| 95 | + .freezeWith(client) |
| 96 | + .sign(privateKey) |
| 97 | + .execute(client) |
| 98 | + .getReceipt(client); |
| 99 | + |
| 100 | + client.close(); |
| 101 | + |
| 102 | + System.out.println("High-Volume Account Create Example Complete!"); |
| 103 | + } |
| 104 | +} |
0 commit comments