簡體   English   中英

智能合約不是通過網站調用

[英]Smart Contract is not calling through a website

我有一個網站,它將輸入放入一個對象,然后將它放在以太坊區塊鏈上。 但是,即使我創建了合同,它也沒有調用? 有人知道為什么嗎? 我使用 web3、infura、metamask 和 remix。 每當我調用 remixContract.methods.setMessage() 時,該函數都會出現在我的控制台中,但我沒有收到來自 metamask 的確認請求

 // Connect a the web3 provider if (typeof web3 !== 'undefined') { web3 = new Web3(web3.currentProvider); } else { web3 = new Web3(new Web3.providers.HttpProvider("INFURA-LINK")); } // Set a default account web3.eth.defaultAccount = 'MY-ACOUNT'; // Get the contract address var RemixContract = new web3.eth.Contract([ { "constant": false, "inputs": [ { "name": "x", "type": "string" } ], "name": "setMessage", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "getMessage", "outputs": [ { "name": "", "type": "string" } ], "payable": false, "stateMutability": "view", "type": "function" } ]); // Get the contract abi RemixContract.options.address = 'CONTRACT-ADDRESS'; console.log(RemixContract); //data object class Data{ constructor(name, school, subject, teacher){ this.name = name this.school = school this.subject = subject this.teacher = teacher } } $("#submit").click(function() { let hashable = new Data($('#name').val(), $('#email').val(), $('#age').val(), $('#tel').val()) let certificateData = JSON.stringify(hashable) RemixContract.methods.setMessage(certificateData) console.log(certificateData); });
 #header{ background-color: darkblue; width: 100%; height: 100px; vertical-align: middle; color: white; } body{ margin: 0px; font-family: Arial, Helvetica, sans-serif; } #navbar{ margin: 0px; list-style: none; } .navbut{ background-color: white; color: darkblue;; float: right; padding: 10px; margin: 30px; border-radius: 10px; text-decoration: none; } #homebutton{ width: 50px; height: 50px; float: left; margin: 20px; } h1{ margin: 20px; margin-top: 30px; float: left; } #box{ border: 1px solid black; width: 80%; margin-left: 10%; margin-top: 5%; background-color: darkblue; color: white; border-radius: 10px; } #submit{ padding: 10px; margin-top: 50px; } span{ color: red; } #margin{ margin: 10px; }
 <!DOCTYPE html> <html> <head> <title>Certificates Online</title> <meta charset="UTF-8"> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script> </head> <body> <div id='box'> <div id="margin"> <label for="name">Full Name: </label> <input type="text" name="name" id="name" required></input> <br><br> <label for="email">Email: </label> <input type="text" name="email" id="email" required></input> <br><br> <label for="age">Age: </label> <input type="text" name="age" id="age" required></input> <br><br> <label for="tel">Telephone: </label> <input type="text" name="tel" id="tel" required></input> <br><br> <button id="submit">Submit</button> </div> </div> </body> <script src="dataHandling.js"></script> </html>

請注意,新的 API 可用:

// Legacy dapp browsers...
if (window.web3 !== undefined) {
    const provider = new Web3.providers.HttpProvider(window.web3.currentProvider);
    const web3 = new Web3(provider);
}

// Modern dapp browsers...
if (window.ethereum !== undefined) {
  const provider = new Web3.providers.HttpProvider(window.ethereum);
  const web3 = new Web3(provider);
}

更多細節:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM