Search code examples
solidityweb3jsmetamask

Why does method return array instead number or string?


I have tried to get data from solidity contract via web3 in Metamask. I don't understand. Why did I get array instead number or string? Maybe do you know other way to getting normal data from ethereum contracts? Consider my code:

let contractAddress = $("#addressContract").val();
instaceContract = web3.eth.contract(contract.abi);
contractInstance= instaceContract.at(contractAddress);

contractInstance.name.call((err, result)=>{
            if(!err){
                console.log('Name ', result)
            } else {
                console.log(err);
            }

        });

contractInstance.decimals.call((err, result)=>{
            if(!err){
                console.log('Decimals ', result)
            } else {
                console.log(err);
            }

        });

I have got responses: For Name: enter image description here

For decimals: enter image description here


Solution

  • The is BigNumber notation.

    Solidity uses big numbers for precision. Javascript numbers have a floating point math problem which means they get rounded up or down when the numbers get very large or very small. Not ideal when those numbers can denote money, as they do in solidity.

    result.toNumber() should do the trick for you.