|
| 1 | +package io.api.etherscan.core.impl; |
| 2 | + |
| 3 | +import io.api.etherscan.core.IEventsApi; |
| 4 | +import io.api.etherscan.core.ILogsApi; |
| 5 | +import io.api.etherscan.error.ApiException; |
| 6 | +import io.api.etherscan.executor.IHttpExecutor; |
| 7 | +import io.api.etherscan.manager.IQueueManager; |
| 8 | +import io.api.etherscan.model.Log; |
| 9 | +import io.api.etherscan.model.event.IEvent; |
| 10 | +import io.api.etherscan.model.event.impl.Event; |
| 11 | +import io.api.etherscan.model.query.impl.LogQuery; |
| 12 | +import io.api.etherscan.model.utility.LogResponseTO; |
| 13 | +import io.api.etherscan.util.BasicUtils; |
| 14 | +import org.jetbrains.annotations.NotNull; |
| 15 | + |
| 16 | +import java.util.Collections; |
| 17 | +import java.util.List; |
| 18 | +import java.util.stream.Collectors; |
| 19 | + |
| 20 | +/** |
| 21 | + * Logs API Implementation |
| 22 | + * |
| 23 | + * @see IEventsApi |
| 24 | + * |
| 25 | + */ |
| 26 | +public class EventsApiProvider extends BasicProvider implements IEventsApi { |
| 27 | + |
| 28 | + private static final String ACT_LOGS_PARAM = ACT_PREFIX + "getLogs"; |
| 29 | + |
| 30 | + EventsApiProvider(final IQueueManager queue, |
| 31 | + final String baseUrl, |
| 32 | + final IHttpExecutor executor) { |
| 33 | + super(queue, "logs", baseUrl, executor); |
| 34 | + } |
| 35 | + |
| 36 | + @NotNull |
| 37 | + @Override |
| 38 | + public List<IEvent> events(final LogQuery query) throws ApiException { |
| 39 | + final String urlParams = ACT_LOGS_PARAM + query.getParams(); |
| 40 | + final LogResponseTO response = getRequest(urlParams, LogResponseTO.class); |
| 41 | + BasicUtils.validateTxResponse(response); |
| 42 | + |
| 43 | + if (BasicUtils.isEmpty(response.getResult())) { |
| 44 | + return Collections.emptyList(); |
| 45 | + }; |
| 46 | + return response |
| 47 | + .getResult() |
| 48 | + .stream() |
| 49 | + .map((log) -> { |
| 50 | + String eventTypeHash = log.getTopics().get(0); |
| 51 | + return IEvent.createEvent(eventTypeHash, log); |
| 52 | + }) |
| 53 | + .collect(Collectors.toList()); |
| 54 | + } |
| 55 | +} |
0 commit comments