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);
}
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());
}
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);
}
Aggregations