JAVA 通过 web3j调用智能合约函数gas不足会导致交易被回滚。问题现象就是调用智能合约函数后,然后再调用对应的查询函数,去查询刚才要求写入的数据,发现数据没写入成功,查询出来的都是写零或空(数字状态变量为零字符串状态变量为空)。
解决办法:
在JAVA代码中加载合约是设置一个很高的 gaslimit。如下面的代码设置 gaslimit 为 100万。
contract2 = OrderData.load(contractAddress2, web3j, credentials, new BigInteger("21000"), new BigInteger("1000000"));
load 方法的代码如下:
public static OrderData load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
return new OrderData(contractAddress, web3j, credentials, gasPrice, gasLimit);
}