use of org.web3j.abi.TypeReference in project winery by eclipse.
the class ProvenanceSmartContractWrapper method getProvenance.
public CompletableFuture<List<ModelProvenanceElement>> getProvenance(final String identifier) {
// eventName, indexed parameters, unindexed parameters
final Event event = new Event("ResourceVersion", Arrays.asList(new TypeReference<Utf8String>() {
}, new TypeReference<Address>() {
}, new TypeReference<Bytes>() {
}));
EthFilter filter = new EthFilter(DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST, contract.getContractAddress()).addSingleTopic(EventEncoder.encode(event)).addOptionalTopics(Hash.sha3String(identifier)).addNullTopic();
final CompletableFuture<List<ModelProvenanceElement>> result = new CompletableFuture<>();
try {
final int recordsCount = web3j.ethGetLogs(filter).send().getLogs().size();
LOGGER.info(recordsCount + " provenance elements detected.");
if (recordsCount > 0) {
final List<ModelProvenanceElement> provenanceElements = new ArrayList<>();
final Disposable subscription = ((Provenance) contract).resourceVersionEventFlowable(filter).subscribe(resourceVersionEventResponse -> {
try {
final ModelProvenanceElement currentElement = generateProvenanceElement(resourceVersionEventResponse);
provenanceElements.add(currentElement);
if (provenanceElements.size() == recordsCount) {
result.complete(provenanceElements);
}
} catch (EthereumException e) {
result.completeExceptionally(e);
}
});
// unsubscribe the observable when the CompletableFuture completes (this frees threads)
result.whenComplete((r, e) -> subscription.dispose());
} else {
// empty result
result.complete(new ArrayList<>());
}
} catch (IOException e) {
final String msg = "Failed detecting the number of provenance elements for the collaboration resource. Reason: " + e.getMessage();
LOGGER.error(msg);
result.completeExceptionally(new EthereumException(msg, e));
}
return result;
}
use of org.web3j.abi.TypeReference in project winery by eclipse.
the class AuthorizationSmartContractWrapper method getAuthorizationTree.
/**
* Retrieves the {@link AuthorizationInfo} from the blockchain.
* If no authorization data can be retrieved, the completable future returns <code>null</code>.
*
* @param identifier The process identifier identifying the collaboration process.
* @return A completable future containing the authorization information.
*/
public CompletableFuture<AuthorizationInfo> getAuthorizationTree(final String identifier) {
// eventName, indexed parameters, unindexed parameters
final Event event = new Event("Authorized", Arrays.asList(new TypeReference<Utf8String>() {
}, new TypeReference<Address>() {
}, new TypeReference<Address>() {
}, new TypeReference<Utf8String>() {
}));
EthFilter filter = new EthFilter(DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST, contract.getContractAddress()).addSingleTopic(EventEncoder.encode(event)).addOptionalTopics(Hash.sha3String(identifier)).addNullTopic().addNullTopic();
final CompletableFuture<AuthorizationInfo> result = new CompletableFuture<>();
try {
final int recordsCount = web3j.ethGetLogs(filter).send().getLogs().size();
LOGGER.info(recordsCount + " authorization elements detected.");
if (recordsCount > 0) {
final List<AuthorizationElement> authorizationElements = new ArrayList<>();
final Disposable subscription = ((Authorization) contract).authorizedEventFlowable(filter).subscribe(authorizedEventResponse -> {
try {
final AuthorizationElement currentElement = generateAuthorizationElement(authorizedEventResponse);
authorizationElements.add(currentElement);
if (authorizationElements.size() == recordsCount) {
final AuthorizationTree tree = new AuthorizationTree(authorizationElements);
result.complete(tree);
}
} catch (EthereumException e) {
result.completeExceptionally(e);
}
});
// unsubscribe the observable when the CompletableFuture completes (this frees threads)
result.whenComplete((r, e) -> subscription.dispose());
} else {
// empty result
result.complete(null);
}
} catch (IOException e) {
final String msg = "Failed detecting the number of authorization elements for the collaboration resource. Reason: " + e.getMessage();
LOGGER.error(msg);
result.completeExceptionally(new EthereumException(msg, e));
}
return result;
}
use of org.web3j.abi.TypeReference in project jbpm-work-items by kiegroup.
the class QueryExistingContractWorkitemHandler method executeWorkItem.
public void executeWorkItem(WorkItem workItem, WorkItemManager workItemManager) {
try {
String serviceURL = (String) workItem.getParameter("ServiceURL");
String contractAddress = (String) workItem.getParameter("ContractAddress");
String contractMethodName = (String) workItem.getParameter("ContractMethodName");
String contractMethodOutputType = (String) workItem.getParameter("MethodOutputType");
if (StringUtils.isNotEmpty(serviceURL) && StringUtils.isNotEmpty(contractAddress) && StringUtils.isNotEmpty(contractMethodName)) {
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();
List<TypeReference<?>> outputTypeList = null;
if (contractMethodOutputType != null) {
Class typeClazz = Class.forName(contractMethodOutputType);
outputTypeList = Collections.singletonList(TypeReference.create(typeClazz));
}
Object queryReturnObj = EthereumUtils.queryExistingContract(credentials, web3j, contractAddress, contractMethodName, null, outputTypeList);
results.put(RESULTS, queryReturnObj);
workItemManager.completeWorkItem(workItem.getId(), results);
} else {
logger.error("Missing service url, valid address or method name to execute.");
throw new IllegalArgumentException("Missing service url, valid address or method name to execute.");
}
} catch (Exception e) {
logger.error("Error executing workitem: " + e.getMessage());
handleException(e);
}
}
use of org.web3j.abi.TypeReference 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;
}
Aggregations