0

I'm new on Ethereum. I'm trying to deploy my first smart contract in my private blockchain but my function always returns 0. I wrote the simple Smart Contract:

pragma solidity^0.5.0;

contract helloNumber {
    int256 internal thisNumber;
    constructor() public {
        thisNumber = 4;
    }

   function showNumber() external pure returns (int256) {
        return 4;  
   }
} 

I generated Abi and BIN with solc

 solcjs --bin helloNumber.sol
 solcjs --bin helloNumber.sol

result Bin

60806040523480156100115760006000fd5b505b600460006000508190909055505b610026565b6090806100346000396000f3fe608060405234801560105760006000fd5b5060043610602c5760003560e01c806376dad22314603257602c565b60006000fd5b6038604e565b6040518082815260200191505060405180910390f35b6000600490506058565b9056fea265627a7a7231582090f812ea4d2db2a047f1e42576b9cd85f3c0bef7e9392797864143a00ce269c664736f6c634300050b0032

result ABI:

[{"constant":true,"inputs":[],"name":"showNumber","outputs":[{"internalType":"int256","name":"","type":"int256"}],"payable":false,"stateMutability":"pure","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]

in my shell conneted to my private blockchain:

var contract = eth.contract(ABI)
var bytecode = '0xBIN'
var deploy = {from:eth.coinbase, data:bytecode, gas: 2000000}
var object= contract .new(deploy)

object.address //returns the hashcode of smart contract on private blockchain. 

object.showNumber() **//returns always 0.** 

Thanks a lot in advance for your help and sorry if it's a silly question but I'new in Ethereum. what am I doing wrong?

0

1 Answer 1

2

Your Javascript code should be asynchronous (either with async/await, or using Promise objects), because both contract.new and object.showNumber return a Promise.

For example:

async function run() {
    var contract = eth.contract(ABI);
    var bytecode = '0xBIN';
    var deploy = {from: eth.coinbase, data: bytecode, gas: 2000000};
    var object = await contract.new(deploy);
    var n = await object.showNumber();
    console.log(n);
}

Then you can either call run() at the end of your script, or call await run() from some other async function.

Your Answer

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

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