|
1 | | -# fhevm-foundry-template |
| 1 | +# FHEVM Foundry Template |
2 | 2 |
|
3 | | -This repo is a POC showing how to use fhevmjs functions inside Solidity Foundry scripts. There are two scripts that you can run on Sepolia test network, after setting up your `.env` file (please reuse same keys but different values than the ones given inside `.env.example` and install all npm packages): |
4 | | -The fist one shows how NOT to use reencrypt in a script: |
| 3 | +A Foundry-based template for developing Fully Homomorphic Encryption (FHE) enabled Solidity smart contracts using the |
| 4 | +FHEVM protocol by Zama. |
5 | 5 |
|
6 | | -``` |
7 | | -forge script script/MyConfidentialERC20Reencrypt.s.sol --rpc-url $SEPOLIA_RPC_URL --ffi --broadcast |
8 | | -``` |
| 6 | +## Quick Start |
9 | 7 |
|
10 | | -This script will run but it won't work "as expected" and will log a `0` for the minted balance by Alice instead of the correct value of `1000` which corresponds to the minted amount. This is unavoidable, since in Foundry FFI calls (and console logs) are always happening during the first simulation step, and will never happen during the real transaction broadcasting phase, so getting a 0 value is unavoidable, since the reencrypt function from fhevmjs depends on the real onchain state of Sepolia, after the mint transaction has been validated (see foundry team explanation [here](https://github.com/foundry-rs/foundry/issues/5776#issuecomment-1867287499)). You can still get the correct reencrypted value after the script is done by running: |
| 8 | +For detailed instructions see: |
| 9 | +[FHEVM Foundry Quick Start Tutorial](https://docs.zama.ai/protocol/solidity-guides/getting-started/quick-start-tutorial) |
11 | 10 |
|
12 | | -``` |
13 | | -ts-node --transpile-only utils/reencrypt.ts [HANDLE] [PRIVATE_KEY] [CONTRACT_ADDRESS] |
14 | | -``` |
| 11 | +### Prerequisites |
15 | 12 |
|
16 | | -All of the [HANDLE], [PRIVATE_KEY] and [CONTRACT_ADDRESS] values will be correctly logged by the previously run script. |
| 13 | +- **Foundry**: [Installation guide](https://book.getfoundry.sh/getting-started/installation) |
17 | 14 |
|
18 | | -On the other hand, we have a second script doing user input encryption (using fhevmjs via FFI) + mint + transfer of a confidential erc20, this script runs successfully as expected, because for encryption, contrarily to reencryption/decryption, data does NOT depend on the onchain Sepolia state. You can run it via: |
| 15 | +### Installation |
19 | 16 |
|
20 | | -``` |
21 | | -forge script script/MyConfidentialERC20Encrypt.s.sol --rpc-url $SEPOLIA_RPC_URL --ffi --broadcast |
22 | | -``` |
| 17 | +1. **Install dependencies** |
| 18 | + |
| 19 | + ```bash |
| 20 | + forge soldeer install |
| 21 | + ``` |
| 22 | + |
| 23 | +2. **Compile and test** |
| 24 | + |
| 25 | + ```bash |
| 26 | + forge build |
| 27 | + forge test -vvv |
| 28 | + ``` |
| 29 | + |
| 30 | +3. **Deploy to local network** |
| 31 | + |
| 32 | + Start an Anvil node with the FHEVM host contracts deployed (requires [forge-fhevm](../forge-fhevm)): |
| 33 | + |
| 34 | + ```bash |
| 35 | + # From the forge-fhevm directory |
| 36 | + ./deploy-local.sh |
| 37 | + ``` |
| 38 | + |
| 39 | + Then deploy to the local network: |
23 | 40 |
|
24 | | -And you can still check that Alice's and Bob's balances are correct after Alice transferred 42 encrypted tokens to Bob via: |
| 41 | + ```bash |
| 42 | + forge script script/DeployFHECounter.s.sol --rpc-url http://localhost:8545 --broadcast |
| 43 | + ``` |
| 44 | + |
| 45 | +4. **Deploy to Sepolia Testnet** |
| 46 | + |
| 47 | + ```bash |
| 48 | + cp .env.example .env |
| 49 | + # Edit .env with your deployer key and RPC URL |
| 50 | + |
| 51 | + source .env |
| 52 | + forge script script/DeployFHECounter.s.sol \ |
| 53 | + --rpc-url $RPC_URL \ |
| 54 | + --private-key $DEPLOYER_PRIVATE_KEY \ |
| 55 | + --broadcast --verify |
| 56 | + ``` |
| 57 | + |
| 58 | +## Project Structure |
25 | 59 |
|
26 | 60 | ``` |
27 | | -ts-node --transpile-only utils/reencrypt.ts [HANDLE] [PRIVATE_KEY] [CONTRACT_ADDRESS] |
| 61 | +fhevm-foundry-template/ |
| 62 | +├── src/ # Smart contract source files |
| 63 | +│ └── FHECounter.sol # Example FHE counter contract |
| 64 | +├── test/ # Test files |
| 65 | +│ └── FHECounter.t.sol # Tests using forge-fhevm |
| 66 | +├── script/ # Deployment scripts |
| 67 | +│ └── DeployFHECounter.s.sol |
| 68 | +├── foundry.toml # Foundry configuration |
| 69 | +└── remappings.txt # Dependency remappings |
28 | 70 | ``` |
| 71 | + |
| 72 | +## Available Scripts |
| 73 | + |
| 74 | +| Script | Description | |
| 75 | +| ------------------------------------------ | ------------------------ | |
| 76 | +| `forge build` | Compile all contracts | |
| 77 | +| `forge test -vvv` | Run all tests | |
| 78 | +| `forge test --match-test test_name -vvv` | Run a single test | |
| 79 | +| `forge fmt` | Format code | |
| 80 | +| `forge fmt --check` | Check formatting | |
| 81 | + |
| 82 | +## Documentation |
| 83 | + |
| 84 | +- [FHEVM Documentation](https://docs.zama.ai/fhevm) |
| 85 | +- [FHEVM Foundry Setup Guide](https://docs.zama.ai/protocol/solidity-guides/getting-started/setup) |
| 86 | +- [FHEVM Testing Guide](https://docs.zama.ai/protocol/solidity-guides/development-guide/foundry/write_test) |
| 87 | +- [forge-fhevm Documentation](../forge-fhevm/docs/) |
| 88 | + |
| 89 | +## License |
| 90 | + |
| 91 | +This project is licensed under the BSD-3-Clause-Clear License. See the [LICENSE](LICENSE) file for details. |
| 92 | + |
| 93 | +## Support |
| 94 | + |
| 95 | +- **GitHub Issues**: [Report bugs or request features](https://github.com/zama-ai/fhevm/issues) |
| 96 | +- **Documentation**: [FHEVM Docs](https://docs.zama.ai) |
| 97 | +- **Community**: [Zama Discord](https://discord.gg/zama) |
0 commit comments