有点晚了,但也许会帮助某人:
你可以试试这个:
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.Bool;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.RawTransaction;
import org.web3j.protocol.core.methods.response.EthGetTransactionCount;
import org.web3j.utils.Convert;
import org.web3j.utils.Numeric;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import java.util.function.Function;
// create credentials
String privateKeyHexValue = Numeric.toHexString(privateKey.data());
Credentials credentials = Credentials.create(privateKeyHexValue);
// get the next available nonce
EthGetTransactionCount ethGetTransactionCount = web3.ethGetTransactionCount(credentials.getAddress(), DefaultBlockParameterName.LATEST).send();
BigInteger nonce = ethGetTransactionCount.getTransactionCount();
// Value to transfer (ERC20 token)
BigInteger balance = Convert.toWei("1", Convert.Unit.ETHER).toBigInteger();
// create transfer function
Function function = transfer(dstAddress, balance);
String encodedFunction = FunctionEncoder.encode(function);
// Gas Parameters
BigInteger gasLimit = BigInteger.valueOf(71000); // you should get this from api
BigInteger gasPrice = new BigInteger("d693a400", 16); // decimal 3600000000
// create the transaction
RawTransaction rawTransaction =
RawTransaction.createTransaction(
nonce, gasPrice, gasLimit, < ERC20_CONTRACT_ADDRESS >, encodedFunction);
// sign the transaction
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
String hexValue = Numeric.toHexString(signedMessage);
// Send transaction
EthSendTransaction ethSendTransaction = web3.ethSendRawTransaction(hexValue).sendAsync().get();
String transactionHash = ethSendTransaction.getTransactionHash();
传递函数可以是:
private Function transfer(String to, BigInteger value) {
return new Function(
"transfer",
Arrays.asList(new Address(to), new Uint256(value)),
Collections.singletonList(new TypeReference<Bool>() {
}));
}