1

I am trying to deploy contract i am using web3.js version web3@0.20.4 with infura node

following code i am trying

const fs = require('fs');
const solc = require('solc');
const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/metamask"));
const Tx = require('ethereumjs-tx');

let privateKey = new Buffer('8A0F4E01AC43A014F9F13CF68E', 'hex');

let source = fs.readFileSync('contract.sol', 'utf8');// solc file source decided by yourself
let compiledContract = solc.compile(source, 1);
let abi = compiledContract.contracts[':Adeel'].interface;
let bytecode = compiledContract.contracts[':Adeel'].bytecode;

// Contract object
const MyContract = new web3.eth.contract(JSON.parse(abi));

// Get contract data
const contractData = MyContract.new.getData({
    data: '0x' + bytecode
});

const gasPrice = web3.eth.gasPrice;
const gasPriceHex = web3.toHex(gasPrice);
const gasLimitHex = web3.toHex(300000);

const nonce = web3.eth.getTransactionCount(web3.eth.coinbase);
const nonceHex = web3.toHex(nonce);

const rawTx = {
    nonce: nonceHex,
    gasPrice: gasPriceHex,
    gasLimit: gasLimitHex,
    data: contractData,
    from: '0x5488365e6785e571762ee980400e64c62853c2c0'
};

const tx = new Tx(rawTx);
tx.sign(privateKey);
const serializedTx = tx.serialize();

web3.eth.sendRawTransaction(serializedTx.toString('hex'), (err, hash) => {
    if (err) { console.log(err); return; }

    // Log the tx, you can explore status manually with eth.getTransaction()
    console.log('contract creation tx: ' + hash);

    // Wait for the transaction to be mined
    waitForTransactionReceipt(hash);
});

function waitForTransactionReceipt(hash) {
    console.log('waiting for contract to be mined');
    const receipt = web3.eth.getTransactionReceipt(hash);
    // If no receipt, try again in 1s
    if (receipt == null) {
        setTimeout(() => {
            waitForTransactionReceipt(hash);
        }, 1000);
    } else {
        // The transaction was mined, we can retrieve the contract address
        console.log('contract address: ' + receipt.contractAddress);
        //testContract(receipt.contractAddress);
    }
}

error is here

Error: Invalid JSON RPC response: ""
    at Object.InvalidResponse (/var/opt/webserver/adeel/adeelotxeth/node_modules/web3/lib/web3/errors.js:38:16)
    at HttpProvider.send (/var/opt/webserver/adeel/adeelotxeth/node_modules/web3/lib/web3/httpprovider.js:101:18)
    at RequestManager.send (/var/opt/webserver/adeel/adeelotxeth/node_modules/web3/lib/web3/requestmanager.js:58:32)
    at Eth.get [as coinbase] (/var/opt/webserver/adeel/adeelotxeth/node_modules/web3/lib/web3/property.js:107:62)
    at Object.<anonymous> (/var/opt/webserver/adeel/adeelotxeth/test.js:26:53)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
  • Did you register with infura.io so they assign your own token to be able to connect? – Ismael Aug 20 '18 at 23:40
1

Never show your private key to anyone! Your 0.023752305 Ether can be stolen.

About question: check connection to https://ropsten.infura.io/metamask Now this page has code 404

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.