您需要使用executeTransaction
包裹在RemoteCall
.
Function function = new Function(...);
RemoteCall<TransactionReceipt> remoteCall = new RemoteCall(() -> {
//call to executeTransaction
});
TransactionReceipt receipt = remoteCall.send();
您可以使用 web3j 的代码生成工具为您的智能合约创建简单的包装器,让自己的生活更轻松。请参阅web3j 文档的这一部分,了解如何生成代码。生成的类处理远程调用(以及constant
函数的本地调用)。您的客户端代码如下所示:
Web3j web3j = Web3j.build(new HttpService());
Credentials credentials = Credentials.create(<YOUR_PRIVATE_KEY>);
SimpleContract contract = SimpleContract.load(<CONTRACT_ADDRESS>, web3j, credentials, BigInteger.valueOf(<GAS_PRICE>), BigInteger.valueOf(<GAS_LIMIT));
RemoteCall<TransactionReceipt> remoteCall = contract.setValue(BigInteger.valueOf(5)); // Call to smart contract setValue(5)
TransactionReceipt receipt = remoteCall.send();
编辑以添加代码生成示例
$ solc --version
solc, the solidity compiler commandline interface
Version: 0.4.19+commit.c4cbbb05.Windows.msvc
$ java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)
$ solc contracts/SimpleContract.sol --bin --abi --optimize -o build/
$ web3j.bat solidity generate build/SimpleContract.bin build/SimpleContract.abi -o ./src -p mypackage
_ _____ _ _
| | |____ (_) (_)
__ _____| |__ / /_ _ ___
\ \ /\ / / _ \ '_ \ \ \ | | | / _ \
\ V V / __/ |_) |.___/ / | _ | || (_) |
\_/\_/ \___|_.__/ \____/| |(_)|_| \___/
_/ |
|__/
Generating mypackage.SimpleContract ... File written to .\src
注意 - 我正在使用 Cygwin 在 Windows 上运行。因此,web3j.bat
用法。