我在可靠方面有简单的智能合约。我想在部署智能合约后调用函数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;
}
}发布于 2021-02-05 20:26:56
是的,在部署合约后,您将看到"hello world 2“,但您永远不会看到"newHello”输出。因为每当您使用setHello()函数时,它都会设置空字符串。
发布于 2021-02-04 14:41:10
为了获取公共变量,编译器自动生成函数。在这种情况下,您可以使用hash c660ab0e函数获取问候字符串

或者使用您的函数getHello()。
对于调用函数(例如,helloStr()),您应该使用:
{"jsonrpc":"2.0","method":"eth_call",“params”:{“to”:“填写您的智能合约地址”,"data":"0xc660ab0e"},"latest","id":1}
或者对call使用web3:
https://stackoverflow.com/questions/66036914
复制相似问题