When we create a contract, we always send a transaction to a empty account and with contract bytecode in input data.

But sometime we need to call the constructor. So some params must be sent with the bytecode.

Which rule does those data assemble together and how can i parse it ?

If this transaction is a normal transaction, we can parse input data using web3. But this kind of transaction can be a little bit more complicated.

  • 1
    you wanted to read the input data used for when a certain contract was created, correct? Did you manage to do it? E.g. here is creation of OmiseGo we can se input data. But even though we press button "Convert to Ascii" I still only see mumbo jumbo, I can only see "OMG" as some readable text in that nonsense text. Can we decode it in some other way? etherscan.io/tx/… – Sajjon Sep 21 '17 at 21:28

As per the solidity docs

Internally, constructor arguments are passed after the code of the contract itself, but you do not have to care about this if you use web3.js.

  • Yes, if i use web3.js, i just need to call a method Contract.new. But i want to parse the constructor argument value from the transaction input data. So it's necessary to figure out the assemble way of those data, and parse them. – rong jialei Aug 3 '16 at 1:58
  • Could you please share your solution? I was helped with doc that share tech details of encoding and I have to write my own parser solidity.readthedocs.io/en/develop/abi-spec.html , but if you already did it it would be good to reuse – Gleichmut Dec 23 '17 at 3:26

In case you are trying to send a raw transaction to deploy your contract you will need to append the encoded params right after the bytecode of your smart contract.

For instance if you have web3 available you could do the following:

var Web3 = require('web3')

var encodeConstructorParams = function(web3, abi, params) {
  return abi.filter(function(json) {
    return json.type === 'constructor' && json.inputs.length === params.length;
  }).map(function(json) {
    return json.inputs.map(function(input) {
      return input.type;
  }).map(function(types) {
    return web3.eth.abi.encodeParameters(types, params).slice(2); // Remove initial 0x
  })[0] || '';

const web3 = new Web3('http://localhost:8545');
const contractABI = [{...}]
const contractBytecode = "0x06..."
const constructorParams = [arg1, arg2];

const encodedParams = encodeConstructorParams(web3, contractABI, constructorParams)
web3.eth.sendTransaction({from, data: contractBytecode + encodeParams, ...})

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.