diff --git a/e2e/.env.local b/e2e/.env.local new file mode 100644 index 0000000..d1d3451 --- /dev/null +++ b/e2e/.env.local @@ -0,0 +1,9 @@ +export MNEMONIC="adapt mosquito move limb mobile illegal tree voyage juice mosquito burger raise father hope layer" + +export TFHE_EXECUTOR_CONTRACT_ADDRESS=0x596E6682c72946AF006B27C131793F2b62527A4b +export ACL_CONTRACT_ADDRESS=0x339EcE85B9E11a3A3AA557582784a15d7F82AAf2 +export PAYMENT_CONTRACT_ADDRESS=0x6d5A11aC509C707c00bc3A0a113ACcC26c532547 +export KMS_VERIFIER_CONTRACT_ADDRESS=0x208De73316E44722e16f6dDFF40881A3e4F86104 +export GATEWAY_CONTRACT_ADDRESS=0x096b4679d45fB675d4e2c1E4565009Cec99A12B1 + +export GATEWAY_URL="http://localhost:7077" diff --git a/e2e/.gitignore b/e2e/.gitignore index 381d33b..1e5e2ba 100644 --- a/e2e/.gitignore +++ b/e2e/.gitignore @@ -24,3 +24,4 @@ abi/ coverage.json pnpm-lock.yaml yarn.lock +contracts/E2EFHEVMConfig.sol diff --git a/e2e/Makefile b/e2e/Makefile new file mode 100644 index 0000000..beea36a --- /dev/null +++ b/e2e/Makefile @@ -0,0 +1,47 @@ +#!/usr/bin/make -f + +# Variables for paths and commands +ENV_FILE=.env +CONFIG_FILE=contracts/E2EFHEVMConfig.sol +HARDHAT_CONFIG=hardhat.config.ts +NPM_CMD=npm run task:accounts + +.PHONY: all print-accounts prepare-test-local-coprocessor prepare-test-sepolia clean + +# Default target +all: print-accounts + +# Prints accounts with a reminder to ensure they are funded +print-accounts: + @echo "Checking accounts. Ensure the first account has sufficient funds." + @$(NPM_CMD) + +# Prepares the environment for local coprocessor testing +prepare-test-local-coprocessor: + @echo "Preparing environment for local coprocessor testing..." + @cp -v .env.local $(ENV_FILE) + @cp -v contracts/E2EFHEVMConfigLocal.sol $(CONFIG_FILE) + @$(MAKE) set-local-network + $(MAKE) print-accounts + +# Prepares the environment for Sepolia network testing +prepare-test-sepolia: + @echo "Preparing environment for Sepolia network testing..." + @cp -v .env.example $(ENV_FILE) + @cp -v contracts/E2EFHEVMConfigSepolia.sol $(CONFIG_FILE) + $(MAKE) print-accounts + +# Sets the default network to "localCoprocessor" in hardhat.config.ts +set-local-network: + @echo "Setting default network to 'localCoprocessor' in $(HARDHAT_CONFIG)..." + @if [ "$(shell uname)" = "Darwin" ]; then \ + sed -i '' 's/defaultNetwork: ".*"/defaultNetwork: "localCoprocessor"/' $(HARDHAT_CONFIG); \ + else \ + sed -i 's/defaultNetwork: ".*"/defaultNetwork: "localCoprocessor"/' $(HARDHAT_CONFIG); \ + fi + +# Cleans up generated or temporary files +clean: + @echo "Cleaning up temporary files..." + @rm -fv $(ENV_FILE) + @rm -fv $(CONFIG_FILE) diff --git a/e2e/README.md b/e2e/README.md index 53eff73..b1a0572 100644 --- a/e2e/README.md +++ b/e2e/README.md @@ -1,6 +1,10 @@ -# e2e tests +# End-to-End (E2E) Tests -## Install +This repository contains end-to-end (E2E) tests to ensure that all components work together as intended. + +## Installation + +Before you begin, ensure that you have [Node.js](https://nodejs.org/) and [npm](https://www.npmjs.com/) installed on your machine. ```bash npm install @@ -8,22 +12,58 @@ npm install ## Configuration -1. Copy `.env.example` to `.env` and edit addresses with the correct one. -2. Edit your `.env` file with correct values. -3. Edit `contracts/E2EFHEVMConfig.sol` and set correct addresses of your fhEVM. -4. Edit `hardhat.config.ts` to set the `defaultNetwork`. By default, it is set to Sepolia, but you can use a different one - or add your own L1 address. -5. Fund the primary wallet derived from your mnemomic. If you don't know what is the public address, run - `npm run task:accounts` +### Default Configuration: Sepolia + +The default configuration is set up for the Sepolia network. To prepare the environment for testing on Sepolia, run the following command: + +```bash +make prepare-test-sepolia +``` + +This will: + +1. Copy `.env.example` to `.env`. +2. Update the `contracts/E2EFHEVMConfig.sol` file with the Sepolia-specific configuration. +3. Print the list of accounts and remind you to ensure the first account is funded. +4. Info: In hardhat.config.ts Seploia is already set as default network + +### Local Testing Configuration -## Run +To prepare the environment for testing with a local coprocessor setup, use the following command: + +```bash +make prepare-test-local-coprocessor +``` + +This will: + +1. Copy `.env.local` to `.env`. +2. Update the `contracts/E2EFHEVMConfig.sol` file with the local configuration. +3. Print the list of accounts and remind you to ensure the first account is funded. +4. Update hardhat.config.ts to make localCorpocessor as default network + +## Running the Tests + +To run the entire test suite: ```bash npm run test ``` -or if you want to run only one test +If you want to focus on a specific test: ```bash npm run test test/encryptedERC20/EncryptedERC20.ts ``` + +## Cleaning Up + +To clean up temporary files and reset the environment, run: + +```bash +make clean +``` + +--- + +**Note:** Always double-check your `.env` file and address configurations before running tests to avoid unexpected errors. diff --git a/e2e/contracts/E2EFHEVMConfigLocal.sol b/e2e/contracts/E2EFHEVMConfigLocal.sol new file mode 100644 index 0000000..2b308bb --- /dev/null +++ b/e2e/contracts/E2EFHEVMConfigLocal.sol @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: BSD-3-Clause-Clear +pragma solidity ^0.8.24; + +import {FHEVMConfig, TFHE} from "fhevm/lib/TFHE.sol"; +import { Gateway } from "fhevm/gateway/GatewayCaller.sol"; + +address constant gatewayAddress = 0x096b4679d45fB675d4e2c1E4565009Cec99A12B1; + +/** + * @title ZamaFHEVMConfig. + * @notice This library returns the TFHE config for different networks + * with the contract addresses for + * (1) ACL, (2) TFHEExecutor, (3) FHEPayment, (4) KMSVerifier, + * which are deployed & maintained by Zama. + */ +library DefaultFHEVMConfig { + function getConfig() internal pure returns (FHEVMConfig.FHEVMConfigStruct memory) { + return + FHEVMConfig.FHEVMConfigStruct({ + ACLAddress: 0x339EcE85B9E11a3A3AA557582784a15d7F82AAf2, + TFHEExecutorAddress: 0x596E6682c72946AF006B27C131793F2b62527A4b, + FHEPaymentAddress: 0x6d5A11aC509C707c00bc3A0a113ACcC26c532547, + KMSVerifierAddress: 0x208De73316E44722e16f6dDFF40881A3e4F86104 + }); + } +} + +/** + * @title MockZamaFHEVMConfig. + * @dev This contract can be inherited by a contract wishing to use these contracts on the mock + * environment provided by Zama. + * Other providers may offer similar contracts deployed at different addresses. + * If you wish to use them, you should rely on the instructions from these providers. + */ +contract E2EFHEVMConfig { + constructor() { + TFHE.setFHEVM(DefaultFHEVMConfig.getConfig()); + Gateway.setGateway(gatewayAddress); + } +} diff --git a/e2e/contracts/E2EFHEVMConfig.sol b/e2e/contracts/E2EFHEVMConfigSepolia.sol similarity index 100% rename from e2e/contracts/E2EFHEVMConfig.sol rename to e2e/contracts/E2EFHEVMConfigSepolia.sol diff --git a/e2e/hardhat.config.ts b/e2e/hardhat.config.ts index f92cd11..f525745 100644 --- a/e2e/hardhat.config.ts +++ b/e2e/hardhat.config.ts @@ -23,6 +23,7 @@ if (!mnemonic) { const chainIds = { zama: 9000, + localCoprocessor: 12345, local: 9000, ethereum: 1, sepolia: 11155111, @@ -34,6 +35,9 @@ function getChainConfig(chain: keyof typeof chainIds): NetworkUserConfig { case "local": jsonRpcUrl = "http://localhost:8545"; break; + case 'localCoprocessor': + jsonRpcUrl = 'http://localhost:8745'; + break; case "zama": jsonRpcUrl = "https://devnet.zama.ai"; break; @@ -85,6 +89,7 @@ const config: HardhatUserConfig = { }, zama: getChainConfig("zama"), local: getChainConfig("local"), + localCoprocessor: getChainConfig('localCoprocessor'), sepolia: getChainConfig("sepolia"), ethereum: getChainConfig("ethereum"), },