Deploy a Sol Contract
Deploy a solidity contract using the JSON-RPC Relay and leveraging Hardhat.
Deploy Contract (Hardhat)
info
For this instance we're using the framework Hardhat. Make sure you install the relevant npm package.
Two key steps before deploying the contract.
Hardhat config
Create a file called hardhat.config.js at the root of your project
require("@nomicfoundation/hardhat-toolbox");require('dotenv').config({path: '.env'});const arkhiaJsonRpcRelayTestnet = `<ARKHIA_JSON_RPC_URL>/<ARKHIA_API_KEY>`;const operatorPrivateKey = `<HEDERA_ACCOUNT_PVKEY>`;module.exports = { defaultNetwork: "hedera", solidity: "0.8.9", networks: { hedera: { url: arkhiaJsonRpcRelayTestnet, accounts: [operatorPrivateKey.toString()] } },};
Contract
Create a Greeter.sol with the code below under a
contracts
folder at the project root.
contract Greeter { string private greeting; constructor(string memory _greeting) { greeting = _greeting; } function greet() public view returns (string memory) { return greeting; } function setGreeting(string memory _greeting) public { greeting = _greeting; }}
Deploy Contract
- Node Js
const { ContractId } = require('@hashgraph/sdk');const { ethers } = require("hardhat");const arkhiaJsonApiUrl = `<YOUR_ARKHIA_JSON_URL>/<YOUR_ARKHIA_API_KEY>`;const provider = new ethers.providers.JsonRpcProvider(arkhiaJsonApiUrl);const operatorPrivateKey = `<HEDERA_ACCOUNT_PRIVATE_KEY>`;deployContract = async () => { try { const wallet = new ethers.Wallet(operatorPrivateKey, provider); // Key method to interact with the contract/constructor const Greeter = await ethers.getContractFactory('Greeter', wallet); const greeter = await Greeter.deploy(`Hello_message_set_in_contract`); const receipt = await greeter.deployTransaction.wait(); // Get deployed data const contractAddress = receipt.contractAddress; const contractID = ContractId.fromSolidityAddress(contractAddress); console.log(`Contract ${contractID} successfully deployed to ${contractAddress}`); return {contractAddress, contractID}; } catch(e) { console.error(e); }}
info
Checkpoint
That's it, now you are able to deploy a contract on Hedera using familiar tools like Hardhat! See in the next section how to query data.