0

I'm starting to read the Ethereum ressources and started with this article. I'm struggling to understand what the fallback will do in the following contract:

pragma solidity ^0.6.4;

contract MyContract {
  event Log(address addr);

  fallback() external payable{
    emit Log(address(this));
  }

  function add(uint256 a, uint256 b) public pure returns (uint256) {
    return a + b;
  }
}

I understand what an event is. From the documentation:"this(current contract’s type):the current contract, explicitly convertible to Address". My understanding is that the fallback will always log the same address (the address of MyContract). Is it correct ?

3
  • Yes............... Apr 1, 2020 at 7:13
  • Thank you for your reply (it doesn't change the anser but I wrongly updated the contract; it shouldn't be a falllback function but a constructor.)
    – Caladay
    Apr 1, 2020 at 7:34
  • Same answer. The expression address(this) will always evaluate to the same value in every function of a given instance of the contract (i.e., if you call the same function but from different instances of the contract, you will get different results). Apr 1, 2020 at 7:40

1 Answer 1

1

The expression address(this) will evaluate to the same value in every function which uses it, when called on a given instance of the contract.

Of course, when such function is called on different instances of the contract, this expression will evaluate to a different value for each instance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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