Search code examples
ethereumsoliditysmartcontractsweb3js

What is the data part of the smart contract deployment in web3?


In order to deploy a smart contract to the network we need three the following information:

From the web3js spec:

// deploy new contract
var contractInstance = MyContract.new([constructorParam1] [, constructorParam2], {data: '0x12345...', from: myAccount, gas: 1000000});

Correct me if I am wrong:

constructorParams - all the data that is passed to the smart contract constructor,

from - determines the address this contract is deployed from

gas - gas limit on how much this transaction can consume

What is data, is it sort of a compiled solidity code of the contract, if so why then do we need it if we have already specified ABI of this contract?

How do I get this data parameter? I also get an error in my console when I try to deploy the contract to the network without specifying data parameter:

Error: "invalid argument 0: json: cannot unmarshal hex string without 0x prefix into Go struct field SendTxArgs.data of type hexutil.Bytes"


Solution

  • Yes, data is the compiled byte code for your smart contract.

    The ABI doesn't have the code for running the contract; it just describes the interface (what functions exist with what parameters).

    You get the bytecode from the compiler. Without knowing what tool you're using, it's hard to be more specific.