\n

The counter is at: \n\n<\/li>\n
  • \n

    We should now be able to view our HTML document in a web browser with the Metamask plugin installed. I won't go through the Javascript, but if you are familiar with JavaScript and following the concepts and what we did in the Hardhat terminal previously, what is happening in the code should be fairly straight-forward. Metamask should prompt you that you are connecting to the site and you'll need to select the Hardnet network that we set up earlier. You should see something like this in the browser:<\/p>\n\n

    \"WebI<\/p>\n<\/li>\n

  • If all went well, you can click on the \"Increment\" button. Metamask will let you know that you are about to make a transaction and inform you of the gas fee. You can Confirm this transaction in Metamask and see the count increment on both the website and in the terminal where you have the hardhat node running!<\/p><\/li>\n

  • Congratulations, we are interacting with our contract through a web UI!<\/p><\/li>\n<\/ol>\n\n

    A few notes as you dive deeper into Hardhat and Metamask for development:<\/p>\n\n

      \n
    • Each transaction has an nonce. When you reset your hardhat node, that nonce gets reset and you might loose sync with what Metamask thinks is a unique nonce. When that happens, Metmask has an option to set a custom nonce with the transaction, or you can reset Metamask's nonces in Settings->Advanced->Clear Activity Tab data. <\/pre>\n<\/li>\n

    • You'll need to redeploy your smart contract every time you restart your Hardhat node.<\/p><\/li>\n

    • \n

      If you are writing contracts that will keep track of users by their public address and want to experiment in the Hardhat console with transactions form different users, you can impersonate different addresses in the console that were displayed when you first started the Hardhat node with something like this before you connect to the contract:
      \n<\/p>\n

      const signers = await ethers.getSigners();\nconst newSigner = signers[1]; \/\/ Change the 1 to a different number that corolates with one of the pre-generated testing addresses\nconst newMain = await main.connect(newSigner);\n\nnewMain.setContractAddress(\"test\",\"0xYourContractAddress\");\n<\/pre>\n\n<\/li>\n\n\n\n          \n\n            \n  \n\n            \n        <\/ol>"}	
      	
      
      
      
      
      
      
      

      国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

      首頁 web前端 js教程 用於簡單智能合約的 WebI

      用於簡單智能合約的 WebI

      Nov 29, 2024 am 03:21 AM

      讓我們?yōu)橹腔酆霞s建立一個(gè) Web 前端!這是我之前關(guān)於使用 Solidity 和 Hardhat 創(chuàng)建簡單智能合約的文章的後續(xù)內(nèi)容。這裡的說明假設(shè)您正在使用我們剛剛部署到 Hardhat 環(huán)境的相同合約。

      在上一篇文章中,我們建立並測試了一個(gè)合約,該合約將增加儲(chǔ)存在狀態(tài)變數(shù)中的計(jì)數(shù)器。使用 Hardhat 控制臺(tái),我們呼叫了incrementCount() 和getCount() 函數(shù)。在現(xiàn)實(shí)世界中,與合約的互動(dòng)不會(huì)透過開發(fā)控制臺(tái)進(jìn)行。創(chuàng)建調(diào)用這些函數(shù)的應(yīng)用程式的一種方法是透過網(wǎng)頁中的 Javascript(透過 ethers.js 庫) - Web3 應(yīng)用程式!

      如上一篇文章所述,與 web3 應(yīng)用程式互動(dòng)需要具有內(nèi)建錢包的瀏覽器。在這個(gè)簡單的範(fàn)例中,我們將使用 Metamask。 Metmask 已針對 Etherium 以及其他一些基於 EVM 的區(qū)塊鏈進(jìn)行了預(yù)配置,但不是我們在 Hardhat 環(huán)境中的模擬區(qū)塊鏈。為了讓這一切運(yùn)作起來,我們將首先設(shè)定 Metmask,然後建立呼叫我們的合約所需的 HTML/Javascript。

      Metamask / Web3 瀏覽器

      1. 安裝 Metamask。我將使用此處找到的 Chrome 擴(kuò)充功能。如果您是 Chrome 用戶,現(xiàn)在您可以查看 web3 內(nèi)容並與之互動(dòng)。

        我不會(huì)引導(dǎo)您完成初始設(shè)置,但係統(tǒng)可能會(huì)提示您匯入現(xiàn)有私鑰或產(chǎn)生新私鑰並記下恢復(fù)短語。就這麼做吧。

      2. 接下來我們將把 Hardhat 網(wǎng)路加入 Metamask。 Metamask 支援您想要的任何 EVM,但需要對其進(jìn)行配置才能實(shí)現(xiàn)。通常這只是添加鏈 ID 和 RPC URL 的問題。從 Metamask 內(nèi)部(您可能需要透過點(diǎn)擊 Chrome 外掛程式並選擇它來啟動(dòng)它),您應(yīng)該在頂部中間看到您的公共地址。在您的地址左側(cè)將有一個(gè)下拉列表,顯示當(dāng)前網(wǎng)路。按一下該按鈕查看其他可用網(wǎng)路:

        WebI For Simple Smart Contract

        點(diǎn)選「新增自訂網(wǎng)路」。填入網(wǎng)路名稱,例如“Hardhat”,網(wǎng)路 RPC URL 以及 Hardhat 節(jié)點(diǎn)的 IP 位址和端口,如果您在本地運(yùn)行,可能是這樣的:
        http://127.0.0.1:8545/

        輸入鏈ID 1337,符號暫時(shí)只能是ETH。請注意,我們在真實(shí)的以太坊網(wǎng)路上處理真實(shí)的 ETH,但如果您的錢包中有真實(shí)的 ??ETH,請務(wù)必小心留在我們的 Hardhat 網(wǎng)路上。

        現(xiàn)在切換到我們剛剛在 Metamask 外掛程式中新增的 Hardhat 網(wǎng)路。在監(jiān)控正在運(yùn)行的 Hardhat 節(jié)點(diǎn)的終端中,您應(yīng)該會(huì)在錢包連接時(shí)看到一些活動(dòng)。

      3. 由於您的 Metamask 錢包目前沒有任何(假)ETH,所以我們發(fā)送一些。從 Metamask 取得您的公共地址(在 Metamask 視窗頂部的錢包名稱下方,按一下複製按鈕)。從運(yùn)行 Hardhat 控制臺(tái)的終端窗口,執(zhí)行:

        const [owner,  feeCollector, operator] = await 
        ethers.getSigners();
        await owner.sendTransaction({ to: "PasteYourMetamaskAddressHere", value: ethers.parseEther("0.1") });
        

        如果您返回 Metamask,您應(yīng)該會(huì)看到您的 Hardhat 錢包中現(xiàn)在有一些 ETH!現(xiàn)在我們準(zhǔn)備在 Hardhat 網(wǎng)路上進(jìn)行一些 web3 交易。

      建立 Web3 網(wǎng)頁

      1. 讓我們建立一個(gè)簡單的網(wǎng)頁來檢視和增加計(jì)數(shù)器。我不會(huì)使用任何重型框架,而只是使用普通的舊式 HTML、Javascript 和 ethers.js 函式庫。但是,您不能僅將瀏覽器指向 .htm 文檔,您需要在某處運(yùn)行網(wǎng)頁伺服器才能使 Metamask 插件正常運(yùn)作。根據(jù)您的作業(yè)系統(tǒng),您也許可以使用輕量級伺服器,例如 http-server 或本地的其他伺服器。

        我們需要上一篇文章中部署合約時(shí)的一些東西。請參閱上一篇文章,並從工件目錄中取得 合約位址 和合約的 ABI JSON 陣列。我們不需要該文件中的其餘JSON,只需要“abi”屬性中的內(nèi)容,它應(yīng)該以[ 開頭,以] 結(jié)尾,並且看起來像一些東西像這樣:

        [
            {
              "inputs": [],
              "stateMutability": "nonpayable",
              "type": "constructor"
            },
            {
              "inputs": [],
              "name": "getCount",
              "outputs": [
                {
                  "internalType": "uint256",
                  "name": "",
                  "type": "uint256"
                }
              ],
              "stateMutability": "view",
              "type": "function"
            },
            {
              "inputs": [],
              "name": "incrementCount",
              "outputs": [],
              "stateMutability": "nonpayable",
              "type": "function"
            }
          ]
        
      2. 讓我們將其放入一些 HTML 和 Javascript 中:

        <html>
            <head>
                    <script src="https://cdnjs.cloudflare.com/ajax/libs/ethers/5.2.0/ethers.umd.min.js" type="application/javascript"></script>
            </head>
        
            <body>
                    <h2>The counter is at: <span>
        
        </li>
        <li>
        <p>We should now be able to view our HTML document in a web browser with the Metamask plugin installed. I won't go through the Javascript, but if you are familiar with JavaScript and following the concepts and what we did in the Hardhat terminal previously, what is happening in the code should be fairly straight-forward. Metamask should prompt you that you are connecting to the site and you'll need to select the Hardnet network that we set up earlier. You should see something like this in the browser:</p>
        
        <p><img src="/static/imghw/default1.png"  data-src="https://img.php.cn/upload/article/000/000/000/173282167151033.jpg"  class="lazy" alt="WebI For Simple Smart Contract" /></p>
        </li>
        <li><p>If all went well, you can click on the "Increment" button. Metamask will let you know that you are about to make a transaction and inform you of the gas fee. You can Confirm this transaction in Metamask and see the count increment on both the website and in the terminal where you have the hardhat node running!</p></li>
        <li><p>Congratulations, we are interacting with our contract through a web UI!</p></li>
        </ol>
        
        <p>A few notes as you dive deeper into Hardhat and Metamask for development:</p>
        
        <ul>
        <li><p>Each transaction has an nonce. When you reset your hardhat node, that nonce gets reset and you might loose sync with what Metamask thinks is a unique nonce. When that happens, Metmask has an option to set a custom nonce with the transaction, or you can reset Metamask's nonces in Settings->Advanced->Clear Activity Tab data. 
      3. You'll need to redeploy your smart contract every time you restart your Hardhat node.

      4. If you are writing contracts that will keep track of users by their public address and want to experiment in the Hardhat console with transactions form different users, you can impersonate different addresses in the console that were displayed when you first started the Hardhat node with something like this before you connect to the contract:

        const signers = await ethers.getSigners();
        const newSigner = signers[1]; // Change the 1 to a different number that corolates with one of the pre-generated testing addresses
        const newMain = await main.connect(newSigner);
        
        newMain.setContractAddress("test","0xYourContractAddress");
        

      以上是用於簡單智能合約的 WebI的詳細(xì)內(nèi)容。更多資訊請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

      本網(wǎng)站聲明
      本文內(nèi)容由網(wǎng)友自願(yuàn)投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請聯(lián)絡(luò)admin@php.cn

      熱AI工具

      Undress AI Tool

      Undress AI Tool

      免費(fèi)脫衣圖片

      Undresser.AI Undress

      Undresser.AI Undress

      人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

      AI Clothes Remover

      AI Clothes Remover

      用於從照片中去除衣服的線上人工智慧工具。

      Clothoff.io

      Clothoff.io

      AI脫衣器

      Video Face Swap

      Video Face Swap

      使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

      熱工具

      記事本++7.3.1

      記事本++7.3.1

      好用且免費(fèi)的程式碼編輯器

      SublimeText3漢化版

      SublimeText3漢化版

      中文版,非常好用

      禪工作室 13.0.1

      禪工作室 13.0.1

      強(qiáng)大的PHP整合開發(fā)環(huán)境

      Dreamweaver CS6

      Dreamweaver CS6

      視覺化網(wǎng)頁開發(fā)工具

      SublimeText3 Mac版

      SublimeText3 Mac版

      神級程式碼編輯軟體(SublimeText3)

      Java vs. JavaScript:清除混亂 Java vs. JavaScript:清除混亂 Jun 20, 2025 am 12:27 AM

      Java和JavaScript是不同的編程語言,各自適用於不同的應(yīng)用場景。 Java用於大型企業(yè)和移動(dòng)應(yīng)用開發(fā),而JavaScript主要用於網(wǎng)頁開發(fā)。

      JavaScript評論:簡短說明 JavaScript評論:簡短說明 Jun 19, 2025 am 12:40 AM

      JavascriptconcommentsenceenceEncorenceEnterential gransimenting,reading and guidingCodeeXecution.1)單inecommentsareusedforquickexplanations.2)多l(xiāng)inecommentsexplaincomplexlogicorprovideDocumentation.3)

      如何在JS中與日期和時(shí)間合作? 如何在JS中與日期和時(shí)間合作? Jul 01, 2025 am 01:27 AM

      JavaScript中的日期和時(shí)間處理需注意以下幾點(diǎn):1.創(chuàng)建Date對像有多種方式,推薦使用ISO格式字符串以保證兼容性;2.獲取和設(shè)置時(shí)間信息可用get和set方法,注意月份從0開始;3.手動(dòng)格式化日期需拼接字符串,也可使用第三方庫;4.處理時(shí)區(qū)問題建議使用支持時(shí)區(qū)的庫,如Luxon。掌握這些要點(diǎn)能有效避免常見錯(cuò)誤。

      為什麼要將標(biāo)籤放在的底部? 為什麼要將標(biāo)籤放在的底部? Jul 02, 2025 am 01:22 AM

      PlacingtagsatthebottomofablogpostorwebpageservespracticalpurposesforSEO,userexperience,anddesign.1.IthelpswithSEObyallowingsearchenginestoaccesskeyword-relevanttagswithoutclutteringthemaincontent.2.Itimprovesuserexperiencebykeepingthefocusonthearticl

      JavaScript與Java:開發(fā)人員的全面比較 JavaScript與Java:開發(fā)人員的全面比較 Jun 20, 2025 am 12:21 AM

      JavaScriptIspreferredforredforwebdevelverment,而Javaisbetterforlarge-ScalebackendsystystemsandSandAndRoidApps.1)JavascriptexcelcelsincreatingInteractiveWebexperienceswebexperienceswithitswithitsdynamicnnamicnnamicnnamicnnamicnemicnemicnemicnemicnemicnemicnemicnemicnddommanipulation.2)

      JavaScript:探索用於高效編碼的數(shù)據(jù)類型 JavaScript:探索用於高效編碼的數(shù)據(jù)類型 Jun 20, 2025 am 12:46 AM

      javascripthassevenfundaMentalDatatypes:數(shù)字,弦,布爾值,未定義,null,object和symbol.1)numberSeadUble-eaduble-ecisionFormat,forwidevaluerangesbutbecautious.2)

      什麼是在DOM中冒泡和捕獲的事件? 什麼是在DOM中冒泡和捕獲的事件? Jul 02, 2025 am 01:19 AM

      事件捕獲和冒泡是DOM中事件傳播的兩個(gè)階段,捕獲是從頂層向下到目標(biāo)元素,冒泡是從目標(biāo)元素向上傳播到頂層。 1.事件捕獲通過addEventListener的useCapture參數(shù)設(shè)為true實(shí)現(xiàn);2.事件冒泡是默認(rèn)行為,useCapture設(shè)為false或省略;3.可使用event.stopPropagation()阻止事件傳播;4.冒泡支持事件委託,提高動(dòng)態(tài)內(nèi)容處理效率;5.捕獲可用於提前攔截事件,如日誌記錄或錯(cuò)誤處理。了解這兩個(gè)階段有助於精確控制JavaScript響應(yīng)用戶操作的時(shí)機(jī)和方式。

      Java和JavaScript有什麼區(qū)別? Java和JavaScript有什麼區(qū)別? Jun 17, 2025 am 09:17 AM

      Java和JavaScript是不同的編程語言。 1.Java是靜態(tài)類型、編譯型語言,適用於企業(yè)應(yīng)用和大型系統(tǒng)。 2.JavaScript是動(dòng)態(tài)類型、解釋型語言,主要用於網(wǎng)頁交互和前端開發(fā)。

      See all articles