Search in sources :

Example 26 with TransactionResponse

use of com.hedera.hashgraph.sdk.TransactionResponse in project hedera-sdk-java by hashgraph.

the class FileAppendChunkedExample 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);
    TransactionResponse transactionResponse = new FileCreateTransaction().setKeys(OPERATOR_KEY.getPublicKey()).setContents("Hello from Hedera.").setMaxTransactionFee(// 2 HBAR
    new Hbar(2)).execute(client);
    TransactionReceipt receipt = transactionResponse.getReceipt(client);
    FileId newFileId = Objects.requireNonNull(receipt.fileId);
    System.out.println("fileId: " + newFileId);
    StringBuilder contents = new StringBuilder();
    for (int i = 0; i <= 4096 * 9; i++) {
        contents.append("1");
    }
    TransactionReceipt fileAppendReceipt = new FileAppendTransaction().setNodeAccountIds(Collections.singletonList(transactionResponse.nodeId)).setFileId(newFileId).setContents(contents.toString()).setMaxChunks(40).setMaxTransactionFee(new Hbar(1000)).freezeWith(client).execute(client).getReceipt(client);
    System.out.println(fileAppendReceipt.toString());
    FileInfo info = new FileInfoQuery().setFileId(newFileId).execute(client);
    System.out.println("File size according to `FileInfoQuery`: " + info.size);
}
Also used : FileCreateTransaction(com.hedera.hashgraph.sdk.FileCreateTransaction) FileInfoQuery(com.hedera.hashgraph.sdk.FileInfoQuery) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) FileInfo(com.hedera.hashgraph.sdk.FileInfo) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) FileAppendTransaction(com.hedera.hashgraph.sdk.FileAppendTransaction) Hbar(com.hedera.hashgraph.sdk.Hbar) FileId(com.hedera.hashgraph.sdk.FileId) Client(com.hedera.hashgraph.sdk.Client)

Example 27 with TransactionResponse

use of com.hedera.hashgraph.sdk.TransactionResponse in project hedera-sdk-java by hashgraph.

the class GetFileContentsExample method main.

public static void main(String[] args) throws ReceiptStatusException, TimeoutException, PrecheckStatusException {
    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);
    // Content to be stored in the file
    byte[] fileContents = "Hedera is great!".getBytes(StandardCharsets.UTF_8);
    // Create the new file and set its properties
    TransactionResponse newFileTransactionResponse = new FileCreateTransaction().setKeys(// The public key of the owner of the file
    OPERATOR_KEY).setContents(// Contents of the file
    fileContents).setMaxTransactionFee(new Hbar(2)).execute(client);
    FileId newFileId = Objects.requireNonNull(newFileTransactionResponse.getReceipt(client).fileId);
    // Print the file ID to console
    System.out.println("The new file ID is " + newFileId.toString());
    // Get file contents
    ByteString contents = new FileContentsQuery().setFileId(newFileId).execute(client);
    // Prints query results to console
    System.out.println("File content query results: " + contents.toStringUtf8());
}
Also used : FileContentsQuery(com.hedera.hashgraph.sdk.FileContentsQuery) FileCreateTransaction(com.hedera.hashgraph.sdk.FileCreateTransaction) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) ByteString(com.google.protobuf.ByteString) Hbar(com.hedera.hashgraph.sdk.Hbar) FileId(com.hedera.hashgraph.sdk.FileId) Client(com.hedera.hashgraph.sdk.Client)

Example 28 with TransactionResponse

use of com.hedera.hashgraph.sdk.TransactionResponse in project hedera-sdk-java by hashgraph.

the class MultiAppTransferExample method main.

public static void main(String[] args) throws ReceiptStatusException, TimeoutException, PrecheckStatusException, InvalidProtocolBufferException {
    // the exchange creates an account for the user to transfer funds to
    AccountId exchangeAccountId = new AccountCreateTransaction().setReceiverSignatureRequired(true).setKey(exchangeKey).freezeWith(client).sign(exchangeKey).execute(client).getReceipt(client).accountId;
    assert exchangeAccountId != null;
    // for the purpose of this example we create an account for
    // the user with a balance of 5 h
    AccountId userAccountId = new AccountCreateTransaction().setInitialBalance(new Hbar(5)).setKey(userKey).execute(client).getReceipt(client).accountId;
    assert userAccountId != null;
    // next we make a transfer from the user account to the
    // exchange account, this requires signing by both parties
    TransferTransaction transferTxn = new TransferTransaction().addHbarTransfer(userAccountId, new Hbar(2).negated()).addHbarTransfer(exchangeAccountId, new Hbar(2)).setTransactionMemo("https://some-exchange.com/user1/account1").freezeWith(client).sign(userKey);
    // the exchange must sign the transaction in order for it to be accepted by the network
    // assume this is some REST call to the exchange API server
    byte[] signedTxnBytes = exchangeSignsTransaction(transferTxn.toBytes());
    // parse the transaction bytes returned from the exchange
    Transaction<?> signedTransferTxn = Transaction.fromBytes(signedTxnBytes);
    // get the amount we are about to transfer
    // we built this with +2, -2
    Hbar transferAmount = ((TransferTransaction) signedTransferTxn).getHbarTransfers().values().toArray(new Hbar[0])[0];
    System.out.println("about to transfer " + transferAmount + "...");
    // we now execute the signed transaction and wait for it to be accepted
    TransactionResponse transactionResponse = signedTransferTxn.execute(client);
    // (important!) wait for consensus by querying for the receipt
    transactionResponse.getReceipt(client);
    Hbar senderBalanceAfter = new AccountBalanceQuery().setAccountId(userAccountId).execute(client).hbars;
    Hbar receiptBalanceAfter = new AccountBalanceQuery().setAccountId(exchangeAccountId).execute(client).hbars;
    System.out.println("" + userAccountId + " balance = " + senderBalanceAfter);
    System.out.println("" + exchangeAccountId + " balance = " + receiptBalanceAfter);
}
Also used : AccountId(com.hedera.hashgraph.sdk.AccountId) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) Hbar(com.hedera.hashgraph.sdk.Hbar) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction)

Aggregations

TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)28 Client (com.hedera.hashgraph.sdk.Client)19 Hbar (com.hedera.hashgraph.sdk.Hbar)19 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)17 AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)15 AccountId (com.hedera.hashgraph.sdk.AccountId)13 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)13 TransferTransaction (com.hedera.hashgraph.sdk.TransferTransaction)13 Var (com.google.errorprone.annotations.Var)9 KeyList (com.hedera.hashgraph.sdk.KeyList)9 FileCreateTransaction (com.hedera.hashgraph.sdk.FileCreateTransaction)6 FileId (com.hedera.hashgraph.sdk.FileId)6 PublicKey (com.hedera.hashgraph.sdk.PublicKey)6 AccountBalanceQuery (com.hedera.hashgraph.sdk.AccountBalanceQuery)4 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 TokenAssociateTransaction (com.hedera.hashgraph.sdk.TokenAssociateTransaction)4 TokenCreateTransaction (com.hedera.hashgraph.sdk.TokenCreateTransaction)4