Search in sources :

Example 31 with Client

use of com.hedera.hashgraph.sdk.Client 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 32 with Client

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

Example 33 with Client

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

the class CreateTopicExample 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);
    // Create three accounts, Alice, Bob, and Charlie.  Alice will be the treasury for our example token.
    // Fees only apply in transactions not involving the treasury, so we need two other accounts.
    TransactionResponse createResponse = new TopicCreateTransaction().execute(client);
    TransactionReceipt createReceipt = createResponse.getReceipt(client);
    System.out.println("topic id = " + createReceipt.topicId);
    TransactionResponse sendResponse = new TopicMessageSubmitTransaction().setTopicId(createReceipt.topicId).setMessage("Hello World").execute(client);
    TransactionReceipt sendReceipt = sendResponse.getReceipt(client);
    System.out.println("topic sequence number = " + sendReceipt.topicSequenceNumber);
}
Also used : TopicCreateTransaction(com.hedera.hashgraph.sdk.TopicCreateTransaction) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) Client(com.hedera.hashgraph.sdk.Client) TopicMessageSubmitTransaction(com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction)

Example 34 with Client

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

the class AccountBalanceFragment method onViewCreated.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    accountBalance = view.findViewById(R.id.accountBalance);
    accountId = view.findViewById(R.id.accountId);
    view.findViewById(R.id.button).setOnClickListener(v -> {
        try {
            final AccountId operatorId = AccountId.fromString(view.getResources().getString(R.string.operator_id));
            final Ed25519PrivateKey operatorKey = Ed25519PrivateKey.fromString(view.getResources().getString(R.string.operator_key));
            Client client = Client.forTestnet().setOperator(operatorId, operatorKey);
            final AccountId id = AccountId.fromString(accountId.getText().toString());
            new AccountBalanceAsyncTask(id).execute(client);
        } catch (IllegalArgumentException e) {
            accountBalance.setText("Error: " + e.getMessage());
        }
    });
}
Also used : Ed25519PrivateKey(com.hedera.hashgraph.sdk.crypto.ed25519.Ed25519PrivateKey) AccountId(com.hedera.hashgraph.sdk.account.AccountId) Client(com.hedera.hashgraph.sdk.Client)

Example 35 with Client

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

the class CryptoTransferFragment method onViewCreated.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    recipientAccountId = view.findViewById(R.id.recipientAccountId);
    amountToSend = view.findViewById(R.id.amountToSend);
    resultText = view.findViewById(R.id.transferResult);
    view.findViewById(R.id.sendHbar).setOnClickListener(v -> {
        try {
            final AccountId operatorId = AccountId.fromString(view.getResources().getString(R.string.operator_id));
            final Ed25519PrivateKey operatorKey = Ed25519PrivateKey.fromString(view.getResources().getString(R.string.operator_key));
            Client client = Client.forTestnet().setOperator(operatorId, operatorKey);
            final AccountId recipientId = AccountId.fromString(recipientAccountId.getText().toString());
            final Hbar amount = new Hbar(new BigDecimal(amountToSend.getText().toString()).longValueExact());
            new TransferAsyncTask(operatorId, recipientId, amount).execute(client);
        } catch (Throwable e) {
            resultText.setText("Error: " + e.getMessage());
        }
    });
}
Also used : Ed25519PrivateKey(com.hedera.hashgraph.sdk.crypto.ed25519.Ed25519PrivateKey) AccountId(com.hedera.hashgraph.sdk.account.AccountId) Hbar(com.hedera.hashgraph.sdk.Hbar) Client(com.hedera.hashgraph.sdk.Client) BigDecimal(java.math.BigDecimal)

Aggregations

Client (com.hedera.hashgraph.sdk.Client)41 Hbar (com.hedera.hashgraph.sdk.Hbar)23 AccountId (com.hedera.hashgraph.sdk.AccountId)20 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)20 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)16 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)16 TransferTransaction (com.hedera.hashgraph.sdk.TransferTransaction)13 AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)12 AccountBalanceQuery (com.hedera.hashgraph.sdk.AccountBalanceQuery)9 Var (com.google.errorprone.annotations.Var)6 FileCreateTransaction (com.hedera.hashgraph.sdk.FileCreateTransaction)6 FileId (com.hedera.hashgraph.sdk.FileId)6 KeyList (com.hedera.hashgraph.sdk.KeyList)6 PublicKey (com.hedera.hashgraph.sdk.PublicKey)6 TimeoutException (java.util.concurrent.TimeoutException)6 AccountDeleteTransaction (com.hedera.hashgraph.sdk.AccountDeleteTransaction)5 ByteString (com.google.protobuf.ByteString)4 AccountBalance (com.hedera.hashgraph.sdk.AccountBalance)4 ScheduleId (com.hedera.hashgraph.sdk.ScheduleId)4 ScheduleInfoQuery (com.hedera.hashgraph.sdk.ScheduleInfoQuery)4