\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òu)建一個 Web 前端!這是我之前關(guān)于使用 Solidity 和 Hardhat 創(chuàng)建簡單智能合約的文章的后續(xù)內(nèi)容。這里的說明假設(shè)您正在使用我們剛剛部署到 Hardhat 環(huán)境的相同合約。

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

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

      Metamask / Web3 瀏覽器

      1. 安裝 Metamask。我將使用此處找到的 Chrome 擴(kuò)展程序。如果您是 Chrome 用戶,現(xiàn)在您可以查看 web3 內(nèi)容并與之交互。

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

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

        WebI For Simple Smart Contract

        點(diǎn)擊“添加自定義網(wǎng)絡(luò)”。填寫網(wǎng)絡(luò)名稱,例如“Hardhat”,網(wǎng)絡(luò) RPC URL 以及 Hardhat 節(jié)點(diǎn)的 IP 地址和端口,如果您在本地運(yùn)行,可能是這樣的:
        http://127.0.0.1:8545/

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

        現(xiàn)在切換到我們剛剛在 Metamask 插件中添加的 Hardhat 網(wǎng)絡(luò)。在監(jiān)控正在運(yùn)行的 Hardhat 節(jié)點(diǎn)的終端中,您應(yīng)該會在錢包連接時看到一些活動。

      3. 由于您的 Metamask 錢包當(dāng)前沒有任何(假)ETH,所以我們發(fā)送一些。從 Metamask 獲取您的公共地址(在 Metamask 窗口頂部的錢包名稱下方,單擊復(fù)制按鈕)。從運(yùn)行 Hardhat 控制臺的終端窗口,執(zhí)行:

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

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

      創(chuàng)建 Web3 網(wǎng)頁

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

        我們需要上一篇文章中部署合約時的一些東西。請參閱上一篇文章,并從工件目錄中獲取 合約地址 和合約的 ABI JSON 數(shù)組。我們不需要該文件中的其余 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)文章!

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

      熱AI工具

      Undress AI Tool

      Undress AI Tool

      免費(fèi)脫衣服圖片

      Undresser.AI Undress

      Undresser.AI Undress

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

      AI Clothes Remover

      AI Clothes Remover

      用于從照片中去除衣服的在線人工智能工具。

      Clothoff.io

      Clothoff.io

      AI脫衣機(jī)

      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è)和移動應(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中與日期和時間合作? 如何在JS中與日期和時間合作? Jul 01, 2025 am 01:27 AM

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

      為什么要將標(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中事件傳播的兩個階段,捕獲是從頂層向下到目標(biāo)元素,冒泡是從目標(biāo)元素向上傳播到頂層。1.事件捕獲通過addEventListener的useCapture參數(shù)設(shè)為true實(shí)現(xiàn);2.事件冒泡是默認(rèn)行為,useCapture設(shè)為false或省略;3.可使用event.stopPropagation()阻止事件傳播;4.冒泡支持事件委托,提高動態(tài)內(nèi)容處理效率;5.捕獲可用于提前攔截事件,如日志記錄或錯誤處理。了解這兩個階段有助于精確控制JavaScript響應(yīng)用戶操作的時機(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是動態(tài)類型、解釋型語言,主要用于網(wǎng)頁交互和前端開發(fā)。

      See all articles