From 2f6256756ac77c0dcc024c943c9abe6a052b705d Mon Sep 17 00:00:00 2001 From: Ben Date: Mon, 22 Apr 2024 14:27:09 +0200 Subject: [PATCH] Updates market averages --- .../DiscordRewards/GiveRewardsCommand.cs | 3 +- Tools/BiblioTech/Commands/MarketCommand.cs | 9 +- Tools/TestNetRewarder/Configuration.cs | 3 - Tools/TestNetRewarder/MarketTracker.cs | 123 +++++++----------- 4 files changed, 49 insertions(+), 89 deletions(-) diff --git a/Framework/DiscordRewards/GiveRewardsCommand.cs b/Framework/DiscordRewards/GiveRewardsCommand.cs index 48dabcc..5a30cfa 100644 --- a/Framework/DiscordRewards/GiveRewardsCommand.cs +++ b/Framework/DiscordRewards/GiveRewardsCommand.cs @@ -15,8 +15,7 @@ public class MarketAverage { - public int NumberOfFinished { get; set; } - public int TimeRangeSeconds { get; set; } + public string Title { get; set; } = string.Empty; public float Price { get; set; } public float Size { get; set; } public float Duration { get; set; } diff --git a/Tools/BiblioTech/Commands/MarketCommand.cs b/Tools/BiblioTech/Commands/MarketCommand.cs index 0df58d5..29100b9 100644 --- a/Tools/BiblioTech/Commands/MarketCommand.cs +++ b/Tools/BiblioTech/Commands/MarketCommand.cs @@ -1,7 +1,6 @@ using BiblioTech.Options; using DiscordRewards; using System.Globalization; -using Utils; namespace BiblioTech.Commands { @@ -31,13 +30,7 @@ namespace BiblioTech.Commands private string[] GetInsight(MarketAverage avg) { - var timeRange = TimeSpan.FromSeconds(avg.TimeRangeSeconds); - var headerLine = $"[Last {Time.FormatDuration(timeRange)}] ({avg.NumberOfFinished} Contracts finished)"; - - if (avg.NumberOfFinished == 0) - { - return new[] { headerLine }; - } + var headerLine = $"[{avg.Title}]"; return new[] { diff --git a/Tools/TestNetRewarder/Configuration.cs b/Tools/TestNetRewarder/Configuration.cs index c180648..9dc06f4 100644 --- a/Tools/TestNetRewarder/Configuration.cs +++ b/Tools/TestNetRewarder/Configuration.cs @@ -19,9 +19,6 @@ namespace TestNetRewarder [Uniform("check-history", "ch", "CHECKHISTORY", true, "Unix epoc timestamp of a moment in history on which processing begins. Required for hosting rewards. Should be 'launch of the testnet'.")] public int CheckHistoryTimestamp { get; set; } = 0; - [Uniform("market-insights", "mi", "MARKETINSIGHTS", false, "Semi-colon separated integers. Each represents a multiple of intervals, for which a market insights average will be generated.")] - public string MarketInsights { get; set; } = "1;96"; - [Uniform("events-overview", "eo", "EVENTSOVERVIEW", false, "When greater than zero, chain event summary will be generated. (default 1)")] public int CreateChainEventsOverview { get; set; } = 1; diff --git a/Tools/TestNetRewarder/MarketTracker.cs b/Tools/TestNetRewarder/MarketTracker.cs index 17e5e82..8705097 100644 --- a/Tools/TestNetRewarder/MarketTracker.cs +++ b/Tools/TestNetRewarder/MarketTracker.cs @@ -6,66 +6,47 @@ namespace TestNetRewarder { public class MarketTracker { - private readonly List buffer = new List(); + private readonly MarketAverage MostRecent = new MarketAverage + { + Title = "Most recent" + }; + private readonly MarketAverage Irf = new MarketAverage + { + Title = "Recent average" + }; public MarketAverage[] ProcessChainState(ChainState chainState) { - var intervalCounts = GetInsightCounts(); - if (!intervalCounts.Any()) return Array.Empty(); + UpdateMostRecent(chainState); + UpdateIrf(chainState); - UpdateBuffer(chainState, intervalCounts.Max()); - var result = intervalCounts - .Select(GenerateMarketAverage) - .Where(a => a != null) - .Cast() - .ToArray(); - - if (!result.Any()) result = Array.Empty(); - return result; - } - - private void UpdateBuffer(ChainState chainState, int maxNumberOfIntervals) - { - buffer.Add(chainState); - while (buffer.Count > maxNumberOfIntervals) + return new[] { - buffer.RemoveAt(0); - } + MostRecent, + Irf + }; } - private MarketAverage? GenerateMarketAverage(int numberOfIntervals) + private void UpdateIrf(ChainState chainState) { - var states = SelectStates(numberOfIntervals); - return CreateAverage(states); + if (!chainState.FinishedRequests.Any()) return; + + MostRecent.Price = GetIrf(MostRecent.Price, chainState, s => s.Request.Ask.Reward); + MostRecent.Duration = GetIrf(MostRecent.Duration, chainState, s => s.Request.Ask.Duration); + MostRecent.Size = GetIrf(MostRecent.Size, chainState, s => GetTotalSize(s.Request.Ask)); + MostRecent.Collateral = GetIrf(MostRecent.Collateral, chainState, s => s.Request.Ask.Collateral); + MostRecent.ProofProbability = GetIrf(MostRecent.ProofProbability, chainState, s => s.Request.Ask.ProofProbability); } - private ChainState[] SelectStates(int numberOfIntervals) + private void UpdateMostRecent(ChainState chainState) { - if (numberOfIntervals < 1) return Array.Empty(); - if (numberOfIntervals > buffer.Count) return Array.Empty(); - return buffer.TakeLast(numberOfIntervals).ToArray(); - } + if (!chainState.FinishedRequests.Any()) return; - private MarketAverage? CreateAverage(ChainState[] states) - { - try - { - return new MarketAverage - { - NumberOfFinished = CountNumberOfFinishedRequests(states), - TimeRangeSeconds = GetTotalTimeRange(states), - Price = Average(states, s => s.Request.Ask.Reward), - Duration = Average(states, s => s.Request.Ask.Duration), - Size = Average(states, s => GetTotalSize(s.Request.Ask)), - Collateral = Average(states, s => s.Request.Ask.Collateral), - ProofProbability = Average(states, s => s.Request.Ask.ProofProbability) - }; - } - catch (Exception ex) - { - Program.Log.Error($"Exception in CreateAverage: {ex}"); - return null; - } + MostRecent.Price = Average(chainState, s => s.Request.Ask.Reward); + MostRecent.Duration = Average(chainState, s => s.Request.Ask.Duration); + MostRecent.Size = Average(chainState, s => GetTotalSize(s.Request.Ask)); + MostRecent.Collateral = Average(chainState, s => s.Request.Ask.Collateral); + MostRecent.ProofProbability = Average(chainState, s => s.Request.Ask.ProofProbability); } private int GetTotalSize(Ask ask) @@ -75,50 +56,40 @@ namespace TestNetRewarder return nSlots * slotSize; } - private float Average(ChainState[] states, Func getValue) + private float Average(ChainState state, Func getValue) { - return Average(states, s => Convert.ToInt32(getValue(s))); + return Average(state, s => Convert.ToInt32(getValue(s))); } - private float Average(ChainState[] states, Func getValue) + private float GetIrf(float current, ChainState state, Func getValue) + { + return GetIrf(current, state, s => Convert.ToInt32(getValue(s))); + } + + private float Average(ChainState state, Func getValue) { var sum = 0.0f; var count = 0.0f; - foreach (var state in states) + foreach (var finishedRequest in state.FinishedRequests) { - foreach (var finishedRequest in state.FinishedRequests) - { - sum += getValue(finishedRequest); - count++; - } + sum += getValue(finishedRequest); + count++; } if (count < 1.0f) return 0.0f; return sum / count; } - private int GetTotalTimeRange(ChainState[] states) + private float GetIrf(float current, ChainState state, Func getValue) { - return Convert.ToInt32((Program.Config.Interval * states.Length).TotalSeconds); - } - - private int CountNumberOfFinishedRequests(ChainState[] states) - { - return states.Sum(s => s.FinishedRequests.Length); - } - - private int[] GetInsightCounts() - { - try + var result = current; + foreach (var finishedRequest in state.FinishedRequests) { - var tokens = Program.Config.MarketInsights.Split(';').ToArray(); - return tokens.Select(t => Convert.ToInt32(t)).ToArray(); + float v = getValue(finishedRequest); + result = (result + v) / 2.0f; } - catch (Exception ex) - { - Program.Log.Error($"Exception when parsing MarketInsights config parameters: {ex}"); - } - return Array.Empty(); + + return result; } } }