73

I am attempting to use 2 contracts together, and I need my 2nd contract to pass his own address to the first contract.

Is there an easy way to do this? Like this.address?

Or should I update my contract own address into a variable?

0

3 Answers 3

108

Solidity v5 Update

As of Solidity v5, this become deprecated. You now have to use address(this).

Solidity v4 and older

Get contract address in Solidity

Short answer: The global variable this is the contract address.

Long answer: this is a variable representing the current contract. Its type is the type of the contract. Since any contract type basically inherits from the address type, this is always convertible to address and in this case contains its own address.

38

I tried this and this.address but found the following works:

function transferTo(address receiver, uint amount) {
    if (   amount == 0
        || receiver == address(this) )
        return;
4
  • Thank you, I was stuck because of this issue from a couple of hours. You trick helped a lot.
    – Sanchit
    Jul 3, 2017 at 22:27
  • What is the difference between address(this) and just this ?
    – mesqueeb
    Oct 22, 2018 at 7:28
  • 4
    this is a deprecated way to get the address of the contract. As of Solidity 0.4.24 at least, address(this) is the only way that works. Oct 25, 2018 at 18:25
  • Yep used address(this) going forward.
    – M H
    Feb 12, 2019 at 22:16
1

If you're looking for an assembly (yul) answer:

contract ContractAddress {

    address public disAddress;

    constructor() public {
        assembly {
            sstore(disAddress_slot, address)
        }
    }
}

Notice the address on the 7th line? That refers to the contract's own address. Test it yourself in Remix.

Your Answer

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

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