Search in sources :

Example 16 with AccountBalanceQuery

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

the class AccountBalanceIntegrationTest method cannotConnectToPreviewnetWhenNetworkNameIsNullAndCertificateVerificationIsEnabled.

@Test
@DisplayName("can connect to previewnet with certificate verification off")
void cannotConnectToPreviewnetWhenNetworkNameIsNullAndCertificateVerificationIsEnabled() throws Exception {
    var client = Client.forPreviewnet().setTransportSecurity(true).setVerifyCertificates(true).setNetworkName(null);
    assertThat(client.getNetwork().isEmpty()).isFalse();
    for (var entry : client.getNetwork().entrySet()) {
        assertThat(entry.getKey().endsWith(":50212")).isTrue();
        assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> {
            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)

Example 17 with AccountBalanceQuery

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

the class AccountBalanceIntegrationTest method getCostBalanceForClientOperator.

@Test
@DisplayName("Can fetch cost for the query")
void getCostBalanceForClientOperator() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    var balance = new AccountBalanceQuery().setAccountId(testEnv.operatorId).setMaxQueryPayment(new Hbar(1));
    var cost = balance.getCost(testEnv.client);
    var accBalance = balance.setQueryPayment(cost).execute(testEnv.client);
    assertThat(accBalance.hbars.toTinybars() > 0).isTrue();
    assertThat(cost.toTinybars()).isEqualTo(0);
    testEnv.close();
}
Also used : 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 18 with AccountBalanceQuery

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

the class AccountBalanceIntegrationTest method canNotFetchBalanceForInvalidAccountId.

@Test
@DisplayName("Cannot fetch balance for invalid account ID")
void canNotFetchBalanceForInvalidAccountId() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    assertThatExceptionOfType(PrecheckStatusException.class).isThrownBy(() -> {
        new AccountBalanceQuery().setAccountId(AccountId.fromString("1.0.3")).execute(testEnv.client);
    }).withMessageContaining(Status.INVALID_ACCOUNT_ID.toString());
    testEnv.close();
}
Also used : AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 19 with AccountBalanceQuery

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

the class AccountBalanceIntegrationTest method getCostBigMaxBalanceForClientOperator.

@Test
@DisplayName("Can fetch cost for the query, big max set")
void getCostBigMaxBalanceForClientOperator() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    var balance = new AccountBalanceQuery().setAccountId(testEnv.operatorId).setMaxQueryPayment(new Hbar(1000000));
    var cost = balance.getCost(testEnv.client);
    var accBalance = balance.setQueryPayment(cost).execute(testEnv.client);
    assertThat(accBalance.hbars.toTinybars() > 0).isTrue();
    testEnv.close();
}
Also used : 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 20 with AccountBalanceQuery

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

the class CreateAccountThresholdKeyExample method main.

public static void main(String[] args) throws PrecheckStatusException, TimeoutException, ReceiptStatusException {
    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);
    // Generate three new Ed25519 private, public key pairs.
    // You do not need the private keys to create the Threshold Key List,
    // you only need the public keys, and if you're doing things correctly,
    // you probably shouldn't have these private keys.
    PrivateKey[] privateKeys = new PrivateKey[3];
    PublicKey[] publicKeys = new PublicKey[3];
    for (int i = 0; i < 3; i++) {
        PrivateKey key = PrivateKey.generateED25519();
        privateKeys[i] = key;
        publicKeys[i] = key.getPublicKey();
    }
    System.out.println("public keys: ");
    for (Key key : publicKeys) {
        System.out.println(key);
    }
    // require 2 of the 3 keys we generated to sign on anything modifying this account
    KeyList transactionKey = KeyList.withThreshold(2);
    Collections.addAll(transactionKey, publicKeys);
    TransactionResponse transactionResponse = new AccountCreateTransaction().setKey(transactionKey).setInitialBalance(new Hbar(10)).execute(client);
    // This will wait for the receipt to become available
    TransactionReceipt receipt = transactionResponse.getReceipt(client);
    AccountId newAccountId = Objects.requireNonNull(receipt.accountId);
    System.out.println("account = " + newAccountId);
    TransactionResponse transferTransactionResponse = new TransferTransaction().addHbarTransfer(newAccountId, new Hbar(10).negated()).addHbarTransfer(new AccountId(3), new Hbar(10)).freezeWith(client).sign(privateKeys[0]).sign(privateKeys[1]).execute(client);
    // (important!) wait for the transfer to go to consensus
    transferTransactionResponse.getReceipt(client);
    Hbar balanceAfter = new AccountBalanceQuery().setAccountId(newAccountId).execute(client).hbars;
    System.out.println("account balance after transfer: " + balanceAfter);
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) AccountId(com.hedera.hashgraph.sdk.AccountId) PublicKey(com.hedera.hashgraph.sdk.PublicKey) KeyList(com.hedera.hashgraph.sdk.KeyList) AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) Hbar(com.hedera.hashgraph.sdk.Hbar) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) Client(com.hedera.hashgraph.sdk.Client) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) Key(com.hedera.hashgraph.sdk.Key) PublicKey(com.hedera.hashgraph.sdk.PublicKey) PrivateKey(com.hedera.hashgraph.sdk.PrivateKey)

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