use of org.web3j.protocol.core.methods.request.Transaction in project jbpm-work-items by kiegroup.
the class EthereumUtils method queryExistingContract.
public static Object queryExistingContract(Credentials credentials, Web3j web3j, String contractAddress, String contractMethodName, List<Type> contractMethodInputTypes, List<TypeReference<?>> contractMethodOutputTypes) throws Exception {
Function function = getFunction(contractMethodName, contractMethodInputTypes, contractMethodOutputTypes);
Transaction transaction = Transaction.createEthCallTransaction(credentials.getAddress(), contractAddress, getEncodedFunction(function));
EthCall response = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).sendAsync().get();
List<Type> responseTypeList = FunctionReturnDecoder.decode(response.getValue(), function.getOutputParameters());
if (responseTypeList != null && responseTypeList.size() > 0) {
return responseTypeList.get(0).getValue();
} else {
return null;
}
}
use of org.web3j.protocol.core.methods.request.Transaction in project nutzboot by nutzam.
the class EthModule method sendTransaction.
// @POST
@At("/eth/sendTransaction/?/?")
public NutMap sendTransaction(String from, String to, @Param("wei") double wei) {
// from 必须是本地账号
// to 必须是address
NutMap re = new NutMap();
// 检查转账金额
if (wei < 0.01) {
re.put("msg", "起码转账 0.01 eth");
return re;
}
if (wei > 100) {
re.put("msg", "最多转账 100 eth");
return re;
}
Web3jAccount account = web3jCredentials.get(from);
if (account == null) {
return re.setv("msg", "不存在这个本地账号: " + from);
}
// 发起转账
BigInteger value = Convert.toWei(new BigDecimal(wei), Convert.Unit.ETHER).toBigInteger();
Transaction transaction = Transaction.createEtherTransaction(account.getAddress(), null, null, null, to, value);
try {
EthSendTransaction est = web3jAdmin.personalSendTransaction(transaction, account.getPassword()).send();
String hash = est.getTransactionHash();
return re.setv("ok", true).setv("hash", hash);
} catch (Exception e) {
log.warn("转账失败!!!", e);
return re.setv("msg", e.getMessage());
}
}
use of org.web3j.protocol.core.methods.request.Transaction in project jbpm-work-items by kiegroup.
the class EthereumUtils method transactExistingContract.
public static TransactionReceipt transactExistingContract(Credentials credentials, Web3j web3j, int etherAmount, BigInteger gasPrice, BigInteger gasLimit, String toAddress, String methodName, List<Type> methodInputTypes, List<TypeReference<?>> methodOutputTypes, boolean waitForReceipt, int sleepDuration, int attempts) throws Exception {
BigInteger etherAmountToSend = BigInteger.valueOf(etherAmount);
Transaction transaction = Transaction.createFunctionCallTransaction(credentials.getAddress(), getNextNonce(credentials.getAddress(), web3j), gasPrice, gasLimit, toAddress, etherAmountToSend, getEncodedFunction(methodName, methodInputTypes, methodOutputTypes));
EthSendTransaction transactionResponse = web3j.ethSendTransaction(transaction).sendAsync().get();
if (waitForReceipt) {
TransactionReceipt transReceipt = waitForTransactionReceipt(transactionResponse.getTransactionHash(), sleepDuration, attempts, web3j);
return transReceipt;
}
// we dont have a transaction receipt
logger.warn("Unable to retrieve transaction receipt.");
return null;
}
Aggregations