5

I wrote a contract greeter and mortal, they are as follows:

contract mortal {

    address owner;

    function mortal() { owner = msg.sender; }

    function kill() { if (msg.sender == owner) selfdestruct(owner); }
}

contract greeter is mortal {

    string greeting;

    function greeter(string _greeting) public {
        greeting = _greeting;
    }

    /* main function */
    function greet() constant returns (string) {
        return greeting;
    }
}

Then i compiled the code using Solidity online compiler. I copied the compiled code and pasted in a text file with name "helloworld.js" as follows:

var _greeting = /* var of type string here */ ;
var greeterContract = web3.eth.contract([
  {"constant":false,"inputs": [], "name":"kill", "outputs":[], "payable":false, "type":"function"}, 
  {"constant":true, "inputs": [], "name":"greet", "outputs":[{"name":"", "type":"string"}], "payable":false, "type":"function"},
  {"inputs": [{"name":"_greeting", "type":"string"}], "type":"constructor"}]);

var greeter = greeterContract.new(_greeting, 
  {
    from: web3.eth.accounts[0], 
    data: 'data (some numbers and alphabets)', 
    gas: 4700000
  }, function (e, contract) {
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
      console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
  }
)

I went to the cmd and started my blockchain network. In geth console i unlocked my account with etherbase and used loadScript("C:/Rahul13615/blockchain/node1/contracts/helloworld.js")

Ideally it should return: Contract mined! address: some address

but it returns:

loadScript("C:/Rahul13615/blockchain/node1/contracts/helloworld.js") null [object Object] true

What should i do?

BokkyPooBah
  • 40,274
  • 14
  • 123
  • 193
Rahul Sharma
  • 1,303
  • 2
  • 16
  • 24
  • 2
    See http://ethereum.stackexchange.com/questions/2751/deploying-the-greeter-contract-via-the-geth-cli-is-not-registering-in-my-private – BokkyPooBah Nov 07 '16 at 04:32
  • 1
    You may want to answer your own question and add in what steps you took to get it working, then self-accept your answer. So others encountering the same issue can work through your solution. – BokkyPooBah Nov 07 '16 at 06:13

1 Answers1

3

I posted this question a week ago and with the help of my peers i was able to solve it. The detailed answer is as follows:

Step 1:

Using the following command start your private blockchain network( change the data directories and other parameters according to your custom settings)

geth --identity "node1" --rpc --rpcport "8000" --rpccorsdomain "*" --datadir     "C:\Rahul13615\blockchain\node1" --port "30303" --nodiscover --ipcapi "admin,db,eth,debug,miner,net,shh,txpool,personal,web3" --rpcapi "db,eth,net,web3" --autodag --networkid 100 --nat "any"

Leave this terminal as it is (but do not close it) after executing the above command.

Step 2

Open another terminal and type:

geth attach

This will open the geth console. Now you must have created few accounts. We will set etherbase for any one account and execute script using it's address. So enter the following commands.

primary = eth.accounts[0]
>true (this is the output of above command)

web3.miner.setEtherbase(primary)
>true

personal.unlockAccount(primary,"passphrase") //here passphrase is the string you used while creating an account
>true

miner.start()
>true

Now in helloworld.js i have saved the compiled code that i copied from online browser solidity compiler. Change the first line of code to:

var _greeting = "Hello World" ;

Save helloworld.js and run the following command

loadScript("helloworld.js")
>null [object Object]
Contract mined! Address : someAddress, Txhash : somevalue

greeter.greet();
>Hello World

miner.stop()
>true

Please comment for any doubts or questions.

Rahul Sharma
  • 1,303
  • 2
  • 16
  • 24