usage of this.

Example

function unwrap(uint256 amount) external {
    require(this.balanceOf(msg.sender) >= amount, "Not Enough WRings to unwrap");
    
    burn(msg.sender, amount);
    iRings.transferFrom(address(this), msg.sender, amount);
    emit Unwrapped(msg.sender, amount);
}
  • Using this to call functions in the same contract makes the call an external call.

  • That means an extra 2100 gas, and rewriting the msg.* variables.

  • better off not using this.* until you find the very few use cases.

Note if you drop this :

  • require(balanceOf(msg.sender) >= amount, "Not Enough WRings to unwrap"

  • it will not work

That is because balanceOf is external, to stop developers using suboptimal (in terms of gas) patterns.

  • since the contract inherits ERC20, balanceOf is native to itself; contract cannot call its own external function,

Solution:

You should access the _balanceOf mapping instead. It is internal

Last updated