Search in sources :

Example 11 with AccountBalanceQuery

use of com.hedera.hashgraph.sdk.AccountBalanceQuery in project hedera-sdk-java by hashgraph.

the class IntegrationTestEnv method wipeAccountHbars.

public void wipeAccountHbars(AccountId newAccountId, PrivateKey newAccountKey) throws Exception {
    var hbarsBalance = new AccountBalanceQuery().setAccountId(newAccountId).execute(originalClient).hbars;
    new TransferTransaction().addHbarTransfer(newAccountId, hbarsBalance.negated()).addHbarTransfer(Objects.requireNonNull(originalClient.getOperatorAccountId()), hbarsBalance).freezeWith(originalClient).sign(Objects.requireNonNull(newAccountKey)).execute(originalClient);
}
Also used : AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction)

Example 12 with AccountBalanceQuery

use of com.hedera.hashgraph.sdk.AccountBalanceQuery in project hedera-sdk-java by hashgraph.

the class IntegrationTestEnv method close.

public void close(@Nullable TokenId newTokenId, @Nullable AccountId newAccountId, @Nullable PrivateKey newAccountKey) throws Exception {
    if (newAccountId != null) {
        wipeAccountHbars(newAccountId, newAccountKey);
    }
    if (!operatorId.equals(originalClient.getOperatorAccountId())) {
        var hbarsBalance = new AccountBalanceQuery().setAccountId(operatorId).execute(originalClient).hbars;
        new TransferTransaction().addHbarTransfer(operatorId, hbarsBalance.negated()).addHbarTransfer(Objects.requireNonNull(originalClient.getOperatorAccountId()), hbarsBalance).freezeWith(originalClient).signWithOperator(client).execute(originalClient);
        client.close();
    }
    originalClient.close();
}
Also used : AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction)

Example 13 with AccountBalanceQuery

use of com.hedera.hashgraph.sdk.AccountBalanceQuery in project hedera-sdk-java by hashgraph.

the class ClientIntegrationTest method testReplaceNodes.

@Test
@DisplayName("setNetwork() functions correctly")
void testReplaceNodes() throws Exception {
    @Var Map<String, AccountId> network = new HashMap<>();
    network.put("0.testnet.hedera.com:50211", new AccountId(3));
    network.put("1.testnet.hedera.com:50211", new AccountId(4));
    var testEnv = new IntegrationTestEnv(1);
    testEnv.client.setMaxQueryPayment(new Hbar(2)).setRequestTimeout(Duration.ofMinutes(2)).setNetwork(network);
    assertThat(testEnv.operatorId).isNotNull();
    // Execute two simple queries so we create a channel for each network node.
    new AccountBalanceQuery().setAccountId(testEnv.operatorId).execute(testEnv.client);
    new AccountBalanceQuery().setAccountId(testEnv.operatorId).execute(testEnv.client);
    network = new HashMap<>();
    network.put("1.testnet.hedera.com:50211", new AccountId(4));
    network.put("2.testnet.hedera.com:50211", new AccountId(5));
    testEnv.client.setNetwork(network);
    network = new HashMap<>();
    network.put("35.186.191.247:50211", new AccountId(4));
    network.put("35.192.2.25:50211", new AccountId(5));
    testEnv.client.setNetwork(network);
    testEnv.close();
}
Also used : AccountId(com.hedera.hashgraph.sdk.AccountId) HashMap(java.util.HashMap) Var(com.google.errorprone.annotations.Var) AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) Hbar(com.hedera.hashgraph.sdk.Hbar) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 14 with AccountBalanceQuery

use of com.hedera.hashgraph.sdk.AccountBalanceQuery in project hedera-sdk-java by hashgraph.

the class ValidateChecksumExample method main.

public static void main(String[] args) throws TimeoutException, PrecheckStatusException {
    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);
    /*
         * Entity IDs, such as TokenId and AccountId, can be constructed from strings.
         * For example, the AccountId.fromString(inputString) static method will attempt to parse
         * the input string and construct the expected AccountId object, and will throw an
         * IllegalArgumentException if the string is incorrectly formatted.
         *
         * From here on, we'll talk about methods on accountId, but equivalent methods exist
         * on every entity ID type.
         *
         * fromString() expects the input to look something like this: "1.2.3-asdfg".
         * Here, 1 is the shard, 2 is the realm, 3 is the number, and "asdfg" is the checksum.
         *
         * The checksum can be used to ensure that an entity ID was inputted correctly.
         * For example, if the string being parsed is from a config file, or from user input,
         * it could contain typos.
         *
         * You can use accountId.getChecksum() to get the checksum of an accountId object that was constructed
         * using fromString().  This will be the checksum from the input string.  fromString() will merely
         * parse the string and create an AccountId object with the expected shard, realm, num, and checksum
         * values.  fromString() will NOT verify that the AccountId maps to a valid account on the Hedera
         * network, and it will not verify the checksum.
         *
         * To verify a checksum, call accountId.validateChecksum(client).  If the checksum
         * is invalid, validateChecksum() will throw a BadEntityIdException, otherwise it will return normally.
         *
         * The validity of a checksum depends on which network the client is connected to (EG mainnet or
         * testnet or previewnet).  For example, a checksum that is valid for a particular shard/realm/num
         * on mainnet will be INVALID for the same shard/realm/num on testnet.
         *
         * As far as fromString() is concerned, the checksum is optional.
         * If you use fromString() to generate an AccountId from a string that does not include a checksum,
         * such as "1.2.3", fromString() will work, but a call to the getChecksum() method on the resulting
         * AccountId object will return null.
         *
         * Generally speaking, AccountId objects can come from three places:
         * 1) AccountId.fromString(inString)
         * 2) new AccountId(shard, realm, num)
         * 3) From the result of a query
         *
         * In the first case, the AccountId object will have a checksum (getChecksum() will not return null) if
         * the input string included a checksum, and it will not have a checksum if the string did not
         * include a checksum.
         *
         * In the second and third cases, the AccountId object will not have a checksum.
         *
         * If you call accountId.validateChecksum(client) and accountId has no checksum to validate,
         * validateChecksum() will silently pass, and will not throw an exception.
         *
         * accountId.toString() will stringify the account ID with no checksum,
         * accountId.toStringWithChecksum(client) will stringify the account ID with the correct checksum
         * for that shard/realm/num on the client's network.
         */
    System.out.println("An example of manual checksum validation:");
    while (true) {
        try {
            System.out.print("Enter an account ID with checksum: ");
            String inString = INPUT_SCANNER.nextLine();
            // Throws IllegalArgumentException if incorrectly formatted
            AccountId id = AccountId.fromString(inString);
            System.out.println("The ID with no checksum is " + id.toString());
            System.out.println("The ID with the correct checksum is " + id.toStringWithChecksum(client));
            if (id.getChecksum() == null) {
                System.out.println("You must enter a checksum.");
                continue;
            }
            System.out.println("The checksum entered was " + id.getChecksum());
            // Throws BadEntityIdException if checksum is incorrect
            id.validateChecksum(client);
            AccountBalance balance = new AccountBalanceQuery().setAccountId(id).execute(client);
            System.out.println(balance);
            // exit the loop
            break;
        } catch (IllegalArgumentException exc) {
            System.out.println(exc.getMessage());
        } catch (BadEntityIdException exc) {
            System.out.println(exc.getMessage());
            System.out.println("You entered " + exc.shard + "." + exc.realm + "." + exc.num + "-" + exc.presentChecksum + ", the expected checksum was " + exc.expectedChecksum);
        }
    }
    /*
         * It is also possible to perform automatic checksum validation.
         *
         * Automatic checksum validation is disabled by default, but it can be enabled with
         * client.setAutoValidateChecksums(true).  You can check whether automatic checksum
         * validation is enabled with client.isAutoValidateChecksumsEnabled().
         *
         * When this feature is enabled, the execute() method of a transaction or query
         * will automatically check the validity of checksums on any IDs in the
         * transaction or query.  It will throw an IllegalArgumentException if an
         * invalid checksum is encountered.
         */
    System.out.println("An example of automatic checksum validation:");
    client.setAutoValidateChecksums(true);
    while (true) {
        try {
            System.out.print("Enter an account ID with checksum: ");
            AccountId id = AccountId.fromString(INPUT_SCANNER.nextLine());
            if (id.getChecksum() == null) {
                System.out.println("You must enter a checksum.");
                continue;
            }
            AccountBalance balance = new AccountBalanceQuery().setAccountId(id).execute(client);
            System.out.println(balance);
            // exit the loop
            break;
        } catch (IllegalArgumentException exc) {
            System.out.println(exc.getMessage());
        }
    }
    System.out.println("Example complete!");
    client.close();
}
Also used : BadEntityIdException(com.hedera.hashgraph.sdk.BadEntityIdException) AccountId(com.hedera.hashgraph.sdk.AccountId) AccountBalance(com.hedera.hashgraph.sdk.AccountBalance) AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) Client(com.hedera.hashgraph.sdk.Client)

Example 15 with AccountBalanceQuery

use of com.hedera.hashgraph.sdk.AccountBalanceQuery in project hedera-sdk-java by hashgraph.

the class AccountBalanceIntegrationTest method canConnectToTestnetWithTLS.

@Test
@DisplayName("can connect to testnet with TLS")
void canConnectToTestnetWithTLS() throws Exception {
    var client = Client.forTestnet().setTransportSecurity(true);
    for (var entry : client.getNetwork().entrySet()) {
        assertThat(entry.getKey().endsWith(":50212")).isTrue();
        new AccountBalanceQuery().setNodeAccountIds(Collections.singletonList(entry.getValue())).setAccountId(entry.getValue()).execute(client);
    }
    client.close();
}
Also used : AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

AccountBalanceQuery (com.hedera.hashgraph.sdk.AccountBalanceQuery)25 Hbar (com.hedera.hashgraph.sdk.Hbar)13 AccountId (com.hedera.hashgraph.sdk.AccountId)10 TransferTransaction (com.hedera.hashgraph.sdk.TransferTransaction)10 Client (com.hedera.hashgraph.sdk.Client)9 DisplayName (org.junit.jupiter.api.DisplayName)9 Test (org.junit.jupiter.api.Test)9 AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)6 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)6 AccountBalance (com.hedera.hashgraph.sdk.AccountBalance)4 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)4 PublicKey (com.hedera.hashgraph.sdk.PublicKey)3 ScheduleSignTransaction (com.hedera.hashgraph.sdk.ScheduleSignTransaction)3 TokenCreateTransaction (com.hedera.hashgraph.sdk.TokenCreateTransaction)3 TransactionRecord (com.hedera.hashgraph.sdk.TransactionRecord)3 AccountDeleteTransaction (com.hedera.hashgraph.sdk.AccountDeleteTransaction)2 KeyList (com.hedera.hashgraph.sdk.KeyList)2 ScheduleId (com.hedera.hashgraph.sdk.ScheduleId)2 ScheduleInfo (com.hedera.hashgraph.sdk.ScheduleInfo)2 ScheduleInfoQuery (com.hedera.hashgraph.sdk.ScheduleInfoQuery)2