2024-06-17 13:34:08 +00:00
|
|
|
|
using CodexContractsPlugin.ChainMonitor;
|
|
|
|
|
using DiscordRewards;
|
|
|
|
|
using GethPlugin;
|
|
|
|
|
using NethereumWorkflow;
|
|
|
|
|
using System.Numerics;
|
|
|
|
|
|
|
|
|
|
namespace TestNetRewarder
|
|
|
|
|
{
|
|
|
|
|
public interface IRewardGiver
|
|
|
|
|
{
|
|
|
|
|
void Give(RewardConfig reward, EthAddress receiver);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class RewardCheck : IChainStateChangeHandler
|
|
|
|
|
{
|
|
|
|
|
private readonly RewardConfig reward;
|
|
|
|
|
private readonly IRewardGiver giver;
|
|
|
|
|
|
|
|
|
|
public RewardCheck(RewardConfig reward, IRewardGiver giver)
|
|
|
|
|
{
|
|
|
|
|
this.reward = reward;
|
|
|
|
|
this.giver = giver;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-27 09:43:25 +00:00
|
|
|
|
public void OnNewRequest(RequestEvent requestEvent)
|
2024-06-17 13:34:08 +00:00
|
|
|
|
{
|
2024-06-27 09:43:25 +00:00
|
|
|
|
if (MeetsRequirements(CheckType.ClientPostedContract, requestEvent))
|
2024-06-17 13:34:08 +00:00
|
|
|
|
{
|
2024-06-27 09:43:25 +00:00
|
|
|
|
GiveReward(reward, requestEvent.Request.Client);
|
2024-06-17 13:34:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-27 09:43:25 +00:00
|
|
|
|
public void OnRequestCancelled(RequestEvent requestEvent)
|
2024-06-17 13:34:08 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-21 11:59:54 +00:00
|
|
|
|
public void OnRequestFailed(RequestEvent requestEvent)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-27 09:43:25 +00:00
|
|
|
|
public void OnRequestFinished(RequestEvent requestEvent)
|
2024-06-17 13:34:08 +00:00
|
|
|
|
{
|
2024-06-27 09:43:25 +00:00
|
|
|
|
if (MeetsRequirements(CheckType.HostFinishedSlot, requestEvent))
|
2024-06-17 13:34:08 +00:00
|
|
|
|
{
|
2024-06-27 09:43:25 +00:00
|
|
|
|
foreach (var host in requestEvent.Request.Hosts.GetHosts())
|
2024-06-17 13:34:08 +00:00
|
|
|
|
{
|
|
|
|
|
GiveReward(reward, host);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-27 09:43:25 +00:00
|
|
|
|
public void OnRequestFulfilled(RequestEvent requestEvent)
|
2024-06-17 13:34:08 +00:00
|
|
|
|
{
|
2024-06-27 09:43:25 +00:00
|
|
|
|
if (MeetsRequirements(CheckType.ClientStartedContract, requestEvent))
|
2024-06-17 13:34:08 +00:00
|
|
|
|
{
|
2024-06-27 09:43:25 +00:00
|
|
|
|
GiveReward(reward, requestEvent.Request.Client);
|
2024-06-17 13:34:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-27 09:43:25 +00:00
|
|
|
|
public void OnSlotFilled(RequestEvent requestEvent, EthAddress host, BigInteger slotIndex)
|
2024-06-17 13:34:08 +00:00
|
|
|
|
{
|
2024-06-27 09:43:25 +00:00
|
|
|
|
if (MeetsRequirements(CheckType.HostFilledSlot, requestEvent))
|
2024-06-17 13:34:08 +00:00
|
|
|
|
{
|
|
|
|
|
if (host != null)
|
|
|
|
|
{
|
|
|
|
|
GiveReward(reward, host);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-27 09:43:25 +00:00
|
|
|
|
public void OnSlotFreed(RequestEvent requestEvent, BigInteger slotIndex)
|
2024-06-17 13:34:08 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void GiveReward(RewardConfig reward, EthAddress receiver)
|
|
|
|
|
{
|
|
|
|
|
giver.Give(reward, receiver);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-27 09:43:25 +00:00
|
|
|
|
private bool MeetsRequirements(CheckType type, RequestEvent requestEvent)
|
2024-06-17 13:34:08 +00:00
|
|
|
|
{
|
|
|
|
|
return
|
|
|
|
|
reward.CheckConfig.Type == type &&
|
2024-06-27 09:43:25 +00:00
|
|
|
|
MeetsDurationRequirement(requestEvent.Request) &&
|
|
|
|
|
MeetsSizeRequirement(requestEvent.Request);
|
2024-06-17 13:34:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool MeetsSizeRequirement(IChainStateRequest r)
|
|
|
|
|
{
|
|
|
|
|
var slotSize = r.Request.Ask.SlotSize.ToDecimal();
|
|
|
|
|
decimal min = reward.CheckConfig.MinSlotSize.SizeInBytes;
|
|
|
|
|
return slotSize >= min;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool MeetsDurationRequirement(IChainStateRequest r)
|
|
|
|
|
{
|
|
|
|
|
var duration = TimeSpan.FromSeconds((double)r.Request.Ask.Duration);
|
|
|
|
|
return duration >= reward.CheckConfig.MinDuration;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|