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