简体   繁体   中英

Accessing smart contract member functions through Web3

I have the following smart contract deployed on Ganache (migrated through 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;
    }
}

and have been writing a web UI for it. However, in my UI's javascript code I keep getting the error "Invalid address" when I call the getter from the public mapping called notes .

My JS code:

$(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'

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

I have made sure I have assigned my default account to web3.eth.accounts[0], so what could be cause of this issue?

Сan I see the complete JS code? I think, error in several lines before code shown by you.

I checked in the Ropsten test network - everything works fine

My code:

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

Error "Invalid address" appears in case of an incorrect address in the line:

const contract_address = '0x***'

If the wrong address were in the line

myContract.notes('0x***', function(error, result){

You would have seen an error "Uncaught Error: new BigNumber() not a base 16 number:"

UP

You have lost quotes in line: var Note = contractABI.at(0xea449D80E775607612Cc6d5ae9232EA10e417Ec1);

It will be correct: var Note = contractABI.at('0xea449D80E775607612Cc6d5ae9232EA10e417Ec1');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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