0

Basically the question is what happens if you try to assign the same value to the(state) variable:

  address someAddress = 0x...abcdef;
   
   function setSomeAddress(address _input) public {
     //What happens if someAddress already equals to _input?
     someAddress = _input;
   } 

What happens if you then call setSomeAddress(0x...abcdef);

Does the transaction go through and nothing changes or...?

1 Answer 1

1

That's a perfectly valid assignment, so the transaction will go through. Simply nothing changes in the contract state.

The only small question is whether you need to pay gas for storing the value. I guess the optimizer may be smart enough to optimize that line out if the value doesn't change, but I really don't know. My guess is that at least at the bytecode level you would need to pay for the assignment, even if you set the same value.

1
  • _input is a variable, so anyway the transaction will be executed and gas will be spent. I believe that dealing conditionally does not lower the cost of gas. Maybe a little, as it would avoid messing with the storage, even if the logical steps are longer, but I don't think it would be worth it. Feb 12 at 21:21

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.