I was following a tutorial where i found that we can create an instance without passing the address of the contract to create the instance. Like this:

Voting.deployed().then(function(contractInstance) {
  contractInstance.voteForCandidate(candidateName, {gas: 140000, from: web3.eth.accounts[0]}).then(function() {
    let div_id = candidates[candidateName];
    return contractInstance.totalVotesFor.call(candidateName).then(function(v) {
      $("#" + div_id).html(v.toString());

I checked the web3.js documentation but couldn't find anything. Is this function deprecated? Is there any other way of creating an instance of a contract without passing the address -I am using truffle and testrpc.


I believe that the deployed function you're calling is part of Truffle, not web3.js.

As to how to deploy a contract via web3.js, here's an example snippet of code that I got from the "details" screen in Remix:

var testContract = web3.eth.contract([{"constant":true,"inputs":[{"name":"n","type":"uint256"}],"name":"test","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"}]);
var test = testContract.new(
     from: web3.eth.accounts[0], 
     data: '0x6060604052341561000f57600080fd5b60ae8061001d6000396000f300606060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806329e99f07146044575b600080fd5b3415604e57600080fd5b606260048080359060200190919050506078565b6040518082815260200191505060405180910390f35b60008190509190505600a165627a7a72305820ad206bc8d103ee40219f8a9e55fd456bcda22c986b40a97ae5eedbaa6dc791f90029', 
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);

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.