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?

Peter
  • 131
  • 2
  • "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. – user19510 Jan 02 '19 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..."). – user19510 Jan 02 '19 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 03 '19 at 00:00

4 Answers4

3

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

Rick Park
  • 3,194
  • 2
  • 8
  • 25
1

if user don't passed any address while calling your solidity function, web3/ether js/py will give Error: invalid address. there is no optional parameters in solidity. means user have to pass it.

aakash4dev
  • 691
  • 3
  • 11
1

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.

Rob Hitchens
  • 55,151
  • 11
  • 89
  • 145
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;
 }
Shane Fontaine
  • 18,036
  • 20
  • 54
  • 82