Search in sources :

Example 1 with FileId

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

the class DeleteFileExample method main.

public static void main(String[] args) throws PrecheckStatusException, TimeoutException, 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);
    // The file is required to be a byte array,
    // you can easily use the bytes of a file instead.
    String fileContents = "Hedera hashgraph is great!";
    TransactionResponse transactionResponse = new FileCreateTransaction().setKeys(OPERATOR_KEY).setContents(fileContents).setMaxTransactionFee(new Hbar(2)).execute(client);
    TransactionReceipt receipt = transactionResponse.getReceipt(client);
    FileId newFileId = Objects.requireNonNull(receipt.fileId);
    System.out.println("file: " + newFileId);
    // now delete the file
    TransactionResponse fileDeleteTransactionResponse = new FileDeleteTransaction().setFileId(newFileId).execute(client);
    // if this doesn't throw then the transaction was a success
    fileDeleteTransactionResponse.getReceipt(client);
    System.out.println("File deleted successfully.");
    new FileInfoQuery().setFileId(newFileId).execute(client);
// note the above fileInfo will fail with FILE_DELETED due to a known issue on Hedera
}
Also used : FileCreateTransaction(com.hedera.hashgraph.sdk.FileCreateTransaction) FileInfoQuery(com.hedera.hashgraph.sdk.FileInfoQuery) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) FileDeleteTransaction(com.hedera.hashgraph.sdk.FileDeleteTransaction) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) Hbar(com.hedera.hashgraph.sdk.Hbar) FileId(com.hedera.hashgraph.sdk.FileId) Client(com.hedera.hashgraph.sdk.Client)

Example 2 with FileId

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

use of com.hedera.hashgraph.sdk.FileId in project hedera-mirror-node by hashgraph.

the class FileClient method createFile.

public NetworkTransactionResponse createFile(byte[] content) {
    log.debug("Create new file");
    String memo = getMemo("Create file");
    FileCreateTransaction fileCreateTransaction = new FileCreateTransaction().setKeys(sdkClient.getExpandedOperatorAccountId().getPublicKey()).setContents(content).setFileMemo(memo).setTransactionMemo(memo);
    NetworkTransactionResponse networkTransactionResponse = executeTransactionAndRetrieveReceipt(fileCreateTransaction, KeyList.of(sdkClient.getExpandedOperatorAccountId().getPrivateKey()));
    FileId fileId = networkTransactionResponse.getReceipt().fileId;
    log.debug("Created new file {}", fileId);
    return networkTransactionResponse;
}
Also used : FileCreateTransaction(com.hedera.hashgraph.sdk.FileCreateTransaction) NetworkTransactionResponse(com.hedera.mirror.test.e2e.acceptance.response.NetworkTransactionResponse) FileId(com.hedera.hashgraph.sdk.FileId)

Example 4 with FileId

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

the class CreateFileExample 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);
    // The file is required to be a byte array,
    // you can easily use the bytes of a file instead.
    String fileContents = "Hedera hashgraph is great!";
    TransactionResponse transactionResponse = new FileCreateTransaction().setKeys(OPERATOR_KEY.getPublicKey()).setContents(fileContents).setMaxTransactionFee(// 2 HBAR
    new Hbar(2)).execute(client);
    TransactionReceipt receipt = transactionResponse.getReceipt(client);
    FileId newFileId = receipt.fileId;
    System.out.println("file: " + newFileId);
}
Also used : FileCreateTransaction(com.hedera.hashgraph.sdk.FileCreateTransaction) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) Hbar(com.hedera.hashgraph.sdk.Hbar) FileId(com.hedera.hashgraph.sdk.FileId) Client(com.hedera.hashgraph.sdk.Client)

Example 5 with FileId

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

the class CreateSimpleContractExample method main.

public static void main(String[] args) throws PrecheckStatusException, IOException, TimeoutException, ReceiptStatusException {
    ClassLoader cl = CreateSimpleContractExample.class.getClassLoader();
    Gson gson = new Gson();
    JsonObject jsonObject;
    try (InputStream jsonStream = cl.getResourceAsStream("hello_world.json")) {
        if (jsonStream == null) {
            throw new RuntimeException("failed to get hello_world.json");
        }
        jsonObject = gson.fromJson(new InputStreamReader(jsonStream, StandardCharsets.UTF_8), JsonObject.class);
    }
    String byteCodeHex = jsonObject.getAsJsonPrimitive("object").getAsString();
    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);
    // create the contract's bytecode file
    TransactionResponse fileTransactionResponse = new FileCreateTransaction().setKeys(OPERATOR_KEY).setContents(byteCodeHex.getBytes(StandardCharsets.UTF_8)).setMaxTransactionFee(new Hbar(2)).execute(client);
    TransactionReceipt fileReceipt = fileTransactionResponse.getReceipt(client);
    FileId newFileId = Objects.requireNonNull(fileReceipt.fileId);
    System.out.println("contract bytecode file: " + newFileId);
    // create the contract itself
    TransactionResponse contractTransactionResponse = new ContractCreateTransaction().setGas(500000).setBytecodeFileId(newFileId).setAdminKey(OPERATOR_KEY).setMaxTransactionFee(new Hbar(16)).execute(client);
    TransactionReceipt contractReceipt = contractTransactionResponse.getReceipt(client);
    System.out.println(contractReceipt);
    ContractId newContractId = Objects.requireNonNull(contractReceipt.contractId);
    System.out.println("new contract ID: " + newContractId);
    ContractFunctionResult contractCallResult = new ContractCallQuery().setGas(500000).setContractId(newContractId).setFunction("greet").setQueryPayment(new Hbar(1)).execute(client);
    if (contractCallResult.errorMessage != null) {
        System.out.println("error calling contract: " + contractCallResult.errorMessage);
        return;
    }
    String message = contractCallResult.getString(0);
    System.out.println("contract message: " + message);
    // now delete the contract
    TransactionReceipt contractDeleteResult = new ContractDeleteTransaction().setContractId(newContractId).setTransferAccountId(contractTransactionResponse.transactionId.accountId).setMaxTransactionFee(new Hbar(1)).execute(client).getReceipt(client);
    if (contractDeleteResult.status != Status.SUCCESS) {
        System.out.println("error deleting contract: " + contractDeleteResult.status);
        return;
    }
    System.out.println("Contract successfully deleted");
}
Also used : FileCreateTransaction(com.hedera.hashgraph.sdk.FileCreateTransaction) ContractDeleteTransaction(com.hedera.hashgraph.sdk.ContractDeleteTransaction) InputStreamReader(java.io.InputStreamReader) ContractFunctionResult(com.hedera.hashgraph.sdk.ContractFunctionResult) InputStream(java.io.InputStream) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) Hbar(com.hedera.hashgraph.sdk.Hbar) ContractId(com.hedera.hashgraph.sdk.ContractId) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) ContractCallQuery(com.hedera.hashgraph.sdk.ContractCallQuery) FileId(com.hedera.hashgraph.sdk.FileId) ContractCreateTransaction(com.hedera.hashgraph.sdk.ContractCreateTransaction) Client(com.hedera.hashgraph.sdk.Client)

Aggregations

FileId (com.hedera.hashgraph.sdk.FileId)8 FileCreateTransaction (com.hedera.hashgraph.sdk.FileCreateTransaction)7 Client (com.hedera.hashgraph.sdk.Client)6 Hbar (com.hedera.hashgraph.sdk.Hbar)6 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)6 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)5 Gson (com.google.gson.Gson)2 JsonObject (com.google.gson.JsonObject)2 ByteString (com.google.protobuf.ByteString)2 ContractCallQuery (com.hedera.hashgraph.sdk.ContractCallQuery)2 ContractCreateTransaction (com.hedera.hashgraph.sdk.ContractCreateTransaction)2 ContractFunctionResult (com.hedera.hashgraph.sdk.ContractFunctionResult)2 ContractId (com.hedera.hashgraph.sdk.ContractId)2 FileContentsQuery (com.hedera.hashgraph.sdk.FileContentsQuery)2 FileInfoQuery (com.hedera.hashgraph.sdk.FileInfoQuery)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 ContractDeleteTransaction (com.hedera.hashgraph.sdk.ContractDeleteTransaction)1 ContractExecuteTransaction (com.hedera.hashgraph.sdk.ContractExecuteTransaction)1 ContractFunctionParameters (com.hedera.hashgraph.sdk.ContractFunctionParameters)1