33

What is the specification of the 'this' keyword in Solidity? How does it work?

Sample code from Solidity Features · ethereum/wiki Wiki

contract Helper {
  function getBalance() returns (uint bal) {
    return this.balance; // balance is "inherited" from the address type
  }
}

3 Answers 3

32

According to this guide:

Contracts Inherit all Members from Address.

It means that this is the pointer to the current instance of the type derived from Address (in your case - current instance of Helper), and balance is a member of Address.

It helps you distinguish between contract's own balance and any other balance, like in this example:

address payable x = 0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF;
if (address(x).balance < address(this).balance) 
    address(x).transfer(10);

And here is a some kind of definition (from this doc):

this: the current contract, explicitly convertible to address

4
  • 4
    why is this necessary? Can't someone just say if(x.balance < balance)? Commented Jun 15, 2017 at 4:04
  • 1
    this (current contract’s type): the current contract, explicitly convertible to Address. This is from the solidity docs. Which means that if you want to access members of the address type (e.g. balance) of the current contract instance, then you can simply use this. In your example, balance will try to access a variable from the contract's storage (i.e. contract public property)
    – ppoliani
    Commented Dec 19, 2017 at 15:18
  • 6
    this.balance is deprecated, the correct way to get the balance of the contracts is through address(this).balance Commented Jun 30, 2018 at 12:50
  • 1
    does simply writing this; in a contract do anything?
    – Ayush
    Commented Feb 25, 2021 at 21:06
6

Contract types are implicitly convertible to address and every contract inherits all members of "address" and overwrites them if necessary. If they are overwritten, the members of address can still be accessed via conversion to address:

address(myContract).send(10). 

This also has the effect that all members of address can be accessed without prefix, i.e. uint x = this.balance; // access the balance parameter of the current contract.

so to summarize "This" represents the current contract instance.

2
  • 1
    sometimes I see in ERC20 contracts balanceOf(this)
    – rstormsf
    Commented Jun 22, 2017 at 0:48
  • balanceOf(this) is just a function to get the balance of ERC20 token for the currrent contract, however address(this).balance is the balance of Ether for a current contract. Commented Jun 30, 2018 at 12:48
3

These answers are good but they're not correct.

pragma solidity 4.24;
contract my contract {
function example() public view returns (address) {
      return this;
}
}

What do you get? You get an address. The address of the contract.

It is NOT a state dependent variable or a use case of scope like in javascript, where you can use this to pull the global state into a callback.

It is NOT just an address, it can be viewed as a class variable which gives access to everything the contract has access to, think of it like a key but honestly I only use it to reference to it's own address.

For future reference, wrap it as such,

address(this)

To properly utilize.

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.