How to access member functions of smart contract using web3

0 votes

I have deployed the following smart contract:

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 I have written a Javascript to interact with the member functions of the smart contract.

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


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

But its not working. Any idea how can I make this work?

Aug 13, 2018 in Blockchain by digger
• 26,740 points
1,605 views

1 answer to this question.

0 votes

I have implemented a similar feature and the code I have used is as follows:


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

I have run it on ropsten network and it works.

answered Aug 13, 2018 by slayer
• 29,370 points

Related Questions In Blockchain

0 votes
1 answer
0 votes
2 answers

how save data from different account using web3.py to smart contract

Hey, @Amal, It will automatically take the address ...READ MORE

answered Jul 28, 2020 in Blockchain by Rajiv
• 8,870 points
1,542 views
0 votes
1 answer

How do I send back ethers to the sender of the tokens in a smart contract?

Whenever a smart contract receives ether via ...READ MORE

answered May 30, 2018 in Blockchain by Perry
• 17,100 points
3,624 views
0 votes
1 answer

How to limit execution of smart contract only to my dapp?

You can use modifiers to do it ...READ MORE

answered Jul 17, 2018 in Blockchain by digger
• 26,740 points
671 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,943 views
0 votes
1 answer
+1 vote
3 answers

Removing double quotes from a string from JSON response in PHP

Just remove the json_encode call, and it should work: $resp ...READ MORE

answered Sep 12, 2018 in Blockchain by digger
• 26,740 points
45,296 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,469 views
0 votes
1 answer

How to store photos in Hyperledger smart contract?

To do this, you need to represent ...READ MORE

answered Jul 9, 2018 in Blockchain by slayer
• 29,370 points
860 views
0 votes
1 answer

How to call external data using smart contracts?

Smart contracts can not directly call external ...READ MORE

answered Jul 16, 2018 in Blockchain by slayer
• 29,370 points
1,698 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP