通过Web3访问智能合约成员函数

Accessing smart contract member functions through Web3

我在 Ganache 上部署了以下智能合约(通过 Truffle 迁移):

pragma solidity ^0.4.24;

contract Logistics{
    address public owner = msg.sender;
    mapping(address => string) public notes;

    function sign(string note) public {
        require(msg.sender == owner);
        notes[owner] = note;
    }

    function transferOwnership(address newOwner) public {
        require(msg.sender == owner);
        owner = newOwner;
    }
}

并一直在为它编写网页 UI。但是,在我的 UI 的 javascript 代码中,当我从名为 的 public 映射中调用 getter 时,我不断收到错误 "Invalid address"笔记.

我的JS代码:

$(document).ready(function() {///////////////////////////


let setUp = new Promise(function(resolve, reject){ 
        if (typeof web3 !== 'undefined') {
            console.log('Web3 Detected! ' + web3.currentProvider.constructor.name)
            window.web3 = new Web3(web3.currentProvider);
            console.log("Web3 initialized!");
            resolve('done');
        } 
        else {
            console.log('No Web3 Detected... using HTTP Provider')
            window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"));
            console.log("Web3 initialized!");
            resolve('done');
        }
});

setUp.then(function(){ //After setup above^

web3.eth.defaultAccount = web3.eth.accounts[0]; //current metamask account
console.log("The defaultAccount is: " + web3.eth.defaultAccount);


var contractABI = web3.eth.contract([
    {
        "constant": false,
        "inputs": [
            {
                "name": "note",
                "type": "string"
            }
        ],
        "name": "sign",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "owner",
        "outputs": [
            {
                "name": "",
                "type": "address"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [
            {
                "name": "",
                "type": "address"
            }
        ],
        "name": "notes",
        "outputs": [
            {
                "name": "",
                "type": "string"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": false,
        "inputs": [
            {
                "name": "newOwner",
                "type": "address"
            }
        ],
        "name": "transferOwnership",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    }
]);

var Note = contractABI.at(0xea449D80E775607612Cc6d5ae9232EA10e417Ec1);


$('#viewButton').click(function(){
    if ($('#viewInput').val()){ //if text input is populated

        Note.notes("0xf35f06208aCcaCF3FaF678df88A76142b923408e", function(err, res){
            if(!err){
                alert(res);

            } else{
                console.log("Error fetching information from given address");
            }

        });
    }

});



}); //initial load promises 'then'

});/////////////////////////////////////////////////////

我确定我已将我的默认帐户分配给 web3.eth.accounts[0],那么这个问题可能是什么原因造成的?

我能看到完整的 JS 代码吗?我认为,您显示的代码之前有几行错误。

我检查了 Ropsten 测试网络 - 一切正常

我的代码:

<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"></script>
<script>
        if (typeof web3 !== 'undefined') {
           web3 = new Web3(web3.currentProvider);
        } else {
           web3 = new Web3(new Web3.providers.HttpProvider("https://api.myetherwallet.com/rop"));
        }
        const abi = [{"constant":false,"inputs":[{"name":"note","type":"string"}],"name":"sign","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"notes","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]


        const contract_address = '0x***'

        var myContract = web3.eth.contract(abi).at(contract_address);

        myContract.notes('0x***', function(error, result){
               if(!error)
                   console.log(result);
               else
                   console.error(error);
        });
</script>

如果行中的地址不正确,将出现错误 "Invalid address":

const contract_address = '0x***'

如果错误的地址在行中

myContract.notes('0x***', 函数(错误, 结果){

你会看到一个错误 "Uncaught Error: new BigNumber() not a base 16 number:"

UP

您丢失了行中的引号: var 注 = contractABI.at(0xea449D80E775607612Cc6d5ae9232EA10e417Ec1);

会是正确的:var Note = contractABI.at('0xea449D80E775607612Cc6d5ae9232EA10e417Ec1');