Deploying a Smart Contract on Hedera Hashgraph Network Using java

Deploying a Smart Contract on Hedera Hashgraph Network with Java

Posted by : suraj.verma | 01-Jan-2020

  • smart contract hedera hashgraph

    Hedera Hashgraph is a different type of distributed ledger technology that uses DAG (Directed Acyclic Graph). DAG is already being used in machine learning, compilers, and statistics. In this case, the data is not stored in blocks but is distributed by events. DAG system is also used to solve the Byzantine General Problems. As a rule, each project developing a distributed registry has its own way of synchronizing nodes, i.e their own consensus algorithm.

    Hashgraph Consensus Algorithm

    Each validator collects its transactions, randomly chooses the other participant, and transfers the information about the transactions to the participant, as well as the information that the other nodes have informed when they connected to him.

    The functionality is very simple: the validator “listens”, receives all messages that are coming to him, and afterward, randomly chooses the other participant and sends him all the transactions along with his own. Hashgraph guarantees that with the final number of steps (rounds of protocol’s work), all nodes reach the same state. The protocol works fast enough and is easily scalable. In the network, there is no leader. As long as 2/3 honest nodes remain, the protocol secures the high level of safeness. For nodes to be synchronized and to achieve the agreement between them, there is also an exchange process going between the other participants of the network randomly.

    Registration (KYC)

    First, you need to register on the portal and pass the identity proof. It is important, registration at the portal is possible as part of the testing phase.  Here are instructions for registration and completing the KYC process.

    Read More – Is Blockchain the Right Underlying Technology for Digital KYC verification

    Network selection

    First of all, you have to choose the network so that the smart contract functionality is available at the moment only in the testnet. So, let’s choose the testnet.

    Network access

    The portal will request that you provide an access code to the network, which can be received via e-mail, by leaving the request at the website.

    Enter the code, and Hedera will return the following information:

    This data will be needed in the future.

    Public and private keys

    First of all, you have to add a dependency in your pom.xml file given below:


    To generate public and private keys, you have to write the code in Java which is given below:

    Ed25519PrivateKey newKey = Ed25519PrivateKey.generate();
    Ed25519PublicKey newPublicKey = newKey.getPublicKey();

    Deploying the smart contract on Hedera Hashgraph network

    To deploy the smart contract on Hedera Hashgraph testnet, you have to compile your solidity code and get the bin file of that smart contract and your bin file containing some byte code. You can get bin file of your smart contract with the use of command and the example of byte code which is given below:-

    solc SimpleToken.sol –allow-paths “.” –bin –abi –optimize -o /home/suraj/Downloads/hedera1/

    Byte code: 302e020100300506032b657004220420d396c78d1d4c31a9f3f5c16eb81eb542803c89beddf30e1e1a583dd2db433704


    package com.suraj.phicoin.service;
    import com.hedera.hashgraph.sdk.Client;
    import com.hedera.hashgraph.sdk.account.AccountId;
    import com.hedera.hashgraph.sdk.crypto.ed25519.Ed25519PrivateKey;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Service;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Objects;
    public class HederaClient {
        private String operatorId;
        private String operatorKey;
        private String nodeAddress1;
        private String nodeAddress2;
        private String nodeAddress3;
        private String nodeAddress4;
        private String node1;
        private String node2;
        private String node3;
        private String node4;
        public Client hederaClientSetup() {
            AccountId operatorId1 = AccountId.fromString(operatorId);
            AccountId node_1 = AccountId.fromString(node1);
            AccountId node_2 = AccountId.fromString(node2);
            AccountId node_3 = AccountId.fromString(node3);
            AccountId node_4 = AccountId.fromString(node4);
            Map<AccountId,String> nodeAddressMap = new HashMap<>();
            Client hederaClient = new Client(nodeAddressMap);
            Ed25519PrivateKey operatorKey1 = Ed25519PrivateKey.fromString(operatorKey);
            return hederaClient;
        public String getOpreatorKey() {
            return operatorKey;
        public String getOperatorId() {
            return operatorId;
    package com.suraj.phicoin.service;
    import com.hedera.hashgraph.sdk.*;
    import com.hedera.hashgraph.sdk.contract.ContractCallQuery;
    import com.hedera.hashgraph.sdk.contract.ContractCreateTransaction;
    import com.hedera.hashgraph.sdk.contract.ContractDeleteTransaction;
    import com.hedera.hashgraph.sdk.contract.ContractId;
    import com.hedera.hashgraph.sdk.crypto.ed25519.Ed25519PrivateKey;
    import com.hedera.hashgraph.sdk.file.FileCreateTransaction;
    import com.hedera.hashgraph.sdk.file.FileId;
    import java.time.Duration;
    import java.time.Instant;
    public class CreateSimpleContract {
        private CreateSimpleContract() { }
        public static void main(String[] args) throws HederaException, IOException {
            HederaClient hederaClient = new HederaClient();
            String byteCodeHex = "608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506101cb806100606000396000f3fe608060405260043610610046576000357c01000000000000000000000000000000000000000000000000000000009004806341c0e1b51461004b578063cfae321714610062575b600080fd5b34801561005757600080fd5b506100606100f2565b005b34801561006e57600080fd5b50610077610162565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b757808201518184015260208101905061009c565b50505050905090810190601f1680156100e45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610160573373ffffffffffffffffffffffffffffffffffffffff16ff5b565b60606040805190810160405280600d81526020017f48656c6c6f2c20776f726c64210000000000000000000000000000000000000081525090509056fea165627a7a72305820ae96fb3af7cde9c0abfe365272441894ab717f816f07f41f07b1cbede54e256e0029";
            byte[] byteCode = byteCodeHex.getBytes();
            Ed25519PrivateKey operatorKey = Ed25519PrivateKey.fromString("302e020100300506032b657004220420d396c78d1d4c31a9f3f5c16eb81eb542803c89beddf30e1e1a583dd2db433704");
            Client client = hederaClient.hederaClientSetup();
            // create the contract's bytecode file
            FileCreateTransaction fileTx = new FileCreateTransaction(client).setExpirationTime(
                    // Use the same key as the operator to "own" this file
            TransactionReceipt fileReceipt = fileTx.executeForReceipt();
            FileId newFileId = fileReceipt.getFileId();
            System.out.println("contract bytecode file: " + newFileId);
            // create the contract itself
            ContractCreateTransaction contractTx = new ContractCreateTransaction(client).setAutoRenewPeriod(Duration.ofHours(1))
                    // set an admin key so we can delete the contract later
            TransactionReceipt contractReceipt = contractTx.executeForReceipt();
            ContractId newContractId = contractReceipt.getContractId();
            System.out.println("new contract ID: " + newContractId);
            FunctionResult contractCallResult = new ContractCallQuery(client).setGas(30000)
            if (contractCallResult.getErrorMessage() != null) {
                System.out.println("error calling contract: " + contractCallResult.getErrorMessage());
            String message = contractCallResult.getString();
            System.out.println("contract message: " + message);
            // now delete the contract
            TransactionReceipt contractDeleteResult = new ContractDeleteTransaction(client)
            if (contractDeleteResult.getStatus() != ResponseCodeEnum.SUCCESS) {
                System.out.println("error deleting contract: " + contractDeleteResult.getStatus());
            System.out.println("Contract successfully deleted");

Leave a Comment

Name is required

Comment is required

Recaptcha is required.

No Comments Yet.

About Author

suraj.verma (Writer)

More From Oodles

Request For Proposal

Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.

We would love to hear from you!

Oodles | Blockchain Development Company

Please enter a valid Phone Number

Please remove URL from text

Recaptcha is required.