Search in sources :

Example 11 with FileContentsQuery

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

the class GetAddressBookExample method main.

public static void main(String[] args) throws PrecheckStatusException, IOException, TimeoutException {
    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);
    FileContentsQuery fileQuery = new FileContentsQuery().setFileId(FileId.ADDRESS_BOOK);
    Hbar cost = fileQuery.getCost(client);
    System.out.println("file contents cost: " + cost);
    fileQuery.setMaxQueryPayment(new Hbar(1));
    ByteString contents = fileQuery.execute(client);
    Files.deleteIfExists(FileSystems.getDefault().getPath("address-book.proto.bin"));
    Files.copy(new ByteArrayInputStream(contents.toByteArray()), FileSystems.getDefault().getPath("address-book.proto.bin"));
    // NEW (Feb 25 2022): you can now fetch the address book for free from a mirror node with AddressBookQuery
    NodeAddressBook addressBook = new AddressBookQuery().setFileId(FileId.ADDRESS_BOOK).execute(client);
    System.out.println(addressBook);
}
Also used : FileContentsQuery(com.hedera.hashgraph.sdk.FileContentsQuery) AddressBookQuery(com.hedera.hashgraph.sdk.AddressBookQuery) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteString(com.google.protobuf.ByteString) NodeAddressBook(com.hedera.hashgraph.sdk.NodeAddressBook) Hbar(com.hedera.hashgraph.sdk.Hbar) Client(com.hedera.hashgraph.sdk.Client)

Example 12 with FileContentsQuery

use of com.hedera.hashgraph.sdk.FileContentsQuery 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)

Aggregations

FileContentsQuery (com.hedera.hashgraph.sdk.FileContentsQuery)12 FileCreateTransaction (com.hedera.hashgraph.sdk.FileCreateTransaction)8 DisplayName (org.junit.jupiter.api.DisplayName)8 Test (org.junit.jupiter.api.Test)8 FileDeleteTransaction (com.hedera.hashgraph.sdk.FileDeleteTransaction)7 ByteString (com.google.protobuf.ByteString)5 Hbar (com.hedera.hashgraph.sdk.Hbar)5 Client (com.hedera.hashgraph.sdk.Client)3 Var (com.google.errorprone.annotations.Var)2 FileAppendTransaction (com.hedera.hashgraph.sdk.FileAppendTransaction)2 FileId (com.hedera.hashgraph.sdk.FileId)2 FileInfoQuery (com.hedera.hashgraph.sdk.FileInfoQuery)2 AddressBookQuery (com.hedera.hashgraph.sdk.AddressBookQuery)1 ExchangeRates (com.hedera.hashgraph.sdk.ExchangeRates)1 FeeSchedules (com.hedera.hashgraph.sdk.FeeSchedules)1 MaxQueryPaymentExceededException (com.hedera.hashgraph.sdk.MaxQueryPaymentExceededException)1 NodeAddressBook (com.hedera.hashgraph.sdk.NodeAddressBook)1 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1