forked from hashgraph/hedera-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetAccountBalanceExample.java
44 lines (34 loc) · 1.71 KB
/
GetAccountBalanceExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.Objects;
import java.util.concurrent.TimeoutException;
import com.hedera.hashgraph.sdk.*;
import com.hedera.hashgraph.sdk.PrecheckStatusException;
import io.github.cdimascio.dotenv.Dotenv;
public final class GetAccountBalanceExample {
// see `.env.sample` in the repository root for how to specify these values
// or set environment variables with the same names
private static final AccountId OPERATOR_ID = AccountId.fromString(Objects.requireNonNull(Dotenv.load().get("OPERATOR_ID")));
private static final PrivateKey OPERATOR_KEY = PrivateKey.fromString(Objects.requireNonNull(Dotenv.load().get("OPERATOR_KEY")));
private static final String HEDERA_NETWORK = Dotenv.load().get("HEDERA_NETWORK");
private static final String CONFIG_FILE = Dotenv.load().get("CONFIG_FILE");
private GetAccountBalanceExample() { }
public static void main(String[] args) throws PrecheckStatusException, TimeoutException {
Client client;
if (HEDERA_NETWORK != null && HEDERA_NETWORK.equals("previewnet")) {
client = Client.forPreviewnet();
} else {
try {
client = Client.fromConfigFile(CONFIG_FILE != null ? CONFIG_FILE : "");
} catch (Exception e) {
client = Client.forTestnet();
}
}
// 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);
Hbar balance = new AccountBalanceQuery()
.setAccountId(OPERATOR_ID)
.execute(client)
.hbars;
System.out.println("balance = " + balance);
}
}