use of com.hedera.hashgraph.sdk.TransactionReceipt 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);
}
}
use of com.hedera.hashgraph.sdk.TransactionReceipt in project hedera-sdk-java by hashgraph.
the class DeleteFileExample 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);
// The file is required to be a byte array,
// you can easily use the bytes of a file instead.
String fileContents = "Hedera hashgraph is great!";
TransactionResponse transactionResponse = new FileCreateTransaction().setKeys(OPERATOR_KEY).setContents(fileContents).setMaxTransactionFee(new Hbar(2)).execute(client);
TransactionReceipt receipt = transactionResponse.getReceipt(client);
FileId newFileId = Objects.requireNonNull(receipt.fileId);
System.out.println("file: " + newFileId);
// now delete the file
TransactionResponse fileDeleteTransactionResponse = new FileDeleteTransaction().setFileId(newFileId).execute(client);
// if this doesn't throw then the transaction was a success
fileDeleteTransactionResponse.getReceipt(client);
System.out.println("File deleted successfully.");
new FileInfoQuery().setFileId(newFileId).execute(client);
// note the above fileInfo will fail with FILE_DELETED due to a known issue on Hedera
}
use of com.hedera.hashgraph.sdk.TransactionReceipt in project hedera-sdk-java by hashgraph.
the class MultiSigOfflineExample method main.
public static void main(String[] args) throws PrecheckStatusException, TimeoutException, ReceiptStatusException, InvalidProtocolBufferException {
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);
PrivateKey user1Key = PrivateKey.generateED25519();
PrivateKey user2Key = PrivateKey.generateED25519();
System.out.println("private key for user 1 = " + user1Key);
System.out.println("public key for user 1 = " + user1Key.getPublicKey());
System.out.println("private key for user 2 = " + user2Key);
System.out.println("public key for user 2 = " + user2Key.getPublicKey());
// create a multi-sig account
KeyList keylist = new KeyList();
keylist.add(user1Key);
keylist.add(user2Key);
TransactionResponse createAccountTransaction = new AccountCreateTransaction().setInitialBalance(new Hbar(2)).setKey(keylist).execute(client);
@Var TransactionReceipt receipt = createAccountTransaction.getReceipt(client);
System.out.println("account id = " + receipt.accountId);
// create a transfer from new account to 0.0.3
TransferTransaction transferTransaction = new TransferTransaction().setNodeAccountIds(Collections.singletonList(new AccountId(3))).addHbarTransfer(Objects.requireNonNull(receipt.accountId), Hbar.from(-1)).addHbarTransfer(new AccountId(3), new Hbar(1)).freezeWith(client);
// convert transaction to bytes to send to signatories
byte[] transactionBytes = transferTransaction.toBytes();
Transaction<?> transactionToExecute = Transaction.fromBytes(transactionBytes);
// ask users to sign and return signature
byte[] user1Signature = user1Key.signTransaction(Transaction.fromBytes(transactionBytes));
byte[] user2Signature = user2Key.signTransaction(Transaction.fromBytes(transactionBytes));
// recreate the transaction from bytes
transactionToExecute.signWithOperator(client);
transactionToExecute.addSignature(user1Key.getPublicKey(), user1Signature);
transactionToExecute.addSignature(user2Key.getPublicKey(), user2Signature);
TransactionResponse result = transactionToExecute.execute(client);
receipt = result.getReceipt(client);
System.out.println(receipt.status);
}
use of com.hedera.hashgraph.sdk.TransactionReceipt in project hedera-mirror-node by hashgraph.
the class TopicFeature method deleteTopic.
@When("I successfully delete the topic")
public void deleteTopic() {
TransactionReceipt receipt = topicClient.deleteTopic(consensusTopicId).getReceipt();
assertNotNull(receipt);
}
use of com.hedera.hashgraph.sdk.TransactionReceipt in project hedera-mirror-node by hashgraph.
the class AbstractNetworkClient method executeTransactionAndRetrieveReceipt.
public NetworkTransactionResponse executeTransactionAndRetrieveReceipt(Transaction transaction, KeyList keyList, ExpandedAccountId payer) {
long startBalance = getBalance();
TransactionId transactionId = executeTransaction(transaction, keyList, payer);
TransactionReceipt transactionReceipt = null;
try {
transactionReceipt = getTransactionReceipt(transactionId);
} catch (Exception e) {
log.error("Failed to get transaction receipt for {}: {}", transactionId, e.getMessage());
}
log.trace("Executed transaction {} cost {} tℏ", transactionId, startBalance - getBalance());
return new NetworkTransactionResponse(transactionId, transactionReceipt);
}
Aggregations