import{_ as i,c as a,a as e,o as n}from"./app-COnnofor.js";const h={};function t(l,s){return n(),a("div",null,[...s[0]||(s[0]=[e(`<h2 id="hardhat2-部署" tabindex="-1"><a class="header-anchor" href="#hardhat2-部署"><span>HardHat2 部署</span></a></h2><h3 id="node-js-v16-安装" tabindex="-1"><a class="header-anchor" href="#node-js-v16-安装"><span>Node.js v16 安装</span></a></h3><p>HardHat2 需要 Node.js v16 及以上的版本 这里给出的是通过 npm 来将旧版本升级到 Node.js v16 的，如果想要直接安装的话通过 <code>yum install nodejs</code> 或者 <code>apt install nodejs</code> 来即可。</p><div class="language-bash line-numbers-mode" data-highlighter="shiki" data-ext="bash" style="--shiki-light:#393a34;--shiki-dark:#dbd7caee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#121212;"><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code class="language-bash"><span class="line"><span style="--shiki-light:#59873A;--shiki-dark:#80A665;">npm</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> config</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> set</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> registry</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> https://registry.npmmirror.com</span></span>
<span class="line"><span style="--shiki-light:#59873A;--shiki-dark:#80A665;">sudo</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> npm</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> install</span><span style="--shiki-light:#A65E2B;--shiki-dark:#C99076;"> -g</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> n</span></span>
<span class="line"><span style="--shiki-light:#59873A;--shiki-dark:#80A665;">sudo</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> n</span><span style="--shiki-light:#2F798A;--shiki-dark:#4C9A91;"> 16</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>安装完成之后可以查看版本，如果没有更新可以重新刷新一下环境（直接退出重连最简单）</p><div class="language-bash line-numbers-mode" data-highlighter="shiki" data-ext="bash" style="--shiki-light:#393a34;--shiki-dark:#dbd7caee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#121212;"><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code class="language-bash"><span class="line"><span style="--shiki-light:#59873A;--shiki-dark:#80A665;">node</span><span style="--shiki-light:#A65E2B;--shiki-dark:#C99076;"> -v</span></span>
<span class="line"><span style="--shiki-light:#A0ADA0;--shiki-dark:#758575DD;"># v16.20.2</span></span>
<span class="line"><span style="--shiki-light:#59873A;--shiki-dark:#80A665;">npm</span><span style="--shiki-light:#A65E2B;--shiki-dark:#C99076;"> -v</span></span>
<span class="line"><span style="--shiki-light:#A0ADA0;--shiki-dark:#758575DD;"># 6.14.15</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="安装-hardhat2" tabindex="-1"><a class="header-anchor" href="#安装-hardhat2"><span>安装 Hardhat2</span></a></h3><p>创建一个 hardhat2-project 目录，初始化 npm 项目，注意这里不要用 hardhat 作为项目名</p><div class="language-bash line-numbers-mode" data-highlighter="shiki" data-ext="bash" style="--shiki-light:#393a34;--shiki-dark:#dbd7caee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#121212;"><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code class="language-bash"><span class="line"><span style="--shiki-light:#59873A;--shiki-dark:#80A665;">mkdir</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> ~/hardhat2-project</span></span>
<span class="line"><span style="--shiki-light:#998418;--shiki-dark:#B8A965;">cd</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> ~/hardhat2-project</span></span>
<span class="line"><span style="--shiki-light:#59873A;--shiki-dark:#80A665;">npm</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> init</span><span style="--shiki-light:#A65E2B;--shiki-dark:#C99076;"> -y</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>安装 Hardhat 2</p><div class="language-bash line-numbers-mode" data-highlighter="shiki" data-ext="bash" style="--shiki-light:#393a34;--shiki-dark:#dbd7caee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#121212;"><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code class="language-bash"><span class="line"><span style="--shiki-light:#59873A;--shiki-dark:#80A665;">npm</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> install</span><span style="--shiki-light:#A65E2B;--shiki-dark:#C99076;"> --save-dev</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> hardhat@^2.23.0</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div></div></div><p>初始化 Hardhat2 项目</p><div class="language-bash line-numbers-mode" data-highlighter="shiki" data-ext="bash" style="--shiki-light:#393a34;--shiki-dark:#dbd7caee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#121212;"><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code class="language-bash"><span class="line"><span style="--shiki-light:#59873A;--shiki-dark:#80A665;">npx</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> hardhat</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> init</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div></div></div><p>选择</p><div class="language-bash line-numbers-mode" data-highlighter="shiki" data-ext="bash" style="--shiki-light:#393a34;--shiki-dark:#dbd7caee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#121212;"><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code class="language-bash"><span class="line"><span style="--shiki-light:#59873A;--shiki-dark:#80A665;">✔</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> What</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> do</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> you</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> want</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> to</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> do?</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> ·</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> Create</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> a</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> JavaScript</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> project</span></span>
<span class="line"><span style="--shiki-light:#59873A;--shiki-dark:#80A665;">✔</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> Hardhat</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> project</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> root:</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> ·</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> /root/hardhat2-project</span></span>
<span class="line"><span style="--shiki-light:#59873A;--shiki-dark:#80A665;">✔</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> Do</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> you</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> want</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> to</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> add</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> a</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> .gitignore?</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;"> (Y/n) · y</span></span>
<span class="line"><span style="--shiki-light:#59873A;--shiki-dark:#80A665;">✔</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> Help</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> us</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> improve</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> Hardhat</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> with</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> anonymous</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> crash</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> reports</span><span style="--shiki-light:#999999;--shiki-dark:#666666;"> &amp;</span><span style="--shiki-light:#59873A;--shiki-dark:#80A665;"> basic</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> usage</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> data?</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;"> (Y/n) · y</span></span>
<span class="line"><span style="--shiki-light:#59873A;--shiki-dark:#80A665;">✔</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> Do</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> you</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> want</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> to</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> install</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> this</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> sample</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> project</span><span style="--shiki-light:#B5695977;--shiki-dark:#C98A7D77;">&#39;</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;">s dependencies with npm (@nomicfoundation/hardhat-toolbox)? (Y/n) · y</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="使用-hardhat2-编译测试合约" tabindex="-1"><a class="header-anchor" href="#使用-hardhat2-编译测试合约"><span>使用 Hardhat2 编译测试合约</span></a></h2><p>在 hardhat2-project 目录下，执行以下命令来编译合约 合约都会放在 <code>contracts</code> 目录下，这里我们选择了一个默认的合约，因此可以直接编译</p><div class="language-bash line-numbers-mode" data-highlighter="shiki" data-ext="bash" style="--shiki-light:#393a34;--shiki-dark:#dbd7caee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#121212;"><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code class="language-bash"><span class="line"><span style="--shiki-light:#A0ADA0;--shiki-dark:#758575DD;"># 测试编译</span></span>
<span class="line"><span style="--shiki-light:#59873A;--shiki-dark:#80A665;">npx</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> hardhat</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> compile</span></span>
<span class="line"><span style="--shiki-light:#A0ADA0;--shiki-dark:#758575DD;"># 运行测试</span></span>
<span class="line"><span style="--shiki-light:#59873A;--shiki-dark:#80A665;">npx</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> hardhat</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> test</span></span>
<span class="line"><span style="--shiki-light:#A0ADA0;--shiki-dark:#758575DD;"># 查看可用任务</span></span>
<span class="line"><span style="--shiki-light:#59873A;--shiki-dark:#80A665;">npx</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> hardhat</span><span style="--shiki-light:#B56959;--shiki-dark:#C98A7D;"> help</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div>`,18)])])}const k=i(h,[["render",t]]),p=JSON.parse('{"path":"/programming/solidity/other/hardhat/","title":"Hardhat 相关知识","lang":"zh-CN","frontmatter":{"title":"Hardhat 相关知识","createTime":"2025/10/14 14:57:06","permalink":"/programming/solidity/other/hardhat/","description":"HardHat2 部署 Node.js v16 安装 HardHat2 需要 Node.js v16 及以上的版本 这里给出的是通过 npm 来将旧版本升级到 Node.js v16 的，如果想要直接安装的话通过 yum install nodejs 或者 apt install nodejs 来即可。 安装完成之后可以查看版本，如果没有更新可以重新刷...","head":[["script",{"type":"application/ld+json"},"{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Article\\",\\"headline\\":\\"Hardhat 相关知识\\",\\"image\\":[\\"\\"],\\"dateModified\\":\\"2025-10-17T15:32:49.000Z\\",\\"author\\":[]}"],["meta",{"property":"og:url","content":"https://notes.simengweb.com/programming/solidity/other/hardhat/"}],["meta",{"property":"og:site_name","content":"仲夏夜之梦"}],["meta",{"property":"og:title","content":"Hardhat 相关知识"}],["meta",{"property":"og:description","content":"HardHat2 部署 Node.js v16 安装 HardHat2 需要 Node.js v16 及以上的版本 这里给出的是通过 npm 来将旧版本升级到 Node.js v16 的，如果想要直接安装的话通过 yum install nodejs 或者 apt install nodejs 来即可。 安装完成之后可以查看版本，如果没有更新可以重新刷..."}],["meta",{"property":"og:type","content":"article"}],["meta",{"property":"og:locale","content":"zh-CN"}],["meta",{"property":"og:updated_time","content":"2025-10-17T15:32:49.000Z"}],["meta",{"property":"article:modified_time","content":"2025-10-17T15:32:49.000Z"}]]},"readingTime":{"minutes":1.11,"words":332},"git":{"createdTime":1760427548000,"updatedTime":1760715169000,"contributors":[{"name":"祀梦","username":"","email":"3501646051@qq.com","commits":2,"avatar":"https://gravatar.com/avatar/6406a81eeddc359cf3d3ce018797689fc6d014ff06215c27d0210b42e8f5a8ab?d=retro"}]},"autoDesc":true,"filePathRelative":"notes/programming/solidity/other/hardhat.md","headers":[]}');export{k as comp,p as data};
