0

I am creating a contract on my local blockchain network using web3. On my node , contact is created and address is generated. But on node application, response is struck.

Here is my node code:

const Web3 = require('web3')
const web3 = new Web3(new Web3.providers.HttpProvider("http://192.168.56.103:8000"))
//const web3 = new Web3(ganache.provider());
const compiledFactory = require('./ethereum/build/CampaignFactory.json')

const deploy = async () => {
    accounts = await web3.eth.getAccounts()
    console.log(accounts)

    addr = await web3.eth.personal.newAccount('123456');
    console.log(addr)

    factory = await new web3.eth.Contract(JSON.parse(compiledFactory.interface))
    .deploy({ data: '0x' + compiledFactory.bytecode })
    .send({ from: accounts[0], gas: '1000000' });

    console.log(factory)
}

deploy();

Here is console from node app that is stuck after

[ '0xDa8512CBD5c5eaa1213e5A7bEefAf35e9bA2c9EA',
  '0x3F5dD664F0A803d546E087Ee03B69aEb97D428DB',
  '0x7Eb0390DbF70147f37459F5d982456527FdBdb98',
  '0xD9FcDA95002A09d7be394F085fAEC2a1f3d28476' ]
0x2Cc72882b17960e07FCeF04E54A397628075654D

and here is the logs from node:

INFO [08-09|03:28:10.027] Submitted contract creation              fullhash=0x63ac15fcc23f19aa44cd225dbacf7ea30069d3e106987daf63a4238caab4b73c contract=0x31F82590e051A224e7d8d63eB54f434d6073aB06

Can anybody help? Why I am not getting address on nodejs application?

  • do you have balance on that account which will deploy the contract?! – Majd TL Aug 9 at 8:15
  • Yes account have balance. Contract is deployed also. But waiting for response in nodejs application. – Ameer Hamza Aug 9 at 8:17
0

I'm not sure what the return value factory is, so dumping it on the console may be blank if it's undefined. When you say it is hanging, do you mean your NodeJS application never completes, or just that it prints nothing after the list of addresses?

In any case, as you say, it seems like just a display problem, not a contract deploying problem. You can add another console.log('Done') line after console.log(factory), or even console.log(JSON.stringify(factory)), to make sure the script is actually completing.

Usually truffle or web3 methods like to return a transaction hash from methods that send transactions to the blockchain, such as 0x63ac15fcc23f19aa44cd225dbacf7ea30069d3e106987daf63a4238caab4b73c in your example above, but I've never used the deploy and send methods above.

  • Yes it is hanging. Nodejs application never completes it. I added logs like done below, but the code execution struck on contract creation. – Ameer Hamza Aug 9 at 17:46
0

if you're using test net from another provider like rinkeby or etc. process for deploy a contract took little longer for getting return value. approximately 6-8 minutes

0

Here is one of my deploy scripts. Feel free to modify it as needed.

Code which deploys a smart contract | deploy.js

const Web3 = require('web3');
const { interface, bytecode } = require('./compile');

const web3 = new Web3(new Web3.providers.HttpProvider("http://192.168.56.103:8000"))

const deploy = async () => {
    const accounts = await web3.eth.getAccounts();

    console.log('Attempting to deploy from account', accounts[0]);

    const result = await new web3.eth.Contract(JSON.parse(interface))
      .deploy( { data:bytecode })
      .send( { gas: '5000000', from:accounts[0] });

    console.log('Contract deployed to', result.options.address);
};
deploy();

Code which compiles smart contract code (creates interface and bytecode) | compile.js

const path = require('path');
const fs = require('fs');
const solc = require('solc');

const contractPath = path.resolve(__dirname, 'contracts', 'Contract.sol');
const source = fs.readFileSync(contractPath, 'utf8');

module.exports = solc.compile(source, 1).contracts[':Contract'];

Contract path needs to contain the path to the smart contract .sol file. In module.exports .contracts[':Contract']; the :Contract needs to be replaced with the name of the contract.

In case you need further information just ask.

New contributor
Pave is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

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.