Search code examples
blockchainethereumsolidity

Retrieve argument pass to constructor


i'm brand new in solidity. I've deployed my first little contract ERC20 with truffle. With the web3 package on python, i've explored the transaction of this creation, and i was seeking of the arguments i passed to the constructor (name, initialsupply ...), but i can't find them. Is there a way to find this argument in the blockchain ? Are they hide in the hash of the input ?

Thanks for your help.


Solution

  • Constructor arguments are encoded at the end of the deployed bytecode.


    Example:

    pragma solidity ^0.8;
    
    contract MyContract {
        constructor(uint256 _foo) {
        }
    }
    

    Compiled bytecode:

    0x6080604052348015600f57600080fd5b5060405160dc38038060dc8339818101604052810190
    602d91906045565b506090565b600081519050603f81607c565b92915050565b60006020828403
    121560585760576077565b5b60006064848285016032565b91505092915050565b600081905091
    9050565b600080fd5b608381606d565b8114608d57600080fd5b50565b603f80609d6000396000
    f3fe6080604052600080fdfea264697066735822122003a184614c306711c11c8169439e473b01
    fb186c8094644efc80975e3fe2660964736f6c63430008060033
    

    Deployed bytecode:

    0x6080604052348015600f57600080fd5b5060405160dc38038060dc8339818101604052810190
    602d91906045565b506090565b600081519050603f81607c565b92915050565b60006020828403
    121560585760576077565b5b60006064848285016032565b91505092915050565b600081905091
    9050565b600080fd5b608381606d565b8114608d57600080fd5b50565b603f80609d6000396000
    f3fe6080604052600080fdfea264697066735822122003a184614c306711c11c8169439e473b01
    fb186c8094644efc80975e3fe2660964736f6c63430008060033
    000000000000000000000000000000000000000000000000000000000001e240
    

    Where the 1e240 is hex value of decimal 123456. The length of 64 hex characters (including the leading zeros) is because of the length of the 256 bit (64 hex chars) uint datatype.