3

I'm implementing an ERC721 contract and I want to execute the following logic.

This is what I want: If the user pass to a method an address, return that address, if not, return the user's address.

I have been playing in Remix and I have this method:

 function showsender(address passsedAdd) public view returns(address) {
     address addToReturn;

     if(passsedAdd==address(0)) {
        addToReturn = msg.sender;
     }
     else {
         addToReturn = passsedAdd;
     }

     return addToReturn;
 }

However, I could not make it work as I want since it looks like in Solidity arguments are NOT optional.

So, running testS... If I pass the arg, example:

console.log(
  await this.contract.showsender(
    '0x14723A09ACff6D2A60DcdF7aA4AFf308FDDC160C'
  )
);

Works, but if I try that, in case the user is not explicitly passing an argument, it doesn´t: this.contract.showsender();

I'm totally new to Solidity. Can anyone help?

  • "it doesn't work" isn't a good description of a problem. If you can tell us what you tried, what happened, and what you expected to happen instead, we can help. But the code does exactly what it says it does. – smarx Jan 2 at 19:57
  • The latter (this.contract.showsender()) is invalid, even if it's allowed. The function takes a parameter, and you haven't passed one. Try this.contract.showsender("0x000000..."). – smarx Jan 2 at 23:44
  • As has been mentioned a few times, you can't have optional parameters. But the solution to your problem may best be left outside Solidity, and dealt with on the front-end. It may be simpler (and cheaper for users!) if you do this logic all on the front-end and then just pass the appropriate address to your contract function. I don't know the specifics of what you're trying to implement but it's always a good idea to ask "does this logic need to be done on-chain?" – AnAllergyToAnalogy Jan 3 at 0:00
1

You can define a function without address:

function showSender() public view returns (address)
{
     return(msg.sender);
 }

AND A SECOND FUNCTION WITH THE SAME NAME with address

 function showSender(address passedAddress) public view returns (address)
  {
     return(passedAddress);
   }

They can be at the same time in the same contract without problems or interference. Like this:

contract MyContract {

function showSender() public view returns (address)
{
     return(msg.sender);
 }


 function showSender(address passedAddress) public view returns (address)
  {
     return(passedAddress);
   }

}

When your user is calling MyContract.showSender without arguments he hits the first, with address passed he hits the second.

Remain to understand how to generate such calls with/without address passed, but this is another story... 😉

0

Solidity does not have optional parameters, so the user would have to explicitly pass in 0x00... to trigger addToReturn = msg.sender;. I am not sure this is what you were thinking.

You code should work, but can be optimized. Try the following:

 function showsender(address passsedAdd) public view returns(address) {

     if(passsedAdd==msg.sender) {
        return msg.sender
     }

     return passsedAdd;
 }
0

As Shane said, function parameters aren't optional although there is overloading you might consider if the following doesn't suit your use-case.

You could do it like this:

function showSender(address passedAddr) public view returns(address) {
  if(passedAddr == address(0) return msg.sender;
  return passedAddr;
}

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.