0

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:

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')

(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}`
);

provider.engine.stop();

})(); 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)
0

why here we are not used our private Key for signing during deployment of contract

Because your code assumes that account1 is unlocked on the node that you're communicating with (whatever you've initialized your web3 instance to connect to). If account1 is not unlocked on that node, then your code will fail to complete.

I'm assuming that account1 is a string which represents an Ethereum address (and not, for example, an account object, in which case you've got a much more basic problem in your code).

In any case, this operation-mode of keeping your unlocked account on a node is not recommended in general, because anyone hacking that node will be able to use your unlocked account at will.

In order to sign the transaction yourself (instead of letting your node do it for you), you can do something like this:

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})
};
const signed = await web3.eth.accounts.signTransaction(options, YOUR_PRIVATE_KEY);
const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction);
console.log(`Contract deployed at address: ${receipt.contractAddress}`);
  • Comments are not for extended discussion; this conversation has been moved to chat. – eth Nov 14 at 8:03

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.