0

I use this easy code to display the public variable of a smart contract:

window.addEventListener('load', function () {

    if(typeof Web3 !== 'undefined'){
        var web3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io/MY_KEY"));
        console.log('Found web3!');
        startApp();
    }else{
        console.log("No web3!");
    }


    function startApp(){

        var SalesABI = web3.eth.contract([{ 
          // loads of lines
        }]);
        var SalesAddress      = 'address';
        var SalesContract     =  SalesABI.at(SalesAddress);

    displayETHRaised(SalesContract, web3);
    }

});


function displayETHRaised(_SalesContract,_web3) {
    _SalesContract.tokenRaised((_err,_resp) => {
        if (_err != null) {
            console.log(_err);
        } else {
            $('#DOM_ID').innerHTML = _web3.fromWei(_resp, "ether").toString(10);
        }
    });
}

I do not get the value of the public var and I don't get an error nor warning.

Everything seems to be fine but I do not get the value of the public variable tokenRaised.

  • Do you mean your callback gets called but there's an error in _err? Or it gets called with no error but the value in _resp is wrong? Or your callback never gets invoked? Are there any errors in the console? – smarx Jan 11 '18 at 5:04
1

Try to use web3js-raw which is a simple wrapper around web3js.

0

Ok, I solved it the next day. The bug was somewhere else.

If someone want to use the code it's fine except do not use .innerHTML on the jQuery object.

  • yep, u should use instead ".html('aabbcc')" – Eli Livshitz Jul 29 '18 at 14:39

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.