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

62 lines
1.9 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;
private readonly MarketTracker marketTracker;
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);
marketTracker = new MarketTracker(config, log);
2024-06-27 08:07:10 +00:00
eventsFormatter = new EventsFormatter();
var handler = new ChainChangeMux(
rewardChecker.Handler,
2024-06-27 08:07:10 +00:00
marketTracker,
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);
var averages = marketTracker.GetAverages();
2024-06-27 08:07:10 +00:00
var events = eventsFormatter.GetEvents();
2024-06-27 08:07:10 +00:00
var request = builder.Build(averages, 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
}
}
}
}