Skip to content

Commit

Permalink
Add more staking stats to /home API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed Jun 19, 2024
1 parent ba6fb09 commit 60ca75e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
21 changes: 21 additions & 0 deletions Tzkt.Api/Models/Home/StakingData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
41 changes: 34 additions & 7 deletions Tzkt.Api/Services/Home/HomeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,17 @@ async Task<StakingData> 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<int>($"""
SELECT COUNT(*)
Expand All @@ -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
};
}

Expand Down

0 comments on commit 60ca75e

Please sign in to comment.