20 各位大佬,我这段代码连不上metamask有什么不对的地方吗?

网上找了一段代码准备试着运行一下,但是在本地搭建访问之后metamask没有弹出来。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"></script>
</head>
<body>
</body>
<script>
    // 获取web3实例
    if (typeof web3 !== 'undefined') {
        web3 = new Web3(web3.currentProvider);
    } else {
        web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
    }
    // 检验链接否成功
    if(!web3.isConnected()) {
        console.log("连接失败!!")
    } else {
        console.log("连接成功!!")
        // 获取账号列表
        var accounts = web3.eth.accounts;
        console.log(accounts)
        // 设置默认账号
        web3.eth.defaultAccount = accounts[0];
        // 获取账号余额
        var balance = web3.eth.getBalance(accounts[0]);
        console.log(balance.toString())
        //单位转换
        var value = web3.fromWei(balance,"ether");
        console.log(value.toString())
        // 判断地址是否正确
        var isAddress = web3.isAddress(accounts[1]);
        console.log(isAddress);
        //发送交易
        web3.eth.sendTransaction({
            from : accounts[0],
            to : accounts[1],
            value: web3.toWei(1,"ether")
        }, function(err, transactionHash) {
            if (!err){
                console.log(transactionHash)
            }else{
                console.log(err)
            }
        });
        //获取交易信息
        var transaction = web3.eth.getTransaction('0x5455fe5ac45a95bdf608573ee1f082c2f48b3539cdec0ed8d18a8201d876a30c');
        console.log(transaction);
        // 智能合约交互
        var contractAddress="0xba8c13eb6ebe2da349dd3786d10a85e75e5122a7";
        var contractABI = [
            {
                "constant": true,
                "inputs": [],
                "name": "getMsg",
                "outputs": [
                    {
                        "name": "",
                        "type": "string"
                    }
                ],
                "payable": false,
                "stateMutability": "view",
                "type": "function"
            },
            {
                "constant": true,
                "inputs": [],
                "name": "str",
                "outputs": [
                    {
                        "name": "",
                        "type": "string"
                    }
                ],
                "payable": false,
                "stateMutability": "view",
                "type": "function"
            },
            {
                "constant": false,
                "inputs": [
                    {
                        "name": "_str",
                        "type": "string"
                    }
                ],
                "name": "setMsg",
                "outputs": [
                    {
                        "name": "",
                        "type": "bool"
                    }
                ],
                "payable": false,
                "stateMutability": "nonpayable",
                "type": "function"
            }
        ];
        // 获取合约实例
        var contract = web3.eth.contract(contractABI).at(contractAddress);
        //调用合约方法
        var result = contract.getMsg();
        console.log(result)
        var result1 = contract.setMsg("update message");
        console.log(result1)
    }
</script>
</html>
请先 登录 后评论

最佳答案 2021-10-11 15:04

需要先进行授权

if (window.ethereum) {
      App.web3Provider = window.ethereum;
      try {
        // Request account access
        await window.ethereum.enable();
      } catch (error) {
        // User denied account access...
        console.error("User denied account access")
      }
    }

可以看看这里

请先 登录 后评论

其它 0 个回答