###### tags: `z-institute` `上課筆記` `Solidity` # Week3 HW-Group 3 ![](https://i.imgur.com/vgOlEh5.png) * [如何搭建免费的GitHub Copilot](https://zhuanlan.zhihu.com/p/561914277) * [copilot](https://github.com/features/copilot) 可自動幫忙寫程式 * [alchemy](https://www.alchemy.com/) * [OpenZeppelin](https://github.com/OpenZeppelin/openzeppelin-contracts) * [hardhat](https://hardhat.org/) plugg in好用 gas-report-可計算出執行交易的成本 graghprotocal hardhat-test-utils:每次改變時 可以寫智能合約測試,快速了解有沒有問題 * [mythril](https://github.com/ConsenSys/mythril) 雖非100%正確,但可用做double check可實際跑一行看看 * [tenderly](https://tenderly.co/) 收費太貴,不太用了 chatgpt-Please help me improve..? 也可以加code comment 中偏簡單可以被取代 部屬合約plugin比較好用 [pancakeswap frontend](https://github.com/pancakeswap/pancake-frontend) 快速抄一個前端 ERC STANDARD-可以看當前最新idea --- ## 團體開發實作作業與加分題 1. 按照指示跑成功後,上傳程式碼至學院 Github 並截圖程式碼畫面,需附說明作法 (README.md) 1. 實作 Mainnet fork with hardhat,用 Ganache-cli 跑 local 的 ethereum mainnet fork 並 connect MetaMask (參考資料 : [https://hardhat.org/hardhat-network/guides/mainnet-forking.html](https://hardhat.org/hardhat-network/guides/mainnet-forking.html)) 2. 加分題(optional): - 如果想跟上最新的區塊鏈開發者趨勢,可以用看看 [https://zerodev.app/](https://zerodev.app/) 與 [https://www.stackup.sh/](https://www.stackup.sh/) 並閱讀網站內的相關文章,對參加區塊鏈論壇和與產業人士交流非常有幫助,會讓人把你歸類為 insider,就算只是知道有這些工具的存在,能夠加入其他人的話題也很有幫助 - 可以將使用心得與重點整理筆記提交至 GitHub README - Group_coding : - 請在此資料夾下放程式碼並且在 README.md 寫下簡易步驟並標明第幾組 - 上傳檔案中需有截圖執行成功畫面 - 每個人皆需上傳團體作業 若在開發實作執行過程中有出現 error message ,請在 README.md 列出相關訊息並提出是如何解決的 [前一屆實作](https://hackmd.io/@minmushroom/SJxDMZuRF) --- ## vera ### hardhat 1. 到 https://www.alchemy.com/ 註冊,獲得key ![](https://i.imgur.com/jWdHsM4.jpg) 2. 打開vscode的terminal,輸入以下指令 ``` npx hardhat node --fork https://eth-mainnet.alchemyapi.io/v2/<key> ``` ![](https://i.imgur.com/iHIBCMI.jpg) 安裝好環境之後再輸入一次指令 ``` npx hardhat node --fork https://eth-mainnet.alchemyapi.io/v2/<key> ``` 即可以看到成功的畫面 ![](https://i.imgur.com/wGNTu2j.jpg) 3. 打開metaMask,選擇localhost 8545,import account,將一組private key貼入,成功匯入 ![](https://i.imgur.com/T4gQcJU.png) ![](https://i.imgur.com/Ujv3EPu.png) ### Ganache-cli 1. 打開vscode的terminal,輸入以下指令 ``` npm install ganache --global ganache-cli -d -f https://eth-mainnet.g.alchemy.com/v2/<key> -p 8545 ``` ![](https://i.imgur.com/YVcY8aJ.jpg) 2. 打開metaMask,選擇localhost 8545,import account,將一組private key貼入,成功匯入 ![](https://i.imgur.com/KbWj5MN.png) ![](https://i.imgur.com/MCYTbg3.png) --- ## Eda # Implement a Mainnet fork with Hardhat/Ganache-cli Run a local Ethereum Mainnet fork with Ganache-cli and connect MetaMask, following the steps below: ## Hardhat 1. Register at https://www.alchemy.com/, go to Apps and click "VIEW KEY" to get the API key. ![](https://i.imgur.com/w3mIehN.png) 3. Open the terminal in vscode, and enter the following command. `npx hardhat node --fork https://eth-mainnet.alchemyapi.io/v2/<key>` 3. You may encounter erros and solutions were provided: ![](https://i.imgur.com/A9Yq1wi.png) Enter the following command: `npm install --save-dev @nomicfoundation/hardhat-toolbox` When you see the following screen: ![](https://i.imgur.com/OBKw6Hw.png) Enter the the following command again: `npx hardhat node --fork https://eth-mainnet.alchemyapi.io/v2/<key>` 4. You will be able to see a success screen. ![](https://i.imgur.com/Q7efQxa.png) Congratulations on solving the error!!! 5. Next, open MetaMask and select 'localhost 8545'. If you didn't show localhost 8545, go to advanced and open the test network. ![](https://i.imgur.com/Zo9ObJc.png)![](https://i.imgur.com/hurkM2l.png) 7. Choose 'import account', paste in a private key, and the import will be successful. ![](https://i.imgur.com/SycrFhk.png) ![](https://i.imgur.com/iegeTIT.png) ## Ganache-cli 1. Open the terminal in VSCode and enter the following command: ``` npm install ganache --global ``` ``` ganache-cli -d -f https://eth-mainnet.g.alchemy.com/v2/<key> -p 8545 ``` 2. You may encounter erros and solutions were provided: ![](https://i.imgur.com/WFeuTJS.png) Enter the following command: `Get-ExecutionPolicy` `Set-ExecutionPolicy Unrestricted` Enter the the following command again: `ganache-cli -d -f https://eth-mainnet.g.alchemy.com/v2/<key> -p 8545` You will be able to see a success screen. ![](https://i.imgur.com/uWvqyDr.png) 3. Next, connect MetaMask and choose 'import account', paste in a private key, and the import will be successful.. ![](https://i.imgur.com/Lb9VJT3.png) --- ### [zerodev](https://demo.zerodev.app/) ![](https://i.imgur.com/3b9HbEb.png) ZeroDev offers smart wallets as a service, powered by account abstraction (AA). This is accomplished through five product pillars: - A set of smart wallet factories for creating smart wallets from any authentication methods, including private keys, RPC providers, OAuth/JWT, biometrics, etc. - A smart contract wallet kernel for building custom AA wallets on top, along with a set of wallet plugins for common smart wallet features such as session keys, subscriptions, etc. - Ethers and Wagmi SDKs for interacting with smart wallets. - A gas sponsoring policy engine for setting up fine-grained gas sponsoring policies, such as "only sponsor up to 0.01 ETH worth of gas for each user for interacting with contract X." - A meta bundler network that spreads AA transactions across multiple bundler providers to ensure high uptime. If we design function to manage private keys for users, we can use ZeroDev to create AA wallets. ``` import { getZeroDevSigner, getPrivateKeyOwner } from '@zerodevapp/sdk' const signer = await getZeroDevSigner({ projectId: "<project id>", owner: getPrivateKeyOwner("<private key>"), }) ``` -- According to [demo](https://demo.zerodev.app/), there are three ways to use ZeroDev: - **Pay Gas for Users**: With ZeroDev, you can pay gas for your users, so they don't have to buy ETH before using your app. ``` Note how our account is identified as a "contract" by the block explorer. This is because in account abstraction, all accounts are smart contract accounts. ``` Click the button to gas-free. ![](https://i.imgur.com/a0yAXdH.png) - **Bundle Transactions**: With ZeroDev, you can execute multiple transactions as a single transaction, so you get to save on confirmation time and gas cost. It's also safer because these transactions either all execute or all revert, no in-between, which is a property known as "atomicity".You can see the demo screen and code. ![](https://i.imgur.com/Fp3RBT5.png) ``` // signer is a ZeroDevSigner // This will mint two NFTs at a time await signer.execBatch([ { to: nftAddress, data: nftContract.interface.encodeFunctionData("mint", [address]), }, { to: nftAddress, data: nftContract.interface.encodeFunctionData("mint", [address]), }, ]) ``` - **NFT Subscription**:Click "Subscribe" to subscribe to an NFT collection, then click "Release" to receive an NFT. Note that the "Release" is sent by the NFT creator, showing that your user can passively pay and receive NFTs. ![](https://i.imgur.com/8H0sD28.png) These transactions record in Polygonscan Mumbai. ![](https://i.imgur.com/Ubb8yAc.png) -- ### [stackup](https://www.stackup.sh/) ![](https://i.imgur.com/9UFU1pa.jpg) Stackup makes it easy to build custom web3 transactions flows and wallets using ERC-4337 Stackup provides a fully managed paymaster service. We charge you the gas at the end of the month, so you only need to worry about the user experience. Stackup's account abstraction infrastructure makes it easy to bundle transactions together, turning complex sequences of transactions into simple one-click experiences for your users. ![](https://i.imgur.com/3uIbfzi.png) ![](https://i.imgur.com/sTN87cc.jpg) In the end, we can download the following command to get ERC-4337 Examples repository : `git clone https://github.com/stackup-wallet/erc-4337-examples.git` ![](https://i.imgur.com/ycFpysQ.png)