0

I'm deploying a contract with web3js successfully, but the fallback function does not get called a second time after finishing the deployment.

I read that this was an on-going issue (people not implementing it properly?), but I couldn't solve it even with dirty fixes like waiting for quiet some time and then try to access the contract.address.

Successfully deployed contract: https://ropsten.etherscan.io/tx/0xee03d8da3546e9c8aef1bc1704d20708f34b33f54c2ccfeccb870f5baeb242e6

var deployedContract = contract.new(constructor_param,params,function(error,result){
if(error){
    console.log("error contract.new: " + error);
} else {
    console.log("fallback no error");
    if(result.address){
        // gets set in the second call
        console.log("contract address: " + result.address);
        console.log(createEtherscanIoUrl('address',result.address));
    } else {
        // gets set in the first call
        console.log("contract transaction hash: " + result.transactionHash);
        console.log(createEtherscanIoUrl('tx',result.transactionHash));
    }
}
});

function createEtherscanIoUrl(type,hashOrNumber){

var etherscanBaseUrl='https://ropsten.etherscan.io/';

var url = etherscanBaseUrl;
if(type === 'tx'){
    url += 'tx/'+hashOrNumber;
} else if(type === 'block'){
    url += 'block/'+hashOrNumber;
} else if(type === 'address'){
    url += 'address/'+hashOrNumber;
}
return url;
}

How can I solve this issue, as I'm not able to continue working with the freshly deployed contract when I'm not receiving the address after deployment.

As I'm quiet new to this topic which causes me being limited in thinking about how to solve this issue. Any hint/help appreciated.

2

1 Answer 1

0

To get the address of your deployed contract, you can follow an approach like this: with async and wait instead of giving a callback.

const path = require('path');
const fs  = require('fs');
const solc = require('solc');
const Web3 = require('web3');
const HDWalletProvider = require('truffle-hdwallet-provider');

const relativeFolderPath = '../../contracts';
const fullPath = path.resolve(__dirname, relativeFolderPath , 'ContractName.sol');

const sourceCode = fs.readFileSync(fullPath, 'utf8');

const {interface, bytecode} = solc.compile(sourceCode, 1).contracts[':ContractName'];

// this is assuming that you are deploying to a local private block chain
const httpProvider = new Web3.providers.HttpProvider("http://localhost:8545");

// if deploying to a rinkeby testnet you can use something like
// provide your own 12-word secret mnemonic and infura node gateway
const rinkebyProvider = new HDWalletProvider(
    'crack use cream badge what ever mnemonic here enter vague tank',
    'https://rinkeby.infura.io/v3/4000b577b214e70bdbff8c5be270'
);

// or if you have metamask extension setup in your browser, you can use this
const metamaskProvider = window.web3.currentProvider;

const web3 = new Web3(httpProvider);

const deploy = async () => {

    // Get a list of all accounts
    accounts = await web3.eth.getAccounts();
    console.log('Attempting to deploy contract from account', accounts[0]);

    console.log(interface);
    const constructorArgumentsList = [] // put them in here, if you have any

    // Use one of those accounts to deploy the contract
    // Note the arguments are randomly chosen, they can be anything
    const result = await new web3.eth.Contract(JSON.parse(interface))
        .deploy({ data: bytecode, arguments: constructorArgumentsList  })
        .send({ from: accounts[0], gas: '4000000' });

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

};

deploy();

Your Answer

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

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