2024-06-17 13:34:08 +00:00
|
|
|
|
using CodexContractsPlugin.ChainMonitor;
|
|
|
|
|
using CodexContractsPlugin.Marketplace;
|
|
|
|
|
using DiscordRewards;
|
|
|
|
|
using System.Numerics;
|
|
|
|
|
|
|
|
|
|
namespace TestNetRewarder
|
|
|
|
|
{
|
|
|
|
|
public class MarketBuffer
|
|
|
|
|
{
|
2024-06-27 09:43:25 +00:00
|
|
|
|
private readonly List<RequestEvent> requestEvents = new List<RequestEvent>();
|
2024-06-17 13:34:08 +00:00
|
|
|
|
private readonly TimeSpan bufferSpan;
|
|
|
|
|
|
|
|
|
|
public MarketBuffer(TimeSpan bufferSpan)
|
|
|
|
|
{
|
|
|
|
|
this.bufferSpan = bufferSpan;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-27 09:43:25 +00:00
|
|
|
|
public void Add(RequestEvent requestEvent)
|
2024-06-17 13:34:08 +00:00
|
|
|
|
{
|
2024-06-27 09:43:25 +00:00
|
|
|
|
requestEvents.Add(requestEvent);
|
2024-06-17 13:34:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update()
|
|
|
|
|
{
|
|
|
|
|
var now = DateTime.UtcNow;
|
2024-06-27 09:43:25 +00:00
|
|
|
|
requestEvents.RemoveAll(r => (now - r.Request.FinishedUtc) > bufferSpan);
|
2024-06-17 13:34:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MarketAverage? GetAverage()
|
|
|
|
|
{
|
2024-06-27 09:43:25 +00:00
|
|
|
|
if (requestEvents.Count == 0) return null;
|
2024-06-17 13:34:08 +00:00
|
|
|
|
|
|
|
|
|
return new MarketAverage
|
|
|
|
|
{
|
2024-06-27 09:43:25 +00:00
|
|
|
|
NumberOfFinished = requestEvents.Count,
|
2024-06-17 13:34:08 +00:00
|
|
|
|
TimeRangeSeconds = (int)bufferSpan.TotalSeconds,
|
|
|
|
|
Price = Average(s => s.Request.Ask.Reward),
|
|
|
|
|
Duration = Average(s => s.Request.Ask.Duration),
|
|
|
|
|
Size = Average(s => GetTotalSize(s.Request.Ask)),
|
|
|
|
|
Collateral = Average(s => s.Request.Ask.Collateral),
|
|
|
|
|
ProofProbability = Average(s => s.Request.Ask.ProofProbability)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private float Average(Func<IChainStateRequest, BigInteger> getValue)
|
|
|
|
|
{
|
2024-06-21 07:33:52 +00:00
|
|
|
|
return Average(s =>
|
|
|
|
|
{
|
|
|
|
|
var value = getValue(s);
|
|
|
|
|
return (int)value;
|
|
|
|
|
});
|
2024-06-17 13:34:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private float Average(Func<IChainStateRequest, int> getValue)
|
|
|
|
|
{
|
|
|
|
|
var sum = 0.0f;
|
2024-06-27 09:43:25 +00:00
|
|
|
|
float count = requestEvents.Count;
|
|
|
|
|
foreach (var r in requestEvents)
|
2024-06-17 13:34:08 +00:00
|
|
|
|
{
|
2024-06-27 09:43:25 +00:00
|
|
|
|
sum += getValue(r.Request);
|
2024-06-17 13:34:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (count < 1.0f) return 0.0f;
|
|
|
|
|
return sum / count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int GetTotalSize(Ask ask)
|
|
|
|
|
{
|
|
|
|
|
var nSlots = Convert.ToInt32(ask.Slots);
|
2024-06-21 07:59:24 +00:00
|
|
|
|
var slotSize = (int)ask.SlotSize;
|
2024-06-17 13:34:08 +00:00
|
|
|
|
return nSlots * slotSize;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|