2
0
mirror of synced 2025-02-16 18:37:01 +00:00

116 lines
3.5 KiB
C#
Raw Normal View History

2024-06-14 11:05:29 +02:00
using CodexContractsPlugin;
using CodexContractsPlugin.ChainMonitor;
using DiscordRewards;
using GethPlugin;
2024-01-26 18:17:56 -05:00
using Logging;
using Newtonsoft.Json;
2024-06-14 11:05:29 +02:00
using System.Numerics;
2024-01-26 18:17:56 -05:00
using Utils;
namespace TestNetRewarder
{
2024-06-14 11:05:29 +02:00
public class Processor : ITimeSegmentHandler, IChainStateChangeHandler
2024-01-26 18:17:56 -05:00
{
2024-06-14 11:05:29 +02:00
private readonly RewardChecker rewardChecker = new RewardChecker();
private readonly MarketTracker marketTracker = new MarketTracker();
private readonly ChainState chainState;
private readonly Configuration config;
2024-01-26 18:17:56 -05:00
private readonly ILog log;
2024-03-27 15:39:42 +01:00
private BlockInterval? lastBlockRange;
2024-01-26 18:17:56 -05:00
2024-06-14 11:05:29 +02:00
public Processor(Configuration config, ICodexContracts contracts, ILog log)
2024-01-26 18:17:56 -05:00
{
2024-06-14 11:05:29 +02:00
this.config = config;
2024-01-26 18:17:56 -05:00
this.log = log;
2024-06-14 11:05:29 +02:00
chainState = new ChainState(log, contracts, this, config.HistoryStartUtc);
2024-01-26 18:17:56 -05:00
}
2024-06-14 11:05:29 +02:00
public async Task OnNewSegment(TimeRange timeRange)
2024-01-26 18:17:56 -05:00
{
try
{
2024-06-14 11:05:29 +02:00
chainState.Update(timeRange.To);
2024-04-01 13:56:07 +02:00
await ProcessChainState(chainState);
2024-01-26 18:17:56 -05:00
}
catch (Exception ex)
{
log.Error("Exception processing time segment: " + ex);
2024-04-01 13:56:07 +02:00
throw;
2024-01-26 18:17:56 -05:00
}
}
2024-04-01 13:56:07 +02:00
private async Task ProcessChainState(ChainState chainState)
2024-01-26 18:17:56 -05:00
{
2024-05-30 11:17:13 +02:00
log.Log(chainState.EntireString());
2024-01-26 18:17:56 -05:00
var outgoingRewards = new List<RewardUsersCommand>();
foreach (var reward in rewardRepo.Rewards)
{
ProcessReward(outgoingRewards, reward, chainState);
}
2024-04-07 14:04:31 +02:00
var marketAverages = GetMarketAverages(chainState);
var eventsOverview = GenerateEventsOverview(chainState);
2024-04-07 14:04:31 +02:00
2024-04-13 11:15:39 +02:00
log.Log($"Found {outgoingRewards.Count} rewards. " +
$"Found {marketAverages.Length} market averages. " +
$"Found {eventsOverview.Length} events.");
2024-04-07 14:04:31 +02:00
if (outgoingRewards.Any() || marketAverages.Any() || eventsOverview.Any())
2024-01-26 18:17:56 -05:00
{
if (!await SendRewardsCommand(outgoingRewards, marketAverages, eventsOverview))
2024-02-19 14:56:49 +01:00
{
log.Error("Failed to send reward command.");
}
2024-01-26 18:17:56 -05:00
}
}
private string[] GenerateEventsOverview(ChainState chainState)
{
return chainState.GenerateOverview();
}
2024-04-07 14:04:31 +02:00
private MarketAverage[] GetMarketAverages(ChainState chainState)
{
return marketTracker.ProcessChainState(chainState);
}
2024-06-14 11:05:29 +02:00
public void OnNewRequest(IChainStateRequest request)
2024-01-26 18:17:56 -05:00
{
2024-06-14 11:05:29 +02:00
throw new NotImplementedException();
}
2024-01-26 18:17:56 -05:00
2024-06-14 11:05:29 +02:00
public void OnRequestStarted(IChainStateRequest request)
{
throw new NotImplementedException();
2024-01-26 18:17:56 -05:00
}
2024-06-14 11:05:29 +02:00
public void OnRequestFinished(IChainStateRequest request)
2024-01-26 18:17:56 -05:00
{
2024-06-14 11:05:29 +02:00
throw new NotImplementedException();
2024-01-26 18:17:56 -05:00
}
2024-06-14 11:05:29 +02:00
public void OnRequestFulfilled(IChainStateRequest request)
{
2024-06-14 11:05:29 +02:00
throw new NotImplementedException();
}
2024-06-14 11:05:29 +02:00
public void OnRequestCancelled(IChainStateRequest request)
{
2024-06-14 11:05:29 +02:00
throw new NotImplementedException();
}
public void OnSlotFilled(IChainStateRequest request, BigInteger slotIndex)
{
throw new NotImplementedException();
}
2024-06-14 11:05:29 +02:00
public void OnSlotFreed(IChainStateRequest request, BigInteger slotIndex)
{
throw new NotImplementedException();
}
2024-01-26 18:17:56 -05:00
}
}