Back

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

I want to create a viewable function (needs to return a string to the user) that searches a mapping for msg.sender and if the senders value is x, I want the contract to proceed accordingly. It all does work inside remix but if I upload it to ropsten, it doesn't anymore. Is this a known issue? I have tried tx.origin as well, same result. That's the problematic code I tried:

function getLink() public view returns(string){
    if(tokenBalances[msg.sender]>0){
        return link;
    }else{
        return "You need to purchase a token at first...";
    }
}

EDIT: I think the problem is, that when using a viewable function there is no msg.sender because there is no actual transaction? Is there a way to return a value to the user without using the "view" functions?

1 Answer

0 votes
by (14.4k points)

Although it is useless as an authorization scheme, msg.sender does work in a view function. Hence, the lookup tool you use should have a mechanism to set the sender configuration. 

Setting the Sender 

It depends on what tool you are using to invoke. That tool might be any from the following: web3.js, web3.py, Mist, MyEtherWallet, MyCrypto, etc. They may or may not have a mechanism to set the sender in a call.

Contract Workarounds

It is not possible to help a user to set the sender from inside the contract. What you can do is supply a different method that takes an address as an argument. Then tools like MyEtherWallet will allow you to set the address of interest. For instance, 

function getLink(address account) public view returns(string){

    if(tokenBalances[account] > 0){

        return link;

    }else{

        return "You need to purchase a token at first...";

    }

}

Browse Categories

...