Skip to content

Instantly share code, notes, and snippets.

@pcaversaccio
Last active May 30, 2022 11:58
Show Gist options
  • Save pcaversaccio/84727c7dd2efa145a50cd2a1615f54df to your computer and use it in GitHub Desktop.
Save pcaversaccio/84727c7dd2efa145a50cd2a1615f54df to your computer and use it in GitHub Desktop.
How to deploy a smart contract on Hedera using Hardhat.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
contract LookupContract {
mapping(string => uint256) public myDirectory;
constructor(string memory _name, uint256 _mobileNumber) {
myDirectory[_name] = _mobileNumber;
}
function setMobileNumber(string memory _name, uint256 _mobileNumber)
public
{
myDirectory[_name] = _mobileNumber;
}
function getMobileNumber(string memory _name)
public
view
returns (uint256)
{
return myDirectory[_name];
}
}
@pcaversaccio
Copy link
Author

pcaversaccio commented May 30, 2022

yarn add --dev @hashgraph/sdk
  • In the .env file store your Hedera testnet account ID and private key:
MY_ACCOUNT_ID=0.0.34...
MY_HEDERA_PRIVATE_KEY=302e0...
  • Create your contract in the contracts/ directory. For this tutorial, we will use the above LookupContract.

  • Now, create a file called index.ts in the root directory with the following content:

import {
  Client,
  AccountId,
  PrivateKey,
  FileCreateTransaction,
  ContractCreateTransaction,
  ContractFunctionParameters,
} from "@hashgraph/sdk";
import { ethers } from "hardhat";
import * as dotenv from "dotenv";

dotenv.config();

// Configure accounts and client
const operatorId = AccountId.fromString(process.env.MY_ACCOUNT_ID);
const operatorKey = PrivateKey.fromString(process.env.MY_HEDERA_PRIVATE_KEY);

if (operatorId == null || operatorKey == null) {
  throw new Error(
    "Environment variables myAccountId and myPrivateKey must be present"
  );
}

const client = Client.forTestnet().setOperator(operatorId, operatorKey);

async function main() {
  const Contract = await ethers.getContractFactory("LookupContract");

  // Create a file on Hedera and store the bytecode
  const fileCreateTx = new FileCreateTransaction()
    .setContents(Contract.bytecode)
    .setKeys([operatorKey])
    .freezeWith(client);
  const fileCreateSign = await fileCreateTx.sign(operatorKey);
  const fileCreateSubmit = await fileCreateSign.execute(client);
  const fileCreateRx = await fileCreateSubmit.getReceipt(client);
  const bytecodeFileId = fileCreateRx.fileId;
  console.log(`- The bytecode file ID is: ${bytecodeFileId} \n`);

  // Instantiate the smart contract
  const contractInstantiateTx = new ContractCreateTransaction()
    .setBytecodeFileId(bytecodeFileId)
    .setGas(100_000)
    .setConstructorParameters(
      new ContractFunctionParameters().addString("Alice").addUint256(111_111)
    );
  const contractInstantiateSubmit = await contractInstantiateTx.execute(client);
  const contractInstantiateRx = await contractInstantiateSubmit.getReceipt(
    client
  );
  const contractId = contractInstantiateRx.contractId;
  const contractAddress = contractId.toSolidityAddress();
  console.log(`- The smart contract ID is: ${contractId} \n`);
  console.log(
    `- The smart contract ID in Solidity format is: ${contractAddress} \n`
  );
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});
  • Now, we are ready to deploy the smart contract. Simply run:
npx hardhat run index.ts

The output will look like the following:
image

  • Eventually, you can look up the deployed smart contract on one of the block explorers. For the sake of this tutorial, I'm using HashScan: 0.0.34940384.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment