forked from hashgraph/hedera-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransferCryptoExample.java
81 lines (62 loc) · 3.2 KB
/
TransferCryptoExample.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.util.Objects;
import java.util.concurrent.TimeoutException;
import com.hedera.hashgraph.sdk.*;
import io.github.cdimascio.dotenv.Dotenv;
public final class TransferCryptoExample {
// 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 TransferCryptoExample() {
}
public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException {
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);
AccountId recipientId = AccountId.fromString("0.0.3");
Hbar amount = Hbar.fromTinybars(10_000);
Hbar senderBalanceBefore = new AccountBalanceQuery()
.setAccountId(OPERATOR_ID)
.execute(client)
.hbars;
Hbar receiptBalanceBefore = new AccountBalanceQuery()
.setAccountId(recipientId)
.execute(client)
.hbars;
System.out.println("" + OPERATOR_ID + " balance = " + senderBalanceBefore);
System.out.println("" + recipientId + " balance = " + receiptBalanceBefore);
TransactionResponse transactionResponse = new TransferTransaction()
// .addSender and .addRecipient can be called as many times as you want as long as the total sum from
// both sides is equivalent
.addHbarTransfer(OPERATOR_ID, amount.negated())
.addHbarTransfer(recipientId, amount)
.setTransactionMemo("transfer test")
.execute(client);
System.out.println("transaction ID: " + transactionResponse);
TransactionRecord record = transactionResponse.getRecord(client);
System.out.println("transferred " + amount + "...");
Hbar senderBalanceAfter = new AccountBalanceQuery()
.setAccountId(OPERATOR_ID)
.execute(client)
.hbars;
Hbar receiptBalanceAfter = new AccountBalanceQuery()
.setAccountId(recipientId)
.execute(client)
.hbars;
System.out.println("" + OPERATOR_ID + " balance = " + senderBalanceAfter);
System.out.println("" + recipientId + " balance = " + receiptBalanceAfter);
System.out.println("Transfer memo: " + record.transactionMemo);
}
}