I am using the following code to deploying a smart contract to a private network with Node-js. But the error TypeError: contract_object.new is not a function occurs. What are .new() and the error's solution?


var Web3=require('web3');
var fs=require('fs');
var solc=require('solc');
var web3=new Web3('ws://');
var address="0xB6EB958b199219Abf33462d6B348C9709C8E24f8";
var contract_sol=fs.readFileSync('E:/Deploy/contracts/Deploy.sol','utf8');
var contract_compiled=solc.compile(contract_sol);
var contract_json="E:/Deploy/build/contracts/Deploy.json";
var contract_js=JSON.parse(fs.readFileSync(contract_json));
var contract_abi=contract_js.abi;
var contract_byteCode="0x608060405234801561001057600080fd5b5061011d806100206000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c806318620ef014602d575b600080fd5b603360ab565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101560715780820151818401526020810190506058565b50505050905090810190601f168015609d5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b60606040518060400160405280601581526020017f436f6e7472616374206973206465706c6f79656421000000000000000000000081525090509056fea265627a7a723158206cddf11cab61deaceb98041f67bc88c98dfa84738b1c39323262ccd385732c7864736f6c634300050c0032";
var gasEstimate=web3.eth.estimateGas({data:contract_byteCode});
var contract_object=new web3.eth.Contract(contract_abi);
var theContract=contract_object.new({ from: address, data:contract_byteCode, gas: gasEstimate+10000})

You required web3 module but used a method of truffle-contract module while you didn't require it. So, you just can use all methods that web3 provides to you. The command:

var theContract=contract_object.new({ from: address, data:contract_byteCode, gas: gasEstimate+10000})

uses truffle-contract module's method. Just replace that with the below command that is a web3 module's command:

contract_object.deploy().send({ from: <address>, data:contract_byteCode, gas: gasEstimate+10000});

new web3.eth.Contract calls the constructor of class web3.eth.Contract.

This constructor (naturally) returns an instance of class web3.eth.Contract.

Your attempt:

var contract_object=new web3.eth.Contract(...);
var theContract=contract_object.new(...);

Fails because class web3.eth.Contract does not have a member function named new.

You are most likely confusing it with Truffle's artifacts.require('<ContractName>').new.

  • Do you mean i use var theContract=artifacts.require(contract_object).new(...) instead of var theContract=contract_object.new(...); or another code is needed? – Alireza Mar 9 '20 at 14:10
  • @A.Kiakojouri: Read the answer. It says artifacts.require('<ContractName>').new(...). And it will only work within a Truffle environment (e.g., when you execute truffle test from a command line). – goodvibration Mar 9 '20 at 14:12

There are some methods to deploy smart contract. But, there is one method that can be applied just in Truffle project. contract_object.new(…) command remind that same method that is applicable just in Truffle project. But, the below command:

contract_object=new web3.eth.Contract(…)

Is applicable using web3 library and even in non-Truffle projects, such that to deploy the contract the command should be used is contract_object.deploy().send({…});, not contract_object.new(…).

Because “truffle-contract” module was not applied in the question code, Just replace the last command with the below:

contract_object.deploy().send({ from: address, data:contract_byteCode, gas: gasEstimate+10000});

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.