using System; using System.Threading.Tasks; using Nethereum.Contracts; using Nethereum.RPC.Eth.DTOs; namespace Nethereum.BlockchainProcessing.Processor { public class EventLogProcessorHandler : ProcessorHandler where TEvent : new() { Func, Task> _eventAction; Func, Task> _eventCriteria; public EventLogProcessorHandler( Func, Task> action) : this(action, null) { } public EventLogProcessorHandler( Func, Task> action, Func, Task> eventCriteria) { _eventAction = action; _eventCriteria = eventCriteria; SetMatchCriteriaForEvent(); } public EventLogProcessorHandler( Action> action) : this(action, null) { } public EventLogProcessorHandler( Action> action, Func, bool> eventCriteria) { _eventAction = (l) => { action(l); return Task.FromResult(0); }; if (eventCriteria != null) { _eventCriteria = async (l) => { return await Task.FromResult(eventCriteria(l)); }; } SetMatchCriteriaForEvent(); } private void SetMatchCriteriaForEvent() { base.SetMatchCriteria(async log => { if (await Task.FromResult(log.IsLogForEvent()) == false) return false; if (_eventCriteria == null) return true; var eventLog = log.DecodeEvent(); return await _eventCriteria(eventLog).ConfigureAwait(false); }); } protected override Task ExecuteInternalAsync(FilterLog value) { var eventLog = value.DecodeEvent(); return _eventAction(eventLog); } } }