use of org.web3j.protocol.core.methods.response.EthSendTransaction in project jbpm-work-items by kiegroup.
the class EthereumUtils method deployContract.
public static String deployContract(Credentials credentials, Web3j web3j, String contractBinary, int toSendEther, boolean waitForReceipt, int sleepDuration, int attempts) throws Exception {
BigInteger depositEtherAmountToSend = BigInteger.valueOf(toSendEther);
RawTransaction rawTransaction = RawTransaction.createContractTransaction(getNextNonce(credentials.getAddress(), web3j), DEFAULT_GAS_PRICE, DEFAULT_GAS_LIMIT, depositEtherAmountToSend, contractBinary);
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
String hexValue = Numeric.toHexString(signedMessage);
EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get();
if (waitForReceipt) {
TransactionReceipt transReceipt = waitForTransactionReceipt(ethSendTransaction.getTransactionHash(), sleepDuration, attempts, web3j);
if (transReceipt != null) {
return transReceipt.getContractAddress();
}
}
// we dont have a contract address
logger.warn("Unable to retrieve contract address.");
return null;
}
use of org.web3j.protocol.core.methods.response.EthSendTransaction in project jbpm-work-items by kiegroup.
the class EthereumWorkitemHandlerTest method setUp.
@Before
public void setUp() {
try {
when(web3j.ethGetBalance(anyString(), any(DefaultBlockParameterName.class))).thenReturn(balanceRequest);
when(balanceRequest.send()).thenReturn(ethGetBalance);
when(ethGetBalance.getBalance()).thenReturn(BigInteger.valueOf(100));
when(transfer.sendFunds(anyString(), any(BigDecimal.class), any(Unit.class))).thenReturn(transferRequest);
when(transferRequest.send()).thenReturn(transactionReceipt);
when(transactionReceipt.getStatus()).thenReturn("testStatus");
when(transactionReceipt.getBlockHash()).thenReturn("testBlockHash");
when(transactionReceipt.getBlockNumber()).thenReturn(BigInteger.valueOf(1));
when(web3j.ethCall(any(Transaction.class), any(DefaultBlockParameterName.class))).thenReturn(ethCallRequest);
when(ethCallRequest.sendAsync()).thenReturn(ethCallCompletable);
when(ethCallCompletable.get()).thenReturn(ethCall);
when(ethCall.getValue()).thenReturn("testResultValue");
when(web3j.ethGetTransactionCount(anyString(), any(DefaultBlockParameterName.class))).thenReturn(ethCountRequest);
when(ethCountRequest.sendAsync()).thenReturn(ethCountCompletable);
when(ethCountCompletable.get()).thenReturn(transactionCount);
when(transactionCount.getTransactionCount()).thenReturn(BigInteger.valueOf(10));
when(web3j.ethSendTransaction(any(Transaction.class))).thenReturn(ethSendRequest);
when(ethSendRequest.sendAsync()).thenReturn(ethSendCompletable);
when(ethSendCompletable.get()).thenReturn(ethSendTransaction);
when(ethSendTransaction.getTransactionHash()).thenReturn("testTransactionHash");
when(web3j.ethSendRawTransaction(anyString())).thenReturn(ethSendRawRequest);
when(ethSendRawRequest.sendAsync()).thenReturn(ethSendRawCompletable);
when(ethSendRawCompletable.get()).thenReturn(ethSendRawTransaction);
when(ethSendRawTransaction.getTransactionHash()).thenReturn("testTransactionHash");
when(web3j.ethGetTransactionReceipt(anyString())).thenReturn(ethSendTransactionReceipt);
when(ethSendTransactionReceipt.send()).thenReturn(ethGetTransactionReceipt);
when(ethGetTransactionReceipt.getTransactionReceipt()).thenReturn(Optional.of(rawTransactionalRecept));
when(ethGetTransactionReceipt.getResult()).thenReturn(rawTransactionalRecept);
when(rawTransactionalRecept.getContractAddress()).thenReturn("testContractAddress");
signalList = new ArrayList();
doAnswer(new Answer() {
public Void answer(InvocationOnMock invocation) {
signalList.add(Arrays.toString(invocation.getArguments()));
return null;
}
}).when(kieSession).signalEvent(anyString(), any(Object.class));
Observable logObservable = PowerMockito.mock(Observable.class);
when(web3j.ethLogObservable(any(EthFilter.class))).thenReturn(logObservable);
when(logObservable.subscribe()).thenReturn(subscription);
when(logObservable.subscribe(any(Action1.class))).thenReturn(subscription);
} catch (Exception e) {
fail(e.getMessage());
}
}
use of org.web3j.protocol.core.methods.response.EthSendTransaction 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.response.EthSendTransaction 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