diff --git a/basic/79-hardhat-foundry/.env.example b/basic/79-hardhat-foundry/.env.example new file mode 100644 index 000000000..8196bef02 --- /dev/null +++ b/basic/79-hardhat-foundry/.env.example @@ -0,0 +1,4 @@ +PRIVATE_KEY=0x0000000000000000000000000000000000000000000000000000000000000000 +ETHERSCAN_BSC= +ETHERSCAN_ETH= +ETHERSCAN_ARB= diff --git a/basic/79-hardhat-foundry/.gitignore b/basic/79-hardhat-foundry/.gitignore new file mode 100644 index 000000000..a14cb90b6 --- /dev/null +++ b/basic/79-hardhat-foundry/.gitignore @@ -0,0 +1,84 @@ +*.swp +*.swo + +# Logs +logs +*.log + +# Runtime data +pids +*.pid +*.seed +allFiredEvents +scTopics + +# Coverage directory used by tools like istanbul +coverage +coverage.json +coverageEnv + +# node-waf configuration +.lock-wscript + +# Dependency directory +node_modules + +# Debug log from npm +npm-debug.log + +# local env variables +.env + +# truffle build directory +build/ + +# macOS +.DS_Store + +# truffle +.node-xmlhttprequest-* + +# IntelliJ IDE +.idea + +# vscode IDE +.vscode + +# docs artifacts +docs/modules/api + +# only used to package @openzeppelin/contracts +contracts/build/ +contracts/README.md + +# temporary artifact from solidity-coverage +.coverage_artifacts +.coverage_cache +.coverage_contracts + +# hardat-exposed +contracts-exposed + +# Hardhat +cache +artifacts + +# Foundry +out +cache_forge + +# Certora +.certora* +.last_confs +certora_* +.zip-output-url.txt + +# extends +/abi +/abi-pure + +/typechain-types +/**/*.js + +/lib +/contracts/diamond-2 diff --git a/basic/79-hardhat-foundry/.gitmodules b/basic/79-hardhat-foundry/.gitmodules new file mode 100644 index 000000000..4e7cc8a7d --- /dev/null +++ b/basic/79-hardhat-foundry/.gitmodules @@ -0,0 +1,6 @@ +[submodule "lib/forge-std"] + path = lib/forge-std + url = https://github.com/foundry-rs/forge-std +[submodule "contracts/diamond-2"] + path = contracts/diamond-2 + url = https://github.com/mudgen/diamond-2-hardhat.git diff --git a/basic/79-hardhat-foundry/README.en.md b/basic/79-hardhat-foundry/README.en.md new file mode 100644 index 000000000..6b628579e --- /dev/null +++ b/basic/79-hardhat-foundry/README.en.md @@ -0,0 +1,559 @@ +# Hardhat-Foundry +## Preface + +1. **Question 1: Why use `hardhat`?** + + A few years ago, `remix` and `truffle` were popular. At that time, `remix` lacked tools like `console`, and `truffle` didn't have a `debug` feature. `tenderly` was nowhere to be found in the browser, and issues in the code could only be addressed through `code review` and `revert` tests. **Back then, we lacked a comprehensive engineering framework, and most projects had only a few contracts.** + + Later, `hardhat` became popular. It allowed debugging code using `console.log`, forked networks without the need for `Ganache` or `Geth`, and wrote tests without needing to review the function names and parameters inside contracts (`Typescript`)... So, **it's time to use `hardhat`; it has truly made significant advancements beyond `truffle`.** + +2. **Question 2: I'm already using `hardhat`, why do I need `foundry`?** + + `hardhat` has **some minor pain points**, such as: + + - Tedious preparation for contract testing (deployment and initialization). + - Cumbersome testing of `Library` contracts. + - Data type conversions in certain scenarios. + - Data encoding and decoding. + - Frustration when reproducing utility functions in the JS layer, and more. + + To address these issues, **`foundry` appeared, innovatively suggesting using the `Solidity` language to test `Solidity`**!!! Now, you can write tests with minimal code! + +3. **Question 3: Why not fully embrace `foundry`?** + + `foundry` has its shortcomings. You may struggle with its dependency management (you might need to copy links from GitHub), cross-platform compatibility, a variety of CMD commands, and perhaps the inability to reuse some mature testing scripts (js/ts), and more. **`foundry` is revolutionary but hasn't fully inherited the wealth of its predecessors—JS ecosystem/hardhat ecosystem**, which is somewhat regrettable. + +4. **Question 4: How should I choose a development framework?** + + I won't go into a detailed discussion of the pros and cons of `remix`, `truffle`, `hardhat`, and `foundry`, and then throw the choice back to you. Here, I will give you a direct answer—**unless there is a major innovation, go ahead and choose `hardhat-foundry` without thinking twice**. This is the best combination I have come up with after experiencing numerous technical iterations and project practices, so feel free to use it. If you really like `foundry`, my friend, unless you have completely transformed into a professional contract tester, it is not recommended to use pure `foundry`. Developers will always need to write or deal with some `js/ts` scripts. + + Combining the strengths of many is always the best choice. + +## Preparation + +- Download the `forge` program: https://github.com/foundry-rs/foundry/releases +- Set up environment variables. +- Create a project (I personally recommend `solidity-framework`). + +----- + +Some useful links: + +- `ChainList`: https://chainlist.org/ + + Here you can find RPC links that you can copy and use with ease. You don't need to register on `Infura` (especially if you're a beginner). + +- `Tenderly` https://dashboard.tenderly.co/explorer + + Here, you can search for transaction call stacks, examine the execution flow of transactions, etc. (if the contract is open-source). + +- `foundry` https://github.com/Dapp-Learning-DAO/Dapp-Learning/tree/main/basic/41-foundry + + Excellent introductory points summarized by the predecessors at `Dapp-Learning`. I recommend browsing this. + +## Core Operations + +**Remember not to try to memorize all functions except the core operations**. + +1. **Contract Compilation** + +- `npx hardhat compile` + +2. **Contract Testing** + +- `npx hardhat test` | For more usage, `npx hardhat test --help` +- `forge test` | `forge test --mc -vv` + +3. **Local Blockchain Network [Recommended `hardhat`]** + +- `npx hardhat node` + +4. **Run TS scripts on a specific blockchain network (e.g., deployment)** + +- `npx hardhat --network run