{"id":5920,"date":"2022-10-11T12:33:34","date_gmt":"2022-10-11T04:33:34","guid":{"rendered":"https:\/\/i007.vip:15443\/wordpress\/?p=5920"},"modified":"2022-10-11T12:33:34","modified_gmt":"2022-10-11T04:33:34","slug":"%e4%b8%80%e7%9d%b9%e6%9b%b4%e5%bf%ab%e6%9b%b4%e5%bc%ba%e7%9a%84ganache-7","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/5920","title":{"rendered":"\u4e00\u7779\u66f4\u5feb\u66f4\u5f3a\u7684Ganache 7"},"content":{"rendered":"<p id=\"\u4e00\u7779\u66f4\u5feb\u66f4\u5f3a\u7684Ganache 7\" class=\"title\"><a href=\"https:\/\/learnblockchain.cn\/article\/3501\">\u539f\u6587\u5730\u5740<\/a><\/p>\n<div class=\"content mt-10\">\n<div class=\"quote mb-20\">\n<p>Ganache 7 \u53d1\u5e03\uff0c \u518d\u4e5f\u4e0d\u5361\u6b7b\u4e86\uff0c\u901f\u5ea6\u66f4\u5feb\uff0c\u529f\u80fd\u66f4\u5f3a\u3002<\/p>\n<\/div>\n<div id=\"md_view\">\n<div class=\"markdown-body\">\n<blockquote>\n<ul>\n<li>\u539f\u6587\uff1a<a href=\"https:\/\/trufflesuite.com\/blog\/introducing-ganache-7\/\">https:\/\/trufflesuite.com\/blog\/introducing-ganache-7\/<\/a><\/li>\n<li>\u8bd1\u6587\u51fa\u81ea\uff1a<a href=\"https:\/\/github.com\/lbc-team\/Pioneer\">\u767b\u94fe\u7ffb\u8bd1\u8ba1\u5212<\/a><\/li>\n<li>\u8bd1\u8005\uff1a<a href=\"https:\/\/learnblockchain.cn\/people\/412\">\u7ffb\u8bd1\u5c0f\u7ec4<\/a><\/li>\n<li>\u6821\u5bf9\uff1a<a href=\"https:\/\/learnblockchain.cn\/people\/15\">Tiny \u718a<\/a><\/li>\n<li>\u672c\u6587\u6c38\u4e45\u94fe\u63a5\uff1a<a href=\"https:\/\/learnblockchain.cn\/article\/3501\">learnblockchain.cn\/article\u2026<\/a><\/li>\n<\/ul>\n<\/blockquote>\n<p>Ganache\u662f\u4ee5\u592a\u574a\u5f00\u53d1\u9886\u57df\u7684\u5148\u9a71\uff0c\u81ea2016\u5e74\u4ee5\u6765\u5e2e\u52a9DApp\u5f00\u53d1\u8005\u548c\u7231\u597d\u8005\u6784\u5efa\u3001\u6d4b\u8bd5\u548c\u63a2\u7d22\u533a\u5757\u94fe\u3002\u6211\u4eec\u5f88\u9ad8\u5174\u5730\u5ba3\u5e03\u6700\u65b0\u7248\u672c\u7684Ganache\u53d1\u5e03\u4e86\uff0c\u5206\u53c9\uff08fork\uff09\u6027\u80fd\u63d0\u9ad8\u4e8630\u500d\uff0c\u5e76\u4e0eInfura\u6574\u5408\uff0c\u5141\u8bb8\u4f60\u514d\u8d39\u8bbf\u95ee\u5b58\u6863\u6570\u636e\uff0c\u91cd\u653e\u5386\u53f2\u4ea4\u6613\u3002<\/p>\n<p>\u4e5f\u8bb8\u4f60\u8fd8\u4e0d\u4e0d\u719f\u6089Ganache\uff0cGanache\u662f\u4e00\u4e2a\u7528\u4e0e\u672c\u5730\u5f00\u53d1\u7684\u533a\u5757\u94fe\uff0c\u7528\u4e8e\u5728\u4ee5\u592a\u574a\u533a\u5757\u94fe\u4e0a\u5f00\u53d1\u53bb\u4e2d\u5fc3\u5316\u7684\u5e94\u7528\u7a0b\u5e8f\u3002Ganache\u6a21\u62df\u4e86\u4ee5\u592a\u574a\u7f51\u7edc\uff0c\u4f60\u53ef\u4ee5\u5728\u53d1\u5e03\u5230\u751f\u4ea7\u73af\u5883\u4e4b\u524d\u770b\u5230\u4f60\u7684DApp\u5c06\u5982\u4f55\u6267\u884c\u3002<\/p>\n<p>\u5728\u4ee5\u592a\u574a\u7f51\u7edc\u4e0a\u5f00\u53d1\u53bb\u4e2d\u5fc3\u5316\u5e94\u7528\uff0c\u901a\u5e38\u7684\u65b9\u5f0f\u662f\u8bbe\u7f6e\u4e00\u4e2a\u4ee5\u592a\u574a\u5ba2\u6237\u7aef\uff0c\u5982Geth\u6216OpenEthereum\uff0c\u4e3a\u4f60\u63d0\u4f9b<a href=\"https:\/\/ethereum.org\/en\/developers\/docs\/evm\/\">\u4ee5\u592a\u574a\u865a\u62df\u673a(EVM)<\/a>\u73af\u5883\u3002<\/p>\n<p>\u867d\u7136\u8fd9\u662f\u4e00\u4e2a\u5728\u4ee5\u592a\u574a\u4e0a\u5f00\u53d1\u53bb\u4e2d\u5fc3\u5316\u5e94\u7528\u7684\u597d\u65b9\u6cd5\uff0c\u4f46\u5b83\u4e0d\u662f\u6700\u6709\u6548\u548c\u6700\u53cb\u597d\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u4f60\u9700\u8981\u624b\u52a8\u914d\u7f6e\u8fd9\u4e9b\u5ba2\u6237\u7aef\u5e76\u4fdd\u6301\u8fd0\u884c\u3002\u7ef4\u62a4\u4e00\u4e2a\u81ea\u6211\u6258\u7ba1\u7684\u8282\u70b9\u53ef\u80fd\u662f\u6602\u8d35\u548c\u8017\u65f6\u7684\uff0c\u4f60\u4e0d\u5e0c\u671b\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u82b1\u8d39\u5b9d\u8d35\u7684\u65f6\u95f4\u6765\u6392\u67e5\u4e00\u4e2a\u5931\u8d25\u7684\u8282\u70b9\u3002<\/p>\n<p>\u6709\u4e86Ganache\uff0c\u4f60\u6240\u9700\u8981\u505a\u7684\u5c31\u662f\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f\uff0c\u4f60\u5c31\u6709\u4e00\u4e2a\u9884\u5148\u914d\u7f6e\u597d\u7684\u4ee5\u592a\u574a\u5ba2\u6237\u7aef\uff0c\u670910\u4e2a\u9884\u5148\u5b58\u6b3e\u548c\u89e3\u9501\u7684\u8d26\u6237\u53ef\u4ee5\u4f7f\u7528\u3002\u8fd9\u4f7f\u4f60\u80fd\u591f\u5728\u6574\u4e2a\u5f00\u53d1\u5468\u671f\u5185\u5feb\u901f\u6d4b\u8bd5\u4f60\u7684DApp\u3002<\/p>\n<h2 id=\"Ganache 7\uff1a\u5206\u53c9\u63d0\u9ad830\u500d\u901f\u5ea6\u548c\u64cd\u4f5c\u4e5f\u66f4\u5feb\">Ganache 7\uff1a\u5206\u53c9\u63d0\u9ad830\u500d\u901f\u5ea6\u548c\u64cd\u4f5c\u4e5f\u66f4\u5feb<\/h2>\n<p>\u8bb8\u591a\u5728\u4ee5\u592a\u574a\u4e0a\u6784\u5efa\u7684Web3\u5f00\u53d1\u8005\u5df2\u7ecf\u4f7f\u7528Ganache\u5728\u4ed6\u4eec\u7684\u672c\u5730\u73af\u5883\u4e2d\u5206\u53c9\u548c\u6d4b\u8bd5\u667a\u80fd\u5408\u7ea6\uff0c\u4ee5\u4fbf\u5728\u90e8\u7f72\u524d\u83b7\u5f97\u4fe1\u5fc3\u3002\u5229\u7528\u4ece\u4ee5\u524d\u7684\u8fed\u4ee3\u4e2d\u83b7\u5f97\u7684\u7ecf\u9a8c\uff0c\u6211\u4eec\u4ece\u5934\u5f00\u59cb\u91cd\u5199\u4e86Ganache\uff0c\u4efb\u52a1\u662f\u8ba9\u8fd9\u4e2a\u533a\u5757\u94fe\u6a21\u62df\u5668\u5de5\u5177\u66f4\u52a0\u7075\u6d3b\u3001\u5feb\u901f\u548c\u7a33\u5b9a\u3002<\/p>\n<p>\u6211\u4eec\u542c\u5230\u4e86\u4f60\u4eec\u5173\u4e8eGanache v6\u5b89\u88c5\u7f13\u6162\uff0c\u8fd0\u884c\u7f13\u6162\uff0c\u5e76\u4e14\u6cc4\u9732\u5185\u5b58\u7684\u53cd\u9988\u3002\u5b83\u8fd8\u6ca1\u6709\u4f7f\u7528\u73b0\u4ee3\u8bed\u8a00\u7279\u6027\u548c\u5f00\u53d1\u4eba\u5458\u4eca\u5929\u4f7f\u7528\u7684\u8303\u5f0f\uff0c\u5982TypeScript\u7c7b\u578b\u548cPromise\u62bd\u8c61\u3002Ganache v6\u7684\u67b6\u6784\u4f7f\u5f97\u6d4b\u8bd5\u3001\u4fee\u590d\u9519\u8bef\u548c\u6dfb\u52a0\u529f\u80fd\u53d8\u5f97\u56f0\u96be\u548c\u8d39\u65f6\u3002<\/p>\n<p>Ganache v7\u5982\u4f55\u66f4\u597d\uff1f<\/p>\n<ul>\n<li>\u51ed\u501f\u6700\u65b0\u7248\u672c\u7684\u9ad8\u7ea7\u7f13\u5b58\u529f\u80fd\uff0cGanache\u7684\u5206\u53c9\u529f\u80fd\uff08\u5982\uff1afork\u4e3b\u7f51 \uff09\u6bd4v6\u5feb30\u500d\u3002<\/li>\n<li>\u96f6\u914d\u7f6e\u7684\u4e3b\u7f51\u5206\u53c9\uff0c Ganache v7\u6709\u4e00\u4e2a<a href=\"https:\/\/blog.infura.io\/fork-ethereum-replay-historical-transactions-with-ganache-7-archive-support\/?__hstc=223080836.c9727f43b80c5b842391f55bf687c7b5.1629084054657.1643006281910.1643361864575.6&#038;__hssc=223080836.1.1643361864575&#038;__hsfp=2608980517\">\u4e0eInfura\u7684\u539f\u751f\u96c6\u6210<\/a>\uff0c\u53ef\u4ee5\u514d\u8d39\u8bbf\u95ee\u5386\u53f2\u6570\u636e<\/li>\n<li>\u4f60\u53ef\u4ee5\u65e0\u9650\u671f\u5730\u8fd0\u884cGanache\uff0c\u800c\u4e0d\u4f1a\u9047\u5230\u56e0\u5185\u5b58\u95ee\u9898\u800c\u5d29\u6e83\u7684\u60c5\u51b5<\/li>\n<li>\u666e\u901a\u64cd\u4f5c\u6bd4v6\u5feb3\u500d\u5de6\u53f3\u3002<\/li>\n<\/ul>\n<h2 id=\"\u5b89\u88c5\u548c\u8bbe\u7f6eGanache 7\">\u5b89\u88c5\u548c\u8bbe\u7f6eGanache 7<\/h2>\n<p>Ganache\u53ef\u4ee5\u7528\u591a\u79cd\u65b9\u5f0f\u4f7f\u7528\uff1a\u5305\u62ec\u4f5c\u4e3a\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u901a\u8fc7Node.js\u8fdb\u884c\u7f16\u7a0b\uff0c\u6216\u5728\u6d4f\u89c8\u5668\u4e2d\u4f7f\u7528\u3002\u8bf7\u770b\u4e0b\u9762\u7684\u8bf4\u660e\uff0c\u4e3a\u6240\u6709\u8fd9\u4e9b\u4f7f\u7528\u60c5\u51b5\u5b89\u88c5\u548c\u4f7f\u7528Ganache 7\u3002<\/p>\n<h3 id=\"A. \u5728\u547d\u4ee4\u884c\u4e0a\u4f7f\u7528Ganache 7\">A. \u5728\u547d\u4ee4\u884c\u4e0a\u4f7f\u7528Ganache 7<\/h3>\n<p>\u8981\u5728\u547d\u4ee4\u884c\u4e0a\u8fd0\u884cGanache 7\uff0c\u4f60\u7684\u7535\u8111\u4e0a\u5fc5\u987b\u5b89\u88c5Node.js >= v12.0.0\u548cNPM >= 6.4.1\u3002\u53c2\u89c1<a href=\"https:\/\/nodejs.org\/en\/download\/\">here<\/a>\u4e0b\u8f7d\u9002\u5408\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\u7684\u6700\u65b0\u7248\u672c\uff0c\u6216\u8005\u4f7f\u7528nvm\u6765\u7ba1\u7406\u4f60\u7684<a href=\"https:\/\/github.com\/nvm-sh\/nvm\">Unix<\/a>\u6216<a href=\"https:\/\/github.com\/coreybutler\/nvm-windows\">Windows<\/a>\u7684Node\u5b89\u88c5\u3002<\/p>\n<p>\u5728\u5b89\u88c5\u4e86Node.js\u548cNPM\u4e4b\u540e\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c<code>npm install ganache --global<\/code>\u6765\u5168\u5c40\u5b89\u88c5Ganache 7\u3002\u4e00\u65e6\u5b89\u88c5\u5b8c\u6bd5\uff0c\u4f60\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u7684\u4efb\u4f55\u5730\u65b9\u8fd0\u884c<code>ganache<\/code>\u3002<\/p>\n<p>\u4f60\u7684\u7ec8\u7aef\u5e94\u8be5\u770b\u8d77\u6765\u50cf\u8fd9\u6837\u3002<\/p>\n<p><img decoding=\"async\" class=\"medium-zoom-image\" src=\"https:\/\/img.learnblockchain.cn\/pics\/20220128172804.png\" alt=\"screenshot-1\" \/><\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cGanache\u63d0\u4f9b\u4e8610\u4e2a\u6d4b\u8bd5\u8d26\u6237\uff0c\u6bcf\u4e2a\u8d26\u6237\u67091000\u4e2a\uff08\u5047\uff09\u4ee5\u592a\u5e01\uff0c\u4ee5\u53ca\u76f8\u5e94\u7684\u79c1\u94a5\u548c\u7528\u4e8e\u751f\u6210\u79c1\u94a5\u7684\u52a9\u8bb0\u8bcd\u3002\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u52a9\u8bb0\u8bcd\u5c06\u8d26\u6237\u5bfc\u5165\u5230<a href=\"https:\/\/metamask.io\/\">MetaMask<\/a>\u7b49\u94b1\u5305\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u5728\u542f\u52a8Ganache 7\u65f6\u901a\u8fc7\u6307\u5b9a\u9009\u9879\u6765\u8986\u76d6\u6240\u6709\u7684\u9ed8\u8ba4\u503c\uff1b\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u63d0\u4f9b\u79c1\u94a5\u4e0e\u8d26\u6237\u4f59\u989d\u7684\u6620\u5c04\u6765\u5bf9\u8d26\u6237\u521b\u5efa\u8fdb\u884c\u66f4\u591a\u7684\u63a7\u5236\uff0c\u5c31\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre><textarea><\/textarea><code class=\"language-bash hljs\">ganache --wallet.accounts <span class=\"hljs-string\">\"0xfd485338e322f5930f7cf475f385341ec88bfc4f8a0a16f30b2fb417d1bb5427, 1000000000000000000000\"<\/span> <span class=\"hljs-string\">\"0x05bba0b9f7a251080aa23feee4eab3f75a1abee905c0271008c93e5d2e2e7541, 10000000000000000000000\"<\/span>\r\n<\/code><\/pre>\n<p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u6307\u5b9a\u4e00\u4e2a\u52a9\u8bb0\u8bcd\u7ec4\u6765\u63a8\u5bfc\u6240\u6709\u7684\u521d\u59cb\u8d26\u6237\uff0c\u4e00\u4e2a\u77ff\u5de5Gas\u4ef7\u683c\uff0c\u4ee5\u53ca\u533a\u5757Gas\u9650\u5236\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><textarea><\/textarea><code class=\"hljs language-solidity\">ganache --miner.defaultGasPrice <span class=\"hljs-number\">200<\/span> --miner.blockGasLimit <span class=\"hljs-number\">90071<\/span> --miner.callGasLimit <span class=\"hljs-number\">898989<\/span> --wallet.mnemonic <span class=\"hljs-string\">\"alarm cause brave super lab glide awake hunt rose win sugar idea\"<\/span>\r\n<\/code><\/pre>\n<p>\u8fd0\u884c\u547d\u4ee4<code>ganache --help<\/code>\u53ef\u4ee5\u83b7\u5f97\u6240\u6709Ganache\u7684\u53ef\u7528\u9009\u9879\u7684\u5217\u8868\u3002<\/p>\n<p>\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>npm install ganache<\/code>\u5c06ganache\u5b89\u88c5\u5230\u672c\u5730\u7684Node.js\u9879\u76ee\u4e2d\uff0c\u4e0d\u4f7f\u7528<code>--global<\/code>\u6807\u5fd7\uff0c\u7136\u540e\u5c06\u5176\u6dfb\u52a0\u5230\u9879\u76ee\u7684package.json<code>scripts<\/code>\u4e2d\uff0c\u4f8b\u5982\uff0c<code>\"scripts\":{\"ganache\": {\"ganache --mnemonic <12 words mnemonic>\"}<\/code>\u3002<\/p>\n<h3 id=\"B. \u4ee5\u7f16\u7a0b\u65b9\u5f0f\u4f7f\u7528Ganache 7\">B. \u4ee5\u7f16\u7a0b\u65b9\u5f0f\u4f7f\u7528Ganache 7<\/h3>\n<p>Ganache 7\u53ef\u4ee5\u5728\u4f60\u7684Node.js\u9879\u76ee\u4e2d\u4ee5\u7f16\u7a0b\u7684\u65b9\u5f0f\u4f7f\u7528\uff0c \u5982\u4f5c\u4e3a\uff1a<\/p>\n<ul>\n<li>\u4e00\u4e2a\u72ec\u7acb\u7684EIP-1193\u63d0\u4f9b\u8005\u3002<\/li>\n<li>\u4e00\u4e2aJSON-RPC\u670d\u52a1\u5668\u548cEIP-1193\u63d0\u4f9b\u8005\u3002<\/li>\n<li>\u4e00\u4e2aweb3.js\u63d0\u4f9b\u8005\u3002<\/li>\n<li>\u4e00\u4e2aethers.js\u63d0\u4f9b\u8005\u3002<\/li>\n<\/ul>\n<p>\u8bf7\u770b\u4e0b\u9762\u8fd9\u4e9b\u4e0d\u540c\u4f7f\u7528\u573a\u666f\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u9996\u5148\uff0c\u5c06Ganache 7\u4f5c\u4e3anpm\u5305\u5b89\u88c5\u5728\u4f60\u7684Node.js\u9879\u76ee\u4e2d<\/p>\n<pre><textarea><\/textarea><code class=\"hljs language-ruby\"><span class=\"hljs-variable\">$ <\/span>npm install ganache\r\n<\/code><\/pre>\n<p>\u4f5c\u4e3a\u4e00\u4e2a\u72ec\u7acb\u7684EIP-1193\u63d0\u4f9b\u8005:<\/p>\n<pre><textarea><\/textarea><code class=\"language-javascript hljs\"><span class=\"hljs-keyword\">const<\/span> ganache = <span class=\"hljs-built_in\">require<\/span>(<span class=\"hljs-string\">\"ganache\"<\/span>);\r\n\r\n<span class=\"hljs-keyword\">const<\/span> options = {};\r\n<span class=\"hljs-keyword\">const<\/span> provider = ganache.<span class=\"hljs-title function_\">provider<\/span>(options);\r\n<span class=\"hljs-keyword\">const<\/span> accounts = <span class=\"hljs-keyword\">await<\/span> provider.<span class=\"hljs-title function_\">request<\/span>({ <span class=\"hljs-attr\">method<\/span>: <span class=\"hljs-string\">\"eth_accounts\"<\/span>, <span class=\"hljs-attr\">params<\/span>: [] });\r\n...\r\n<\/code><\/pre>\n<p>\u4f5c\u4e3a\u4e00\u4e2aJSON-RPC\u7f51\u7edc\u670d\u52a1\u5668\u548cEIP-1193\u63d0\u4f9b\u8005:<\/p>\n<pre><textarea><\/textarea><code class=\"language-js hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> ganache = <span class=\"hljs-built_in\">require<\/span>(<span class=\"hljs-string\">\"ganache\"<\/span>);\r\n\r\n<span class=\"hljs-keyword\">const<\/span> options = {};\r\n<span class=\"hljs-keyword\">const<\/span> server = ganache.<span class=\"hljs-title function_\">server<\/span>(options);\r\n<span class=\"hljs-keyword\">const<\/span> <span class=\"hljs-variable constant_\">PORT<\/span> = <span class=\"hljs-number\">8545<\/span>;\r\nserver.<span class=\"hljs-title function_\">listen<\/span>(<span class=\"hljs-variable constant_\">PORT<\/span>, <span class=\"hljs-function\"><span class=\"hljs-params\">err<\/span> =><\/span> {\r\n  <span class=\"hljs-keyword\">if<\/span> (err) <span class=\"hljs-keyword\">throw<\/span> err;\r\n\r\n  <span class=\"hljs-variable language_\">console<\/span>.<span class=\"hljs-title function_\">log<\/span>(<span class=\"hljs-string\">`ganache listening on port <span class=\"hljs-subst\">${PORT}<\/span>...`<\/span>);\r\n  <span class=\"hljs-keyword\">const<\/span> provider = server.<span class=\"hljs-property\">provider<\/span>;\r\n  <span class=\"hljs-keyword\">const<\/span> accounts = <span class=\"hljs-keyword\">await<\/span> provider.<span class=\"hljs-title function_\">request<\/span>({ <span class=\"hljs-attr\">method<\/span>: <span class=\"hljs-string\">\"eth_accounts\"<\/span>, <span class=\"hljs-attr\">params<\/span>:[] });\r\n});\r\n...\r\n<\/code><\/pre>\n<p>\u4f5c\u4e3a\u4e00\u4e2aWeb3.js\u63d0\u4f9b\u8005<\/p>\n<pre><textarea><\/textarea><code class=\"language-js hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> <span class=\"hljs-title class_\">Web3<\/span> = <span class=\"hljs-built_in\">require<\/span>(<span class=\"hljs-string\">\"web3\"<\/span>);\r\n<span class=\"hljs-keyword\">const<\/span> ganache = <span class=\"hljs-built_in\">require<\/span>(<span class=\"hljs-string\">\"ganache\"<\/span>);\r\n\r\n<span class=\"hljs-keyword\">const<\/span> web3 = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">Web3<\/span>(ganache.<span class=\"hljs-title function_\">provider<\/span>());\r\n...\r\n<\/code><\/pre>\n<p>\u4f5c\u4e3aEthers.js\u7684\u63d0\u4f9b\u8005<\/p>\n<pre><textarea><\/textarea><code class=\"language-js hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> ganache = <span class=\"hljs-built_in\">require<\/span>(<span class=\"hljs-string\">\"ganache\"<\/span>);\r\n\r\n<span class=\"hljs-keyword\">const<\/span> provider = <span class=\"hljs-keyword\">new<\/span> ethers.<span class=\"hljs-property\">providers<\/span>.<span class=\"hljs-title class_\">Web3Provider<\/span>(ganache.<span class=\"hljs-title function_\">provider<\/span>());\r\n<\/code><\/pre>\n<h3 id=\"C. \u5728\u6d4f\u89c8\u5668\u4e2d\u4f7f\u7528Ganache 7\">C. \u5728\u6d4f\u89c8\u5668\u4e2d\u4f7f\u7528Ganache 7<\/h3>\n<p>\u4f60\u53ef\u4ee5\u5728\u6d4f\u89c8\u5668\u4e2d\u4f7f\u7528Ganache 7\uff0c\u5728\u4f60\u7684HTML\u4ee3\u7801\u4e2d\u52a0\u5165\u4ee5\u4e0b\u811a\u672c\u6807\u7b7e\u3002<\/p>\n<pre><textarea><\/textarea><code class=\"language-js hljs language-javascript\"><script src=<span class=\"hljs-string\">\"https:\/\/cdn.jsdelivr.net\/npm\/ganache@7.0.0\/dist\/web\/ganache.min.js\"<\/span>><\/script>\r\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u6837\u505a\uff0cGanache 7 \u5c31\u4f1a\u81ea\u52a8\u5728\u4f60\u7684\u6d4f\u89c8\u5668\u4e2d\u4f9b\u4f60\u4f7f\u7528\u3002<\/p>\n<pre><textarea><\/textarea><code class=\"hljs language-cpp\"><span class=\"hljs-type\">const<\/span> options = {};\r\n<span class=\"hljs-type\">const<\/span> provider = Ganache.<span class=\"hljs-built_in\">provider<\/span>(options);\r\n<\/code><\/pre>\n<p>\u540c\u6837\uff0c\u8bf7\u53c2\u9605<a href=\"https:\/\/github.com\/trufflesuite\/ganache#readme\">\u8fd9\u91cc<\/a>\uff0c\u4e86\u89e3\u4f60\u53ef\u4ee5\u4f20\u5165\u7684\u53ef\u7528\u9009\u9879\u7684\u6e05\u5355\u3002<\/p>\n<h2 id=\"Ganache 7 \u65b0\u529f\u80fd\">Ganache 7 \u65b0\u529f\u80fd<\/h2>\n<h3 id=\"1. \u96f6\u914d\u7f6e\u7684\u4e3b\u7f51\u5206\u53c9\">1. \u96f6\u914d\u7f6e\u7684\u4e3b\u7f51\u5206\u53c9<\/h3>\n<p>Ganache\u652f\u6301\u5f00\u7bb1\u5373\u7528\u7684\u4e3b\u7f51\u5206\u53c9\uff0c\u65e0\u9700\u4efb\u4f55\u914d\u7f6e\uff0c\u5141\u8bb8\u4f60\u5728\u672c\u5730\u673a\u5668\u4e0a\u6a21\u62df\u62e5\u6709\u4e0e\u4ee5\u592a\u574a\u4e3b\u7f51\u76f8\u540c\u7684\u72b6\u6001\u3002<\/p>\n<p>\u8fd9\u5e26\u6765\u4e86\u8bb8\u591a\u53ef\u80fd\u6027\uff1b\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u4e0e\u771f\u5b9e\u4e16\u754c\u7684\u534f\u8bae\/\u5408\u7ea6\u8fdb\u884c\u672c\u5730\u4ea4\u4e92\uff0c\u901a\u8fc7\uff0c\u5e76\u5728\u672c\u5730\u8c03\u8bd5\u4e3b\u7f51\u4ea4\u6613\uff08\u4f7f\u7528<a href=\"https:\/\/trufflesuite.com\/docs\/truffle\/getting-started\/using-the-truffle-debugger\"><code>truffle debug<\/code><\/a>\uff09\uff0c\u5141\u8bb8\u5bf9\u67e5\u770b\u5408\u7ea6\u8c03\u7528\u7ec6\u8282\u3002<\/p>\n<p><a href=\"https:\/\/www.reddit.com\/r\/ethereum\/comments\/4yd1o4\/testrpc_teaser_instantaneous_forking_coming_soon\/\">\u8fd9\u91cc\u6709<\/a>2016\u5e74Ganache\u56e2\u961f\u4f7f\u7528\u4e3b\u7f51\u5206\u53c9\u529f\u80fd\u6765\u6f14\u793a<a href=\"https:\/\/www.gemini.com\/cryptopedia\/the-dao-hack-makerdao\">DAO\u9ed1\u5ba2\u653b\u51fb<\/a>\u3002<\/p>\n<p>\u8981\u4f7f\u7528\u8fd9\u4e2a\u529f\u80fd\uff0c\u4f7f\u7528<code>ganache --fork<\/code>\u547d\u4ee4\u542f\u52a8Ganache 7\u3002Ganache\u9ed8\u8ba4\u4f7f\u7528<a href=\"https:\/\/infura.io\/docs?__hstc=223080836.c9727f43b80c5b842391f55bf687c7b5.1629084054657.1643006281910.1643361864575.6&#038;__hssc=223080836.1.1643361864575&#038;__hsfp=2608980517\">Infura<\/a>\u4f5c\u4e3a\u5176\u5f15\u64ce\u7684\u63d0\u4f9b\u8005\uff0c\u4f46\u4f60\u4e5f\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u4e00\u4e2aURL\u6765\u6307\u5b9a\u4e00\u4e2a\u63d0\u4f9b\u8005\uff1b\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c\u8fd9\u4e2a\u547d\u4ee4\u4f7f\u7528\u4f60\u81ea\u5df1\u7684Infura URL\u3002<\/p>\n<pre><textarea><\/textarea><code class=\"hljs language-ruby\"><span class=\"hljs-variable\">$ <\/span>ganache --fork.url <span class=\"hljs-symbol\">wss:<\/span>\/<span class=\"hljs-regexp\">\/mainnet.infura.io\/ws<\/span><span class=\"hljs-regexp\">\/v3\/<\/span><<span class=\"hljs-variable constant_\">PROJECT<\/span> <span class=\"hljs-variable constant_\">ID<\/span>>\r\n<\/code><\/pre>\n<p>Ganache\u9ed8\u8ba4\u4ece\u6700\u65b0\u7684\u533a\u5757\u4e2d\u56de\u9000\u4e94\u4e2a\u533a\u5757\uff0c\u4ee5\u907f\u514d\u56e0\u91cd\u65b0\u6392\u5e8f\u800c\u4e22\u5931\u533a\u5757\u3002<\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u53ef\u914d\u7f6e\u7684\u884c\u4e3a\uff1b\u4f60\u53ef\u4ee5\u4f7f\u7528<code>--fork.blockNumber <BLOCK NUMBER><\/code>\u6216\u8005\u901a\u8fc7\u8bbe\u7f6e<code>--fork.preLatestConfirmations 0<\/code>\u6765\u6307\u5b9a\u4f60\u60f3\u8981\u5206\u53c9\u7684\u786e\u5207\u5757\u3002<\/p>\n<h3 id=\"2. \u5206\u53c9\u4efb\u4f55\u4ee5\u592a\u574a\u6d4b\u8bd5\u7f51\u7edc\uff0c\u65e0\u9700\u7b49\u5f85\u540c\u6b65\u65f6\u95f4\">2. \u5206\u53c9\u4efb\u4f55\u4ee5\u592a\u574a\u6d4b\u8bd5\u7f51\u7edc\uff0c\u65e0\u9700\u7b49\u5f85\u540c\u6b65\u65f6\u95f4<\/h3>\n<p>\u9664\u4e86\u80fd\u591f\u4ee5\u96f6\u914d\u7f6e\u5206\u53c9\u4ee5\u592a\u574a\u4e3b\u7f51\u7edc\u5916\uff0cGanache\u8fd8\u5141\u8bb8\u4f60\u4ece\u4efb\u4f55\u4ee5\u592a\u574a\u6d4b\u8bd5\u7f51\u7edc\u5206\u53c9\uff0c\u5305\u62ecRopsten\u3001Kovan\u3001Rinkeby\u548cG\u00f6rli\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c\u5e26\u6709\u7f51\u7edc\u9009\u9879\u7684fork\u547d\u4ee4\u6765\u505a\u5230\u8fd9\u4e00\u70b9\uff1a<code>ganache -- fork.network <NETWORK NAME><\/code>\u3002\u5f88\u9177\uff0c\u662f\u5417\uff1f\u4e0b\u9762\u662fRinkeby\u6d4b\u8bd5\u7f51\u7684\u5206\u53c9\u60c5\u51b5\u3002<\/p>\n<p><img decoding=\"async\" class=\"medium-zoom-image\" src=\"https:\/\/img.learnblockchain.cn\/pics\/20220128172813.png\" alt=\"screenshot-2\" \/><\/p>\n<h3 id=\"3. \u652f\u6301\u5927\u89c4\u6a21\u4ea4\u6613\u8ffd\u8e2a\uff08\u8d85\u8fc710GB\u4ee5\u4e0a\uff09\">3. \u652f\u6301\u5927\u89c4\u6a21\u4ea4\u6613\u8ffd\u8e2a\uff08\u8d85\u8fc710GB\u4ee5\u4e0a\uff09<\/h3>\n<p>Ganache\u4f7f\u7528<code>debug_traceTransaction<\/code>\u00a0RPC\u65b9\u6cd5\u652f\u6301\u5927\u89c4\u6a21\u4ea4\u6613\u8ffd\u8e2a\u3002<\/p>\n<p>\u8981\u4f7f\u7528\u8fd9\u4e2a\u529f\u80fd\uff0c\u9700\u8981\u4eceMainnet(\u6216\u4efb\u4f55\u6d4b\u8bd5\u7f51\u7edc)\u4e2d\u5206\u53c9\u51fa\u4e00\u4e2a\u6bd4\u4f60\u8981\u8ffd\u8e2a\u7684\u4ea4\u6613\u9ad8\u4e00\u4e2a\u533a\u5757\u53f7\u7684\u53c2\u6570\u542f\u52a8Ganache\uff1b\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u8ffd\u8e2a\u4e00\u4e2a\u5728\u533a\u575713,886,877\u5185\u7684\u4ea4\u6613\uff0c\u4f60\u9700\u8981\u5728\u533a\u5757\u53f713,886,878\u5904\u542f\u52a8Ganache\u3002<\/p>\n<pre><textarea><\/textarea><code class=\"language-sh hljs language-bash\">$ ganache --fork.network mainnet --fork.blockNumber <blockNumber>\r\n<\/code><\/pre>\n<p>\u7136\u540e\u4f7f\u7528debug_traceTransaction\u65b9\u6cd5\u53d1\u9001\u4e00\u4e2acurl\u8bf7\u6c42\uff0c\u6307\u5b9a\u4ea4\u6613\u54c8\u5e0c\u503c\uff0c\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre><textarea><\/textarea><code class=\"language-sh hljs language-bash\">$ curl -H <span class=\"hljs-string\">'Content-Type: application\/json'<\/span> --data <span class=\"hljs-string\">'{\"jsonrpc\":\"2.0\", \"id\": 1, \"method\": \"debug_traceTransaction\", \"params\": [ \"<TRANSACTION HASH>\" ] }'<\/span> http:\/\/localhost:8545\r\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8fd4\u56de\u8be5\u4ea4\u6613\u7684\u6c47\u603b\u4fe1\u606f\uff0c\u7528\u4e8e\u540e\u5904\u7406\u3002<\/p>\n<p>\u7528\u4e00\u4e2a\u5927\u7684\u4ea4\u6613\u8bd5\u8bd5\uff0c\u6bd4\u5982<code>0x8bb8dc5c7c830bac85fa48acad2505e9300a91c3ff239c9517d0cae33b595090<\/code>\uff08Warp Finance\u9ed1\u5ba2\uff09\u3002<\/p>\n<p>\u9996\u5148\uff0c\u542f\u52a8ganache\uff08\u7528\u5927\u91cf\u7684\u989d\u5916\u5185\u5b58\uff09\u3002<\/p>\n<pre><textarea><\/textarea><code class=\"language-sh hljs language-bash\">$ NODE_OPTIONS=--max-old-space-size=16384 ganache --fork --fork.blockNumber 14037983\r\n<\/code><\/pre>\n<p>\u7136\u540e\u6267\u884c<code>debug_traceTransaction<\/code>\u5e76\u5c06\u8f93\u51fa\u53d1\u9001\u5230trace.json\u3002<\/p>\n<pre><textarea><\/textarea><code class=\"hljs language-rust\">$ curl -H <span class=\"hljs-symbol\">'Content<\/span>-Type: application\/json'   --data '{<span class=\"hljs-string\">\"jsonrpc\"<\/span>:<span class=\"hljs-string\">\"2.0\"<\/span>, <span class=\"hljs-string\">\"id\"<\/span>: <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-string\">\"method\"<\/span>: <span class=\"hljs-string\">\"debug_traceTransaction\"<\/span>, <span class=\"hljs-string\">\"params\"<\/span>: [ <span class=\"hljs-string\">\"0x8bb8dc5c7c830bac85fa48acad2505e9300a91c3ff239c9517d0cae33b595090\"<\/span> ] }' http:<span class=\"hljs-comment\">\/\/localhost:8545 -o trace.json<\/span>\r\n<\/code><\/pre>\n<p>\u5982\u679c\u4f60\u771f\u7684\u60f3\u6d4b\u8bd5\u4e00\u4e0b\u53ef\u80fd\u7684\u6781\u9650\uff08\u5e76\u4e14\u6709\u5f88\u591a\u65f6\u95f4&#8211;\u5927\u7ea61-2\u5c0f\u65f6\uff09\uff0c\u8bd5\u8bd510GB\u4ee5\u4e0a\u7684Cream Finance\u9ed1\u5ba2\u4ea4\u6613\uff0c<code>0x0fe2542079644e107cbf13690eb9c2c65963ccb79089ff96bfaf8dced2331c92<\/code>!<\/p>\n<h3 id=\"4. \u5feb\u7167\u548c\u8fd8\u539f\u72b6\u6001\">4. \u5feb\u7167\u548c\u8fd8\u539f\u72b6\u6001<\/h3>\n<p>\u5728\u533a\u5757\u94fe\u672f\u8bed\u4e2d\uff0c\u5feb\u7167\u6307\u7684\u662f\u6355\u6349\u548c\u8bb0\u5f55\u533a\u5757\u94fe\u5728\u67d0\u4e2a\u7279\u5b9a\u533a\u5757\u7f16\u53f7\u7684\u72b6\u6001\u7684\u884c\u4e3a\u3002\u5feb\u7167\u662f\u533a\u5757\u94fe\u5728\u8be5\u7279\u5b9a\u533a\u5757\u7684\u5b8c\u6574\u89c6\u56fe\uff0c\u5305\u62ec\u6240\u6709\u73b0\u6709\u5730\u5740\u53ca\u5176\u76f8\u5173\u6570\u636e\uff0c\u5305\u62ec\u4ea4\u6613\u3001\u8d39\u7528\u3001\u4f59\u989d\u3001\u5143\u6570\u636e\u7b49\u3002<\/p>\n<p>\u5728Ganache\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>evm_snapshot<\/code>\u00a0RPC\u65b9\u6cd5\u5bf9\u4f60\u7684\u5f00\u53d1\u533a\u5757\u94fe\uff08\u751a\u81f3\u662fMainnet\u7684\u5206\u53c9\uff09\u8fdb\u884c\u5feb\u7167\uff1b<code>evm_snapshot<\/code>\u4e0d\u9700\u8981\u4efb\u4f55\u53c2\u6570\uff0c\u5b83\u8fd4\u56de\u521b\u5efa\u7684\u5feb\u7167\u7684ID\u3002<\/p>\n<p>Ganache\u8fd8\u652f\u6301\u4f7f\u7528<code>evm_revert<\/code>RPC\u65b9\u6cd5\u5c06\u72b6\u6001\u6062\u590d\u5230\u4e4b\u524d\u7684\u5feb\u7167\uff0c\u5c3d\u7ba1\u4e00\u65e6\u8c03\u7528\u5e76\u6210\u529f\u6062\u590d\uff0c\u4f60\u5c31\u4e0d\u80fd\u518d\u4f7f\u7528\u5177\u6709\u76f8\u5e94ID\u7684\u5feb\u7167\uff0c\u56e0\u4e3a\u4f60\u53ea\u80fd\u6062\u590d\u5feb\u7167\u4e00\u6b21\u3002<\/p>\n<p>\u5982\u679c\u4f60\u9700\u8981\u591a\u6b21\u6062\u590d\u5230\u540c\u4e00\u4e2a\u70b9\uff0c\u4f60\u5e94\u8be5\u8003\u8651\u5728\u6bcf\u6b21evm_revert\u4e4b\u540e\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5feb\u7167\uff1b\u8fd9\u91cc\u6709\u4e00\u4e2a\u5728Ganache\u4e2d\u62cd\u6444\u5feb\u7167\u548c\u6062\u590d\u72b6\u6001\u7684\u6f14\u793a\u3002<\/p>\n<pre><textarea><\/textarea><code class=\"language-javascript hljs\"><span class=\"hljs-keyword\">const<\/span> provider = ganache.<span class=\"hljs-title function_\">provider<\/span>();\r\n<span class=\"hljs-keyword\">const<\/span> [<span class=\"hljs-keyword\">from<\/span>, to] = <span class=\"hljs-keyword\">await<\/span> provider.<span class=\"hljs-title function_\">send<\/span>(<span class=\"hljs-string\">\"eth_accounts\"<\/span>);\r\n<span class=\"hljs-keyword\">const<\/span> startingBalance = <span class=\"hljs-title class_\">BigInt<\/span>(<span class=\"hljs-keyword\">await<\/span> provider.<span class=\"hljs-title function_\">send<\/span>(<span class=\"hljs-string\">\"eth_getBalance\"<\/span>, [<span class=\"hljs-keyword\">from<\/span>] ));\r\n\r\n<span class=\"hljs-comment\">\/\/ take a snapshot<\/span>\r\n<span class=\"hljs-keyword\">const<\/span> snapshotId = <span class=\"hljs-keyword\">await<\/span> provider.<span class=\"hljs-title function_\">send<\/span>(<span class=\"hljs-string\">\"evm_snapshot\"<\/span>);\r\n\r\n<span class=\"hljs-comment\">\/\/ send value to another account (over-simplified example)<\/span>\r\n<span class=\"hljs-keyword\">await<\/span> provider.<span class=\"hljs-title function_\">send<\/span>(<span class=\"hljs-string\">\"eth_subscribe\"<\/span>, [<span class=\"hljs-string\">\"newHeads\"<\/span>] );\r\n<span class=\"hljs-keyword\">await<\/span> provider.<span class=\"hljs-title function_\">send<\/span>(<span class=\"hljs-string\">\"eth_sendTransaction\"<\/span>, [{<span class=\"hljs-keyword\">from<\/span>, to, <span class=\"hljs-attr\">value<\/span>: <span class=\"hljs-string\">\"0xffff\"<\/span>}] );\r\n<span class=\"hljs-keyword\">await<\/span> provider.<span class=\"hljs-title function_\">once<\/span>(<span class=\"hljs-string\">\"message\"<\/span>); <span class=\"hljs-comment\">\/\/ Note: `await provider.once` is non-standard<\/span>\r\n\r\n<span class=\"hljs-comment\">\/\/ ensure balance has updated<\/span>\r\n<span class=\"hljs-keyword\">const<\/span> newBalance = <span class=\"hljs-keyword\">await<\/span> provider.<span class=\"hljs-title function_\">send<\/span>(<span class=\"hljs-string\">\"eth_getBalance\"<\/span>, [<span class=\"hljs-keyword\">from<\/span>] );\r\n<span class=\"hljs-title function_\">assert<\/span>(<span class=\"hljs-title class_\">BigInt<\/span>(newBalance) < startingBalance);\r\n\r\n<span class=\"hljs-comment\">\/\/ revert the snapshot<\/span>\r\n<span class=\"hljs-keyword\">const<\/span> isReverted = <span class=\"hljs-keyword\">await<\/span> provider.<span class=\"hljs-title function_\">send<\/span>(<span class=\"hljs-string\">\"evm_revert\"<\/span>, [snapshotId] );\r\n<span class=\"hljs-title function_\">assert<\/span>(isReverted);\r\n\r\n<span class=\"hljs-comment\">\/\/ ensure balance has reverted<\/span>\r\n<span class=\"hljs-keyword\">const<\/span> endingBalance = <span class=\"hljs-keyword\">await<\/span> provider.<span class=\"hljs-title function_\">send<\/span>(<span class=\"hljs-string\">\"eth_getBalance\"<\/span>, [<span class=\"hljs-keyword\">from<\/span>] );\r\n<span class=\"hljs-keyword\">const<\/span> isBalanceReverted = assert.<span class=\"hljs-title function_\">strictEqual<\/span>(<span class=\"hljs-title class_\">BigInt<\/span>(endingBalance), startingBalance);\r\n<span class=\"hljs-variable language_\">console<\/span>.<span class=\"hljs-title function_\">log<\/span>({<span class=\"hljs-attr\">isBalanceReverted<\/span>: isBalanceReverted});\r\n<\/code><\/pre>\n<h3 id=\"5. \u53ef\u5373\u65f6\u3001\u95f4\u9694\u6216\u6309\u9700\u51fa\u5757\">5. \u53ef\u5373\u65f6\u3001\u95f4\u9694\u6216\u6309\u9700\u51fa\u5757<\/h3>\n<p>Ganache\u5141\u8bb8\u4f60\u914d\u7f6e\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u914d\u7f6e\u6309\u9700\u51fa\u5757\uff1b\u9ed8\u8ba4\u914d\u7f6e\u4e0b\uff0c\u533a\u5757\u4f1a\u5728\u6536\u5230\u4ea4\u6613\u540e\u7acb\u5373\u88ab\u5f00\u91c7\uff0c\u4f46\u4f60\u4e5f\u53ef\u4ee5\u9009\u62e9\u4ee5\u4e0b\u9009\u9879\uff1a<\/p>\n<ul>\n<li><strong>\u8bbe\u7f6e\u95f4\u9694\u65f6\u95f4\u51fa\u5757<\/strong>\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7<code>--miner.blockTime <TIME IN SECONDS><\/code>\u9009\u9879\u6765\u6307\u5b9aGanache\u5728\u5f00\u91c7\u4e0b\u4e00\u4e2a\u4ea4\u6613\u4e4b\u524d\u5e94\u8be5\u7b49\u5f85\u7684\u65f6\u95f4\uff08\u79d2\uff09\u3002\u8bbe\u7f6e\u4e3a0\u610f\u5473\u7740Ganache \u4f1a\u7acb\u5373\u6316\u6398\u65b0\u7684\u4ea4\u6613\u3002<\/li>\n<li><strong>\u6839\u636e\u9700\u8981\u51fa\u5757<\/strong>\u3002Ganache\u8fd8\u516c\u5f00\u4e86\u4e00\u4e2a<code>evm_mine<\/code>\u7684RPC\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u5f3a\u5236\u5f00\u91c7\u4e00\u4e2a\u533a\u5757\uff0c\u65e0\u8bba\u5f00\u91c7\u662f\u5728\u8fd0\u884c\u8fd8\u662f\u505c\u6b62\u3002\u5982\u679cmempool\u4e2d\u6ca1\u6709\u4ea4\u6613\u5b58\u5728\uff0c\u8fd9\u5c06\u6316\u6398\u4e00\u4e2a\u7a7a\u5757\u3002\u8bf7\u770b\u4e0b\u9762\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/li>\n<\/ul>\n<pre><textarea><\/textarea><code class=\"language-javascript hljs\"><span class=\"hljs-variable language_\">console<\/span>.<span class=\"hljs-title function_\">log<\/span>(<span class=\"hljs-string\">\"start\"<\/span>, <span class=\"hljs-keyword\">await<\/span> provider.<span class=\"hljs-title function_\">send<\/span>(<span class=\"hljs-string\">\"eth_blockNumber\"<\/span>));\r\n<span class=\"hljs-keyword\">await<\/span> provider.<span class=\"hljs-title function_\">send<\/span>(<span class=\"hljs-string\">\"evm_mine\"<\/span>, [{<span class=\"hljs-attr\">blocks<\/span>: <span class=\"hljs-number\">5<\/span>}] ); <span class=\"hljs-comment\">\/\/ mines 5 blocks<\/span>\r\n<span class=\"hljs-variable language_\">console<\/span>.<span class=\"hljs-title function_\">log<\/span>(<span class=\"hljs-string\">\"end\"<\/span>, <span class=\"hljs-keyword\">await<\/span> provider.<span class=\"hljs-title function_\">send<\/span>(<span class=\"hljs-string\">\"eth_blockNumber\"<\/span>));\r\n<\/code><\/pre>\n<h3 id=\"6. \u5feb\u8fdb\u65f6\u95f4\">6. \u5feb\u8fdb\u65f6\u95f4<\/h3>\n<p>Ganache \u8fd8\u63d0\u4f9b\u4e86\u4e24\u4e2aRPC\u65b9\u6cd5\uff0c\u7528\u4e8e\u64cd\u7eb5\u94fe\u4e0a\u65f6\u95f4\u3002<\/p>\n<p>\u4e00\u4e2a\u5178\u578b\u7684\u4f7f\u7528\u573a\u666f\u662f\uff0c\u667a\u80fd\u5408\u7ea6\u8981\u6c42\u5728\u7528\u6237\u91c7\u53d6\u67d0\u4e9b\u884c\u52a8\u4e4b\u524d\u5fc5\u987b\u7ecf\u8fc7\u7279\u5b9a\u7684\u65f6\u95f4\uff1b\u5982\u679c\u4f60\u4e0d\u60f3\u7b49\u5f85\u7684\u8fd9\u6bb5\u65f6\u95f4\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>evm_increaseTime<\/code>\u5c06\u533a\u5757\u94fe\u5f53\u524d\u7684\u65f6\u95f4\u6233\u589e\u52a0\u6307\u5b9a\u7684\u79d2\uff08\u4ee5\u5341\u516d\u8fdb\u5236\u4f20\u9012\uff09\u3002<\/p>\n<pre><textarea><\/textarea><code class=\"language-bash hljs\">curl -H <span class=\"hljs-string\">'Content-Type: application\/json'<\/span> --data<span class=\"hljs-string\">' {\"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"evm_increaseTime\", \"params\": [\"0x15180\"] }'<\/span> http:\/\/localhost:8545\r\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8fd4\u56de\u8c03\u6574\u540e\u7684\u603b\u65f6\u95f4\uff0c\u5355\u4f4d\u4e3a\u6beb\u79d2\u3002\u6b64\u5916\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>evm_setTime<\/code>\u6765\u8bbe\u7f6e\u4e3a\u7279\u5b9a\u7684\u65f6\u95f4\u6233\uff1b\u5b83\u63a5\u53d7\u7cbe\u5ea6\u4e3a\u6beb\u79d2\u7684JavaScript\u65f6\u95f4\u6233\uff08Unix epoch\uff09\uff0c\u5e76\u8fd4\u56de\u7ed9\u5b9a\u65f6\u95f4\u6233\u548c\u5f53\u524d\u65f6\u95f4\u4e4b\u95f4\u7684\u79d2\u6570\u3002<\/p>\n<pre><textarea><\/textarea><code class=\"hljs language-rust\">curl -H <span class=\"hljs-symbol\">'Content<\/span>-Type: application\/json' --data '{<span class=\"hljs-string\">\"jsonrpc\"<\/span>:<span class=\"hljs-string\">\"2.0\"<\/span>, <span class=\"hljs-string\">\"id\"<\/span>: <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-string\">\"method\"<\/span>: <span class=\"hljs-string\">\"evm_increaseTime\"<\/span>, <span class=\"hljs-string\">\"params\"<\/span>: [<span class=\"hljs-string\">\"0x15180\"<\/span>] }' http:<span class=\"hljs-comment\">\/\/localhost:8545<\/span>\r\n<\/code><\/pre>\n<p>\u4f60\u5e94\u8be5\u8c28\u614e\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u4f60\u5411\u540e\u79fb\u52a8\u65f6\u95f4\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u65b0\u533a\u5757\u51fa\u73b0\u5728\u65e7\u533a\u5757\u4e4b\u524d\u88ab\u5f00\u91c7\uff0c\u4ece\u800c\u4f7f\u533a\u5757\u94fe\u72b6\u6001\u65e0\u6548\u3002<\/p>\n<h3 id=\"7. \u5192\u5145\u4efb\u4f55\u8d26\u6237\">7. \u5192\u5145\u4efb\u4f55\u8d26\u6237<\/h3>\n<p>Ganache\u5141\u8bb8\u4f60\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u5192\u5145\uff08Impersonate\uff09\u53e6\u4e00\u4e2a\u8d26\u6237\u3002\u8fd9\u5728\u4e0e\u5206\u53c9\u529f\u80fd\u4e00\u8d77\u4f7f\u7528\u65f6\u66f4\u52a0\u6709\u8da3\uff1b\u4f60\u53ef\u4ee5\u5206\u53c9Mainnet\u5e76\u5192\u5145\u4efb\u4f55\u8d26\u6237\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff0c\u4f60\u53ef\u4ee5\u5206\u53c9Mainnet\uff0c\u5192\u5145\u4e00\u4e2a\u8d26\u6237\uff0c\u5e76\u53d1\u9001\u4e00\u4e9b\u4ee3\u5e01\u7ed9\u53e6\u4e00\u4e2a\u8d26\u6237\u3002<\/p>\n<ul>\n<li>\u901a\u8fc7\u5206\u53c9Mainnet\u5e76\u5192\u5145\uff08\u89e3\u9501\uff09\u4e00\u4e2a\u968f\u673a\u8d26\u6237\uff0c\u542f\u52a8Ganache\u3002<code>ganache --fork --wallet.unlockedAccounts <Account Address Here><\/code>\u3002<\/li>\n<li>\u83b7\u53d6\u5df2\u89e3\u9501\u8d26\u6237\u7684\u4f59\u989d:\u00a0<code>curl -H 'Content-Type: application\/json' --data '{\"jsonrpc\":\"2.0\", \"id\": 1, \"method\": \"eth_getBalance\", \"params\": [\"<Account Address Here>\"] }' http:\/\/localhost:8545<\/code>. \u8fd9\u5c06\u8fd4\u56de\u4ee5WEI\u7f16\u7801\u7684\u5341\u516d\u8fdb\u5236\u6570\u5b57\u7684\u4f59\u989d\u3002<\/li>\n<li>\u4ece\u89e3\u9501\u7684\u8d26\u6237\u53d1\u9001\u4e00\u4e9b\u4ee5\u592a\u5e01\u5230\u53e6\u4e00\u4e2a\u8d26\u6237:\u00a0<code>curl -H 'Content-Type: application\/json' --data '{\"jsonrpc\":\"2.0\", \"id\": 1, \"method\": \"eth_sendTransaction\", \"params\": [{\"from\": \"UNLOCKED ACCOUNT\", \"to\": \"RECEIVING ACCOUNT\", \"value\": \"AMOUNT TO SEND\"}] }' http:\/\/localhost:8545<\/code><\/li>\n<\/ul>\n<p>\u73b0\u5728\u9a8c\u8bc1\u4e00\u4e0b\uff0c\u89e3\u9501\u8d26\u6237\u7684\u4f59\u989d\u662f\u5426\u51cf\u5c11\u4e86\u53d1\u9001\u7684\u4ee5\u592a\u6570\u91cf\u3002<\/p>\n<p>\u5411\u53e6\u4e00\u4e2a\u8d26\u6237\u53d1\u9001\u4ee5\u592a\u5e01\u5e76\u4e0d\u662f\u552f\u4e00\u7684\u4f7f\u7528\u573a\u666f\uff0c\u56e0\u4e3a\u4f60\u53ef\u4ee5\u5192\u5145\u4e00\u4e2a\u8d26\u6237\u6765\u8c03\u7528\u667a\u80fd\u5408\u7ea6\u4e2d\u67d0\u4e9b\u53ea\u5c5e\u4e8e\u6240\u6709\u8005\u7684\u529f\u80fd\uff0c\u5c31\u50cf\u6211\u4eec\u4e4b\u524d\u770b\u5230\u7684convex benchmark\u5408\u7ea6\u90a3\u6837\u3002<\/p>\n<h3 id=\"8. \u5f85\u5b9a\u4ea4\u6613\uff08pending\uff09\">8. \u5f85\u5b9a\u4ea4\u6613\uff08pending\uff09<\/h3>\n<p>\u5728\u4ee5\u592a\u574a\u4e2d\uff0c\u6bcf\u4e2a\u4ea4\u6613\u90fd\u6709\u4e00\u4e2anonce\u3002nonce\u662f\u4e00\u4e2a\u7ed9\u5b9a\u5730\u5740\u53d1\u9001\u7684\u6240\u6709\u4ea4\u6613\u6570\u91cf\uff0c\u65e0\u8bba\u4f55\u65f6\u4f60\u53d1\u9001\u4e00\u4e2a\u4ea4\u6613\uff0cnonce\u90fd\u4f1a\u589e\u52a01\u3002<\/p>\n<p>\u5728\u65e9\u671f\u7248\u672c\u7684Ganache\u4e2d\uff0c\u5982\u679c\u4f60\u53d1\u9001\u7684\u4ea4\u6613\u7684nonce\u503c\u6bd4\u4e0a\u4e00\u4e2anonce\u503c\u59271\uff0cGanache\u4f1a\u51fa\u9519\uff0c\u4ea4\u6613\u4f1a\u88ab\u62d2\u7edd\u3002<\/p>\n<p>\u5728Ganache 7\u4e2d\uff0c\u5982\u679c\u524d\u4e00\u4e2a\u4ea4\u6613\u7684nonce\u662f1\uff0c\u800c\u4f60\u7531\u4e8e\u67d0\u79cd\u539f\u56e0\u53d1\u9001\u4e86\u4e00\u4e2anonce\u8bbe\u7f6e\u4e3a3\u7684\u4ea4\u6613\uff0c\u90a3\u4e48\u8fd9\u4e2anonce\u4e3a3\u7684\u4ea4\u6613\u5c06\u5728\u4ea4\u6613\u6c60\u4e2d\u5750\u7b49\u4e00\u4e2anonce\u4e3a2\u7684\u4ea4\u6613\u88ab\u53d1\u9001\uff0c\u5c4a\u65f6\u8fd9\u4e24\u4e2a\u4ea4\u6613\u5c06\u88ab\u6316\u51fa\u5e76\u6dfb\u52a0\u5230\u533a\u5757\u94fe\u72b6\u6001\u3002<\/p>\n<hr \/>\n<p>\u4eca\u5929\u5c31\u5f00\u59cb\u4f7f\u7528Ganache 7\uff0c\u53ea\u9700\u8fd0\u884c<code>npm install ganache --global && ganache --help<\/code>\uff0c\u6216\u8005\u67e5\u770b<a href=\"https:\/\/github.com\/trufflesuite\/ganache\/releases\/tag\/v7.0.0\">v7\u53d1\u884c\u8bf4\u660e<\/a>\u3002<\/p>\n<hr \/>\n<p>\u8fd9\u4e2a\u7248\u672c\u5df2\u7ecf\u915d\u917f\u4e86\u591a\u5e74\uff0c\u6211\u4eec\u5bf9\u6211\u4eec\u6240\u505a\u7684\u5de5\u4f5c\u611f\u5230\u975e\u5e38\u81ea\u8c6a\u3002\u6211\u4eec\u5e0c\u671b\u4f60\u548c\u6211\u4eec\u4e00\u6837\u559c\u6b22\u5b83\u3002<\/p>\n<p>\u611f\u8c22\u6bcf\u4e00\u4e2a\u53c2\u4e0e\u5b9e\u73b0\u8fd9\u4e2a\u7248\u672c\u7684\u4eba&#8211;\u8d21\u732e\u8005\u3001\u6307\u5bfc\u8005\u3001\u5ba1\u67e5\u8005\u3001\u95ee\u9898\u62a5\u544a\u8005\u548c\u793e\u533a\u53c2\u4e0e\u8005\u90fd\u5bf9Ganache v7\u7684\u5b9e\u73b0\u8d77\u5230\u4e86\u91cd\u8981\u4f5c\u7528\u3002\u6211\u4eec\u5bf9\u4f60\u4eec\u6240\u6709\u4eba\u8868\u793a\u8877\u5fc3\u611f\u8c22\u3002<\/p>\n<p>\u672c\u7ffb\u8bd1\u7531\u00a0<a href=\"https:\/\/duet.finance\/?utm_souce=learnblockchain.cn\">Duet Protocol<\/a>\u00a0\u8d5e\u52a9\u652f\u6301\u3002<\/p>\n<\/div>\n<\/div>\n<p class=\"text-muted mt-30\">\u672c\u6587\u53c2\u4e0e<a class=\"\" href=\"https:\/\/learnblockchain.cn\/site\/coins\">\u767b\u94fe\u793e\u533a\u5199\u4f5c\u6fc0\u52b1\u8ba1\u5212<\/a>\u00a0\uff0c\u597d\u6587\u597d\u6536\u76ca\uff0c\u6b22\u8fce\u6b63\u5728\u9605\u8bfb\u7684\u4f60\u4e5f\u52a0\u5165\u3002<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740 Ganache 7 \u53d1\u5e03\uff0c \u518d<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[24],"tags":[121],"class_list":["post-5920","post","type-post","status-publish","format-standard","hentry","category-value_docs","tag-05-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/5920","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=5920"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/5920\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=5920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=5920"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=5920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}