Search in sources :

Example 6 with TransactionReceipt

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);
    }
}
Also used : TopicCreateTransaction(com.hedera.hashgraph.sdk.TopicCreateTransaction) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) TopicId(com.hedera.hashgraph.sdk.TopicId) Client(com.hedera.hashgraph.sdk.Client) TopicMessageQuery(com.hedera.hashgraph.sdk.TopicMessageQuery) TopicMessageSubmitTransaction(com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction)

Example 7 with TransactionReceipt

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
}
Also used : FileCreateTransaction(com.hedera.hashgraph.sdk.FileCreateTransaction) FileInfoQuery(com.hedera.hashgraph.sdk.FileInfoQuery) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) FileDeleteTransaction(com.hedera.hashgraph.sdk.FileDeleteTransaction) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) Hbar(com.hedera.hashgraph.sdk.Hbar) FileId(com.hedera.hashgraph.sdk.FileId) Client(com.hedera.hashgraph.sdk.Client)

Example 8 with TransactionReceipt

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);
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) AccountId(com.hedera.hashgraph.sdk.AccountId) Var(com.google.errorprone.annotations.Var) KeyList(com.hedera.hashgraph.sdk.KeyList) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) Hbar(com.hedera.hashgraph.sdk.Hbar) Client(com.hedera.hashgraph.sdk.Client) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction)

Example 9 with TransactionReceipt

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);
}
Also used : TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) When(io.cucumber.java.en.When)

Example 10 with TransactionReceipt

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);
}
Also used : NetworkTransactionResponse(com.hedera.mirror.test.e2e.acceptance.response.NetworkTransactionResponse) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) TransactionId(com.hedera.hashgraph.sdk.TransactionId)

Aggregations

TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)25 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)17 Client (com.hedera.hashgraph.sdk.Client)16 Hbar (com.hedera.hashgraph.sdk.Hbar)12 AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)11 AccountId (com.hedera.hashgraph.sdk.AccountId)11 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)11 KeyList (com.hedera.hashgraph.sdk.KeyList)8 TransferTransaction (com.hedera.hashgraph.sdk.TransferTransaction)8 Var (com.google.errorprone.annotations.Var)6 PublicKey (com.hedera.hashgraph.sdk.PublicKey)6 TransactionId (com.hedera.hashgraph.sdk.TransactionId)6 FileCreateTransaction (com.hedera.hashgraph.sdk.FileCreateTransaction)5 FileId (com.hedera.hashgraph.sdk.FileId)5 AccountDeleteTransaction (com.hedera.hashgraph.sdk.AccountDeleteTransaction)4 ScheduleId (com.hedera.hashgraph.sdk.ScheduleId)4 ScheduleInfoQuery (com.hedera.hashgraph.sdk.ScheduleInfoQuery)4 ScheduleSignTransaction (com.hedera.hashgraph.sdk.ScheduleSignTransaction)4 ScheduleCreateTransaction (com.hedera.hashgraph.sdk.ScheduleCreateTransaction)3 ScheduleInfo (com.hedera.hashgraph.sdk.ScheduleInfo)3