Deploy Your First Smart Contract

Summary

In this tutorial, you will learn how to create a simple smart contract on Hedera using Solidity.

We recommend you complete the following introduction to get a basic understanding of Hedera transactions. This example does not build upon the previous examples.

Prerequisites

We recommend you complete the following introduction to get a basic understanding of Hedera transactions. This example does not build upon the previous examples.

1. Create a "Hello Hedera" Smart Contract

Create a smart contract in solidity using the remix IDE. The "Hello Hedera" contract Solidity file is sampled below along with the "Hello Hedera" JSON file that is produced after the contract has been compiled. You can use remix to create and compile the contract yourself or you can copy the files below into your project. If you are not familiar with Solidity you can check out the docs here. Hedera supports the latest version of Solidity (v0.8.9) on previewnet and testnet.

The contract stores two variables the owner and message. The constructor passes in the message parameter. The set_message function allows the owner to update the message variable and the get_message function allows you to return the message.

The HelloHedera.sol will serve as a reference to the contract that was compiled. The HelloHedera.json file contains the data.bytecode.object field that will be used to store the contract bytecode in a file on the Hedera network.

pragma solidity >=0.7.0 <0.8.9;

contract HelloHedera {
    // the contract's owner, set in the constructor
    address owner;

    // the message we're storing
    string message;

    constructor(string memory message_) {
        // set the owner of the contract for `kill()`
        owner = msg.sender;
        message = message_;
    }

    function set_message(string memory message_) public {
        // only allow the owner to update the message
        if (msg.sender != owner) return;
        message = message_;
    }

    // return a string
    function get_message() public view returns (string memory) {
        return message;
    }

    // recover the funds of the contract
    function kill() public { if (msg.sender == owner) selfdestruct(payable(msg.sender)); }
}

2. Store the Smart Contract Bytecode on Hedera

Create a file using the FileCreateTransaction() API to store the hex-encoded byte code of the "Hello Hedera" contract. Once the file is created, you can obtain the file ID from the receipt of the transaction.

You can alternatively use the CreateContractFlow() API that creates the bytecode file for you and subsequently creates the contract on Hedera in a single API.

3. Deploy a Hedera Smart Contract

Create the contract and set the file ID to the file ID that stores the hex-encoded byte code from the previous step. You will also need to set gas the value that will create the contract and pass the constructor parameters using ContractFunctionParameters() API. In this example, "hello from Hedera!" was passed to the constructor. After the transaction is successfully executed, you can get the contract ID from the receipt.

4. Call the get_message contract function

In the previous step, the contract message variable was set to "hello from Hedera!." You can return this message from the contract by submitting a query that will return the stored message string. The ContractCallQuery() similarly does not modify the state of the contract like other Hedera queries. It only reads stored values.

5. Call the set_message contract function

Call the set_message function of the contract. To do this you will need to use the ContractExecuteTransaction() API. This transaction will update the contract message. Once the transaction is successfully submitted you can verify the message was updated by requesting ContractCallQuery(). The message returned from the contract should now log "Hello from Hedera again!"

Congratulations 🎉! You have completed the following:

  • Created a simple smart contract on Hedera

  • Interacted with contract functions

Video tutorial

Have a question? Ask it on StackOverflow

Last updated