Search in sources :

Example 1 with FileContentsQuery

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

the class GetExchangeRatesExample method main.

public static void main(String[] args) throws ReceiptStatusException, TimeoutException, PrecheckStatusException, 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);
    // Get contents of file 0.0.112
    ByteString fileContentsByteString = new FileContentsQuery().setFileId(FileId.fromString("0.0.112")).execute(client);
    byte[] fileContents = fileContentsByteString.toByteArray();
    ExchangeRates exchangeRateSet = ExchangeRates.fromBytes(fileContents);
    // Prints query results to console
    System.out.println("Current numerator: " + exchangeRateSet.currentRate.cents);
    System.out.println("Current denominator: " + exchangeRateSet.currentRate.hbars);
    System.out.println("Current expiration time: " + exchangeRateSet.currentRate.expirationTime.toString());
    System.out.println("Current Exchange Rate: " + exchangeRateSet.currentRate.exchangeRateInCents);
    System.out.println("Next numerator: " + exchangeRateSet.nextRate.cents);
    System.out.println("Next denominator: " + exchangeRateSet.nextRate.hbars);
    System.out.println("Next expiration time: " + exchangeRateSet.nextRate.expirationTime.toString());
    System.out.println("Next Exchange Rate: " + exchangeRateSet.nextRate.exchangeRateInCents);
}
Also used : FileContentsQuery(com.hedera.hashgraph.sdk.FileContentsQuery) ByteString(com.google.protobuf.ByteString) ExchangeRates(com.hedera.hashgraph.sdk.ExchangeRates) Client(com.hedera.hashgraph.sdk.Client)

Example 2 with FileContentsQuery

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

the class FeeSchedulesTest method canFetchFeeSchedules.

@Test
@DisplayName("FeeSchedules (CurrentAndNextFeeSchedule) is fetched and parsed from file 0.0.111")
void canFetchFeeSchedules() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    ByteString feeSchedulesBytes = new FileContentsQuery().setFileId(new FileId(0, 0, 111)).execute(testEnv.client);
    FeeSchedules feeSchedules = FeeSchedules.fromBytes(feeSchedulesBytes.toByteArray());
    /*
         * Test whether the file 0.0.111 actually contains stuff
         */
    assertThat(feeSchedules.getCurrent()).isNotNull();
    testEnv.close();
}
Also used : FileContentsQuery(com.hedera.hashgraph.sdk.FileContentsQuery) FeeSchedules(com.hedera.hashgraph.sdk.FeeSchedules) ByteString(com.google.protobuf.ByteString) FileId(com.hedera.hashgraph.sdk.FileId) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 3 with FileContentsQuery

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

the class FileAppendIntegrationTest method canAppendLargeContentsToFileDespiteExpiration.

@Test
@DisplayName("Can append large contents to file despite TRANSACTION_EXPIRATION response codes")
void canAppendLargeContentsToFileDespiteExpiration() throws Exception {
    // There are potential bugs in FileAppendTransaction which require more than one node to trigger.
    var testEnv = new IntegrationTestEnv(2);
    var response = new FileCreateTransaction().setKeys(testEnv.operatorKey).setContents("[e2e::FileCreateTransaction]").execute(testEnv.client);
    var fileId = Objects.requireNonNull(response.getReceipt(testEnv.client).fileId);
    Thread.sleep(5000);
    @Var var info = new FileInfoQuery().setFileId(fileId).execute(testEnv.client);
    assertThat(info.fileId).isEqualTo(fileId);
    assertThat(info.size).isEqualTo(28);
    assertThat(info.isDeleted).isFalse();
    assertThat(info.keys).isNotNull();
    assertThat(info.keys.getThreshold()).isNull();
    assertThat(info.keys).isEqualTo(KeyList.of(testEnv.operatorKey));
    var appendTx = new FileAppendTransaction().setFileId(fileId).setContents(Contents.BIG_CONTENTS).setTransactionValidDuration(Duration.ofSeconds(25)).execute(testEnv.client).getReceipt(testEnv.client);
    var contents = new FileContentsQuery().setFileId(fileId).execute(testEnv.client);
    assertThat(contents.toStringUtf8()).isEqualTo("[e2e::FileCreateTransaction]" + Contents.BIG_CONTENTS);
    info = new FileInfoQuery().setFileId(fileId).execute(testEnv.client);
    assertThat(info.fileId).isEqualTo(fileId);
    assertThat(info.size).isEqualTo(13522);
    assertThat(info.isDeleted).isFalse();
    assertThat(info.keys).isNotNull();
    assertThat(info.keys.getThreshold()).isNull();
    assertThat(info.keys).isEqualTo(KeyList.of(testEnv.operatorKey));
    new FileDeleteTransaction().setFileId(fileId).execute(testEnv.client).getReceipt(testEnv.client);
    testEnv.close();
}
Also used : FileContentsQuery(com.hedera.hashgraph.sdk.FileContentsQuery) FileCreateTransaction(com.hedera.hashgraph.sdk.FileCreateTransaction) FileInfoQuery(com.hedera.hashgraph.sdk.FileInfoQuery) Var(com.google.errorprone.annotations.Var) FileDeleteTransaction(com.hedera.hashgraph.sdk.FileDeleteTransaction) FileAppendTransaction(com.hedera.hashgraph.sdk.FileAppendTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 4 with FileContentsQuery

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

the class FileContentsIntegrationTest method canQueryEmptyFileContents.

@Test
@DisplayName("Can query empty file contents")
void canQueryEmptyFileContents() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    var response = new FileCreateTransaction().setKeys(testEnv.operatorKey).execute(testEnv.client);
    var fileId = Objects.requireNonNull(response.getReceipt(testEnv.client).fileId);
    var contents = new FileContentsQuery().setFileId(fileId).execute(testEnv.client);
    assertThat(contents.size()).isEqualTo(0);
    new FileDeleteTransaction().setFileId(fileId).execute(testEnv.client).getReceipt(testEnv.client);
    testEnv.close();
}
Also used : FileContentsQuery(com.hedera.hashgraph.sdk.FileContentsQuery) FileCreateTransaction(com.hedera.hashgraph.sdk.FileCreateTransaction) FileDeleteTransaction(com.hedera.hashgraph.sdk.FileDeleteTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 5 with FileContentsQuery

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

the class FileContentsIntegrationTest method getCostBigMaxQueryFileContents.

@Test
@DisplayName("Can get cost, even with a big max")
void getCostBigMaxQueryFileContents() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    var response = new FileCreateTransaction().setKeys(testEnv.operatorKey).setContents("[e2e::FileCreateTransaction]").execute(testEnv.client);
    var fileId = Objects.requireNonNull(response.getReceipt(testEnv.client).fileId);
    var contentsQuery = new FileContentsQuery().setFileId(fileId).setMaxQueryPayment(new Hbar(1000));
    var contents = contentsQuery.execute(testEnv.client);
    assertThat(contents.toStringUtf8()).isEqualTo("[e2e::FileCreateTransaction]");
    new FileDeleteTransaction().setFileId(fileId).execute(testEnv.client).getReceipt(testEnv.client);
    testEnv.close();
}
Also used : FileContentsQuery(com.hedera.hashgraph.sdk.FileContentsQuery) FileCreateTransaction(com.hedera.hashgraph.sdk.FileCreateTransaction) FileDeleteTransaction(com.hedera.hashgraph.sdk.FileDeleteTransaction) Hbar(com.hedera.hashgraph.sdk.Hbar) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

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