Search in sources :

Example 6 with FileCreateTransaction

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

the class ContractCallIntegrationTest method canCallContractFunction.

@Test
@DisplayName("Can call contract function")
void canCallContractFunction() 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 7 with FileCreateTransaction

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

the class ContractCallIntegrationTest method cannotCallContractFunctionWhenContractFunctionIsNotSet.

@Test
@DisplayName("Cannot call contract function when contract function is not set")
void cannotCallContractFunctionWhenContractFunctionIsNotSet() 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).setGas(100000).execute(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) 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 8 with FileCreateTransaction

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

the class ContractCreateIntegrationTest method cannotCreateContractWhenGasIsNotSet.

@Test
@DisplayName("Cannot create contract when gas is not set")
void cannotCreateContractWhenGasIsNotSet() 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);
    assertThatExceptionOfType(ReceiptStatusException.class).isThrownBy(() -> {
        new ContractCreateTransaction().setAdminKey(testEnv.operatorKey).setConstructorParameters(new ContractFunctionParameters().addString("Hello from Hedera.")).setBytecodeFileId(fileId).setContractMemo("[e2e::ContractCreateTransaction]").execute(testEnv.client).getReceipt(testEnv.client);
    }).withMessageContaining(Status.INSUFFICIENT_GAS.toString());
    new FileDeleteTransaction().setFileId(fileId).execute(testEnv.client).getReceipt(testEnv.client);
    testEnv.close();
}
Also used : FileCreateTransaction(com.hedera.hashgraph.sdk.FileCreateTransaction) 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 9 with FileCreateTransaction

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

the class ContractDeleteIntegrationTest method canDeleteContractWithAdminKey.

@Test
@DisplayName("Can delete contract with admin key")
void canDeleteContractWithAdminKey() 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 info = new ContractInfoQuery().setContractId(contractId).execute(testEnv.client);
    assertThat(info.contractId).isEqualTo(contractId);
    assertThat(info.accountId).isNotNull();
    assertThat(Objects.requireNonNull(info.accountId).toString()).isEqualTo(contractId.toString());
    assertThat(info.adminKey).isNotNull();
    assertThat(Objects.requireNonNull(info.adminKey).toString()).isEqualTo(Objects.requireNonNull(testEnv.operatorKey).toString());
    assertThat(info.storage).isEqualTo(128);
    assertThat(info.contractMemo).isEqualTo("[e2e::ContractCreateTransaction]");
    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) FileDeleteTransaction(com.hedera.hashgraph.sdk.FileDeleteTransaction) ContractInfoQuery(com.hedera.hashgraph.sdk.ContractInfoQuery) ContractCreateTransaction(com.hedera.hashgraph.sdk.ContractCreateTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 10 with FileCreateTransaction

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

the class ContractDeleteIntegrationTest method cannotDeleteContractWhichHasNoAdminKey.

@Test
@DisplayName("Can delete contract which has no admin key")
void cannotDeleteContractWhichHasNoAdminKey() 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().setGas(100000).setConstructorParameters(new ContractFunctionParameters().addString("Hello from Hedera.")).setBytecodeFileId(fileId).setContractMemo("[e2e::ContractCreateTransaction]").execute(testEnv.client).getReceipt(testEnv.client).contractId);
    var info = new ContractInfoQuery().setContractId(contractId).execute(testEnv.client);
    assertThat(info.contractId).isEqualTo(contractId);
    assertThat(info.accountId).isNotNull();
    assertThat(Objects.requireNonNull(info.accountId).toString()).isEqualTo(contractId.toString());
    assertThat(info.adminKey).isNotNull();
    // assertEquals(info.adminKey, contractId);
    assertThat(info.storage).isEqualTo(128);
    assertThat(info.contractMemo).isEqualTo("[e2e::ContractCreateTransaction]");
    assertThatExceptionOfType(ReceiptStatusException.class).isThrownBy(() -> {
        new ContractDeleteTransaction().setContractId(contractId).execute(testEnv.client).getReceipt(testEnv.client);
    }).withMessageContaining(Status.MODIFYING_IMMUTABLE_CONTRACT.toString());
    testEnv.close();
}
Also used : FileCreateTransaction(com.hedera.hashgraph.sdk.FileCreateTransaction) ContractDeleteTransaction(com.hedera.hashgraph.sdk.ContractDeleteTransaction) ContractFunctionParameters(com.hedera.hashgraph.sdk.ContractFunctionParameters) ContractInfoQuery(com.hedera.hashgraph.sdk.ContractInfoQuery) ContractCreateTransaction(com.hedera.hashgraph.sdk.ContractCreateTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

FileCreateTransaction (com.hedera.hashgraph.sdk.FileCreateTransaction)54 DisplayName (org.junit.jupiter.api.DisplayName)47 Test (org.junit.jupiter.api.Test)47 FileDeleteTransaction (com.hedera.hashgraph.sdk.FileDeleteTransaction)40 ContractCreateTransaction (com.hedera.hashgraph.sdk.ContractCreateTransaction)30 ContractFunctionParameters (com.hedera.hashgraph.sdk.ContractFunctionParameters)28 ContractDeleteTransaction (com.hedera.hashgraph.sdk.ContractDeleteTransaction)23 Var (com.google.errorprone.annotations.Var)22 FileInfoQuery (com.hedera.hashgraph.sdk.FileInfoQuery)16 Hbar (com.hedera.hashgraph.sdk.Hbar)16 ContractInfoQuery (com.hedera.hashgraph.sdk.ContractInfoQuery)10 ContractCallQuery (com.hedera.hashgraph.sdk.ContractCallQuery)9 FileContentsQuery (com.hedera.hashgraph.sdk.FileContentsQuery)8 FileId (com.hedera.hashgraph.sdk.FileId)7 Client (com.hedera.hashgraph.sdk.Client)6 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)6 MaxQueryPaymentExceededException (com.hedera.hashgraph.sdk.MaxQueryPaymentExceededException)5 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)5 ContractByteCodeQuery (com.hedera.hashgraph.sdk.ContractByteCodeQuery)4 ContractExecuteTransaction (com.hedera.hashgraph.sdk.ContractExecuteTransaction)4