Skip to content

Commit

Permalink
Merge pull request #2 from kadena-community/vote-popup
Browse files Browse the repository at this point in the history
Vote popup
  • Loading branch information
aserranoni authored Nov 28, 2024
2 parents 49ccfe0 + 384bd51 commit 17ea9d1
Show file tree
Hide file tree
Showing 46 changed files with 1,728 additions and 1,583 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ Kadena Cabinet is a Community Advisory Board (CAB) where KDA token holders come
- **Chainweb Node Instance:**
- An instance of the [Chainweb Node](https://github.com/kadena-io/chainweb-node) with the `allowReadsInLocal: true` parameter set.

- **One of the Following:**
- A [Kadena GraphQL](https://github.com/kadena-community/kadena.js/tree/main/packages/apps/graph) server.
- Access to the `events` endpoint from [Chainweb Data](https://github.com/kadena-io/chainweb-data).
- SQL access to the PostgreSQL database from [Chainweb Data](https://github.com/kadena-io/chainweb-data).


- A **[Kadena GraphQL](https://github.com/kadena-community/kadena.js/tree/main/packages/apps/graph)** server

## Project Structure

- `pact/`: Smart contracts and related files for on-chain governance.
Expand Down
1 change: 1 addition & 0 deletions backend/API/Interfaces/IBondService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public interface IBondService
Task<List<Lockup>> GetAllLockups(bool ignoreCache = false);
Task<List<LockEvent>> GetAllLockupEvents(bool ignoreCache = false);
Task<List<ClaimEvent>> GetAllClaimEvents(bool ignoreCache = false);
Task<List<VoteEvent>> GetAllVoteEvents(bool ignoreCache = false);
Task<Lockup> GetLockup(string lockupId, bool ignoreCache = false);
Task<List<Lockup>> GetAccountLockups(string account, bool ignoreCache = false);
Task<List<IBondEvent>> GetAllLockupsFromBond(string bondId, bool ignoreCache = false);
Expand Down
9 changes: 0 additions & 9 deletions backend/API/Interfaces/IChainwebDataRetriever.cs

This file was deleted.

37 changes: 35 additions & 2 deletions backend/API/Models/BondDashboard.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Dab.API.Models.Events;

namespace Dab.API.Models.Dashboard;

public class LockDTO
Expand All @@ -17,6 +19,17 @@ public LockDTO(EventDTO evt)
RequestKey = evt.RequestKey;
LockTime = DateTime.TryParse(evt.BlockTime, out var lockTime) ? lockTime : default;
}

public LockDTO(LockEvent evt)
{
BondId = evt.BondId;
Account = evt.Account;
LockedAmount = evt.Amount;
MaxRewards = evt.Rewards;
RequestKey = evt.RequestKey;
LockTime = evt.Timestamp;
}

}

public class ClaimDTO
Expand All @@ -36,6 +49,16 @@ public ClaimDTO(EventDTO evt)
RequestKey = evt.RequestKey;
ClaimTime = DateTime.TryParse(evt.BlockTime, out var lockTime) ? lockTime : default;
}
public ClaimDTO(ClaimEvent evt)
{
BondId = evt.BondId;
Account = evt.Account;
Amount = evt.OriginalAmount;
TotalAmount = evt.TotalAmount;
RequestKey = evt.RequestKey;
ClaimTime = evt.Timestamp;
}

}

public class PollVoteEventDTO
Expand All @@ -47,12 +70,21 @@ public class PollVoteEventDTO
public string RequestKey { get; set; } = "";
public PollVoteEventDTO(EventDTO evt)
{
PollId = evt.Params[0]?.ToString();
Account = evt.Params[1]?.ToString();
PollId = evt.Params[1]?.ToString();
Account = evt.Params[0]?.ToString();
Action = evt.Params[2]?.ToString();
RequestKey = evt.RequestKey;
VoteTime = DateTime.TryParse(evt.BlockTime, out var lockTime) ? lockTime : default;
}
public PollVoteEventDTO(VoteEvent evt)
{
PollId = evt.PollId;
Account = evt.Account;
Action = evt.Action;
RequestKey = evt.RequestKey;
VoteTime = evt.Timestamp;
}

}


Expand Down Expand Up @@ -84,6 +116,7 @@ public class BondDashboard
public List<LockDTO> LatestLocks { get; set; } = new();
public List<ClaimDTO> LatestClaims { get; set; } = new();
public List<PollVoteEventDTO> LatestVotes { get; set; } = new();
public bool ActivePool {get; set;}
}

public class VoteDistributionDTO
Expand Down
2 changes: 0 additions & 2 deletions backend/API/Models/Bonder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Dab.API.Models.Bonder;

//FIXME these types require more testing (not handled {decimal:})

public class LockupOption
{
[JsonPropertyName("option-name")]
Expand Down
2 changes: 2 additions & 0 deletions backend/API/Models/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public class CacheKeys

public static string AllVotes() => $"all-votes";

public static string AllVoteEvents() => $"all-vote-events";

public static string ApiAnalytics() => $"api-dashboard";
}
}
9 changes: 9 additions & 0 deletions backend/API/Models/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,12 @@ public class ClaimEvent : IBondEvent

public decimal Amount => TotalAmount;
}

public class VoteEvent
{
public string Account { get; set; } = "";
public string PollId { get; set; } = "";
public string Action { get; set; } = "";
public DateTime Timestamp { get; set; }
public string RequestKey { get; set; } = "";
}
1 change: 0 additions & 1 deletion backend/API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@
// Business logic services
// builder.Services.AddSingleton<IDaoService, DaoService>();
builder.Services.AddSingleton<IPactService, PactService>();
builder.Services.AddSingleton<IChainwebDataRetriever, ChainwebDataRetriever>();
builder.Services.AddSingleton<ChainwebGraphQLRetriever>();
builder.Services.AddSingleton<IBondService, BondService>();
builder.Services.AddSingleton<IPollService, PollService>();
Expand Down
40 changes: 21 additions & 19 deletions backend/API/Services/AnalyticsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,9 @@ private async Task<List<EventDTO>> CacheEventSearch(string evt, bool ignoreCache

public async Task<List<LockDTO>> GetLatestLocks(bool ignoreCache = false)
{
var lockupsInContract = await _bondService.GetAllLockups();
if (!lockupsInContract.Any()) return new List<LockDTO>();
var lockupsInContract = await _bondService.GetAllLockupEvents();

var evt = $"{ns}.bonder.LOCK";
var query = await CacheEventSearch(evt, ignoreCache);

return query
return lockupsInContract
.Select(evt => new LockDTO(evt))
.OrderByDescending(dto => dto.LockTime)
.Take(3)
Expand All @@ -83,13 +79,9 @@ public async Task<List<LockDTO>> GetLatestLocks(bool ignoreCache = false)

public async Task<List<ClaimDTO>> GetLatestClaims(bool ignoreCache = false)
{
var lockupsInContract = await _bondService.GetAllLockups();
if (!lockupsInContract.Any()) return new List<ClaimDTO>();

var evt = $"{ns}.bonder.CLAIM";
var query = await CacheEventSearch(evt, ignoreCache);
var claimsInContract = await _bondService.GetAllClaimEvents();

return query
return claimsInContract
.Select(evt => new ClaimDTO(evt))
.OrderByDescending(dto => dto.ClaimTime)
.Take(3)
Expand All @@ -98,13 +90,9 @@ public async Task<List<ClaimDTO>> GetLatestClaims(bool ignoreCache = false)

public async Task<List<PollVoteEventDTO>> GetLatestVotes(bool ignoreCache = false)
{
var lockupsInContract = await _bondService.GetAllLockups();
if (!lockupsInContract.Any()) return new List<PollVoteEventDTO>();

var evt = $"{ns}.poller.VOTE";
var query = await CacheEventSearch(evt, ignoreCache);
var votesInContract = await _bondService.GetAllVoteEvents();

return query
return votesInContract
.Select(evt => new PollVoteEventDTO(evt))
.OrderByDescending(dto => dto.VoteTime)
.Take(3)
Expand Down Expand Up @@ -240,7 +228,8 @@ public async Task<ServiceResult> GetApiAnalytics(bool ignoreCache = false)
MostVotedPoll = await SafeExecute(async () => await GetMostVotedPoll(ignoreCache), ""),
AverageLockup = await SafeExecute(async () => await GetAverageLockup(ignoreCache), ""),
MaxReturnRate = await SafeExecute(async () => await GetMaxReturnRate(ignoreCache), 0m),
TotalLockedAmount = await SafeExecute(async () => (await _bondService.GetAllLockupEvents(ignoreCache)).Sum(x => x.Amount + x.Rewards), 0m)
TotalLockedAmount = await SafeExecute(async () => (await _bondService.GetAllLockupEvents(ignoreCache)).Sum(x => x.Amount + x.Rewards), 0m),
ActivePool = await SafeExecute(async () => (await IsPollActive(ignoreCache)), false)
};

var jsonResult = Utils.JsonPrettify(ret);
Expand Down Expand Up @@ -268,6 +257,19 @@ private async Task<T> SafeExecute<T>(Func<Task<T>> func, T defaultValue)
}
}

private async Task<bool> IsPollActive(bool ignoreCache = false)
{

try
{
var pools = await _pollService.GetActivePolls(ignoreCache);
return pools.Any();
}
catch
{
return false;
}
}

private async Task<decimal> GetGlobalGivenRewards(bool ignoreCache = false)
{
Expand Down
76 changes: 43 additions & 33 deletions backend/API/Services/BondService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@ public class BondService : IBondService
private readonly ILogger<BondService> _logger;
private readonly ICacheService _cacheService;
private readonly IPactService _pactService;
private readonly IChainwebDataRetriever _dataRetriever;
private readonly ChainwebGraphQLRetriever _graphRetriever;
private readonly DabContractConfig _dabConfig;
private readonly string chain;
private readonly string ns;
private readonly int expirySeconds = 20;

public BondService(ILogger<BondService> logger, ICacheService cacheService, IPactService pactService, IConfiguration configuration, IChainwebDataRetriever dataRetriever, ChainwebGraphQLRetriever graphRetriever)
public BondService(ILogger<BondService> logger, ICacheService cacheService, IPactService pactService, IConfiguration configuration, ChainwebGraphQLRetriever graphRetriever)
{
_logger = logger;
_cacheService = cacheService;
_pactService = pactService;
_dataRetriever = dataRetriever;
_graphRetriever = graphRetriever;

_dabConfig = (configuration.GetSection("DabContractConfig").Get<DabContractConfig>() ??
Expand All @@ -34,47 +32,55 @@ public BondService(ILogger<BondService> logger, ICacheService cacheService, IPac
ns = _dabConfig.Namespace;
}

public async Task<List<LockEvent>> GetAllLockupEvents(bool ignoreCache = false)
{
// var locksInContract = await GetAllLockups(ignoreCache);
public async Task<List<LockEvent>> GetAllLockupEvents(bool ignoreCache = false)
{
var cacheKey = CacheKeys.AllLockupEvents();

if (!ignoreCache && await _cacheService.HasItem(cacheKey))
{
var cached = await _cacheService.GetItem<string>(cacheKey);
return JsonSerializer.Deserialize<List<LockEvent>>(cached) ?? new();
}

var lockEvents = (await _graphRetriever.RetrieveLockData())
.GroupBy(e => e.RequestKey)
.Select(g => g.First())
.ToList();

// if (!locksInContract.Any()) return new List<LockEvent>();
var ret = Utils.JsonPrettify(lockEvents);

var cacheKey = CacheKeys.AllLockupEvents();
await _cacheService.SetItem(cacheKey, ret, 30 * expirySeconds);
return lockEvents;
}

if (!ignoreCache && await _cacheService.HasItem(cacheKey))
public async Task<List<VoteEvent>> GetAllVoteEvents(bool ignoreCache = false)
{
var cached = await _cacheService.GetItem<string>(cacheKey);
return JsonSerializer.Deserialize<List<LockEvent>>(cached) ?? new();
var cacheKey = CacheKeys.AllVoteEvents();

if (!ignoreCache && await _cacheService.HasItem(cacheKey))
{
var cached = await _cacheService.GetItem<string>(cacheKey);
return JsonSerializer.Deserialize<List<VoteEvent>>(cached) ?? new();
}

var voteEvents = (await _graphRetriever.RetrieveVoteData())
.GroupBy(e => e.RequestKey)
.Select(g => g.First())
.ToList();


var ret = Utils.JsonPrettify(voteEvents);

await _cacheService.SetItem(cacheKey, ret, 30 * expirySeconds);
return voteEvents;
}

// Temporarily commented out
var lockEvents = await _graphRetriever.RetrieveLockData();

// var lockEvents = locksInContract.Select(lockup => new LockEvent
// {
// BondId = lockup.BondId,
// Account = lockup.Account,
// Amount = lockup.KdaLocked,
// Rewards = lockup.MaxKdaRewards,
// LockupLength = lockup.LockupOption.OptionLength,
// Timestamp = lockup.LockupStartTime.Date,
// RequestKey = "", //lockup.LockupId,
// Type = "Lock"
// }).ToList();

var ret = Utils.JsonPrettify(lockEvents);

await _cacheService.SetItem(cacheKey, ret, 30 * expirySeconds);
return lockEvents;
}



public async Task<List<ClaimEvent>> GetAllClaimEvents(bool ignoreCache = false)
{


var cacheKey = CacheKeys.AllClaimEvents();

if (!ignoreCache && await _cacheService.HasItem(cacheKey))
Expand All @@ -83,7 +89,11 @@ public async Task<List<ClaimEvent>> GetAllClaimEvents(bool ignoreCache = false)
return JsonSerializer.Deserialize<List<ClaimEvent>>(cached) ?? new();
}

var claimEvents = await _graphRetriever.RetrieveClaimData();
var claimEvents = (await _graphRetriever.RetrieveClaimData())
.GroupBy(e => e.RequestKey)
.Select(g => g.First())
.ToList();


var ret = Utils.JsonPrettify(claimEvents);

Expand Down
Loading

0 comments on commit 17ea9d1

Please sign in to comment.