2

I am able to successfully mine one simple contract through NodeJs. But don't know how to use that contract mined address in another nodeJs file or geth server. Below Steps I have followed

1) started the geth server as geth --dev --rpc --rpcaddr "10.203.60.97" --rpcport "8046" --rpccorsdomain "*" --datadir "./data_2" --port "30303" --maxpeers 4 --unlock "0" --ipcpath "C:\\Block_Chain\\Eth_2\\data_2\\geth.ipc" console

Contract:

contract Test {
    uint testVal;
   function Test(uint testValue){
       testVal=testValue;
   }
   function multiply(uint a) returns(uint d) {
       return a * 7 *testVal;
   }
}

2) created the node js file ethereum.js file

var Web3 = require('web3'); 
var web3 = new Web3();
var solc = require('solc');
web3.setProvider(new web3.providers.HttpProvider('http://10.203.60.97:8046/'));

var input = 'contract TestMultiply { uint testVal; function Test(uint testValue){ testVal=testValue;}function multiply(uint a) returns(uint d) { return a * 7 *testVal; } }';

var output = solc.compile(input, 1);

for (var contractName in output.contracts) {
    var byteCode = output.contracts[contractName].bytecode;
    var abi =  JSON.parse(output.contracts[contractName].interface);
    var myContract = web3.eth.contract(abi);
    var testValue = 30 ;
    var multiplyContract = myContract.new(
   testValue,
   {
     from: web3.eth.accounts[0], 
     data: byteCode, 
     gas: 4700000
   },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(multiplyContract);
       console.log('Multiply Val  ' + multiplyContract.multiply.call(10));
}
    }
  else{
  console.log("Error Occured");
  console.log(e);
  }
});
console.log("Print Contract : " + multiplyContract );
}

3) executed the js file in node js

C:\Block_Chain\Node_js>node ethereum.js
Print Contract : [object Object]
Contract transaction send: TransactionHash: 0x852d568a21fbea5f7e09e96d1b5a2fd977a3ea91bf5d9f2709899d7247e90e4f waiting to be mined...
Contract mined! Address: 0xf98c996940904c994694df2bc7a5810d455a519a

4) trying to use contract in geth server it is giving the error

multiplyContract.multiply.call(10))
ReferenceError : multiplyContract is not defined

And if I am trying to use this contract using another js file it is returning 0 value instead of 2100 as per my contract function.

Test.js:

var Web3 = require('web3'); 
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://10.203.60.97:8046/'));
var mylTest= web3.eth.contract([{"constant":false,"inputs":[{"name":"testValue","type":"uint256"}],"name":"Test","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"a","type":"uint256"}],"name":"multiply","outputs":[{"name":"d","type":"uint256"}],"type":"function"}]).at('0xf98c996940904c994694df2bc7a5810d455a519a');

console.log('Multiply Val  ' + mylTest.multiply.call(10));  --> returning 0

Could you please look into the issue and help

Any help will be highly appreciated. What I am doing wrong.

1
  • When you run the deployment from Node, it does not mean that the Geth console will automagically learn about this contract. You have to tell it about this new contract. In a way similar to what you did with Node:
var myContract = web3.eth.contract(abi);
var myContractInstance = myContract.at("0xf98c996940904c994694df2bc7a5810d455a519a");
  • Change your contract to make testVal public like uint public testVal;. Then in Test.js:
console.log('testVal ' + mylTest.testVal().toString(10)); // Confirm the value is correct
console.log('Multiply Val  ' + mylTest.multiply.call(10).toString(10)); // Notice the toString
  • Constructor initialization is not happening properly mylTest.testVal().toString(10) is returning 0 and in Geth Console mylTest.multiply.call(10).toString(10) is also returning 0 – Akash Sagar Sep 7 '16 at 15:27
  • @AkashSagar show some love, up vote the answer. – niksmac Sep 8 '16 at 10:29

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.