Web3J에서
1. rawTransaction을 이용하여 트랜잭션을 실행할 수도 있고,
RawTransaction rawTransaction = RawTransaction.createTransaction(
nonce,
BigInteger.valueOf(2000000020L), //gasPrice
BigInteger.valueOf(500000L), //gasLimit
contractAddr,
encodedFunction);
Credentials credentials = WalletUtils.loadCredentials("passwd1234",
"/root/.ethereum/keystore/UTC--2018-09-14T05-50-45.788742882Z--59cb2cf3e712c4...");
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
String hexValue = Numeric.toHexString(signedMessage);
ethCall = web3j.ethSendRawTransaction(hexValue).sendAsync().get();
2. createFunctionCallTransaction을 이용해 트랜잭션을 실행할 수도 있다. <- 이걸 추천한다.
단, 이 방식은 수행 하기 전에 unlock Account하는 게 정석이다.
Web3j web3j = Web3j.build(new HttpService(web3jAddress));
List<Type> inputParameters = Arrays.asList(new Uint8(param));
List<TypeReference<?>> outputParameters = Arrays.asList(new TypeReference<Type>() {});
Function function = new Function("setValue", inputParameters, outputParameters);
String encodedFunction = FunctionEncoder.encode(function);
EthSendTransaction ethCall = null;
try {
//Nonce
EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
fromAddr, DefaultBlockParameterName.LATEST).sendAsync().get();
BigInteger nonce = ethGetTransactionCount.getTransactionCount();
log.info("==NONCE:" + nonce);
//Run
ethCall = web3j.ethSendTransaction(
Transaction.createFunctionCallTransaction(
fromAddr,
nonce, //or nullL
this.getGasPrice(), //gasPrice
BigInteger.valueOf(27000), //gasLimit
contractAddr,
encodedFunction)
).sendAsync().get();
}catch (Exception e) {
e.printStackTrace();
}
String transactionHash = ethCall.getTransactionHash();