use of com.hedera.hashgraph.sdk.TransactionResponse in project hedera-sdk-java by hashgraph.
the class TopicWithAdminKeyExample method createTopicWithAdminKey.
private void createTopicWithAdminKey() throws TimeoutException, PrecheckStatusException, ReceiptStatusException {
// Generate the initial keys that are part of the adminKey's thresholdKey.
// 3 ED25519 keys part of a 2-of-3 threshold key.
initialAdminKeys = new PrivateKey[3];
J8Arrays.setAll(initialAdminKeys, i -> PrivateKey.generate());
KeyList thresholdKey = KeyList.withThreshold(2);
Collections.addAll(thresholdKey, initialAdminKeys);
Transaction<?> transaction = new TopicCreateTransaction().setTopicMemo("demo topic").setAdminKey(thresholdKey).freezeWith(hapiClient);
// Sign the transaction with 2 of 3 keys that are part of the adminKey threshold key.
J8Arrays.stream(initialAdminKeys, 0, 2).forEach(k -> {
System.out.println("Signing ConsensusTopicCreateTransaction with key " + k);
transaction.sign(k);
});
TransactionResponse transactionResponse = transaction.execute(hapiClient);
topicId = transactionResponse.getReceipt(hapiClient).topicId;
System.out.println("Created new topic " + topicId + " with 2-of-3 threshold key as adminKey.");
}
use of com.hedera.hashgraph.sdk.TransactionResponse in project hedera-sdk-java by hashgraph.
the class TransferCryptoExample 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);
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().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);
}
use of com.hedera.hashgraph.sdk.TransactionResponse in project hedera-sdk-java by hashgraph.
the class TransferTokensExample method main.
public static void main(String[] args) throws Exception {
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 a Ed25519 private, public key pair
PrivateKey key1 = PrivateKey.generateED25519();
PrivateKey key2 = PrivateKey.generateED25519();
System.out.println("private key = " + key1);
System.out.println("public key = " + key1.getPublicKey());
System.out.println("private key = " + key2);
System.out.println("public key = " + key2.getPublicKey());
@Var TransactionResponse response = new AccountCreateTransaction().setKey(key1.getPublicKey()).setInitialBalance(Hbar.fromTinybars(1000)).execute(client);
// This will wait for the receipt to become available
@Var TransactionReceipt receipt = response.getReceipt(client);
AccountId accountId1 = Objects.requireNonNull(receipt.accountId);
System.out.println("accountId1 = " + accountId1);
response = new AccountCreateTransaction().setKey(key2.getPublicKey()).setInitialBalance(Hbar.fromTinybars(1000)).execute(client);
// This will wait for the receipt to become available
receipt = response.getReceipt(client);
AccountId accountId2 = Objects.requireNonNull(receipt.accountId);
System.out.println("accountId2 = " + accountId1);
response = new TokenCreateTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setTokenName("ffff").setTokenSymbol("F").setDecimals(3).setInitialSupply(1000000).setTreasuryAccountId(OPERATOR_ID).setAdminKey(OPERATOR_KEY.getPublicKey()).setFreezeKey(OPERATOR_KEY.getPublicKey()).setWipeKey(OPERATOR_KEY.getPublicKey()).setKycKey(OPERATOR_KEY.getPublicKey()).setSupplyKey(OPERATOR_KEY.getPublicKey()).setFreezeDefault(false).execute(client);
TokenId tokenId = Objects.requireNonNull(response.getReceipt(client).tokenId);
System.out.println("token = " + tokenId);
new TokenAssociateTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setAccountId(accountId1).setTokenIds(Collections.singletonList(tokenId)).freezeWith(client).sign(OPERATOR_KEY).sign(key1).execute(client).getReceipt(client);
System.out.println("Associated account " + accountId1 + " with token " + tokenId);
new TokenAssociateTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setAccountId(accountId2).setTokenIds(Collections.singletonList(tokenId)).freezeWith(client).sign(OPERATOR_KEY).sign(key2).execute(client).getReceipt(client);
System.out.println("Associated account " + accountId2 + " with token " + tokenId);
new TokenGrantKycTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setAccountId(accountId1).setTokenId(tokenId).execute(client).getReceipt(client);
System.out.println("Granted KYC for account " + accountId1 + " on token " + tokenId);
new TokenGrantKycTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setAccountId(accountId2).setTokenId(tokenId).execute(client).getReceipt(client);
System.out.println("Granted KYC for account " + accountId2 + " on token " + tokenId);
new TransferTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).addTokenTransfer(tokenId, OPERATOR_ID, -10).addTokenTransfer(tokenId, accountId1, 10).execute(client).getReceipt(client);
System.out.println("Sent 10 tokens from account " + OPERATOR_ID + " to account " + accountId1 + " on token " + tokenId);
new TransferTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).addTokenTransfer(tokenId, accountId1, -10).addTokenTransfer(tokenId, accountId2, 10).freezeWith(client).sign(key1).execute(client).getReceipt(client);
System.out.println("Sent 10 tokens from account " + accountId1 + " to account " + accountId2 + " on token " + tokenId);
new TransferTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).addTokenTransfer(tokenId, accountId2, -10).addTokenTransfer(tokenId, accountId1, 10).freezeWith(client).sign(key2).execute(client).getReceipt(client);
System.out.println("Sent 10 tokens from account " + accountId2 + " to account " + accountId1 + " on token " + tokenId);
new TokenWipeTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setTokenId(tokenId).setAccountId(accountId1).setAmount(10).execute(client).getReceipt(client);
System.out.println("Wiped balance of account " + accountId1);
new TokenDeleteTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setTokenId(tokenId).execute(client).getReceipt(client);
System.out.println("Deleted token " + tokenId);
new AccountDeleteTransaction().setAccountId(accountId1).setTransferAccountId(OPERATOR_ID).freezeWith(client).sign(OPERATOR_KEY).sign(key1).execute(client).getReceipt(client);
System.out.println("Deleted accountId1 " + accountId1);
new AccountDeleteTransaction().setAccountId(accountId2).setTransferAccountId(OPERATOR_ID).freezeWith(client).sign(OPERATOR_KEY).sign(key2).execute(client).getReceipt(client);
System.out.println("Deleted accountId2" + accountId2);
}
use of com.hedera.hashgraph.sdk.TransactionResponse in project hedera-sdk-java by hashgraph.
the class DeleteAccountExample 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);
// Generate a Ed25519 private, public key pair
PrivateKey newKey = PrivateKey.generateED25519();
PublicKey newPublicKey = newKey.getPublicKey();
System.out.println("private key = " + newKey);
System.out.println("public key = " + newPublicKey);
TransactionResponse transactionResponse = new AccountCreateTransaction().setKey(newKey).setInitialBalance(new Hbar(2)).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);
new AccountDeleteTransaction().setTransactionId(TransactionId.generate(newAccountId)).setAccountId(newAccountId).setTransferAccountId(OPERATOR_ID).freezeWith(client).sign(newKey).execute(client).getReceipt(client);
}
use of com.hedera.hashgraph.sdk.TransactionResponse in project hedera-sdk-java by hashgraph.
the class ConsensusPubSubExample method main.
public static void main(String[] args) throws TimeoutException, InterruptedException, 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);
TransactionResponse transactionResponse = new TopicCreateTransaction().execute(client);
TransactionReceipt transactionReceipt = transactionResponse.getReceipt(client);
TopicId topicId = Objects.requireNonNull(transactionReceipt.topicId);
System.out.println("New topic created: " + topicId);
Thread.sleep(5000);
new TopicMessageQuery().setTopicId(topicId).subscribe(client, resp -> {
String messageAsString = new String(resp.contents, StandardCharsets.UTF_8);
System.out.println(resp.consensusTimestamp + " received topic message: " + messageAsString);
});
// noinspection InfiniteLoopStatement
for (int i = 0; ; i++) {
new TopicMessageSubmitTransaction().setTopicId(topicId).setMessage("hello, HCS! " + i).execute(client).getReceipt(client);
Thread.sleep(2500);
}
}
Aggregations