From 60ca75e6ce690585afa6467e537cccffdbdcfa79 Mon Sep 17 00:00:00 2001 From: 257Byte <257byte@gmail.com> Date: Wed, 19 Jun 2024 20:28:47 +0300 Subject: [PATCH] Add more staking stats to /home API endpoint --- Tzkt.Api/Models/Home/StakingData.cs | 21 ++++++++++++++ Tzkt.Api/Services/Home/HomeService.cs | 41 ++++++++++++++++++++++----- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/Tzkt.Api/Models/Home/StakingData.cs b/Tzkt.Api/Models/Home/StakingData.cs index f3f720471..320b2ecec 100644 --- a/Tzkt.Api/Models/Home/StakingData.cs +++ b/Tzkt.Api/Models/Home/StakingData.cs @@ -8,5 +8,26 @@ public class StakingData public double Inflation { get; set; } public int Bakers { get; set; } public int FundedBakers { get; set; } + + public long OwnStaked { get; set; } + public double OwnStakedPercentage { get; set; } + + public long ExternalStaked { get; set; } + public double ExternalStakedPercentage { get; set; } + + public long TotalStaked { get; set; } + public double TotalStakedPercentage { get; set; } + + public long OwnDelegated { get; set; } + public double OwnDelegatedPercentage { get; set; } + + public long ExternalDelegated { get; set; } + public double ExternalDelegatedPercentage { get; set; } + + public long TotalDelegated { get; set; } + public double TotalDelegatedPercentage { get; set; } + + public double StakingApy { get; set; } + public double DelegationApy { get; set; } } } \ No newline at end of file diff --git a/Tzkt.Api/Services/Home/HomeService.cs b/Tzkt.Api/Services/Home/HomeService.cs index d3822a1c8..3265e9215 100644 --- a/Tzkt.Api/Services/Home/HomeService.cs +++ b/Tzkt.Api/Services/Home/HomeService.cs @@ -511,8 +511,17 @@ async Task GetStakingData(IDbConnection db, long totalSupply) { var protocol = Protocols.Current; - var total = await db.QueryFirstOrDefaultAsync( - $@"SELECT COUNT(*)::integer as bakers, COALESCE(SUM(""StakingBalance""), 0)::bigint AS staking FROM ""Accounts"" WHERE ""Type"" = 1 AND ""Staked"" = true"); + var total = await db.QueryFirstOrDefaultAsync($""" + SELECT COUNT(*)::integer as "ActiveBakers", + COALESCE(SUM("StakingBalance"), 0)::bigint AS "TotalStaking", + COALESCE(SUM("OwnStakedBalance"), 0)::bigint AS "OwnStaked", + COALESCE(SUM("ExternalStakedBalance"), 0)::bigint AS "ExternalStaked", + COALESCE(SUM("Balance" - "OwnStakedBalance"), 0)::bigint AS "OwnDelegated", + COALESCE(SUM("DelegatedBalance"), 0)::bigint AS "ExternalDelegated" + FROM "Accounts" + WHERE "Type" = 1 + AND "Staked" = true + """); var funded = await db.ExecuteScalarAsync($""" SELECT COUNT(*) @@ -539,14 +548,32 @@ LIMIT 1 var totalRewardsPerYear = maxRewardsPerBlock * blocksPerYear; var totalCreatedPerYear = (maxRewardsPerBlock + lbSubsidyPerBlock) * blocksPerYear; + var totalStaked = (long)total.OwnStaked + (long)total.ExternalStaked; + var totalDelegated = (long)total.OwnDelegated + (long)total.ExternalDelegated; + var totalBakingPower = totalStaked + totalDelegated / protocol.StakePowerMultiplier; + return new StakingData { - TotalStaking = total.staking, - StakingPercentage = Math.Round(100.0 * total.staking / totalSupply, 2), - AvgRoi = Math.Round(100.0 * totalRewardsPerYear / total.staking, 2), + TotalStaking = total.TotalStaking, + StakingPercentage = Math.Round(100.0 * total.TotalStaking / totalSupply, 2), + AvgRoi = Math.Round(100.0 * totalRewardsPerYear / total.TotalStaking, 2), Inflation = Math.Round(100.0 * totalCreatedPerYear / totalSupply, 2), - Bakers = total.bakers, - FundedBakers = funded + Bakers = total.ActiveBakers, + FundedBakers = funded, + OwnStaked = total.OwnStaked, + OwnStakedPercentage = Math.Round(100.0 * total.OwnStaked / totalSupply, 2), + ExternalStaked = total.ExternalStaked, + ExternalStakedPercentage = Math.Round(100.0 * total.ExternalStaked / totalSupply, 2), + TotalStaked = totalStaked, + TotalStakedPercentage = Math.Round(100.0 * totalStaked / totalSupply, 2), + OwnDelegated = total.OwnDelegated, + OwnDelegatedPercentage = Math.Round(100.0 * total.OwnDelegated / totalSupply, 2), + ExternalDelegated = total.ExternalDelegated, + ExternalDelegatedPercentage = Math.Round(100.0 * total.ExternalDelegated / totalSupply, 2), + TotalDelegated = totalDelegated, + TotalDelegatedPercentage = Math.Round(100.0 * totalDelegated / totalSupply, 2), + StakingApy = Math.Round(100.0 * totalRewardsPerYear / totalBakingPower, 2), + DelegationApy = Math.Round(100.0 * totalRewardsPerYear / totalBakingPower, 2) / protocol.StakePowerMultiplier }; }