5

I want to create a new contract by JSON-RPC and cpp-ethereum.

I find out eth_sendTransaction can help me to do this.

And let do it, I need to set "to" parameter like that:

to: DATA, 20 Bytes - (optional when creating new contract) The address the transaction is directed to.

But when I try to work on it with sample code with port 8079(own setup):

curl -X POST --data '{"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{
  "from": "0x009fbfed8d5ca3b7c1fe680e1dd61fc726ef177b",
  "to": "0x005fecaea1b18f5247fdb457793f3da72df12e61",
  "gas": "0x76c0",
  "gasPrice": "0x9184e72a000",
  "value": "0x9184e72a",
  "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"}],"id":1}' http://localhost:8079

I received nothing from above command.

I also know another way to create a new contract by solidity.

But I really want to use only JSON-RPC API.

Please give me advise to create a new contract by this way.

1 Answer 1

2

Summary

  • You cannot include the to: field in your JSON-RPC eth_sendTransaction message.
  • Check that your account is not locked



Let's Find Out What Data Is Required

I'm using geth and JavaScript with the example Syntax for calling contract state changing methods to work out the data required.

Here is my geth command line:

user@Kumquat:~$ geth --datadir ~/EtherDev/data --dev \
  --mine --minerthreads 1 --verbosity 3              \
  --unlock 0 --password ~/EtherDev/etc/passwordfile  \
  --rpc console

(Note that I have unlocked my account, and this may be one of the reasons why your contract creation transaction failed. See Can I unlock an account using JSON-RPC? for further information.)

And then run the following commands:

> var cSource='contract C { uint[] public numbers; function initNumbers() { numbers.push(1); numbers.push(2); } function stateChanger(uint a) { numbers.push(a); }      }'
undefined
> var cCompiled = web3.eth.compile.solidity(cSource);
Version: 0.3.5-0/RelWithDebInfo-Linux/g++/Interpreter

path: /usr/bin/solc
undefined
> var cContract = web3.eth.contract(cCompiled.C.info.abiDefinition);
undefined
// Turn up the debugging to view the intermediate information
> debug.verbosity(7)
null
> var c = cContract.new({
    from:web3.eth.accounts[0], 
    data: cCompiled.C.code, gas: 400000}, 
    function(e, contract) {
      if (!e) {
        if (!contract.address) {
          console.log("Contract transaction send: TransactionHash: " + 
            contract.transactionHash + " waiting to be mined...");
        } else {
          console.log("Contract mined! Address: " + contract.address);
          console.log(contract);
        }
    }
})
...
I0731 00:16:36.230020 core/tx_pool.go:547] removed tx (
    TX(46f833281c8c7652c0734773723e61fc5b6d2cbe731024ff730e4d2626d39b59)
    Contract: false
    From:     a7857047907d53a2e494d5f311b4b586dc6a96d2
    To:       [contract creation]
    Nonce:    37
    GasPrice: 20000000000
    GasLimit  400000
    Value:    0
    Data:     0x6060604052610164806100126000396000f3606060405260e060020a60003504635a7dc89781146100315780636506077514610074578063d39fa233146100aa575b005b61002f600080546001810180835582818380158290116100f8578183600052602060002091820191016100f891905b808211156101395760008155600101610060565b61002f60043560008054600181018083558281838015829011610152578183600052602060002091820191016101529190610060565b6100e6600435600080548290811015610002575080527f290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563015481565b60408051918252519081900360200190f35b50505060009283525060208220600191018190558154908101808355828183801582901161013d5781836000526020600020918201910161013d9190610060565b5090565b50505060009283525060209091206002910155565b5050506000928352506020909120015556
    V:        0x1b
    R:        0x6726a5204268ea430c32087f6ce85750f51e6792dd55139d3e3c7f9ce6d19812
    S:        0x3fb400f4bb13ad95dfa03023e42ee91aade477aa8ed921f5111b666753d60b3
    Hex:      f901c9258504a817c80083061a808080b901766060604052610164806100126000396000f3606060405260e060020a60003504635a7dc89781146100315780636506077514610074578063d39fa233146100aa575b005b61002f600080546001810180835582818380158290116100f8578183600052602060002091820191016100f891905b808211156101395760008155600101610060565b61002f60043560008054600181018083558281838015829011610152578183600052602060002091820191016101529190610060565b6100e6600435600080548290811015610002575080527f290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563015481565b60408051918252519081900360200190f35b50505060009283525060208220600191018190558154908101808355828183801582901161013d5781836000526020600020918201910161013d9190610060565b5090565b50505060009283525060209091206002910155565b50505060009283525060209091200155561ba06726a5204268ea430c32087f6ce85750f51e6792dd55139d3e3c7f9ce6d19812a003fb400f4bb13ad95dfa03023e42ee91aade477aa8ed921f5111b666753d60b3
) from pool: low tx nonce or out of funds
...
Contract mined! Address: 0x2bdd1725a5cdf212d6dfaa488ae2cecbe7b1dc1f


Let's Try Sending The Contract Creation Transaction With JSON-RPC

I'm using geth rather than cpp-ethereum as I don't have cpp-ethereum configured, but the JSON-RPC should be the same. I'm using port 8545 for the JSON-RPC port.

I removed the to: field with a copy of your JSON-RPC call, and added the data from my debugging trace.

Here is my first attempt:

user@Kumquat:~$ curl -X POST --data '{"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{
>   "from": "0xa7857047907d53a2e494d5f311b4b586dc6a96d2",
>   "gas": "0x76c0",
>   "gasPrice": "0x9184e72a000",
>   "value": "0",
>   "data": "0x6060604052610164806100126000396000f3606060405260e060020a60003504635a7dc89781146100315780636506077514610074578063d39fa233146100aa575b005b61002f600080546001810180835582818380158290116100f8578183600052602060002091820191016100f891905b808211156101395760008155600101610060565b61002f60043560008054600181018083558281838015829011610152578183600052602060002091820191016101529190610060565b6100e6600435600080548290811015610002575080527f290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563015481565b60408051918252519081900360200190f35b50505060009283525060208220600191018190558154908101808355828183801582901161013d5781836000526020600020918201910161013d9190610060565b5090565b50505060009283525060209091206002910155565b5050506000928352506020909120015556"}],"id":1}' http://localhost:8545
{"jsonrpc":"2.0","id":1,"error":{"code":-32000,"message":"Intrinsic gas too low"}}

I bumped up gas: and removed gasPrice:. Following is my second attempt:

user@Kumquat:~$ curl -X POST --data '{"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{
>   "from": "0xa7857047907d53a2e494d5f311b4b586dc6a96d2",
>   "gas": "0x776c0",
>   "value": "0",
>   "data": "0x6060604052610164806100126000396000f3606060405260e060020a60003504635a7dc89781146100315780636506077514610074578063d39fa233146100aa575b005b61002f600080546001810180835582818380158290116100f8578183600052602060002091820191016100f891905b808211156101395760008155600101610060565b61002f60043560008054600181018083558281838015829011610152578183600052602060002091820191016101529190610060565b6100e6600435600080548290811015610002575080527f290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563015481565b60408051918252519081900360200190f35b50505060009283525060208220600191018190558154908101808355828183801582901161013d5781836000526020600020918201910161013d9190610060565b5090565b50505060009283525060209091206002910155565b5050506000928352506020909120015556"}],"id":1}' http://localhost:8545
{"jsonrpc":"2.0","id":1,"result":"0x938040c15d0cabe9a9dbb1b629156cd9184c32e0538fc9159450bb5c056f4be4"}

A transaction hash has been returned - looking good. Let's see what the geth console shows:

I0731 00:53:16.024486 core/tx_pool.go:547] removed tx (
    TX(938040c15d0cabe9a9dbb1b629156cd9184c32e0538fc9159450bb5c056f4be4)
    Contract: false
    From:     a7857047907d53a2e494d5f311b4b586dc6a96d2
    To:       [contract creation]
    Nonce:    40
    GasPrice: 20000000000
    GasLimit  489152
    Value:    0
    Data:     0x6060604052610164806100126000396000f3606060405260e060020a60003504635a7dc89781146100315780636506077514610074578063d39fa233146100aa575b005b61002f600080546001810180835582818380158290116100f8578183600052602060002091820191016100f891905b808211156101395760008155600101610060565b61002f60043560008054600181018083558281838015829011610152578183600052602060002091820191016101529190610060565b6100e6600435600080548290811015610002575080527f290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563015481565b60408051918252519081900360200190f35b50505060009283525060208220600191018190558154908101808355828183801582901161013d5781836000526020600020918201910161013d9190610060565b5090565b50505060009283525060209091206002910155565b5050506000928352506020909120015556
    V:        0x1b
    R:        0xb57458702e0d4033cd52bbdf8c458639a5fed2359189ccd18e19be274c14ba19
    S:        0x3dcfaae9b58268d5aea27250719930e60c2213382e4b50355c7f72b248e34596
    Hex:      f901c9288504a817c800830776c08080b901766060604052610164806100126000396000f3606060405260e060020a60003504635a7dc89781146100315780636506077514610074578063d39fa233146100aa575b005b61002f600080546001810180835582818380158290116100f8578183600052602060002091820191016100f891905b808211156101395760008155600101610060565b61002f60043560008054600181018083558281838015829011610152578183600052602060002091820191016101529190610060565b6100e6600435600080548290811015610002575080527f290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563015481565b60408051918252519081900360200190f35b50505060009283525060208220600191018190558154908101808355828183801582901161013d5781836000526020600020918201910161013d9190610060565b5090565b50505060009283525060209091206002910155565b50505060009283525060209091200155561ba0b57458702e0d4033cd52bbdf8c458639a5fed2359189ccd18e19be274c14ba19a03dcfaae9b58268d5aea27250719930e60c2213382e4b50355c7f72b248e34596
) from pool: low tx nonce or out of funds

Let's get the newly created contract address (you can use the JSON-PC web3.eth.getTransactionReceipt):

> eth.getTransactionReceipt("938040c15d0cabe9a9dbb1b629156cd9184c32e0538fc9159450bb5c056f4be4")
{
  blockHash: "0xe5884a160ffc2e9d88219e2638ed772abbfcc6b5ab272e66881803794baf4dce",
  blockNumber: 559,
  contractAddress: "0x5e792e0af19475caee3ff242bb58edda4b455eac",
  cumulativeGasUsed: 115811,
  from: "0xa7857047907d53a2e494d5f311b4b586dc6a96d2",
  gasUsed: 115811,
  logs: [],
  root: "7750bdc145e773d42f5cabbd1b97bfab45a53ba7f3b2b08762446fd44ab0f2d5",
  to: null,
  transactionHash: "0x938040c15d0cabe9a9dbb1b629156cd9184c32e0538fc9159450bb5c056f4be4",
  transactionIndex: 0
}

Let's check the code at the contract address:

> eth.getCode("0x5e792e0af19475caee3ff242bb58edda4b455eac")
"0x606060405260e060020a60003504635a7dc89781146100315780636506077514610074578063d39fa233146100aa575b005b61002f600080546001810180835582818380158290116100f8578183600052602060002091820191016100f891905b808211156101395760008155600101610060565b61002f60043560008054600181018083558281838015829011610152578183600052602060002091820191016101529190610060565b6100e6600435600080548290811015610002575080527f290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563015481565b60408051918252519081900360200190f35b50505060009283525060208220600191018190558154908101808355828183801582901161013d5781836000526020600020918201910161013d9190610060565b5090565b50505060009283525060209091206002910155565b5050506000928352506020909120015556"

Success! A new contract has been created using the JSON-RPC eth_sendTransaction API. This same JSON-RPC API should work with cpp-ethereum.



What Happens If You Include to: In The Data Sent

I tested what happens if you include the to: field with the JSON-RPC sendTransaction.

Here's the request and the resulting transaction hash:

user@Kumquat:~$ curl -X POST --data '{"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{
  "from": "0xa7857047907d53a2e494d5f311b4b586dc6a96d2",
  "to": "0x005fecaea1b18f5247fdb457793f3da72df12e61",
  "gas": "0x776c0",
  "value": "0",
  "data": "0x6060604052610164806100126000396000f3606060405260e060020a60003504635a7dc89781146100315780636506077514610074578063d39fa233146100aa575b005b61002f600080546001810180835582818380158290116100f8578183600052602060002091820191016100f891905b808211156101395760008155600101610060565b61002f60043560008054600181018083558281838015829011610152578183600052602060002091820191016101529190610060565b6100e6600435600080548290811015610002575080527f290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563015481565b60408051918252519081900360200190f35b50505060009283525060208220600191018190558154908101808355828183801582901161013d5781836000526020600020918201910161013d9190610060565b5090565b50505060009283525060209091206002910155565b5050506000928352506020909120015556"}],"id":1}' http://localhost:8545
{"jsonrpc":"2.0","id":1,"result":"0xa92bbeee20563c239715b2956a755fa3fdabe7e5eaba1de6652a96360fa799be"}

Here's the transaction information:

> eth.getTransaction("0xa92bbeee20563c239715b2956a755fa3fdabe7e5eaba1de6652a96360fa799be")
{
  blockHash: "0x3aa9ba9513d881903079e7eaceb08dcd2db933d2f5ac6aa9f11d8626a006ab6d",
  blockNumber: 613,
  from: "0xa7857047907d53a2e494d5f311b4b586dc6a96d2",
  gas: 489152,
  gasPrice: 20000000000,
  hash: "0xa92bbeee20563c239715b2956a755fa3fdabe7e5eaba1de6652a96360fa799be",
  input: "0x6060604052610164806100126000396000f3606060405260e060020a60003504635a7dc89781146100315780636506077514610074578063d39fa233146100aa575b005b61002f600080546001810180835582818380158290116100f8578183600052602060002091820191016100f891905b808211156101395760008155600101610060565b61002f60043560008054600181018083558281838015829011610152578183600052602060002091820191016101529190610060565b6100e6600435600080548290811015610002575080527f290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563015481565b60408051918252519081900360200190f35b50505060009283525060208220600191018190558154908101808355828183801582901161013d5781836000526020600020918201910161013d9190610060565b5090565b50505060009283525060209091206002910155565b5050506000928352506020909120015556",
  nonce: 41,
  to: "0x005fecaea1b18f5247fdb457793f3da72df12e61",
  transactionIndex: 0,
  value: 0
}

And here's the transaction receipt information:

> eth.getTransactionReceipt("0xa92bbeee20563c239715b2956a755fa3fdabe7e5eaba1de6652a96360fa799be")
{
  blockHash: "0x3aa9ba9513d881903079e7eaceb08dcd2db933d2f5ac6aa9f11d8626a006ab6d",
  blockNumber: 613,
  contractAddress: null,
  cumulativeGasUsed: 44512,
  from: "0xa7857047907d53a2e494d5f311b4b586dc6a96d2",
  gasUsed: 44512,
  logs: [],
  root: "71bd24394df7c5feb534b7e6081af0550e2b58c552002c03550ec3fc64a520c6",
  to: "0x005fecaea1b18f5247fdb457793f3da72df12e61",
  transactionHash: "0xa92bbeee20563c239715b2956a755fa3fdabe7e5eaba1de6652a96360fa799be",
  transactionIndex: 0
}

Including the to: field results in a regular transaction, not a contract creation transaction



What Happens When Your Account Is Locked

Here's the response:

user@Kumquat:~/EtherDev$ curl -X POST --data '{"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{
>   "from": "0xa7857047907d53a2e494d5f311b4b586dc6a96d2",
>   "gas": "0x776c0",
>   "value": "0",
>   "data": "0x6060604052610164806100126000396000f3606060405260e060020a60003504635a7dc89781146100315780636506077514610074578063d39fa233146100aa575b005b61002f600080546001810180835582818380158290116100f8578183600052602060002091820191016100f891905b808211156101395760008155600101610060565b61002f60043560008054600181018083558281838015829011610152578183600052602060002091820191016101529190610060565b6100e6600435600080548290811015610002575080527f290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563015481565b60408051918252519081900360200190f35b50505060009283525060208220600191018190558154908101808355828183801582901161013d5781836000526020600020918201910161013d9190610060565b5090565b50505060009283525060209091206002910155565b5050506000928352506020909120015556"}],"id":1}' http://localhost:8545
{"jsonrpc":"2.0","id":1,"error":{"code":-32000,"message":"account is locked"}}
1
  • If i have cContract.new(savedataIncontract , address, { from:web3.eth.accounts[0], data: cCompiled.C.code, gas: 400000}, like of syntax on that case how can i write RPC request Commented Mar 1, 2017 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.