3

I am using web3.js to deploy a contract. I am also using Ganache to mock the network and visualise the accounts.

When the code below executes, the contract gets created, and Ganache shows a successful transaction with the contract address displayed, however the contract.options.address is null, not matter where I try to print it.

web3.eth.getAccounts().then(accounts => {
    const customerAddr = accounts[0];
    let contract = new web3.eth.Contract(ContractConfigs.ABI);
    contract.deploy({ data: ContractConfigs.ByteCode.object })
    .send({
        gas: ContractConfigs.GasFees,
        from: customerAddr,
        value: price
    }, () => {
        console.log(contract.options.address); // null
    });
}).catch(err => { console.log(err); });

Is there anything wrong I am doing? Also, I am okay with any other way to get the address of the contract if any.

1

Change this:

    send({...}, () => {
        console.log(contract.options.address); // null
    });

To this:

    .send({...}).on('receipt', (receipt) => {
      console.log(receipt.contractAddress); // 0xf4cb...
    });
| improve this answer | |
  • This fired an error, it looks like receipt don't get passed to the callback, but it gave me a hint to try .on('receipt', (receipt) => { console.log(receipt.contractAddress); } instead of the callback, and it worked! Thanks. – Ahmed Hammad Jun 24 at 14:59
  • @AhmedHammad: You're welcome, feel free to update the answer :) – goodvibration Jun 24 at 15:00

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.