var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
var accounts_list = web3.eth.accounts;
var code = fs.readFileSync('Voting.sol').toString();
var compiledCode = solc.compile(code);
var abiDefinition = JSON.parse(compiledCode.contracts[':Voting'].interface)
var VotingContract = web3.eth.contract(abiDefinition)
var byteCode = compiledCode.contracts[':Voting'].bytecode
var deployedContract = VotingContract.new(['Itachi','Luffy','Midoriya'],{data: byteCode, from: web3.eth.accounts[0], gas: 4700000})
var deployedContractAddress = deployedContract.address;
var contractInstance = VotingContract.at(deployedContract.address);
// contractInstance.voteForCandidate('Itachi', {from: web3.eth.accounts[0]}) // function to vote for Itachi
// contractInstance.totalVotesFor.call('Itachi').toLocaleString() // function to return Itachi's votes
var deployedAddress = contractInstance.address;
console.log(contractInstance.address);
the ouput is: undefined
but when i manually run each command on node Console it is not the case.
when i tried typeof contractInstance.address
it outputs as "String"
but i dont want to run each command manually every time hence tried running it in a script
Per the documentation, if you deploy the contract synchronously, the function returns immediately with a transaction hash, but you'll need to poll the transaction's status until it's mined. (Only then will the contract's address be available.)
As an alternative, you can deploy the contract asynchronously:
VotingContract.new(['Itachi','Luffy','Midoriya'],{data: byteCode, from: web3.eth.accounts[0], gas: 4700000}, function (err, deployedContract) {
if (deployedContract.address) {
console.log(`Address: ${deployedContract.address}`);
// use deployedContract here
}
});