3

I'm deploying a smart contract using the following guide but getting a strange error -

Guide: https://tokenmarket.net/blog/creating-ethereum-smart-contract-transactions-in-client-side-javascript/

let fs = require("fs");
let Web3 = require('web3');
let web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));

let source = fs.readFileSync("LMS.json");
let contracts = JSON.parse(source)["contracts"];
let abi = JSON.parse(contracts["LMS.sol:LMS"]["abi"]);
let code = contracts['LMS.sol:LMS'].bin
let LMS = web3.eth.contract(abi);

var lms = LMS.new("sanchit", "sanchit@sanchit.com", {
   from:web3.eth.coinbase,
   data:code,
   gas:600000}, function(err, myContract){
    console.log(err);
    if(!err) {
       if(!myContract.address) {
           console.log(myContract.transactionHash)
       } else {
           console.log(myContract.address) // the contract address
       }
    }
  });

This gives me following error -

Error: invalid argument 0: missing 0x prefix for hex data
at Object.InvalidResponse (/home/sanchitb/blockchain-workshop/lms/node_modules/web3/lib/web3/errors.js:35:16)
at /home/sanchitb/blockchain-workshop/lms/node_modules/web3/lib/web3/requestmanager.js:86:36
at XMLHttpRequest.request.onreadystatechange (/home/sanchitb/blockchain-workshop/lms/node_modules/web3/lib/web3/httpprovider.js:118:13)
at XMLHttpRequestEventTarget.dispatchEvent (/home/sanchitb/blockchain-workshop/lms/node_modules/xhr2/lib/xhr2.js:64:18)
at XMLHttpRequest._setReadyState (/home/sanchitb/blockchain-workshop/lms/node_modules/xhr2/lib/xhr2.js:354:12)
at XMLHttpRequest._onHttpResponseEnd (/home/sanchitb/blockchain-workshop/lms/node_modules/xhr2/lib/xhr2.js:509:12)
at IncomingMessage.<anonymous> (/home/sanchitb/blockchain-workshop/lms/node_modules/xhr2/lib/xhr2.js:469:24)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:188:7)
at endReadableNT (_stream_readable.js:975:12)

Can someone advise what wrong I'm doing here?

Note: The default account is unlocked and console.log(abi) shows abi definitions.

3

Adding '0x' as a prefix to bytecode fixes the issue, below is the code which works perfectly fine -

let fs = require("fs");
let Web3 = require('web3');
let web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));

let source = fs.readFileSync("LMS.json");
let contracts = JSON.parse(source)["contracts"];

let abi = JSON.parse(contracts["LMS.sol:LMS"]["abi"]);
let code = '0x'+contracts['LMS.sol:LMS'].bin
let gasEstimate = web3.eth.estimateGas({data: code});
let LMS = web3.eth.contract(abi);

var lms = LMS.new("sanchit", "s@a.com", {
   from:web3.eth.coinbase,
   data:code,
   gas: gasEstimate
 }, function(err, myContract){
    if(!err) {
       if(!myContract.address) {
           console.log(myContract.transactionHash) 
       } else {
           console.log(myContract.address) 
       }
    }
  });

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.