Solidity中部署智能合约后调用智能合约函数

Call function of smart contract after deploying smart contract in Solidity

我在 Solidity 中有简单的智能合约。我想在部署智能合约后调用函数 getHello()(以查看已部署合约中的变量而无需自己调用)。我可以这样做吗?

    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;
        
    }
    
    
}

获取public变量编译器自动形成函数。 在您的情况下,您可以获得带有散列 c660ab0e

函数的 hello 字符串

或者使用你的函数 getHello()。

对于调用函数(例如,helloStr()),您应该使用:

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

或者使用 web3 调用:

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

是的,部署合约后你会看到“hello world 2”,但你永远看不到“newHello”输出。因为每当您使用 setHello() 函数时它都会设置空字符串。