2024-06-14 11:05:29 +02:00
|
|
|
|
using CodexContractsPlugin;
|
|
|
|
|
using CodexContractsPlugin.ChainMonitor;
|
|
|
|
|
using DiscordRewards;
|
2024-01-29 11:02:47 -05:00
|
|
|
|
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-05-29 14:05:16 +02:00
|
|
|
|
|
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);
|
2024-04-08 16:07:52 +02:00
|
|
|
|
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
|
|
|
|
|
2024-04-12 08:35:20 +02:00
|
|
|
|
if (outgoingRewards.Any() || marketAverages.Any() || eventsOverview.Any())
|
2024-01-26 18:17:56 -05:00
|
|
|
|
{
|
2024-04-08 16:07:52 +02: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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-08 16:07:52 +02: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-01-29 11:02:47 -05:00
|
|
|
|
|
2024-06-14 11:05:29 +02:00
|
|
|
|
public void OnRequestFulfilled(IChainStateRequest request)
|
2024-01-29 11:02:47 -05:00
|
|
|
|
{
|
2024-06-14 11:05:29 +02:00
|
|
|
|
throw new NotImplementedException();
|
2024-01-29 11:02:47 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-14 11:05:29 +02:00
|
|
|
|
public void OnRequestCancelled(IChainStateRequest request)
|
2024-01-29 11:02:47 -05:00
|
|
|
|
{
|
2024-06-14 11:05:29 +02:00
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnSlotFilled(IChainStateRequest request, BigInteger slotIndex)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
2024-01-29 11:02:47 -05:00
|
|
|
|
|
2024-06-14 11:05:29 +02:00
|
|
|
|
public void OnSlotFreed(IChainStateRequest request, BigInteger slotIndex)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
2024-01-29 11:02:47 -05:00
|
|
|
|
}
|
2024-01-26 18:17:56 -05:00
|
|
|
|
}
|
|
|
|
|
}
|