Search in sources :

Example 21 with AccountBalanceQuery

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

the class AccountAliasExample method main.

public static void main(String[] args) throws TimeoutException, PrecheckStatusException, 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);
    /*
         * Hedera supports a form of lazy account creation.
         *
         * You can "create" an account by generating a private key, and then deriving the public key,
         * without any need to interact with the Hedera network.  The public key more or less acts as the user's
         * account ID.  This public key is an account's aliasKey: a public key that aliases (or will eventually alias)
         * to a Hedera account.
         *
         * An AccountId takes one of two forms: a normal AccountId with a null aliasKey member takes the form 0.0.123,
         * while an account ID with a non-null aliasKey member takes the form
         * 0.0.302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777
         * Note the prefix of "0.0." indicating the shard and realm.  Also note that the aliasKey is stringified
         * as a hex-encoded ASN1 DER representation of the key.
         *
         * An AccountId with an aliasKey can be used just like a normal AccountId for the purposes of queries and
         * transactions, however most queries and transactions involving such an AccountId won't work until Hbar has
         * been transferred to the aliasKey account.
         *
         * There is no record in the Hedera network of an account associated with a given aliasKey
         * until an amount of Hbar is transferred to the account.  The moment that Hbar is transferred to that aliasKey
         * AccountId is the moment that that account actually begins to exist in the Hedera ledger.
         */
    System.out.println("\"Creating\" a new account");
    PrivateKey privateKey = PrivateKey.generateED25519();
    PublicKey publicKey = privateKey.getPublicKey();
    // Assuming that the target shard and realm are known.
    // For now they are virtually always 0 and 0.
    AccountId aliasAccountId = publicKey.toAccountId(0, 0);
    System.out.println("New account ID: " + aliasAccountId);
    System.out.println("Just the aliasKey: " + aliasAccountId.aliasKey);
    /*
         * Note that no queries or transactions have taken place yet.
         * This account "creation" process is entirely local.
         *
         * AccountId.fromString() can construct an AccountId with an aliasKey.
         * It expects a string of the form 0.0.123 in the case of a normal AccountId, or of the form
         * 0.0.302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777
         * in the case of an AccountId with aliasKey.  Note the prefix of "0.0." to indicate the shard and realm.
         *
         * If the shard and realm are known, you may use PublicKey.fromString().toAccountId() to construct the
         * aliasKey AccountId
         */
    AccountId fromString = AccountId.fromString("0.0.302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777");
    AccountId fromKeyString = PublicKey.fromString("302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777").toAccountId(0, 0);
    System.out.println("Transferring some Hbar to the new account");
    new TransferTransaction().addHbarTransfer(OPERATOR_ID, new Hbar(10).negated()).addHbarTransfer(aliasAccountId, new Hbar(10)).execute(client).getReceipt(client);
    AccountBalance balance = new AccountBalanceQuery().setAccountId(aliasAccountId).execute(client);
    System.out.println("Balances of the new account: " + balance);
    AccountInfo info = new AccountInfoQuery().setAccountId(aliasAccountId).execute(client);
    System.out.println("Info about the new account: " + info);
    /*
         * Note that once an account exists in the ledger, it is assigned a normal AccountId, which can be retrieved
         * via an AccountInfoQuery.
         *
         * Users may continue to refer to the account by its aliasKey AccountId, but they may also
         * now refer to it by its normal AccountId
         */
    System.out.println("The normal account ID: " + info.accountId);
    System.out.println("The alias key: " + info.aliasKey);
    System.out.println("Example complete!");
    client.close();
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) AccountId(com.hedera.hashgraph.sdk.AccountId) PublicKey(com.hedera.hashgraph.sdk.PublicKey) AccountBalance(com.hedera.hashgraph.sdk.AccountBalance) AccountInfoQuery(com.hedera.hashgraph.sdk.AccountInfoQuery) AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) Hbar(com.hedera.hashgraph.sdk.Hbar) Client(com.hedera.hashgraph.sdk.Client) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountInfo(com.hedera.hashgraph.sdk.AccountInfo)

Example 22 with AccountBalanceQuery

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

the class AccountAllowanceExample method printBalances.

private void printBalances() throws PrecheckStatusException, TimeoutException {
    System.out.println("Alice's balance: " + new AccountBalanceQuery().setAccountId(aliceId).execute(client).hbars);
    System.out.println("Bob's balance: " + new AccountBalanceQuery().setAccountId(bobId).execute(client).hbars);
    System.out.println("Charlie's balance: " + new AccountBalanceQuery().setAccountId(charlieId).execute(client).hbars);
}
Also used : AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery)

Example 23 with AccountBalanceQuery

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

the class MultiAppTransferExample method main.

public static void main(String[] args) throws ReceiptStatusException, TimeoutException, PrecheckStatusException, InvalidProtocolBufferException {
    // the exchange creates an account for the user to transfer funds to
    AccountId exchangeAccountId = new AccountCreateTransaction().setReceiverSignatureRequired(true).setKey(exchangeKey).freezeWith(client).sign(exchangeKey).execute(client).getReceipt(client).accountId;
    assert exchangeAccountId != null;
    // for the purpose of this example we create an account for
    // the user with a balance of 5 h
    AccountId userAccountId = new AccountCreateTransaction().setInitialBalance(new Hbar(5)).setKey(userKey).execute(client).getReceipt(client).accountId;
    assert userAccountId != null;
    // next we make a transfer from the user account to the
    // exchange account, this requires signing by both parties
    TransferTransaction transferTxn = new TransferTransaction().addHbarTransfer(userAccountId, new Hbar(2).negated()).addHbarTransfer(exchangeAccountId, new Hbar(2)).setTransactionMemo("https://some-exchange.com/user1/account1").freezeWith(client).sign(userKey);
    // the exchange must sign the transaction in order for it to be accepted by the network
    // assume this is some REST call to the exchange API server
    byte[] signedTxnBytes = exchangeSignsTransaction(transferTxn.toBytes());
    // parse the transaction bytes returned from the exchange
    Transaction<?> signedTransferTxn = Transaction.fromBytes(signedTxnBytes);
    // get the amount we are about to transfer
    // we built this with +2, -2
    Hbar transferAmount = ((TransferTransaction) signedTransferTxn).getHbarTransfers().values().toArray(new Hbar[0])[0];
    System.out.println("about to transfer " + transferAmount + "...");
    // we now execute the signed transaction and wait for it to be accepted
    TransactionResponse transactionResponse = signedTransferTxn.execute(client);
    // (important!) wait for consensus by querying for the receipt
    transactionResponse.getReceipt(client);
    Hbar senderBalanceAfter = new AccountBalanceQuery().setAccountId(userAccountId).execute(client).hbars;
    Hbar receiptBalanceAfter = new AccountBalanceQuery().setAccountId(exchangeAccountId).execute(client).hbars;
    System.out.println("" + userAccountId + " balance = " + senderBalanceAfter);
    System.out.println("" + exchangeAccountId + " balance = " + receiptBalanceAfter);
}
Also used : AccountId(com.hedera.hashgraph.sdk.AccountId) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) Hbar(com.hedera.hashgraph.sdk.Hbar) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction)

Example 24 with AccountBalanceQuery

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

the class AccountClient method getBalance.

@SneakyThrows
public long getBalance(AccountId accountId) {
    Hbar balance = new AccountBalanceQuery().setAccountId(accountId).execute(client).hbars;
    log.info("{} balance is {}", accountId, balance);
    return balance.toTinybars();
}
Also used : AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) Hbar(com.hedera.hashgraph.sdk.Hbar) SneakyThrows(lombok.SneakyThrows)

Example 25 with AccountBalanceQuery

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

the class SDKClient method validateNode.

private boolean validateNode(String accountId, Client client) {
    boolean valid = false;
    try {
        AccountId nodeAccountId = AccountId.fromString(accountId);
        new AccountBalanceQuery().setAccountId(nodeAccountId).setNodeAccountIds(List.of(nodeAccountId)).execute(client, Duration.ofSeconds(10L));
        log.trace("Validated node: {}", accountId);
        valid = true;
    } catch (Exception e) {
        log.warn("Unable to validate node {}: ", accountId, e);
    }
    return valid;
}
Also used : AccountId(com.hedera.hashgraph.sdk.AccountId) ExpandedAccountId(com.hedera.mirror.test.e2e.acceptance.props.ExpandedAccountId) AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) TimeoutException(java.util.concurrent.TimeoutException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) PrecheckStatusException(com.hedera.hashgraph.sdk.PrecheckStatusException)

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