Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Blockchain by (4.1k points)

not sure if I should post this on the Ethereum SE but I have a feeling it is more javascript related, so I'll try here:

I have a very simple smart contract that consists of essentially just a getNum function and a setNum function. The smart contract can be viewed here: https://pastebin.com/ci6mbPDq

I am trying to construct a simple frontend to call it. Essentially, I follow this guide. A working codepen of my frontend (demonstrating the janky functionality) can be found here: https://codepen.io/heh/pen/PeMmKe As you can see in my codepen, I call my getNum function like:

BasicToken.getNum(0x64319ca297239d8652a0b5f0f12dd6666cb0e05b,

        function(error, result)
        {
            console.log(result.toNumber());
            document.getElementById("target").innerText = result.toNumber();

        }
    );


However, I keep getting "0" as the result. On the other hand, my setNum function is able to post a result to the Ropsten blockchain. However, I notice that both function calls seem to be firing off their callback instantly.

Could anyone help me figure out why the function calls return instantly?

Thanks!

1 Answer

0 votes
by (14.4k points)
edited by

The function calls return instantly because an actual address (in the form of String) is not being sent. Rather, a number is sent. Moreover, you are getting ‘0’ because the address that you are sending is invalid and on top of that it does not exist in the mapping. 

And when this happens, the default value for uint variables are returned. The default value is zero. 

If you want to make sure that only the current user can retrieve their own value, and other users cannot retrieve that particular set of data, then you should use msg.sender and at the same, you should also let go of the function parameter. Your code should look like: 

function getNum() public view returns (uint) {

   return map23[msg.sender];

}

Give yourself time to explore the Blockchain field properly. Enroll in Blockchain Online Training now.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...