Problems deploying Counter example with hardhat

I have the Counter example (docs .zama .ai/ fhevm/ fundamentals/ decryption/ decrypt) and am using the Hardhat template (github .com/ zama-ai/ fhevm-hardhat-template/)

$ pnpm localfhenix:faucet

> @zama-ai/fhevm-hardhat-template@1.0.0 localfhenix:faucet /workspaces/fhevm-fullstack/fhevm-hardhat-template
> hardhat task:fhenix:usefaucet --network localfhenix

Getting funds from faucet for 0xd7702EB6Ca4C101C918f7d4eaBeDc36e36260482
Success!
node ➜ /workspaces/fhevm-fullstack/fhevm-hardhat-template (main) $ pnpm localfhenix:deploy

> @zama-ai/fhevm-hardhat-template@1.0.0 localfhenix:deploy /workspaces/fhevm-fullstack/fhevm-hardhat-template
> hardhat deploy --network localfhenix

Nothing to compile
No need to generate any newer typings.
deploying "MyConfidentialERC20" (tx: 0xb12322324734acd73894a85659b167894151006c84ccfed7988f1c29282432ea)...: deployed at 0xbeb4eF1fcEa618C6ca38e3828B00f8D481EC2CC2 with 1398891 gas
MyConfidentialERC20 contract:  0xbeb4eF1fcEa618C6ca38e3828B00f8D481EC2CC2
deploying "EncryptedCounter3" (tx: 0x18330029ea3c789fdca3258edfe292884c35199ce617ad2a1127295c4310a317)...An unexpected error occurred:

Error: ERROR processing /workspaces/fhevm-fullstack/fhevm-hardhat-template/deploy/deploy_counter.ts:
Error: transaction failed [ See: https://links.ethers.org/v5-errors-CALL_EXCEPTION ] (transactionHash="0x18330029ea3c789fdca3258edfe292884c35199ce617ad2a1127295c4310a317", transaction={"hash":"0x18330029ea3c789fdca3258edfe292884c35199ce617ad2a1127295c4310a317","type":2,"accessList":[],"blockHash":"0x1e3e80cb33073b0fb31aa312494a9e533b5a1b58efc3008ed592bffc04a80b59","blockNumber":7,"transactionIndex":1,"confirmations":1,"from":"0xd7702EB6Ca4C101C918f7d4eaBeDc36e36260482","gasPrice":{"type":"BigNumber","hex":"0x05f5e101"},"maxPriorityFeePerGas":{"type":"BigNumber","hex":"0x01"},"maxFeePerGas":{"type":"BigNumber","hex":"0x078b30c4"},"gasLimit":{"type":"BigNumber","hex":"0x65a23a"},"to":null,"value":{"type":"BigNumber","hex":"0x00"},"nonce":1,"data":"0x608060405234801561000f575f80fd5b506101776100ae604080516080810182525f808252602082018190529181018290526060810191909152506040805160808101825273fee8407e2f5e3ee68ad77cae98c434e637f516e5815273687408ab54661ba0b4aef3a44156c616c6955e07602082015273fb03be574d14c256d56f09a198b586bdfc0a9de291810191909152739d6891a6240d6130c54ae243d8005063d05fe14b606082015290565b80515f80516020610c2983398151915280546001600160a01b03199081166001600160a01b039384161790915560208301517fed8d60e34876f751cc8b014c560745351147d9de11b9347c854e881b128ea6018054831691841691909117905560408301517fed8d60e34876f751cc8b014c560745351147d9de11b9347c854e881b128ea6028054831691841691909117905560608301517fed8d60e34876f751cc8b014c560745351147d9de11b9347c854e881b128ea6038054909216921691909117905550565b7f93ab6e17f2c461cce6ea5d4ec117e51dda77a64affc2b2c05f8cd440def0e70080546001600160a01b0319167333347831500f1e73f0cccbb95c9f86b94d7b11231790556101c55f6101db565b600a8190556101d3906101ed565b610337565b50565b5f6101e78260026101f7565b92915050565b6101d881306102aa565b5f805f80516020610c298339815191526001810154604051631ce2e8d760e31b8152600481018790527fff0000000000000000000000000000000000000000000000000000000000000060f887901b1660248201529192506001600160a01b03169063e71746b8906044016020604051808303815f875af115801561027e573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906102a29190610320565b949350505050565b5f5f80516020610c298339815191528054604051631974142760e21b8152600481018690526001600160a01b0385811660248301529293509116906365d0509c906044015f604051808303815f87803b158015610305575f80fd5b505af1158015610317573d5f803e3d5ffd5b50505050505050565b5f60208284031215610330575f80fd5b5051919050565b6108e5806103445f395ff3fe608060405234801561000f575f80fd5b5060043610610064575f3560e01c80637ef6bece1161004d5780637ef6bece146100955780639a3e5a60146100a8578063ea4f0a46146100bb575f80fd5b806361c5ca5a1461006857806364417a451461008b575b5f80fd5b600b546100759060ff1681565b60405160ff909116815260200160405180910390f35b6100936100c6565b005b6100936100a33660046106cf565b61012f565b6100756100b6366004610744565b610191565b600b5460ff16610075565b6040805160018082528183019092525f91602080830190803683370190505090506100f0600a5490565b815f8151811061010257610102610777565b602090810291909101015261012b816304d1f2d360e51b5f61012542606461078b565b5f6101f7565b5050565b5f61016f8484848080601f0160208091040260200160405190810160405280939291908181526020018383808284375f9201919091525061032192505050565b905061017d600a5482610335565b600a81905561018b90610363565b50505050565b5f6101c37f93ab6e17f2c461cce6ea5d4ec117e51dda77a64affc2b2c05f8cd440def0e700546001600160a01b031690565b6001600160a01b0316336001600160a01b0316146101df575f80fd5b50600b805460ff191660ff8316179055805b92915050565b7fed8d60e34876f751cc8b014c560745351147d9de11b9347c854e881b128ea600805460405163155f840160e21b81525f92916001600160a01b03169063557e100490610248908a906004016107e4565b5f604051808303815f87803b15801561025f575f80fd5b505af1158015610271573d5f803e3d5ffd5b505050505f61029d7f93ab6e17f2c461cce6ea5d4ec117e51dda77a64affc2b2c05f8cd440def0e70090565b8054604051634c0d927760e01b81529192506001600160a01b031690634c0d9277906102d5908b908b908b908b908b906004016107f6565b6020604051808303815f875af11580156102f1573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906103159190610850565b98975050505050505050565b5f61032e83836002610370565b9392505050565b5f82610347576103445f610494565b92505b81610358576103555f610494565b91505b61032e83835f6104a0565b61036d8130610580565b50565b7fed8d60e34876f751cc8b014c560745351147d9de11b9347c854e881b128ea601546040516302e817ff60e41b81525f917fed8d60e34876f751cc8b014c560745351147d9de11b9347c854e881b128ea600916001600160a01b0390911690632e817ff0906103ed9088903390899060f88a901b90600401610867565b6020604051808303815f875af1158015610409573d5f803e3d5ffd5b505050506040513d601f19601f8201168201806040525081019061042d9190610850565b81546040516346ce4e4960e11b8152600481018390523360248201529193506001600160a01b031690638d9c9c92906044015f604051808303815f87803b158015610476575f80fd5b505af1158015610488573d5f803e3d5ffd5b50505050509392505050565b5f6101f1826002610609565b5f8082156104b35750600160f81b6104b6565b505f5b5f7fed8d60e34876f751cc8b014c560745351147d9de11b9347c854e881b128ea600600181015460405163f953e42760e01b815260048101899052602481018890527fff00000000000000000000000000000000000000000000000000000000000000851660448201529192506001600160a01b03169063f953e427906064016020604051808303815f875af1158015610552573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906105769190610850565b9695505050505050565b5f7fed8d60e34876f751cc8b014c560745351147d9de11b9347c854e881b128ea6008054604051631974142760e21b8152600481018690526001600160a01b0385811660248301529293509116906365d0509c906044015f604051808303815f87803b1580156105ee575f80fd5b505af1158015610600573d5f803e3d5ffd5b50505050505050565b5f807fed8d60e34876f751cc8b014c560745351147d9de11b9347c854e881b128ea6006001810154604051631ce2e8d760e31b8152600481018790527fff0000000000000000000000000000000000000000000000000000000000000060f887901b1660248201529192506001600160a01b03169063e71746b8906044016020604051808303815f875af11580156106a3573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906106c79190610850565b949350505050565b5f805f604084860312156106e1575f80fd5b83359250602084013567ffffffffffffffff808211156106ff575f80fd5b818601915086601f830112610712575f80fd5b813581811115610720575f80fd5b876020828501011115610731575f80fd5b6020830194508093505050509250925092565b5f8060408385031215610755575f80fd5b82359150602083013560ff8116811461076c575f80fd5b809150509250929050565b634e487b7160e01b5f52603260045260245ffd5b808201808211156101f157634e487b7160e01b5f52601160045260245ffd5b5f815180845260208085019450602084015f5b838110156107d9578151875295820195908201906001016107bd565b509495945050505050565b602081525f61032e60208301846107aa565b60a081525f61080860a08301886107aa565b7fffffffff0000000000000000000000000000000000000000000000000000000096909616602083015250604081019390935260608301919091521515608090910152919050565b5f60208284031215610860575f80fd5b5051919050565b8481525f60206001600160a01b03861660208401526080604084015284518060808501525f5b818110156108a95786810183015185820160a00152820161088d565b505f60a0828601015260a0601f19601f8301168501019250505060ff60f81b831660608301529594505050505056fea164736f6c6343000818000aed8d60e34876f751cc8b014c560745351147d9de11b9347c854e881b128ea600","r":"0x97ea686a74f5713a81c4331afcb406f51ad4c1d9296dabb44bb9fca517358c97","s":"0x6dfb904f6b0062e332cf4de8949be5c54c7946754418e5d0ea326bf0d471f02e","v":0,"creates":"0xB170fC5BAC4a87A63fC84653Ee7e0db65CC62f96","chainId":420105}, receipt={"to":null,"from":"0xd7702EB6Ca4C101C918f7d4eaBeDc36e36260482","contractAddress":"0xB170fC5BAC4a87A63fC84653Ee7e0db65CC62f96","transactionIndex":1,"gasUsed":{"type":"BigNumber","hex":"0x034bab"},"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","blockHash":"0x1e3e80cb33073b0fb31aa312494a9e533b5a1b58efc3008ed592bffc04a80b59","transactionHash":"0x18330029ea3c789fdca3258edfe292884c35199ce617ad2a1127295c4310a317","logs":[],"blockNumber":7,"confirmations":1,"cumulativeGasUsed":{"type":"BigNumber","hex":"0x034bab"},"effectiveGasPrice":{"type":"BigNumber","hex":"0x05f5e100"},"status":0,"type":2,"byzantium":true}, code=CALL_EXCEPTION, version=providers/5.7.2)
    at Logger.makeError (/workspaces/fhevm-fullstack/fhevm-hardhat-template/node_modules/.pnpm/@ethersproject+logger@5.7.0/node_modules/@ethersproject/logger/src.ts/index.ts:269:28)
    at Logger.throwError (/workspaces/fhevm-fullstack/fhevm-hardhat-template/node_modules/.pnpm/@ethersproject+logger@5.7.0/node_modules/@ethersproject/logger/src.ts/index.ts:281:20)
    at Web3Provider.<anonymous> (/workspaces/fhevm-fullstack/fhevm-hardhat-template/node_modules/.pnpm/@ethersproject+providers@5.7.2/node_modules/@ethersproject/providers/src.ts/base-provider.ts:1549:24)
    at step (/workspaces/fhevm-fullstack/fhevm-hardhat-template/node_modules/.pnpm/@ethersproject+providers@5.7.2/node_modules/@ethersproject/providers/lib/base-provider.js:48:23)
    at Object.next (/workspaces/fhevm-fullstack/fhevm-hardhat-template/node_modules/.pnpm/@ethersproject+providers@5.7.2/node_modules/@ethersproject/providers/lib/base-provider.js:29:53)
    at fulfilled (/workspaces/fhevm-fullstack/fhevm-hardhat-template/node_modules/.pnpm/@ethersproject+providers@5.7.2/node_modules/@ethersproject/providers/lib/base-provider.js:20:58)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at DeploymentsManager.executeDeployScripts (/workspaces/fhevm-fullstack/fhevm-hardhat-template/node_modules/.pnpm/hardhat-deploy@0.12.4/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1215:19)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async DeploymentsManager.runDeploy (/workspaces/fhevm-fullstack/fhevm-hardhat-template/node_modules/.pnpm/hardhat-deploy@0.12.4/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1061:5)
    at async SimpleTaskDefinition.action (/workspaces/fhevm-fullstack/fhevm-hardhat-template/node_modules/.pnpm/hardhat-deploy@0.12.4/node_modules/hardhat-deploy/src/index.ts:450:5)
    at async Environment._runTaskDefinition (/workspaces/fhevm-fullstack/fhevm-hardhat-template/node_modules/.pnpm/hardhat@2.22.17_ts-node@10.9.2_@types+node@18.19.67_typescript@5.7.2__typescript@5.7.2/node_modules/hardhat/src/internal/core/runtime-environment.ts:351:14)
    at async Environment.run (/workspaces/fhevm-fullstack/fhevm-hardhat-template/node_modules/.pnpm/hardhat@2.22.17_ts-node@10.9.2_@types+node@18.19.67_typescript@5.7.2__typescript@5.7.2/node_modules/hardhat/src/internal/core/runtime-environment.ts:184:14)
    at async SimpleTaskDefinition.action (/workspaces/fhevm-fullstack/fhevm-hardhat-template/node_modules/.pnpm/hardhat-deploy@0.12.4/node_modules/hardhat-deploy/src/index.ts:601:32)
    at async Environment._runTaskDefinition (/workspaces/fhevm-fullstack/fhevm-hardhat-template/node_modules/.pnpm/hardhat@2.22.17_ts-node@10.9.2_@types+node@18.19.67_typescript@5.7.2__typescript@5.7.2/node_modules/hardhat/src/internal/core/runtime-environment.ts:351:14)
    at async Environment.run (/workspaces/fhevm-fullstack/fhevm-hardhat-template/node_modules/.pnpm/hardhat@2.22.17_ts-node@10.9.2_@types+node@18.19.67_typescript@5.7.2__typescript@5.7.2/node_modules/hardhat/src/internal/core/runtime-environment.ts:184:14)
    at async SimpleTaskDefinition.action (/workspaces/fhevm-fullstack/fhevm-hardhat-template/node_modules/.pnpm/hardhat-deploy@0.12.4/node_modules/hardhat-deploy/src/index.ts:690:5)
 ELIFECYCLE  Command failed with exit code 1.

I’ve also retried and it gives a gas error.

$ pnpm localfhenix:deploy

> @zama-ai/fhevm-hardhat-template@1.0.0 localfhenix:deploy /workspaces/fhevm-fullstack/fhevm-hardhat-template
> hardhat deploy --network localfhenix

Nothing to compile
No need to generate any newer typings.
transaction 0x18330029ea3c789fdca3258edfe292884c35199ce617ad2a1127295c4310a317 still pending... It used a gas pricing config of (gasPrice: 100000001 wei) ,
              current gas price is 100000000 wei
              new baseFee is 100000000
              
? Choose what to do with the pending transaction: … 
  increase gas
▸ continue waiting
  skip (forget tx)

Hello!
It seems you’re using localfhenix:faucet so you probably are not using the fhevm-hardhat-template. Fhenix is a different protocol and is not compatible with fhEVM.

To write your contract, simply clone GitHub - zama-ai/fhevm-hardhat-template: fhEVM hardhat template, then run pnpm install and pnpm test :wink:

Does it work on hardhat’s mock network?

fhenix-hardhat-network - Using mocked FHE on Hardhat network
Nothing to compile
No need to generate any newer typings.
deploying "MyConfidentialERC20" (tx: 0x4125cee59484b3dbb0d30074a5d53e42780305a4eb2b241cfce5d9d215cf0a63)...: deployed at 0x24abC9757b6749B53204Dd5BC49CFCa8C097C274 with 1398547 gas
MyConfidentialERC20 contract:  0x24abC9757b6749B53204Dd5BC49CFCa8C097C274
deploying "EncryptedCounter3"Transaction reverted: function returned an unexpected amount of data {"stackTrace":[{"type":0,"sourceReference":{"sourceName":"contracts/EncryptedCounter.sol","sourceContent":"// SPDX-License-Identifier: MIT\npragma solidity ^0.8.24;\n\nimport \"fhevm/lib/TFHE.sol\";\nimport { SepoliaZamaFHEVMConfig } from \"fhevm/config/ZamaFHEVMConfig.sol\";\nimport { SepoliaZamaGatewayConfig } from \"fhevm/config/ZamaGatewayConfig.sol\";\nimport \"fhevm/gateway/GatewayCaller.sol\";\n\n/// @title EncryptedCounter3\n/// @notice A contract that maintains an encrypted counter and is meant for demonstrating how decryption works\n/// @dev Uses TFHE library for fully homomorphic encryption operations and Gateway for decryption\n/// @custom:experimental This contract is experimental and uses FHE technology with decryption capabilities\ncontract EncryptedCounter3 is SepoliaZamaFHEVMConfig, SepoliaZamaGatewayConfig, GatewayCaller {

Your project is on fhenix, I can’t help you more if you don’t use the template mentioned above. Fhenix is not fhEVM, it’s a different technology provider.

Can you share the repository or your contract code?

I don’t want to deploy to sepolia. Trying to run it with pnpm hardhat node and using the frontend with localhost mocked mode.

Here
github /nicoleneo/ fhevm-react-template/ tree/ counter

The mocked fhevm server at localhost:3000 doesn’t seem accessible.

 /workspaces/fhevm-react-template/hardhat (counter) $ pnpm hardhat node
Nothing to compile
No need to generate any newer typings.
deploying "MyConfidentialERC20" (tx: 0x759e772118d2a2441668daebdd6172883f98a3642d2e8d029bff78c39b744288)...: deployed at 0x24abC9757b6749B53204Dd5BC49CFCa8C097C274 with 1704810 gas
MyConfidentialERC20 contract:  0x24abC9757b6749B53204Dd5BC49CFCa8C097C274
Alice minted 10000 tokens to herself
deploying "EncryptedCounter3" (tx: 0xe7a4d049c2b9ca55313e20d040bd37bc3cc6bfcae01a0bf597236ec30117a076)...Server running at http://localhost:3000
Error: connect ECONNREFUSED 127.0.0.1:8545
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1611:16) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 8545
}
: deployed at 0x5e70eE77DC3cEC038bA4d4a90663f651c0538fDD with 741401 gas
EncryptedCounter3 contract:  0x5e70eE77DC3cEC038bA4d4a90663f651c0538fDD
Started HTTP and WebSocket JSON-RPC server at http://0.0.0.0:8545/
/workspaces/fhevm-react-template/frontend (counter) $ pnpm run dev-mocked

> fhevmjs-react-template@0.0.0 dev-mocked /workspaces/fhevm-react-template/frontend
> MOCKED=true node scripts/dev.js

Preview server is running...
Listening on http://localhost:4173

using docs. zama. ai/ fhevm/ guides/ frontend/ webapp #using-the-mocked-coprocessor-for-front-end

Here’s the fix for the mock server. The issue is the mock server on port 3000 was started before the JSON RPC server when it relies on the JSON RPC server.

github / zama-ai/ fhevm-react-template/ commit /4d6579399230bf0ec8c6911f620df205bd5943b2

I now have a working rudimentary front-end for interacting with EncryptedCounter3 in my repo’s counter branch.