cs-codex-dist-tests/Tools/MarketInsights/AverageHistory.cs

51 lines
1.7 KiB
C#
Raw Normal View History

2024-08-21 13:03:20 +00:00
using CodexContractsPlugin;
using CodexContractsPlugin.ChainMonitor;
using Nethereum.Model;
using TestNetRewarder;
2024-08-21 11:59:54 +00:00
using Utils;
namespace MarketInsights
{
2024-08-21 13:03:20 +00:00
public class AverageHistory : ITimeSegmentHandler
2024-08-21 11:59:54 +00:00
{
2024-08-21 13:03:20 +00:00
private readonly List<MarketTimeSegment> contributions = new List<MarketTimeSegment>();
private readonly ChainStateChangeHandlerMux mux = new ChainStateChangeHandlerMux();
private readonly AppState appState;
private readonly int maxContributions;
private readonly ChainState chainState;
2024-08-21 11:59:54 +00:00
2024-08-21 13:03:20 +00:00
public AverageHistory(AppState appState, ICodexContracts contracts, int maxContributions)
2024-08-21 11:59:54 +00:00
{
2024-08-21 13:03:20 +00:00
this.appState = appState;
this.maxContributions = maxContributions;
chainState = new ChainState(appState.Log, contracts, mux, appState.Config.HistoryStartUtc);
2024-08-21 11:59:54 +00:00
}
2024-08-21 13:03:20 +00:00
public MarketTimeSegment[] Segments { get; private set; } = Array.Empty<MarketTimeSegment>();
2024-08-21 11:59:54 +00:00
public Task<TimeSegmentResponse> OnNewSegment(TimeRange timeRange)
2024-08-21 11:59:54 +00:00
{
2024-08-21 13:03:20 +00:00
var contribution = BuildContribution(timeRange);
contributions.Add(contribution);
2024-08-21 11:59:54 +00:00
2024-08-21 13:03:20 +00:00
while (contributions.Count > maxContributions)
{
contributions.RemoveAt(0);
}
2024-08-21 11:59:54 +00:00
2024-08-21 13:03:20 +00:00
Segments = contributions.ToArray();
2024-08-21 11:59:54 +00:00
return Task.FromResult(TimeSegmentResponse.OK);
2024-08-21 11:59:54 +00:00
}
2024-08-21 13:03:20 +00:00
private MarketTimeSegment BuildContribution(TimeRange timeRange)
2024-08-21 11:59:54 +00:00
{
2024-08-21 13:03:20 +00:00
var builder = new ContributionBuilder(timeRange);
mux.Handlers.Add(builder);
chainState.Update(timeRange.To);
mux.Handlers.Remove(builder);
return builder.GetSegment();
2024-08-21 11:59:54 +00:00
}
}
}