Search in sources :

Example 31 with ContractFunctionParameters

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

the class ContractInfoIntegrationTest method getCostSmallMaxContractInfoFunction.

@Test
@DisplayName("Error, max is smaller than set payment.")
void getCostSmallMaxContractInfoFunction() 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 infoQuery = new ContractInfoQuery().setContractId(contractId).setMaxQueryPayment(Hbar.fromTinybars(1));
    assertThatExceptionOfType(MaxQueryPaymentExceededException.class).isThrownBy(() -> {
        infoQuery.execute(testEnv.client);
    });
    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) MaxQueryPaymentExceededException(com.hedera.hashgraph.sdk.MaxQueryPaymentExceededException) 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 32 with ContractFunctionParameters

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

ContractFunctionParameters (com.hedera.hashgraph.sdk.ContractFunctionParameters)32 DisplayName (org.junit.jupiter.api.DisplayName)30 Test (org.junit.jupiter.api.Test)30 ContractCreateTransaction (com.hedera.hashgraph.sdk.ContractCreateTransaction)29 FileCreateTransaction (com.hedera.hashgraph.sdk.FileCreateTransaction)28 ContractDeleteTransaction (com.hedera.hashgraph.sdk.ContractDeleteTransaction)23 FileDeleteTransaction (com.hedera.hashgraph.sdk.FileDeleteTransaction)22 Var (com.google.errorprone.annotations.Var)18 ContractInfoQuery (com.hedera.hashgraph.sdk.ContractInfoQuery)10 ContractCallQuery (com.hedera.hashgraph.sdk.ContractCallQuery)8 Hbar (com.hedera.hashgraph.sdk.Hbar)8 ContractExecuteTransaction (com.hedera.hashgraph.sdk.ContractExecuteTransaction)6 ContractByteCodeQuery (com.hedera.hashgraph.sdk.ContractByteCodeQuery)4 MaxQueryPaymentExceededException (com.hedera.hashgraph.sdk.MaxQueryPaymentExceededException)3 ContractUpdateTransaction (com.hedera.hashgraph.sdk.ContractUpdateTransaction)2 Gson (com.google.gson.Gson)1 JsonObject (com.google.gson.JsonObject)1 Client (com.hedera.hashgraph.sdk.Client)1 ContractCreateFlow (com.hedera.hashgraph.sdk.ContractCreateFlow)1 ContractFunctionResult (com.hedera.hashgraph.sdk.ContractFunctionResult)1