简体   繁体   中英

Web3js - Return smart contract address immediately after deployment

I have a smart contract which I am deploying using the Web3.js package. I have a function called deploySmartContract() which does so, and I am expecting this method to return contract address to the calling function. Below is the snippet for deploySmartContract() -

function deploySmartContract(shareName, symbol, maxSupply) {
    var _shareName = shareName;
    var _symbol = symbol;
    var _maxSupply = maxSupply;
    var contractAddr = '';

    var sharesregistry = contractObj.new(
        _shareName,
        _symbol,
        _maxSupply,
        {
            from: primaryAccount, 
            data: byteCode, 
            gas: '5000000'
        }, function (e, contract){
            console.log(e, contract);
            if (typeof contract.address !== 'undefined') {
                console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
            }
        }
    );
    console.log(sharesregistry.address);

    return sharesregistry;
}

How can I keep my return statement waiting unless the whole transaction is completed and execute return when contract is mined? Else I am getting just an skeleton of shareregistry object at the calling function.

Here, I confirm that this code deploys the smart contract perfectly.

Checkout API new method and how to call it synchronously and asynchronously. You have provided callback functions which means you run it asynchronously. By removing it, you force it to wait for a result.

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