簡體   English   中英

如何修復無效地址錯誤? 帶有 MetaMask 的 Web3 JS

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

我將合約部署到 Goerli 測試網。 部署成功。 我在 Remix 中對其進行了測試。

合約代碼:

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;
    }
}

然后我嘗試使用 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>

當我運行它時,它返回:

未捕獲的錯誤:地址無效

在 c (inpage.js:1)

在 inputCallFormatter (inpage.js:1)

在 inpage.js:1

在 Array.map ()

在 o.formatInput (inpage.js:1)

在 o.toPayload (inpage.js:1)

在我們 [as call] (inpage.js:1)

在 u.call (inpage.js:1)

在 u.execute (inpage.js:1)

在(索引):46

我需要做什么?

你的變量address應該是一個十六進制字符串,而不是一個十六進制數字——即使它最終被以太坊節點變成了一個數字。

試試這個:

        var address = '0x4316d047388e61EBC3Ed34DFf4cEE215840decDa';

暫無
暫無

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

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