1

I'm currently using web3 0.20.6 and I'm planning to stay in [email protected] because there are issues with HTTP in the beta versions of [email protected].* like

The current provider doesn't support subscriptions: HttpProvider

Now I'm trying to figure out how to get the contract address after being deployed. Because in [email protected].*, we can use like the example below to get the contract address.

    contract.deploy({
        data: '0x'+bin,
        arguments: [request.body.vin, cost, request.body.buyer]
    })
    .send({
        from: request.body.dealer,
        gas: gas
    })
    .on('receipt', receipt => {
        util.log(`>>>>> contractApi - Contract sucessfully deployed @ address: ${receipt.contractAddress}`);

        data.contract_address = receipt.contractAddress;

        response.json(data);
    });

But how do you do it in [email protected] ? I couldn't find the solutions online without using truffle. I'm not planning to use truffle as well.

What I did try was,

const deployedContract = LoginContract.new ({
    data: '0x'+binLogin,
    from: '6ded1c5b448819a6cde4293e33fbe54583ef5c52',
    gas: 4700000
});

contractData = deployedContract.address;
util.log(`>>>>> setup - Login Contract sucessfully deployed @ address: ${contractData}`)

But the result in console is undefined address

22 Mar 14:04:15 - >>>>> setup - Login Contract sucessfully deployed @ address: undefined

Then after about 5-10 seconds, this popped up in the console

Error: The contract code couldn't be stored, please check your gas amount.

I'm like super confused right now because the gas amount should be sufficient.

EDIT 1 Thanks to @mirg's answer, the deployment is successful. However, I noticed that the instructions below the contract deployment:

contractData = deployedContract.address;
util.log(`>>>>> setup - Login Contract sucessfully deployed @ address: ${contractData}`)

continue to run and does not wait for the contract deployment to complete/finish.

How do you make further instructions to wait and process the result of that contract deployment? In [email protected].* it is achievable by using

.on('receipt', receipt => {}

But I have no idea how it is achieved using [email protected].

1 Answer 1

0

In the second case you're not passing the constructor parameters to the deploy function.

const deployedContract = LoginContract.new (request.body.vin, cost, request.body.buyer, {
    data: '0x'+binLogin,
    from: '6ded1c5b448819a6cde4293e33fbe54583ef5c52',
    gas: 4700000
}, function(err, myContract){
    if(!err) {
       // NOTE: The callback will fire twice!
       // Once the contract has the transactionHash property set and once its deployed on an address.

       // e.g. check tx hash on the first call (transaction send)
       if(!myContract.address) {
           console.log(myContract.transactionHash) // The hash of the transaction, which deploys the contract

       // check address on the second call (contract deployed)
       } else {
           console.log(myContract.address) // the contract address
       }

       // Note that the returned "myContractReturned" === "myContract",
       // so the returned "myContractReturned" object will also get the address set.
    }
  }));

this should work ref

2
  • Thanks for the answer @mirg, I noticed that the instructions after the contract deployment continue to run without waiting for the deployment to finish. How to make the instructions to wait in [email protected] just like in [email protected].*? Mar 22, 2018 at 4:11
  • you have to provide a callback (which will be fired twice). See my updated answer
    – qbsp
    Mar 22, 2018 at 4:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.