Deploy Smart Contracts on Hedera Using Truffle

The Hedera JSON RPC Relayarrow-up-right enables developers to use their favorite EVM-compatible tools such as Truffle, Hardhat, Web3JS, EthersJS, to deploy and interact with smart contracts on the Hedera network. As highlighted in a previous articlearrow-up-right, the relay provides applications and tools seamless access to Hedera while masking implementation complexities and preventing reductions in performance, security, and scalability.

This tutorial shows you how to deploy smart contracts on Hedera using Truffle and the JSON RPC Relay with the following steps:

  1. Create an account that has ECDSA keys using the Javascript SDK

  2. Compile a contract using Truffle

  3. Deploy the smart contract to Hedera network through the JSON RPC Relay

You can find more examples using Truffle, Web3JS, and Hardhat in this GitHub repositoryarrow-up-right.

Prerequisites

Create an Account that Has ECDSA Keys

Hedera supports two popular types of signature algorithms, ED25519 and ECDSA. Both are used in many blockchain platforms, including Bitcoin and Ethereum. Currently, the JSON RPC Relay only supports Hedera accounts with an alias set (i.e. public address) based on its ECDSA public key. To deploy a smart contract using Truffle, we first have to create a new account that meets these criteria. The main() function in create-account.js helps us do just that.

In case you’re interested in more details about auto account creation and alias, check out the documentationarrow-up-right and HIP-32arrow-up-right.

Executing this code generates a new ECDSA key pair, displays the information about the keys in Hedera and EVM formats, and transfers HBAR to the account alias (newAliasAccountId) to auto-create a Hedera account that meets the criteria mentioned before. Information about the new account is obtained in two ways, a transaction record queryarrow-up-right and a mirror node queryarrow-up-right.

Console Output:

IMPORTANT NOTE: Private keys for Testnet are displayed here for educational purposes only. Never share your private key(s) with others, as that may result in lost funds, or loss of control over your account.

The next step is to deploy a smart contract using Truffle and the newly created Hedera account. Copy the value from “New private key (RAW EVM)” in the console output and paste it into the ETH_PRIVATE_KEY variable in the .env file (if you cloned the repository, you may need to rename the file from .env_sample to .env).

Helper Functions

The functions autoCreateAccountFcn() and mirrorQueryFcn() perform the auto account creation and mirror query, respectively.

Compile a Smart Contract Using Truffle

Now it’s time to compile SimpleStorage, which is a basic smart contract that allows anyone to set and get data.

Use the following command to perform the compilation:

Console Output:

Deploy the Smart Contract to Hedera Using Truffle

Finally, deploy the contract on Hedera through the JSON RPC Relay. Be sure to configure the following parameters in your .env file to be able to deploy to the Hedera testnet with Truffle.

This example uses the Hashio instance of the JSON RPC Relayarrow-up-right, hosted by Swirlds Labs. URLs are also available for the Hedera Mainnet and Previewnet.

Deploy the contract with the following command:

Console Output ✅

You can obtain more information about the newly deployed contract using the mirror node REST APIarrow-up-right. Additional context for that API is provided in this blog postarrow-up-right. Based on the console output of the example above, here are two mirror node queries that provide more information about the new contract and account based on their respective Solidity addresses:

chevron-rightMirror node querieshashtag

Now you know how to deploy smart contracts on Hedera using Truffle and the JSON RPC Relay. The first part of this example used the Hedera JavaScript SDK. However, you can try this with the other officially supported SDKs for Java and Go.

Feel free to reach out if you have any questions:

a

Last updated