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
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.