0

Reference - http://solidity.readthedocs.io/en/develop/assembly.html

Objective - example of a function returning the contract address.

Code -

pragma solidity ^0.4.18;

contract d4{
    function showAddress() view public returns (address addr_v) {
        assembly {
            addr_v := mload(0x30)
        }
    }
}

This compiled but just returns 0.

I used this link for the opcodes - https://github.com/ethereum/pyethereum/blob/develop/ethereum/opcodes.py

Has anyone a code sample of using opcodes to determine the contract address?

2

Per https://solidity.readthedocs.io/en/v0.4.20/julia.html, this() should work.

(Of course, so should return address(this) without assembly, and there's no way to call the function without knowing the address already, so it's not clear to me what your needs are.)

  • I am only testing opcodes. My interest initially was in the context of libraries. I read about delegatecalls and how there were security issues. So I wanted to understand more how the assembly worked. – Trevor Lee Oakley Mar 11 '18 at 10:02
  • You'll have to write actual assembly to test the opcodes you linked to. Solidity's inline assembly is a bit higher level. – user19510 Mar 11 '18 at 10:04
  • I guessed it was harder then solidity. I tried googling this but I found little information. – Trevor Lee Oakley Mar 11 '18 at 10:05
0
function showAddress() view public returns (address addr_v) {
    assembly {
        addr_v := address()
    }
}

See documentation for details.

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.