简体   繁体   English

如何修复无效地址错误? 带有 MetaMask 的 Web3 JS

[英]How to fix invalid address Error? Web3 JS with MetaMask

I deployed my contract to Goerli test net.我将合约部署到 Goerli 测试网。 Deploying was successful.部署成功。 And I tested it in Remix.我在 Remix 中对其进行了测试。

Contract code:合约代码:

pragma solidity ^0.5.0;

contract Test{
    
    string private str;
    
    constructor() public {
        str = " ";
    }
    
    function getStr() public view returns(string memory  _str){
        _str = str;
    }
    
    function setStr(string memory  _str) public{
        str = _str;
    }
}

Then I tried to make a web interface using web3 and MetaMask.然后我尝试使用 web3 和 MetaMask 制作一个 web 接口。

<script>
        ethereum.enable()
        var abi = [
    {
        "constant": false,
        "inputs": [
            {
                "internalType": "string",
                "name": "_str",
                "type": "string"
            }
        ],
        "name": "setStr",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "inputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "constructor"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "getStr",
        "outputs": [
            {
                "internalType": "string",
                "name": "_str",
                "type": "string"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    }
    ];
        var address = 0x4316d047388e61EBC3Ed34DFf4cEE215840decDa;
        var myContract = web3.eth.contract(abi);
        var TestC = myContract.at(address);
        console.log(TestC);
    var x = TestC.getStr();
</script>

And when I ran it, it returned:当我运行它时,它返回:

Uncaught Error: invalid address未捕获的错误:地址无效

at c (inpage.js:1)在 c (inpage.js:1)

at inputCallFormatter (inpage.js:1)在 inputCallFormatter (inpage.js:1)

at inpage.js:1在 inpage.js:1

at Array.map ()在 Array.map ()

at o.formatInput (inpage.js:1)在 o.formatInput (inpage.js:1)

at o.toPayload (inpage.js:1)在 o.toPayload (inpage.js:1)

at we [as call] (inpage.js:1)在我们 [as call] (inpage.js:1)

at u.call (inpage.js:1)在 u.call (inpage.js:1)

at u.execute (inpage.js:1)在 u.execute (inpage.js:1)

at (index):46在(索引):46

What I need to do?我需要做什么?

Your variable address should be a hex string, not a literal hex number - even though it is eventually turned into a number by the ethereum node.你的变量address应该是一个十六进制字符串,而不是一个十六进制数字——即使它最终被以太坊节点变成了一个数字。

Try this instead:试试这个:

        var address = '0x4316d047388e61EBC3Ed34DFf4cEE215840decDa';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM