How can I deploy a compiled contract with constructor arguments using geth rpc?
I have tested my call deployed the compiled contract bytecode just fine, I just don't understand how I can pass constructor arguments via an rpc call in the form:
{'jsonrpc': '2.0', 'method': 'eth_sendTransaction', 'params': [{'from': account, 'contract': contract}], 'id': 1}
Thanks
Best Answer
It is quite complicated to get this correct, but here's some information on how to work it out.
I'm using a simple contract as an example:
Here is my deployment to a
--dev
blockchain using Browser Solidity:Here's the bytecode from the Browser Solidity:
Before deploying the contract to the
--dev
blockchain, I ran the commanddebug.verbosity(4)
in thegeth
console command line. When Browser Solidity is deploying the contract, the following information is displayed ingeth
:Sending the
Data:
part above as thedata:
parameter in your JSON-RPC eth_sendTransaction transaction will achieve the same action of deploying the contract onto the blockchain with the same constructor arguments.You can see the first part of the
Data:
field (not bolded) matching the contract bytecode, and the second part of theData:
field (bolded) represents the constructor arguments.The constructor arguments are:
Further references:
ethabi
toolResponding to comment below
I've added two more examples to demonstrate the calculation of the offsets.
Example 2
Deployed contract above with the following parameters:
Here is the data to deploy the contract with the constructor, with the parameters bolded:
Example 3
Deployed contract above with the following parameters:
Here is the data to deploy the contract with the constructor, with the parameters bolded:
And here is the decoding of the parameters: