Solidity
在 Web3J 發送方法中傳遞特定地址
如何在Web3J的發送方法中傳遞特定地址?
例如,使用 web3 javascript 庫,我們可以從如下地址傳遞。
contract.someMethod().send({ from: this.props.accounts[0] })
但是在 Java 包裝器中,沒有為
send()
方法定義參數。有沒有辦法在web3j中傳遞特定地址?
如果您想發送
unsigned
交易並讓客戶拿起from
用於簽署交易的帳戶。您可以執行以下操作:乙太交易
Transaction transaction = Transaction.createEtherTransaction(from, nonce, gasPrice, gasLimit, to, value); web3.ethSendTransaction(transaction);
智能合約交易
Function function = new Function<>( "functionName", // function we're calling Arrays.asList(new Type(value), ...), // Parameters to pass as Solidity Types Arrays.asList(new TypeReference<Type>() {}, ...)); String encodedFunction = FunctionEncoder.encode(function) Transaction transaction = Transaction.createContractTransaction(from, nonce, gasPrice, encodedFunction); web3.ethSendTransaction(transaction);
交易收據:
String txHash = web3.ethSendTransaction(transaction).send().getTransactionHash(); EthGetTransactionReceipt transactionReceipt = web3j.ethGetTransactionReceipt(txHash).send(); if (transactionReceipt.getTransactionReceipt.isPresent()) { String contractAddress = transactionReceipt.get().getContractAddress(); } else { // try again until it's mined }