微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

智能合约未通过网站致电

如何解决智能合约未通过网站致电

我有一个网站,将输入内容放入对象,然后将其放置在以太坊区块链上。但是,即使我创建了合同,也不会打电话吗?有人知道为什么吗?我使用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": "","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);
}

更多详细信息:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。