4

I am using the npm package of solc to compile the contracts and accessing it as given here. It is compiling my contracts. But when I try to deploy the contract using

web3.eth.sendTransaction({data: code}, function(err, address) {
  if (!err)
    console.log(address); // "0x7f9fade1c0d57a7af66ab4ead7c2eb7b11a91385"
});

as given here. I am doing all this in a JavaScript file. I am running a node locally using geth, and I am accessing it using the web3 httpprovider. When I execute the file, I am getting error as:

D:\Projectfolder\node_modules\solc\soljson.js:1
(function (exports, require, module, __filename, __dirname) { var Module;if(!Module)Module=(typeof Module!=="undefined"?Module:null)||{};var moduleOve
rrides={};for(var key in Module){if(Module.hasOwnProperty(key)){moduleOverrides[key]=Module[key]}}var ENVIRONMENT_IS_WEB=typeof window==="object";var
ENVIRONMENT_IS_WORKER=typeof importScripts==="function";var ENVIRONMENT_IS_NODE=typeof process==="object"&&typeof require==="function"&&!ENVIRONMENT_I
S_WEB&&!ENVIRONMENT_IS_WORKER;var ENVIRONMENT_IS_SHELL=!ENVIRONMENT_IS_WEB&&!ENVIRONMENT_IS_NODE&&!ENVIRONMENT_IS_WORKER;if(ENVIRONMENT_IS_NODE){if(!M
odule["print"])Module["print"]=function print(x){process["stdout"].write(x+"\n")};if(!Module["printErr"])Module["printErr"]=function printErr(x){proce
ss["stderr"].write(x+"\n")};var nodeFS=require("fs");var nodePath=require("path");Module["read"]=function read(filename,binary){filename=nodePath["nor
malize"](filename);var ret=nodeFS["readFileSync"](filename);if(!

Error: invalid address
    at inputAddressFormatter (D:\Projectfolder\node_modules\web3\lib\web3\formatters.js:271:11)
    at inputTransactionFormatter (D:\Projectfolder\node_modules\web3\lib\web3\formatters.js:97:20)
    at D:\Projectfolder\node_modules\web3\lib\web3\method.js:89:28
    at Array.map (native)
    at Method.formatInput (D:\Projectfolder\node_modules\web3\lib\web3\method.js:88:32)
    at Method.toPayload (D:\Projectfolder\node_modules\web3\lib\web3\method.js:114:23)
    at Eth.send [as sendTransaction] (D:\Projectfolder\node_modules\web3\lib\web3\method.js:139:30)
    at Object.<anonymous> (D:\FILEPATH\check.js:22:10)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)

How do I remove the error?

I tried another contract, changed the line as web3.eth.sendTransaction({from:web3.eth.accounts[0], data: bytecode, gas: 300000}
And it's working. But when I try to deploy famous greeter contract using web3 line as: web3.eth.sendTransaction(_greeting,{from:web3.eth.accounts[0], data: bytecode, gas: 300000}
It still throws error.

Oh, finally this is resolved. I just needed to add JSON.parse(abiDefinition). Now the greeter contract is also working. This one is deployed using greeterContract.new(_greeting,{from:web3.eth.accounts[0], data:bytecode, gas: 300000}

3

Your issue is most likely caused by a missing from: eth.accounts[0] parameter from your eth.sendTransaction(...) call.

Try running either of the following step-by-step examples to see what kind of parameters and data is expected at each step:

If you can run either of the examples above, your geth and solc installation is working as expected, and your problem is most likely in the parameters of your eth.sendTransaction(...) call.



Answering question in the comment below

I would next check your data: code statement.

You should see the following type of result when you type code:

> code
"0x606060405260405161023e38038061023e8339810160405280510160008054600160a060020a031916331790558060016000509080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10609f57805160ff19168380011785555b50608e9291505b8082111560cc57600081558301607d565b50505061016e806100d06000396000f35b828001600101855582156076579182015b82811115607657825182600050559160200191906001019060b0565b509056606060405260e060020a600035046341c0e1b58114610026578063cfae321714610068575b005b6100246000543373ffffffffffffffffffffffffffffffffffffffff908116911614156101375760005473ffffffffffffffffffffffffffffffffffffffff16ff5b6100c9600060609081526001805460a06020601f6002600019610100868816150201909416939093049283018190040281016040526080828152929190828280156101645780601f1061013957610100808354040283529160200191610164565b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600f02600301f150905090810190601f1680156101295780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b565b820191906000526020600020905b81548152906001019060200180831161014757829003601f168201915b505050505090509056"

If your data looks different, you are not passing your compiled code to the eth.sendTransaction(...) call.



Further edit - seeing that you are using JSON-RPC to deploy

There is some example code in https://github.com/ethereum/web3.js/blob/develop/example/contract.html to deploy a contract using JSON-RPC.

There is an assignment to the default account in the example:

web3.eth.defaultAccount = web3.eth.coinbase;

And the Application Binary Interface is passed using the eth.contract(...).new(...) call.

var abi = compiled.test.info.abiDefinition;
...
web3.eth.contract(abi).new({data: code}, function (err, contract) {

One other thing is to make sure that you account is unlocked, so you will have to run geth with parameters similar to

geth --rpc --unlock 0 console
...
Unlocking account 0 | Attempt 1/3
Passphrase: 
  • I have changed that line as "web3.eth.sendTransaction(_greeting,{from:web3.eth.accounts[0], data: bytecode, gas: 300000}, function(err, address){}". Still it is not working.@BokkyPooBah – Aniket Jul 15 '16 at 14:01
  • See addition to my answer – Bokky WeAreAllAssange PooBah Jul 15 '16 at 14:08
  • I chose different contract, added '0x' in bytecode and its working. But when i pass '_greeting' as a value for constructor, its not working – Aniket Jul 15 '16 at 14:24
2

As Bokky mentioned, you need a from key and sending address tuple when calling send transaction. Have a look at a valid transaction here (contract deployment transactions are very similar, see below):

web3.eth.sendTransaction({from: web3.eth.coinbase, to: '0x2910543af39aba0cd09dbb2d50200b3e800a63d2', value: web3.toWei(1, 'ether'), data: web3.toHex('EN7FQHHVG'), gas:25000});

A contract deployment transaction would look like:

var _greeting = "Hello World!"
var greeterContract = web3.eth.contract(greeterCompiled.greeter.info.abiDefinition);

var greeter = greeterContract.new(_greeting,{from:web3.eth.accounts[0], data: greeterCompiled.greeter.code, gas: 300000}, 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);
      }

    }
})
  • I am also trying the same. I am trying to deploy it using web3 & JSON-RPC. And after adding the 'from' field, i am still having same error. – Aniket Jul 15 '16 at 14:05
  • Can you please edit your question to show all the field values you are passing to sendTransaction? – Sebi Jul 15 '16 at 14:12

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.