Search in sources :

Example 1 with TransactionReceipt

use of org.web3j.protocol.core.methods.response.TransactionReceipt 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;
}
Also used : EthSendTransaction(org.web3j.protocol.core.methods.response.EthSendTransaction) RawTransaction(org.web3j.crypto.RawTransaction) TransactionReceipt(org.web3j.protocol.core.methods.response.TransactionReceipt) EthGetTransactionReceipt(org.web3j.protocol.core.methods.response.EthGetTransactionReceipt) BigInteger(java.math.BigInteger)

Example 2 with TransactionReceipt

use of org.web3j.protocol.core.methods.response.TransactionReceipt in project jbpm-work-items by kiegroup.

the class SendEtherWorkitemHandler method executeWorkItem.

public void executeWorkItem(WorkItem workItem, WorkItemManager workItemManager) {
    try {
        String serviceURL = (String) workItem.getParameter("ServiceURL");
        String amount = (String) workItem.getParameter("Amount");
        String toAddress = (String) workItem.getParameter("ToAddress");
        if (StringUtils.isNotEmpty(serviceURL) && StringUtils.isNotEmpty(toAddress) && StringUtils.isNotEmpty(amount) && NumberUtils.isDigits(amount)) {
            Map<String, Object> results = new HashMap<String, Object>();
            if (web3j == null) {
                web3j = Web3j.build(new HttpService(serviceURL));
            }
            auth = new EthereumAuth(walletPassword, walletPath, classLoader);
            Credentials credentials = auth.getCredentials();
            TransactionManager transactionManager = new RawTransactionManager(web3j, credentials);
            if (transfer == null) {
                transfer = new Transfer(web3j, transactionManager);
            }
            int amountToSend = 0;
            if (amount != null) {
                amountToSend = Integer.parseInt(amount);
            }
            TransactionReceipt transactionReceipt = EthereumUtils.sendFundsToContract(credentials, web3j, amountToSend, toAddress, transfer);
            results.put(RESULTS, transactionReceipt);
            workItemManager.completeWorkItem(workItem.getId(), results);
        } else {
            logger.error("Missing service url or valid ether amount or toAddress.");
            throw new IllegalArgumentException("Missing service url or valid ether amount or toAddress.");
        }
    } catch (Exception e) {
        logger.error("Error executing workitem: " + e.getMessage());
        handleException(e);
    }
}
Also used : HashMap(java.util.HashMap) TransactionReceipt(org.web3j.protocol.core.methods.response.TransactionReceipt) RawTransactionManager(org.web3j.tx.RawTransactionManager) HttpService(org.web3j.protocol.http.HttpService) RawTransactionManager(org.web3j.tx.RawTransactionManager) TransactionManager(org.web3j.tx.TransactionManager) Transfer(org.web3j.tx.Transfer) Credentials(org.web3j.crypto.Credentials)

Example 3 with TransactionReceipt

use of org.web3j.protocol.core.methods.response.TransactionReceipt in project jbpm-work-items by kiegroup.

the class TransactExistingContractWorkitemHandler method executeWorkItem.

public void executeWorkItem(WorkItem workItem, WorkItemManager workItemManager) {
    try {
        String serviceURL = (String) workItem.getParameter("ServiceURL");
        String contractAddress = (String) workItem.getParameter("ContractAddress");
        String waitForReceiptStr = (String) workItem.getParameter("WaitForReceipt");
        String methodName = (String) workItem.getParameter("MethodName");
        Type methodInputType = (Type) workItem.getParameter("MethodInputType");
        String depositAmount = (String) workItem.getParameter("DepositAmount");
        if (StringUtils.isNotEmpty(serviceURL) && StringUtils.isNotEmpty(contractAddress) && StringUtils.isNotEmpty(methodName)) {
            Map<String, Object> results = new HashMap<String, Object>();
            if (web3j == null) {
                web3j = Web3j.build(new HttpService(serviceURL));
            }
            auth = new EthereumAuth(walletPassword, walletPath, classLoader);
            Credentials credentials = auth.getCredentials();
            int depositEtherAmountToSend = 0;
            if (depositAmount != null) {
                depositEtherAmountToSend = Integer.parseInt(depositAmount);
            }
            boolean waitForReceipt = false;
            if (waitForReceiptStr != null) {
                waitForReceipt = Boolean.parseBoolean(waitForReceiptStr);
            }
            List<Type> methodInputTypeList = new ArrayList<>();
            if (methodInputType != null) {
                methodInputTypeList = Collections.singletonList(methodInputType);
            }
            TransactionReceipt transactionReceipt = EthereumUtils.transactExistingContract(credentials, web3j, depositEtherAmountToSend, EthereumUtils.DEFAULT_GAS_PRICE, EthereumUtils.DEFAULT_GAS_LIMIT, contractAddress, methodName, methodInputTypeList, null, waitForReceipt, EthereumUtils.DEFAULT_SLEEP_DURATION, EthereumUtils.DEFAULT_ATTEMPTS);
            results.put(RESULTS, transactionReceipt);
            workItemManager.completeWorkItem(workItem.getId(), results);
        } else {
            logger.error("Missing service url, valid toAddress or method name to execute.");
            throw new IllegalArgumentException("Missing service url, valid toAddress or method name to execute.");
        }
    } catch (Exception e) {
        logger.error("Error executing workitem: " + e.getMessage());
        handleException(e);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TransactionReceipt(org.web3j.protocol.core.methods.response.TransactionReceipt) Type(org.web3j.abi.datatypes.Type) HttpService(org.web3j.protocol.http.HttpService) Credentials(org.web3j.crypto.Credentials)

Example 4 with TransactionReceipt

use of org.web3j.protocol.core.methods.response.TransactionReceipt in project AppCoins-ethereumj by AppStoreFoundation.

the class TransactionFactory method fromEthGetTransactionReceipt.

public static Transaction fromEthGetTransactionReceipt(EthGetTransactionReceipt ethGetTransactionReceipt) {
    TransactionReceipt transactionReceipt = ethGetTransactionReceipt.getTransactionReceipt();
    String hash = transactionReceipt.getTransactionHash();
    String from = transactionReceipt.getFrom();
    Log log = transactionReceipt.getLogs().get(0);
    String to = log.getAddress();
    String value = extractValueFromEthGetTransactionReceipt(log.getData());
    Status status = parseStatus(transactionReceipt.getStatus());
    String contractAddress = ethGetTransactionReceipt.getTransactionReceipt().getTo();
    return new Transaction(hash, from, to, value, status);
}
Also used : Status(com.asf.appcoins.sdk.iab.entity.Transaction.Status) EthTransaction(org.web3j.protocol.core.methods.response.EthTransaction) Transaction(com.asf.appcoins.sdk.iab.entity.Transaction) Log(org.web3j.protocol.core.methods.response.Log) TransactionReceipt(org.web3j.protocol.core.methods.response.TransactionReceipt) EthGetTransactionReceipt(org.web3j.protocol.core.methods.response.EthGetTransactionReceipt)

Example 5 with TransactionReceipt

use of org.web3j.protocol.core.methods.response.TransactionReceipt in project jbpm-work-items by kiegroup.

the class EthereumWorkitemHandlerTest method testSendEther.

@Test
public void testSendEther() throws Exception {
    TestWorkItemManager manager = new TestWorkItemManager();
    WorkItemImpl workItem = new WorkItemImpl();
    workItem.setParameter("ServiceURL", "http://localhost:8545/");
    workItem.setParameter("Amount", "10");
    workItem.setParameter("ToAddress", "0x00211e7e");
    SendEtherWorkitemHandler handler = new SendEtherWorkitemHandler(TEST_WALLET_PASSWORD, "wallet/testwallet.json");
    handler.setWeb3j(web3j);
    handler.setTransfer(transfer);
    handler.executeWorkItem(workItem, manager);
    assertNotNull(manager.getResults());
    assertEquals(1, manager.getResults().size());
    assertTrue(manager.getResults().containsKey(workItem.getId()));
    TransactionReceipt receipt = (TransactionReceipt) manager.getResults().get(workItem.getId()).get("Receipt");
    assertNotNull(receipt);
}
Also used : TestWorkItemManager(org.jbpm.process.workitem.core.TestWorkItemManager) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) TransactionReceipt(org.web3j.protocol.core.methods.response.TransactionReceipt) EthGetTransactionReceipt(org.web3j.protocol.core.methods.response.EthGetTransactionReceipt) SendEtherWorkitemHandler(org.jbpm.process.workitem.ethereum.SendEtherWorkitemHandler) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

TransactionReceipt (org.web3j.protocol.core.methods.response.TransactionReceipt)7 EthGetTransactionReceipt (org.web3j.protocol.core.methods.response.EthGetTransactionReceipt)4 BigInteger (java.math.BigInteger)2 HashMap (java.util.HashMap)2 Credentials (org.web3j.crypto.Credentials)2 RawTransaction (org.web3j.crypto.RawTransaction)2 EthSendTransaction (org.web3j.protocol.core.methods.response.EthSendTransaction)2 HttpService (org.web3j.protocol.http.HttpService)2 Transaction (com.asf.appcoins.sdk.iab.entity.Transaction)1 Status (com.asf.appcoins.sdk.iab.entity.Transaction.Status)1 LocalDateTime (java.time.LocalDateTime)1 ArrayList (java.util.ArrayList)1 WorkItemImpl (org.drools.core.process.instance.impl.WorkItemImpl)1 Provenance (org.eclipse.winery.accountability.blockchain.ethereum.generated.Provenance)1 TestWorkItemManager (org.jbpm.process.workitem.core.TestWorkItemManager)1 SendEtherWorkitemHandler (org.jbpm.process.workitem.ethereum.SendEtherWorkitemHandler)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 Type (org.web3j.abi.datatypes.Type)1 Transaction (org.web3j.protocol.core.methods.request.Transaction)1