2
const Web3 = require('web3');
const fs = require('fs');
const solc = require('solc');
let web3;
if (typeof web3 !== 'undefined') {
    web3 = new Web3(web3.currentProvider);
} else {
    web3 = new Web3(new Web3.providers.HttpProvider('https://ropsten.infura.io/my key'));
}
const source = fs.readFileSync('./contract/coin.sol', 'utf8');
const compile = solc.compile(source, 1);
const contractCompile = Object.values(compile.contracts)[0];
const deployAddr = '0x.....';
const contractByteCode = '0x' + contractCompile.bytecode;
const contractAbi = JSON.parse(contractCompile.interface);
const gasEstimate = web3.eth.estimateGas({data: contractByteCode});
const gasPrice = '20000000000';
const myContract = new web3.eth.Contract(contractAbi, {
    from: deployAddr,
    gas: '15000000',
    gasPrice: gasPrice,
});
const initialSupply = 10000000000;
const tokenName = 'LLT';
const tokenSymbol = 'LLT';

myContract.deploy({
    data: contractByteCode,
    arguments: [initialSupply, tokenName, tokenSymbol]
}).send({
    from: deployAddr,
    gas: '15000000',
    gasPrice: gasPrice
}, function (error) {
    console.log(error);
})
.on('transactionHash', function (transactionHash) {console.log(transactionHash)})
.on('receipt', function (receipt) {
    console.log(receipt.contractAddress) // contains the new contract address
}).on('confirmation', function (confirmationNumber, receipt) {
}).then(function (newContractInstance) {
    console.log(newContractInstance.options.address) // instance with the new contract address
}).catch(err => {
    console.log("Error: failed to deploy, detail:", err)
});

I get Error: Invalid JSON RPC response: "" when I use node to run this code.

How can I solve this problem? Thank you

  • 2
    The thing I noticed was: new Web3(new Web3.providers.HttpProvider('https://ropsten.infura.io/my key')) should be: new Web3(new Web3.providers.HttpProvider('https://ropsten.infura.io/')) I don't know if this will solve the issue though. – jasper Jun 4 '18 at 9:38
1

Try to use .sendRawTransaction() instead of .send(), so:

myContract.deploy({
    data: contractByteCode,
    arguments: [initialSupply, tokenName, tokenSymbol]
}).sendRawTransaction({
    from: deployAddr,
    gas: '15000000',
    gasPrice: gasPrice
}, function (error) {
    console.log(error);
})
.on('transactionHash', function (transactionHash) {console.log(transactionHash)})
.on('receipt', function (receipt) {
    console.log(receipt.contractAddress) // contains the new contract address
}).on('confirmation', function (confirmationNumber, receipt) {
}).then(function (newContractInstance) {
    console.log(newContractInstance.options.address) // instance with the new contract address
}).catch(err => {
    console.log("Error: failed to deploy, detail:", err)
});

I am not sure it can works, probably you have to prepare and sign the transaction before send it using Infura.

0

I'm guessing you're using Metamask and actually use their provider, therefore going into the true of this condition:

if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
}
else {
web3 = new Web3(new Web3.providers.HttpProvider('https://ropsten.infura.io/my key'));
}

so this could happen because of a sync issue with metamask(I got it a few times when using a custom RPC node)
my suggestion is to try going to MM settings and reset account(it will only delete transactions) and if that doesn't work, try uninstalling and reinstalling metamask

0

Besides the comment, correctly stating the missing infura key, this cannot work, as you do not have a code component actually signing the transaction. The referenced web3 api here relies on node clients with private keys to do that, which is of course not feasible with infura. Try this solution instead, consider the last comment of that gist also.

0

I guess it's because you are trying to deploy a contract from an address (deployAddr) but you are not sending any private key to verify the ownership of that address (deployAddr).

So you need to add a private key of that address (deployAddr) before deploying the contract, like this:

const add = await web3.eth.accounts.wallet.add(privateKeyOfDeployAddr);

Hope it helps.

Cheers!

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.