I want to deploy a smart contract (provided in .json file) and need its address (on testnet blockchain) and then try to send some transactions to it. All these should be done through javascript. Here is my code which i have tried to deploy, but could not run it. Also , I am confused that why here we are not used our private Key for signing during deployment of contract. UPDATED CODE:
var Tx = require('ethereumjs-tx').Transaction
const Web3 = require('web3');
const provider = new Web3.providers.HttpProvider("https://ropsten.infura.io/v3/7f....90b30dd22f0");
const web3 = new Web3(provider);
const account1 = '0xd458d3B03A3D4025Ae3DD5a3358afDa832c7507e'
const privateKey1 = Buffer.from('8005F9FE6F1......','hex')
var compiledContract = require('./build/MyContract.json');
// bytecode ="0x"+ compiledContract.bytecode;
// abi = compiledContract.abi;
// console.log(web3.eth.accounts.create());
(async () => {
const deployedContract = await new web3.eth.Contract(compiledContract.abi)
.deploy({
data: '0x' + compiledContract.bytecode,
arguments: [account1]
})
.send({
from: account1,
gas: '2000000'
});
console.log(
`Contract deployed at address: ${deployedContract.options.address}`
);
here is my output:
(async () => {
^
TypeError: Buffer.from(...) is not a function
at Object.<anonymous> (C:\Users\aa\MyProject\deploy.js:62:1)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
I also tried this code
(async () => {
const contract = new web3.eth.Contract(compiledContract.abi);
const params = {
data: '0x' + compiledContract.bytecode,
arguments: [account1]
};
const transaction = contract.deploy(params);
const options = {
data: transaction.encodeABI(),
gas: await transaction.estimateGas({from: account1})
};
console.log(options)
const signed = await web3.eth.accounts.signTransaction(options, privateKey1);
const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction).then(console.log);
console.log(`Contract deployed at address: ${receipt.contractAddress}`);
})()
but it also give insufficient gas error . However, my account has balance more than 5 ether.
(node:3004) UnhandledPromiseRejectionWarning: Error: Returned error: insufficien
t funds for gas * price + value