5. Real DEPLOY FHEVM on SEPOLIA

5.1 . New tree folder

fhevm-counter-demo/
├── contracts/
   ├── Counter.sol
   └── FHECounter.sol
├── test/
   ├── Counter.ts
   ├── FHECounter.ts
   └── FHECounterSepolia.ts        # Test on Sepolia (have in repository)
├── deploy/                        # new folder
   └── deploy-fhe-counter.ts       # Script deploy
├── hardhat.config.ts
├── package.json
├── .env                            # save MNEMONIC + INFURA
├── tsconfig.json                   # (if use TypeScript)
└── types/                          # (create auto when compile)
    └── ...

5.2 . deploy/deploy-fhe-counter.ts – Script deploy

nano deploy/deploy-fhe-counter.ts

coppy this

import { ethers } from "hardhat";

async function main() {
  console.log("Deploying FHECounter to Sepolia...");

  const FHECounter = await ethers.getContractFactory("FHECounter");
  const counter = await FHECounter.deploy();

  await counter.waitForDeployment();
  const address = await counter.getAddress();

  console.log("FHECounter deployed to:", address);
  console.log("View on Sepolia Explorer:");
  console.log(`https://sepolia.etherscan.io/address/${address}`);
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

5.3. env – save infor wallet + Infura

nano .env

coppy this:

MNEMONIC="word1 word2 word3 ... word12"
INFURA_API_KEY="abc123def456ghi789"

5.4. hardhat.config.ts – Config Sepolia chain

nano hardhat.config.ts

coppy this

import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";
import "@fhevm/hardhat-plugin";
import * as dotenv from "dotenv";

dotenv.config();

const config: HardhatUserConfig = {
  solidity: "0.8.24",
  networks: {
    hardhat: {},
    sepolia: {
      url: `https://sepolia.infura.io/v3/${process.env.INFURA_API_KEY || ""}`,
      chainId: 11155111,
      accounts: {
        mnemonic: process.env.MNEMONIC || "",
      },
    },
  },
};

export default config;

5.5. Setup full DEPENDENCIES

# 1. Hardhat Toolbox (need for ethers, chai, etc.)
npm install --save-dev @nomicfoundation/hardhat-toolbox

# 2. install plugin FHEVM (have in template but need again)
npm install --save-dev @fhevm/hardhat-plugin

# 3. Install dotenv (để đọc .env)
npm install --save-dev dotenv

# 4. Install TypeScript 
npm install --save-dev typescript ts-node @types/node

# 5. Install Relayer SDK
npm install --save-dev @zama-fhe/relayer-sdk

5.6. compile and deploy

npx hardhat compile
npx hardhat run deploy/deploy-fhe-counter.ts --network sepolia

5.7 . Export you contract adress + ABI

export ABI

grep -A 1000 '"abi":' artifacts/contracts/FHECounter.sol/FHECounter.json > ~/FHECounter-ABI.txt

=⇒ your ABI file export here : /root/FHECounter-ABI.txt

or

node -e "const fs = require('fs'); const data = JSON.parse(fs.readFileSync('artifacts/contracts/FHECounter.sol/FHECounter.json')); fs.writeFileSync('FHECounter-ABI.json', JSON.stringify(data.abi)); console.log('Done!');"

Last updated