Search in sources :

Example 6 with ContractCallQuery

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

the class ContractCallIntegrationTest method cannotCallContractFunctionWhenGasIsNotSet.

@Test
@DisplayName("Cannot call contract function when gas is not set")
void cannotCallContractFunctionWhenGasIsNotSet() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    var response = new FileCreateTransaction().setKeys(testEnv.operatorKey).setContents(SMART_CONTRACT_BYTECODE).execute(testEnv.client);
    var fileId = Objects.requireNonNull(response.getReceipt(testEnv.client).fileId);
    var contractId = Objects.requireNonNull(new ContractCreateTransaction().setAdminKey(testEnv.operatorKey).setGas(100000).setConstructorParameters(new ContractFunctionParameters().addString("Hello from Hedera.")).setBytecodeFileId(fileId).setContractMemo("[e2e::ContractCreateTransaction]").execute(testEnv.client).getReceipt(testEnv.client).contractId);
    assertThatExceptionOfType(PrecheckStatusException.class).isThrownBy(() -> {
        new ContractCallQuery().setContractId(contractId).setFunction("getMessage").execute(testEnv.client);
    }).withMessageContaining(Status.INSUFFICIENT_GAS.toString());
    new ContractDeleteTransaction().setTransferAccountId(testEnv.operatorId).setContractId(contractId).execute(testEnv.client).getReceipt(testEnv.client);
    new FileDeleteTransaction().setFileId(fileId).execute(testEnv.client).getReceipt(testEnv.client);
    testEnv.close();
}
Also used : FileCreateTransaction(com.hedera.hashgraph.sdk.FileCreateTransaction) ContractDeleteTransaction(com.hedera.hashgraph.sdk.ContractDeleteTransaction) ContractFunctionParameters(com.hedera.hashgraph.sdk.ContractFunctionParameters) ContractCallQuery(com.hedera.hashgraph.sdk.ContractCallQuery) FileDeleteTransaction(com.hedera.hashgraph.sdk.FileDeleteTransaction) ContractCreateTransaction(com.hedera.hashgraph.sdk.ContractCreateTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 7 with ContractCallQuery

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

the class ContractCallIntegrationTest method getCostBigMaxContractCallFunction.

@Test
@DisplayName("Can get cost, even with a big max")
void getCostBigMaxContractCallFunction() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    @Var var response = new FileCreateTransaction().setKeys(testEnv.operatorKey).setContents(SMART_CONTRACT_BYTECODE).execute(testEnv.client);
    var fileId = Objects.requireNonNull(response.getReceipt(testEnv.client).fileId);
    response = new ContractCreateTransaction().setAdminKey(testEnv.operatorKey).setGas(100000).setConstructorParameters(new ContractFunctionParameters().addString("Hello from Hedera.")).setBytecodeFileId(fileId).setContractMemo("[e2e::ContractCreateTransaction]").execute(testEnv.client);
    var contractId = Objects.requireNonNull(response.getReceipt(testEnv.client).contractId);
    var callQuery = new ContractCallQuery().setContractId(contractId).setGas(100000).setFunction("getMessage").setQueryPayment(new Hbar(1));
    var result = callQuery.execute(testEnv.client);
    assertThat(result.getString(0)).isEqualTo("Hello from Hedera.");
    new ContractDeleteTransaction().setTransferAccountId(testEnv.operatorId).setContractId(contractId).execute(testEnv.client).getReceipt(testEnv.client);
    new FileDeleteTransaction().setFileId(fileId).execute(testEnv.client).getReceipt(testEnv.client);
    testEnv.close();
}
Also used : FileCreateTransaction(com.hedera.hashgraph.sdk.FileCreateTransaction) ContractDeleteTransaction(com.hedera.hashgraph.sdk.ContractDeleteTransaction) Var(com.google.errorprone.annotations.Var) ContractFunctionParameters(com.hedera.hashgraph.sdk.ContractFunctionParameters) ContractCallQuery(com.hedera.hashgraph.sdk.ContractCallQuery) FileDeleteTransaction(com.hedera.hashgraph.sdk.FileDeleteTransaction) Hbar(com.hedera.hashgraph.sdk.Hbar) ContractCreateTransaction(com.hedera.hashgraph.sdk.ContractCreateTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 8 with ContractCallQuery

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

Example 9 with ContractCallQuery

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

the class CreateStatefulContractExample method main.

public static void main(String[] args) throws PrecheckStatusException, TimeoutException, IOException, ReceiptStatusException {
    ClassLoader cl = CreateStatefulContractExample.class.getClassLoader();
    Gson gson = new Gson();
    JsonObject jsonObject;
    try (InputStream jsonStream = cl.getResourceAsStream("stateful.json")) {
        if (jsonStream == null) {
            throw new RuntimeException("failed to get stateful.json");
        }
        jsonObject = gson.fromJson(new InputStreamReader(jsonStream, StandardCharsets.UTF_8), JsonObject.class);
    }
    String byteCodeHex = jsonObject.getAsJsonPrimitive("object").getAsString();
    byte[] byteCode = byteCodeHex.getBytes(StandardCharsets.UTF_8);
    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);
    // default max fee for all transactions executed by this client
    client.setDefaultMaxTransactionFee(new Hbar(100));
    client.setDefaultMaxQueryPayment(new Hbar(10));
    // create the contract's bytecode file
    TransactionResponse fileTransactionResponse = new FileCreateTransaction().setKeys(OPERATOR_KEY).setContents(byteCode).execute(client);
    TransactionReceipt fileReceipt = fileTransactionResponse.getReceipt(client);
    FileId newFileId = Objects.requireNonNull(fileReceipt.fileId);
    System.out.println("contract bytecode file: " + newFileId);
    TransactionResponse contractTransactionResponse = new ContractCreateTransaction().setBytecodeFileId(newFileId).setGas(500000).setConstructorParameters(new ContractFunctionParameters().addString("hello from hedera!")).execute(client);
    TransactionReceipt contractReceipt = contractTransactionResponse.getReceipt(client);
    ContractId newContractId = Objects.requireNonNull(contractReceipt.contractId);
    System.out.println("new contract ID: " + newContractId);
    ContractFunctionResult contractCallResult = new ContractCallQuery().setContractId(newContractId).setGas(500000).setFunction("get_message").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 returned message: " + message);
    TransactionResponse contractExecTransactionResponse = new ContractExecuteTransaction().setContractId(newContractId).setGas(500000).setFunction("set_message", new ContractFunctionParameters().addString("hello from hedera again!")).execute(client);
    // if this doesn't throw then we know the contract executed successfully
    contractExecTransactionResponse.getReceipt(client);
    // now query contract
    ContractFunctionResult contractUpdateResult = new ContractCallQuery().setContractId(newContractId).setGas(500000).setFunction("get_message").setQueryPayment(new Hbar(1)).execute(client);
    if (contractUpdateResult.errorMessage != null) {
        System.out.println("error calling contract: " + contractUpdateResult.errorMessage);
        return;
    }
    String message2 = contractUpdateResult.getString(0);
    System.out.println("contract returned message: " + message2);
}
Also used : FileCreateTransaction(com.hedera.hashgraph.sdk.FileCreateTransaction) ContractExecuteTransaction(com.hedera.hashgraph.sdk.ContractExecuteTransaction) 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) ContractFunctionParameters(com.hedera.hashgraph.sdk.ContractFunctionParameters) 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

ContractCallQuery (com.hedera.hashgraph.sdk.ContractCallQuery)9 ContractCreateTransaction (com.hedera.hashgraph.sdk.ContractCreateTransaction)9 FileCreateTransaction (com.hedera.hashgraph.sdk.FileCreateTransaction)9 ContractDeleteTransaction (com.hedera.hashgraph.sdk.ContractDeleteTransaction)8 ContractFunctionParameters (com.hedera.hashgraph.sdk.ContractFunctionParameters)8 FileDeleteTransaction (com.hedera.hashgraph.sdk.FileDeleteTransaction)7 DisplayName (org.junit.jupiter.api.DisplayName)7 Test (org.junit.jupiter.api.Test)7 Hbar (com.hedera.hashgraph.sdk.Hbar)5 Var (com.google.errorprone.annotations.Var)4 Gson (com.google.gson.Gson)2 JsonObject (com.google.gson.JsonObject)2 Client (com.hedera.hashgraph.sdk.Client)2 ContractFunctionResult (com.hedera.hashgraph.sdk.ContractFunctionResult)2 ContractId (com.hedera.hashgraph.sdk.ContractId)2 FileId (com.hedera.hashgraph.sdk.FileId)2 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)2 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2