Search in sources :

Example 1 with ContractExecuteTransaction

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

the class ContractCreateFlowIntegrationTest method createContractWithFlow.

@Test
@DisplayName("Create contract with flow")
void createContractWithFlow() throws Throwable {
    var testEnv = new IntegrationTestEnv(1);
    var response = new ContractCreateFlow().setBytecode(SMART_CONTRACT_BYTECODE).setAdminKey(testEnv.operatorKey).setGas(100000).setConstructorParameters(new ContractFunctionParameters().addString("Hello from Hedera.")).setContractMemo("[e2e::ContractCreateFlow]").execute(testEnv.client);
    var contractId = Objects.requireNonNull(response.getReceipt(testEnv.client).contractId);
    var receipt = new ContractExecuteTransaction().setContractId(contractId).setGas(100000).setFunction("setMessage", new ContractFunctionParameters().addString("new message")).execute(testEnv.client).getReceipt(testEnv.client);
    assertThat(receipt.status).isEqualTo(Status.SUCCESS);
    new ContractDeleteTransaction().setTransferAccountId(testEnv.operatorId).setContractId(contractId).setTransferAccountId(testEnv.operatorId).execute(testEnv.client).getReceipt(testEnv.client);
    testEnv.close();
}
Also used : ContractDeleteTransaction(com.hedera.hashgraph.sdk.ContractDeleteTransaction) ContractExecuteTransaction(com.hedera.hashgraph.sdk.ContractExecuteTransaction) ContractFunctionParameters(com.hedera.hashgraph.sdk.ContractFunctionParameters) ContractCreateFlow(com.hedera.hashgraph.sdk.ContractCreateFlow) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 2 with ContractExecuteTransaction

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

the class ContractExecuteIntegrationTest method cannotExecuteContractWhenContractFunctionParametersAreNotSet.

@Test
@DisplayName("Cannot execute contract when contract function parameters are not set")
void cannotExecuteContractWhenContractFunctionParametersAreNotSet() 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(ReceiptStatusException.class).isThrownBy(() -> {
        new ContractExecuteTransaction().setContractId(contractId).setGas(100000).execute(testEnv.client).getReceipt(testEnv.client);
    }).withMessageContaining(Status.CONTRACT_REVERT_EXECUTED.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) ContractExecuteTransaction(com.hedera.hashgraph.sdk.ContractExecuteTransaction) ContractFunctionParameters(com.hedera.hashgraph.sdk.ContractFunctionParameters) 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 3 with ContractExecuteTransaction

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

the class ContractExecuteIntegrationTest method cannotExecuteContractWhenContractIDIsNotSet.

@Test
@DisplayName("Cannot execute contract when contract ID is not set")
void cannotExecuteContractWhenContractIDIsNotSet() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    assertThatExceptionOfType(ReceiptStatusException.class).isThrownBy(() -> {
        new ContractExecuteTransaction().setGas(100000).setFunction("setMessage", new ContractFunctionParameters().addString("new message")).execute(testEnv.client).getReceipt(testEnv.client);
    }).withMessageContaining(Status.INVALID_CONTRACT_ID.toString());
    testEnv.close();
}
Also used : ContractExecuteTransaction(com.hedera.hashgraph.sdk.ContractExecuteTransaction) ContractFunctionParameters(com.hedera.hashgraph.sdk.ContractFunctionParameters) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 4 with ContractExecuteTransaction

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

the class ContractClient method executeContract.

public NetworkTransactionResponse executeContract(ContractId contractId, long gas, String functionName, ContractFunctionParameters parameters, Hbar payableAmount) {
    log.debug("Call contract {}'s function {}", contractId, functionName);
    ContractExecuteTransaction contractExecuteTransaction = new ContractExecuteTransaction().setContractId(contractId).setGas(gas).setTransactionMemo(getMemo("Execute contract")).setMaxTransactionFee(Hbar.from(100));
    if (parameters == null) {
        contractExecuteTransaction.setFunction(functionName);
    } else {
        contractExecuteTransaction.setFunction(functionName, parameters);
    }
    if (payableAmount != null) {
        contractExecuteTransaction.setPayableAmount(payableAmount);
    }
    NetworkTransactionResponse networkTransactionResponse = executeTransactionAndRetrieveReceipt(contractExecuteTransaction);
    TransactionRecord transactionRecord = getTransactionRecord(networkTransactionResponse.getTransactionId());
    logContractFunctionResult(functionName, transactionRecord.contractFunctionResult);
    return networkTransactionResponse;
}
Also used : ContractExecuteTransaction(com.hedera.hashgraph.sdk.ContractExecuteTransaction) NetworkTransactionResponse(com.hedera.mirror.test.e2e.acceptance.response.NetworkTransactionResponse) TransactionRecord(com.hedera.hashgraph.sdk.TransactionRecord)

Example 5 with ContractExecuteTransaction

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

the class ContractExecuteIntegrationTest method cannotExecuteContractWhenGasIsNotSet.

@Test
@DisplayName("Cannot execute contract when gas is not set")
void cannotExecuteContractWhenGasIsNotSet() 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(ReceiptStatusException.class).isThrownBy(() -> {
        new ContractExecuteTransaction().setContractId(contractId).setFunction("setMessage", new ContractFunctionParameters().addString("new message")).execute(testEnv.client).getReceipt(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) ContractExecuteTransaction(com.hedera.hashgraph.sdk.ContractExecuteTransaction) ContractFunctionParameters(com.hedera.hashgraph.sdk.ContractFunctionParameters) FileDeleteTransaction(com.hedera.hashgraph.sdk.FileDeleteTransaction) ContractCreateTransaction(com.hedera.hashgraph.sdk.ContractCreateTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

ContractExecuteTransaction (com.hedera.hashgraph.sdk.ContractExecuteTransaction)7 ContractFunctionParameters (com.hedera.hashgraph.sdk.ContractFunctionParameters)6 DisplayName (org.junit.jupiter.api.DisplayName)5 Test (org.junit.jupiter.api.Test)5 ContractCreateTransaction (com.hedera.hashgraph.sdk.ContractCreateTransaction)4 ContractDeleteTransaction (com.hedera.hashgraph.sdk.ContractDeleteTransaction)4 FileCreateTransaction (com.hedera.hashgraph.sdk.FileCreateTransaction)4 FileDeleteTransaction (com.hedera.hashgraph.sdk.FileDeleteTransaction)3 Var (com.google.errorprone.annotations.Var)1 Gson (com.google.gson.Gson)1 JsonObject (com.google.gson.JsonObject)1 Client (com.hedera.hashgraph.sdk.Client)1 ContractCallQuery (com.hedera.hashgraph.sdk.ContractCallQuery)1 ContractCreateFlow (com.hedera.hashgraph.sdk.ContractCreateFlow)1 ContractFunctionResult (com.hedera.hashgraph.sdk.ContractFunctionResult)1 ContractId (com.hedera.hashgraph.sdk.ContractId)1 FileId (com.hedera.hashgraph.sdk.FileId)1 Hbar (com.hedera.hashgraph.sdk.Hbar)1 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)1 TransactionRecord (com.hedera.hashgraph.sdk.TransactionRecord)1