Write FHEVM-enabled Hardhat Tasks
Prerequisite
1
A Basic Hardhat Task.
task("task:get-count", "Calls the getCount() function of Counter Contract")
.addOptionalParam("address", "Optionally specify the Counter contract address")
.setAction(async function (taskArguments: TaskArguments, hre) {
const { ethers, deployments } = hre;
const CounterDeployement = taskArguments.address
? { address: taskArguments.address }
: await deployments.get("Counter");
console.log(`Counter: ${CounterDeployement.address}`);
const counterContract = await ethers.getContractAt("Counter", CounterDeployement.address);
const clearCount = await counterContract.getCount();
console.log(`Clear count : ${clearCount}`);
});2
Comment Out Existing Logic and rename
task("task:get-count", "Calls the getCount() function of Counter Contract")
.addOptionalParam("address", "Optionally specify the Counter contract address")
.setAction(async function (taskArguments: TaskArguments, hre) {
// const { ethers, deployments } = hre;
// const CounterDeployement = taskArguments.address
// ? { address: taskArguments.address }
// : await deployments.get("Counter");
// console.log(`Counter: ${CounterDeployement.address}`);
// const counterContract = await ethers.getContractAt("Counter", CounterDeployement.address);
// const clearCount = await counterContract.getCount();
// console.log(`Clear count : ${clearCount}`);
});task("task:get-count", "Calls the getCount() function of Counter Contract")task("task:decrypt-count", "Calls the getCount() function of Counter Contract")3
4
Call the view function getCount from the FHECounter contract
getCount from the FHECounter contract // const CounterDeployement = taskArguments.address
// ? { address: taskArguments.address }
// : await deployments.get("Counter");
// console.log(`Counter: ${CounterDeployement.address}`);
// const counterContract = await ethers.getContractAt("Counter", CounterDeployement.address);
// const clearCount = await counterContract.getCount(); const FHECounterDeployement = taskArguments.address
? { address: taskArguments.address }
: await deployments.get("FHECounter");
console.log(`FHECounter: ${FHECounterDeployement.address}`);
const fheCounterContract = await ethers.getContractAt("FHECounter", FHECounterDeployement.address);
const encryptedCount = await fheCounterContract.getCount();
if (encryptedCount === ethers.ZeroHash) {
console.log(`encrypted count: ${encryptedCount}`);
console.log("clear count : 0");
return;
}5
Decrypt the encrypted count value.
// console.log(`Clear count : ${clearCount}`); const signers = await ethers.getSigners();
const clearCount = await fhevm.userDecryptEuint(
FhevmType.euint32,
encryptedCount,
FHECounterDeployement.address,
signers[0],
);
console.log(`Encrypted count: ${encryptedCount}`);
console.log(`Clear count : ${clearCount}`);6
Last updated