I am trying to deploy smart contract using web3 1.2.6 , my smart contract accepts 2 argument in constructor : id , name. I dont know what i am doing wrong , but both approaches are not working. Error (Approach 1 - You must provide the json interface of the contract when instantiating a contract object || myContract.new.getData is not a function) Error (Approach 2 - The method eth_sendTransaction not found or does not exist.)
Approach - 1
var priKey = Buffer.from(config.private_key_sender.slice(2), 'hex');
var myContract = new web3.eth.Contract(abi.data);
var contractData = myContract.new.getData(
'id12345',
'John Shelby'
{
data: bytecode.data,
});
web3.eth.getTransactionCount(config.send_add, (err, txCount) => {
if (txCount) {
var txObject = {
nonce: web3.utils.toHex(txCount),
gasLimit: web3.utils.toHex('1000000'),
gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')),
data: contractData
};
} else if (err && err.message) {
next(err.message, null);
}
// sign the transaction
const tx = new Tx(txObject);
tx.sign(priKey);
// serialize the transaction
const serializeTransaction = tx.serialize();
const raw = '0x' + serializeTransaction.toString('hex')
web3.eth.sendSignedTransaction(raw, (err, txHash) => {
if (txHash) {
console.log("transaction hash : ", txHash)
next(null, txHash);
}
else if (err && err.message) {
next(err.message, null);
}
else {
next('Unable to sendRawTransaction', null);
}
});
})
Approach - 2
exports.deployContract = async (next) => {
var priKey = Buffer.from(config.private_key_sender.slice(2), 'hex');
const sourceCode = fs.readFileSync('/home/puneet/puneet/projects/EstateBlocks/estateBlocks/contract/17april_1.sol', 'utf8').toString();
const compiledCode = compiler.compile(sourceCode, 1).contracts[':EstateBlock']
const interface = JSON.parse(compiledCode.interface);
const bytecode = compiledCode.bytecode;
function deploy_contract(){
const myContract = new web3.eth.Contract(interface);
myContract.deploy({data:bytecode,arguments:['id12345' , 'John Shelby']}).send({
from: config.send_add,
gas: web3.utils.toHex('1000000'),
gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei'))
},(err,res) => {
if(err){
console.log(err);
}
if(res){
console.log(res);
}
})
}
deploy_contract()
}
myContract.new.getData
to something which conforms to the syntax? Aweb3.eth.Contract
instance does not have a fieldnew
AFAIK. – goodvibration Apr 22 '20 at 10:45