简体   繁体   中英

How to deploy and get address of smart contract in same javascript program

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

I can see you're refering to the web3js documentation. That documentation is made for one using the local node.As you're using the Infura as provider, you will face issues when you're gonna need to do transactions from your account. As you have stated that you didn't require to use the private key , that is because the deploy function here assumes that the account is already unlocked. You should refer " https://infura.io/docs " for the changes you will need to do while using the web3js library with infura as a provider. Also, you have to use the private key for signing the transaction (iefor paying the gas fee from your account)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM