cs-codex-dist-tests/Tools/TestNetRewarder/Processor.cs

58 lines
1.7 KiB
C#
Raw Normal View History

2024-06-14 09:05:29 +00:00
using CodexContractsPlugin;
using CodexContractsPlugin.ChainMonitor;
2024-01-26 23:17:56 +00:00
using Logging;
using Utils;
namespace TestNetRewarder
{
public class Processor : ITimeSegmentHandler
2024-01-26 23:17:56 +00:00
{
private readonly RequestBuilder builder;
private readonly RewardChecker rewardChecker;
2024-06-27 08:07:10 +00:00
private readonly EventsFormatter eventsFormatter;
2024-06-14 09:05:29 +00:00
private readonly ChainState chainState;
private readonly BotClient client;
2024-01-26 23:17:56 +00:00
private readonly ILog log;
public Processor(Configuration config, BotClient client, ICodexContracts contracts, ILog log)
2024-01-26 23:17:56 +00:00
{
this.client = client;
2024-01-26 23:17:56 +00:00
this.log = log;
2024-06-14 09:05:29 +00:00
builder = new RequestBuilder();
rewardChecker = new RewardChecker(builder);
2024-06-27 08:07:10 +00:00
eventsFormatter = new EventsFormatter();
2024-08-21 11:59:54 +00:00
var handler = new ChainStateChangeHandlerMux(
rewardChecker.Handler,
2024-06-27 08:07:10 +00:00
eventsFormatter
);
2024-06-27 08:07:10 +00:00
chainState = new ChainState(log, contracts, handler, config.HistoryStartUtc);
2024-01-26 23:17:56 +00:00
}
2024-06-14 09:05:29 +00:00
public async Task OnNewSegment(TimeRange timeRange)
2024-01-26 23:17:56 +00:00
{
try
{
2024-06-14 09:05:29 +00:00
chainState.Update(timeRange.To);
2024-06-27 08:07:10 +00:00
var events = eventsFormatter.GetEvents();
var request = builder.Build(events);
if (request.HasAny())
{
await client.SendRewards(request);
}
2024-01-26 23:17:56 +00:00
}
catch (Exception ex)
{
2024-06-21 06:56:20 +00:00
var msg = "Exception processing time segment: " + ex;
log.Error(msg);
2024-06-27 08:07:10 +00:00
eventsFormatter.AddError(msg);
2024-04-01 11:56:07 +00:00
throw;
2024-01-26 23:17:56 +00:00
}
}
}
}