1
votes

I have simple smart contract in Solidity. I want to call the function getHello() after I deployed the smart contract (to see variable in deployed contract without calling it by myself). Can I do this?

    pragma solidity 0.5.12;

    contract Hello {
    
    string public helloStr = "Hello, world 2!";
    
    uint[] public nums = [10, 20, 30];
    
    function getHello() public view returns (string memory) {
        
        
        return helloStr;
        
    }
    
    function pushNewElement(uint newElement) public returns (uint) {
        
        nums.push(newElement);
        
    }
    
    function popLastElement () public returns (uint) {
        
        nums.pop();
        
    }
    
   
    function setHello(string memory newHello) public {
        
        
        helloStr = newHello;
        
    }
    
    
}
2

2 Answers

0
votes

Yes, you will see "hello world 2" after deployed the contract but you will never be able to see "newHello" output. because it sets the empty string whenever you setHello() function is using.

1
votes

For getting public variable the compiler automatically formed function. In you case you can get hello string with function with hash c660ab0e

enter image description here

Or use your function getHello().

For calling function(example, helloStr()) you should use:

{"jsonrpc":"2.0","method":"eth_call", "params":[{"to":"address your smart contract", "data":"0xc660ab0e"}, "latest"],"id":1}

Or use web3 with call:

https://web3js.readthedocs.io/en/v1.2.0/web3-eth.html#call