1

My purpose is deploying a smart contract to a private network with Node-js. I work on Truffle and Ganache. I tried it with "truffle-contract". But in deploying process an error occurs. My contract name is "Deploy".

Command i used to deploy: myContract.deploy().send(...);.

The error: TypeError: myContract.deploy is not a function.

I don't know what is the problem. Please provide an appropriate code.

app.js:

var fs=require("fs");
var solc=require("solc");
var contract=require("truffle-contract");
var Web3=require('web3');
var web3=new Web3('ws://127.0.0.1:8545');
var address="0xfE72f8B082CF099715785d23FBeaed9272F27559";
var DeployJson=fs.readFileSync("E:/Deploy/build/contracts/Deploy.json");
var DeployJs=JSON.parse(DeployJson);
var DeployAbi=DeployJs.abi;
var contract=require("truffle-contract");
var myContract=contract({abi: DeployAbi, unlinked_binary:"0x608060405234801561001057600080fd5b5061011d806100206000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c806318620ef014602d575b600080fd5b603360ab565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101560715780820151818401526020810190506058565b50505050905090810190601f168015609d5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b60606040518060400160405280601581526020017f436f6e7472616374206973206465706c6f79656421000000000000000000000081525090509056fea265627a7a723158206cddf11cab61deaceb98041f67bc88c98dfa84738b1c39323262ccd385732c7864736f6c634300050c0032"});
     myContract.setProvider(web3);
     myContract.deploy().send({from:address, gas:1000000, gasPrice:200});
1

There's no deploy method in the API provided by require('truffle-contract').

Given truffleContract = require('truffle-contract'), you need to do something like this:

const myContract = truffleContract({abi: ..., unlinked_binary: ...});
const block = await web3.eth.getBlock('latest');
myContract.setProvider(web3.currentProvider);
myContract.defaults({from: address, gas: block.gasLimit});
const myInstance = await myContract.new({from: address, gas: 1000000, gasPrice: 200});
return myInstance;

You'll have to do it inside an async function of course.

BTW, a gasPrice of 200 is extremely low.

You might wanna change it to something like "2e9" (2 gwei)...

| improve this answer | |
1

Using below code you can deploy your contract using truffle-contract.

const contract = require('truffle-contract');
const DeployJson = fs.readFileSync("E:/Deploy/build/contracts/Deploy.json");
const DeployContract = contract(DeployJson);
DeployContract.setNetwork('network_id');

// Unlock the account from which you want to deploy the contract
await web3.eth.personal.unlockAccount(web3.eth.coinbase, 'password', 'duration');
const contractInstance = await DeployContract.new({
    from: web3.eth.coinbase,
    gas: 8000000
})
| improve this answer | |
  • Your work seems to be incomplete. You didn't use the contract binary and an error emerges: Error: Contract error: contract binary not set. Can't deploy new instance. – Alireza Mar 13 at 15:02
  • @A.Kiakojouri, Thank you for pointing that out, it should be await DeployContract.new(DeployContract.bytecode, ... }). This will fix it. – balajipachai Mar 14 at 16:55

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.