Search in sources :

Example 1 with Type

use of org.web3j.abi.datatypes.Type in project jbpm-work-items by kiegroup.

the class EthereumUtils method observeContractEvent.

public static void observeContractEvent(Web3j web3j, String contractEventName, String contractAddress, List<TypeReference<?>> indexedParameters, List<TypeReference<?>> nonIndexedParameters, String eventReturnType, KieSession kieSession, String signalName, boolean doAbortOnUpdate, WorkItemManager workItemManager, WorkItem workItem) {
    Event event = new Event(contractEventName, indexedParameters, nonIndexedParameters);
    EthFilter filter = new EthFilter(DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST, contractAddress);
    filter.addSingleTopic(EventEncoder.encode(event));
    Class<Type> type = (Class<Type>) AbiTypes.getType(eventReturnType);
    TypeReference<Type> typeRef = TypeReference.create(type);
    web3j.ethLogObservable(filter).subscribe(eventTrigger -> {
        kieSession.signalEvent(signalName, FunctionReturnDecoder.decode(eventTrigger.getData(), Arrays.asList(typeRef)).get(0).getValue());
        if (doAbortOnUpdate) {
            workItemManager.completeWorkItem(workItem.getId(), null);
        }
    });
}
Also used : EthFilter(org.web3j.protocol.core.methods.request.EthFilter) Type(org.web3j.abi.datatypes.Type) Event(org.web3j.abi.datatypes.Event)

Example 2 with Type

use of org.web3j.abi.datatypes.Type 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;
    }
}
Also used : Function(org.web3j.abi.datatypes.Function) Type(org.web3j.abi.datatypes.Type) Transaction(org.web3j.protocol.core.methods.request.Transaction) RawTransaction(org.web3j.crypto.RawTransaction) EthSendTransaction(org.web3j.protocol.core.methods.response.EthSendTransaction) EthCall(org.web3j.protocol.core.methods.response.EthCall)

Example 3 with Type

use of org.web3j.abi.datatypes.Type 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 Type

use of org.web3j.abi.datatypes.Type in project jbpm-work-items by kiegroup.

the class EthereumUtils method observeContractEvent.

public static void observeContractEvent(Web3j web3j, String contractEventName, String contractAddress, List<TypeReference<?>> indexedParameters, List<TypeReference<?>> nonIndexedParameters, String eventReturnType, KieSession kieSession, String signalName, boolean doAbortOnUpdate, WorkItemManager workItemManager, WorkItem workItem, rx.functions.Action1 action1) {
    Event event = new Event(contractEventName, indexedParameters, nonIndexedParameters);
    EthFilter filter = new EthFilter(DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST, contractAddress);
    filter.addSingleTopic(EventEncoder.encode(event));
    Class<Type> type = (Class<Type>) AbiTypes.getType(eventReturnType);
    TypeReference<Type> typeRef = TypeReference.create(type);
    web3j.ethLogObservable(filter).subscribe(action1);
}
Also used : EthFilter(org.web3j.protocol.core.methods.request.EthFilter) Type(org.web3j.abi.datatypes.Type) Event(org.web3j.abi.datatypes.Event)

Example 5 with Type

use of org.web3j.abi.datatypes.Type in project jbpm-work-items by kiegroup.

the class EthereumUtils method getFunction.

public static Function getFunction(String queryName, List<Type> queryInputTypes, List<TypeReference<?>> queryOutputTypes) {
    List<Type> inputParameters;
    if (queryInputTypes != null) {
        inputParameters = queryInputTypes;
    } else {
        inputParameters = new ArrayList<>();
    }
    List<TypeReference<?>> outputTypes;
    if (queryOutputTypes != null) {
        outputTypes = queryOutputTypes;
    } else {
        outputTypes = new ArrayList<>();
    }
    Function function = new Function(queryName, inputParameters, outputTypes);
    return function;
}
Also used : Function(org.web3j.abi.datatypes.Function) Type(org.web3j.abi.datatypes.Type) TypeReference(org.web3j.abi.TypeReference)

Aggregations

Type (org.web3j.abi.datatypes.Type)5 Event (org.web3j.abi.datatypes.Event)2 Function (org.web3j.abi.datatypes.Function)2 EthFilter (org.web3j.protocol.core.methods.request.EthFilter)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 TypeReference (org.web3j.abi.TypeReference)1 Credentials (org.web3j.crypto.Credentials)1 RawTransaction (org.web3j.crypto.RawTransaction)1 Transaction (org.web3j.protocol.core.methods.request.Transaction)1 EthCall (org.web3j.protocol.core.methods.response.EthCall)1 EthSendTransaction (org.web3j.protocol.core.methods.response.EthSendTransaction)1 TransactionReceipt (org.web3j.protocol.core.methods.response.TransactionReceipt)1 HttpService (org.web3j.protocol.http.HttpService)1