Deploy a Contract
Deploying a Contract Using the Hedera Smart Contract Service
This tutorial will walk you through writing and compiling a Solidity smart contract. You'll then deploy and interact with it on the Hedera network using the Hedera Smart Contract Service (HSCS) and familiar EVM tools like Ethers.js, connecting via the JSON-RPC relay.
What you will accomplish
By the end of this tutorial, you will be able to:
Write a smart contract
Compile a smart contract
Deploy a smart contract
Update smart contract state
Query smart contract state
Note: This tutorial is currently supported only in the Getting Started JavaScript series and is not available for other languages.
Prerequisites
Before you begin, you should have completed the following tutorials:
Step 1: Navigate to the hscs
Directory and Start the Relay
hscs
Directory and Start the RelayNavigate to the hscs
directory to deploy a smart contract example:
If you completed a previous example in the series, you can go back to the root directory and cd
into this example.
If you want to get back to the root directory, you can cd
out from any directory with this command
Start the JSON-RPC Relay
From the root directory of the hedera-future-world-js
project, run a Hedera JSON-RPC Relay instance with the below script:
What exactly is the JSON-RPC Relay?
The Hedera JSON-RPC Relay connects your dApps to Hedera’s EVM-compatible nodes, translating JSON-RPC requests from tools like ethers.js
or web3.js
into commands Hedera understands. This enables seamless interaction with smart contracts on Hedera.
Why is it important?
Developers can use familiar EVM tools and workflows on Hedera without learning new tooling.
Provides compatibility and support for Solidity smart contracts.
Enables smooth communication between dApps and Hedera.
Next: Code Walkthrough
In the next code walkthrough step, you can follow along or skip ahead to Step 3 to execute and deploy the contract. If you decide to skip ahead, compile the smart contract first.
Step 2: Guided Code Walkthrough
Open these two files located in the /hscs
directory in a code editor, such as VS Code or your GitPod instance, to follow along:
my_contract.sol
script-hscs-smart-contract
Write the Smart Contract
For this tutorial, a simple smart contract, my_contract.sol
, has already been prepared for you. You will only need to make one modification (outlined below) for it to compile successfully.
Get the Name Stored in Mapping
Inside the greet()
function, we want to access the names
mapping to retrieve the name of the account calling this function. The account is identified by its EVM account alias and can be accessed using msg.sender
in Solidity.
Note: This smart contract has two functions, introduce
and greet
. You will invoke both of them later on.
Compile the Smart Contract
Once you have completed writing the smart contract in Solidity, you must compile it using the Solidity compiler.
Install the dependencies using npm
. You will also need to install a Solidity compiler using the --global
flag.
Invoke the compiler on your Solidity file. Then list (ls
) files in the current directory.
You should see a console output similar to the following:
The
.abi
file contains the JSON representation of the interface used to interact with the smart contract.The
.bin
file contains EVM bytecode, which is used in deploying the smart contract.
Note that while the .abi
file is human-readable, the .bin
file is not intended to be human-readable.
After compiling your smart contract, your project directory should look similar to the following:
Write the Deployment Script
Initialize Wallet and RPC Connection
The following code snippet from the script-hscs-smart-contract
file will read your credentials from the .env
file to initialize your wallet (operator account) and establish an RPC connection to interact with the Hedera network.
If you have not configured your RPC connection to the Hedera network, do so by choosing one of the options from the How to Connect to Hedera Networks Over RPC tutorial before moving on to the next step. Then run ./util/04-rpcrelay-run.sh
to run a Hedera JSON-RPC relay instance.
Prepare Smart Contract for Deployment
This section of the code reads the compiled ABI and bytecode files of the smart contract using the fs
(file system) module. The first 32 characters of both the ABI and bytecode are then displayed in the console to confirm that they have been successfully read and are ready for deployment.
The ContractFactory
class deploys the smart contract to the network using the ABI, bytecode, and the operatorWallet
to authorize the transaction. After deployment, the contract's address, accessible via the address property of the myContract
object, is displayed in the console and uniquely identifies the contract on the network. This deployment result is stored in the myContract
variable, which is used in subsequent steps and already set up in the script.
Write Data to the Smart Contract
Next, you will invoke the introduce
function on your deployed smart contract. This function call initiates a state-changing transaction on the smart contract.
The transaction hash and a link to view it on HashScan will be provided in your console. See the example output here.
Read Data from the Smart Contract
In the previous step, you changed the state of the smart contract by invoking the introduce
function. This involved submitting a transaction to the network, which stored new data in the smart contract, such as an introduction message or relevant information. This change was recorded on the network as part of the transaction.
This time, you will read the state of the smart contract. Unlike the previous operation, reading the state is simpler since it does not require submitting a transaction or modifying the contract.
Finally, invoke the greet
function to read data from the smart contract and save its response to a variable, myContractQueryResult
. This operation is read-only and does not modify the state. This function will return stored data without creating a transaction.
When invoking functions in a smart contract, you may do so in two different ways:
With a transaction → Smart contract state may be changed.
Without a transaction → Smart contract state may be queried but may not be changed.
Step 3: Deploy Contract and Verify on HashScan Mirror Node Explorer
In the terminal, cd
into the hscs
directory and run the contract create transaction script to deploy the smart contract:
Sample output:
Open the smart contract deployment Hashscan URL in your browser and check that:
The contract exists
Under the "Contract Bytecode" section, its "Compiler Version" field matches the version of the Solidity compiler that you used (
0.8.17
) (2)Under the "Recent Contract Calls" section, There should be two transactions:
The transaction with the earlier timestamp (bottom) should be the deployment transaction. (3A)
Navigate to this transaction by clicking on the timestamp.
Under the "Contract Result" section, the "Input - Function & Args" field should be a relatively long set of hexadecimal values.
This is the EVM bytecode output by the Solidity compiler.
Navigate back to the Contract page (browser
⬅
button).
The transaction with the later timestamp (top) should be the function invocation transaction, of the
introduce
function. (3B)Navigate to this transaction by clicking on the timestamp.
Under the "Contract Result" section, the "Input - Function & Args" field should be a relatively short set of hexadecimal values.
This is the representation of
the function identifier as the first eight characters (e.g.
0xc63193f6
for theintroduce
function), andthe input string value (e.g.
0x5626775697a0
forbguiz
).
Navigate back to the Contract page (browser
⬅
button).
Code Check ✅
File:
my_contract.sol
Complete
Congratulations, you have completed the Deploy a Contract tutorial in the Getting Started series for the EVM Developers learning path! 🎉🎉🎉!
You learned how to:
Next Steps
Explore the tutorials below to discover all the EVM-compatible tools available on Hedera. Happy building!
Have questions? Join the Hedera Discord and post them in the developer-general
channel or ask on Stack Overflow.
Last updated