Web3j
是否可以使用 web3j 的智能合約包裝器呼叫智能合約的應付功能?
我們如何使用 web3j 呼叫智能合約的應付功能?
更新:
我將手動更改生成的智能合約包裝類。我現在有:
public Future<TransactionReceipt> uploadSignedString(Uint256 verificationId, Utf8String signedString) { Function function = new Function("uploadSignedString", Arrays.<Type>asList( verificationId, signedString ), Collections.<TypeReference<?>>emptyList()); return executeTransactionAsync(function); }
我要手動添加:
public TransactionReceipt uploadSignedStringPayable(Uint256 verificationId, Utf8String signedString, BigInteger weiValue) throws InterruptedException, TransactionTimeoutException, IOException { Function function = new Function( "uploadSignedString", Arrays.<Type>asList( verificationId, signedString ), Collections.<TypeReference<?>>emptyList()); return executeTransaction(FunctionEncoder.encode(function), weiValue); }
正如我所見,它只能作為同步呼叫來完成。
更新
從 2.3.0 版開始,web3j 生成帶有附加參數的包裝器,
weiValue
用於支付功能。例如生成這樣的程式碼
public Future<TransactionReceipt> pay(Utf8String name, BigInteger weiValue) { Function function = new Function("pay", Arrays.<Type>asList(name), Collections.<TypeReference<?>>emptyList()); return executeTransactionAsync(function, weiValue); }
對於這個 Solidiy 程式碼:
function pay(string name) payable public {}
舊答案
似乎在目前版本的 web3j 中是不可能的。
這是它為不帶參數的應付函式生成的包裝器程式碼:
public Future<TransactionReceipt> foo() { Function function = new Function("foo", Arrays.<Type>asList(), Collections.<TypeReference<?>>emptyList()); return executeTransactionAsync(function); }
executeTransactionAsync
不接受乙太幣,但有同步executeTransaction(String data, BigInteger weiValue)
的。可以手動呼叫
foo
ether:Function function = new Function("foo", Arrays.<Type>asList(), Collections.<TypeReference<?>>emptyList()); String encodedFunction = FunctionEncoder.encode(function); Transaction transaction = Transaction.createFunctionCallTransaction( from, gasPrice, gasLimit, contractAddress, BigInteger.ONE, encodedFunction); EthSendTransaction transactionResponse = web3j.ethSendTransaction(transaction).sendAsync().get();