forked from baking-bad/tzkt
-
Notifications
You must be signed in to change notification settings - Fork 1
Delegation Summary for delegator #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| using System.Linq; | ||
| using System.Net.Http; | ||
| using System.Threading.Tasks; | ||
| using Dynamic.Json; | ||
| using Dynamic.Json.Extensions; | ||
| using Xunit; | ||
|
|
||
| namespace Mvkt.Api.Tests.Api | ||
| { | ||
| public class TestDelegationSummaryQueries : IClassFixture<SettingsFixture> | ||
| { | ||
| readonly SettingsFixture Settings; | ||
| readonly HttpClient Client; | ||
|
|
||
| public TestDelegationSummaryQueries(SettingsFixture settings) | ||
| { | ||
| Settings = settings; | ||
| Client = settings.Client; | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestSummary_ReturnsFullStructure() | ||
| { | ||
| dynamic res = await Client.GetJsonAsync($"/v1/rewards/delegators/{Settings.Baker}/summary"); | ||
|
|
||
| Assert.True(res is DJsonObject); | ||
| Assert.NotNull(res.summary); | ||
| Assert.NotNull(res.actualRewards); | ||
|
|
||
| Assert.NotNull(res.summary.isDelegating); | ||
| Assert.NotNull(res.summary.isStaking); | ||
| Assert.NotNull(res.summary.totalRewardsEarned); | ||
| Assert.NotNull(res.summary.rewardsByValidator); | ||
| Assert.NotNull(res.summary.rewardsByCycle); | ||
| Assert.NotNull(res.summary.totalValidatorsUsed); | ||
| Assert.NotNull(res.summary.currentValidatorCount); | ||
| Assert.NotNull(res.summary.pastValidatorCount); | ||
| Assert.NotNull(res.summary.totalCyclesWithRewards); | ||
|
|
||
| Assert.NotNull(res.actualRewards.totalActualRewards); | ||
| Assert.NotNull(res.actualRewards.delegationPayouts); | ||
| Assert.NotNull(res.actualRewards.stakingRewardsRestaked); | ||
| Assert.NotNull(res.actualRewards.expectedTotalRewards); | ||
| Assert.NotNull(res.actualRewards.pendingDelegation); | ||
| Assert.NotNull(res.actualRewards.byValidator); | ||
|
|
||
| if (res.summary.rewardsByCycle is DJsonArray arr && arr.Count > 0) | ||
| { | ||
| Assert.NotNull(res.summary.firstRewardCycle); | ||
| Assert.NotNull(res.summary.lastRewardCycle); | ||
| Assert.True((int)res.summary.totalCyclesWithRewards >= 0); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestSummary_NonExistentAddress_Returns200WithEmptyInfo() | ||
| { | ||
| const string nonExistent = "/v1/rewards/delegators/mv1V4h45W3p4e1sjSBvRkK2uYbvkTnSuHg1c/summary"; | ||
| var response = await Client.GetAsync(nonExistent); | ||
| Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode); | ||
|
|
||
| dynamic res = await Client.GetJsonAsync(nonExistent); | ||
| Assert.True(res is DJsonObject); | ||
| Assert.NotNull(res.summary); | ||
| Assert.NotNull(res.actualRewards); | ||
| Assert.False((bool)res.summary.isDelegating); | ||
| Assert.False((bool)res.summary.isStaking); | ||
| Assert.NotNull(res.summary.stakedValidators); | ||
| Assert.True(res.summary.stakedValidators is DJsonArray); | ||
| Assert.Equal(0, ((DJsonArray)res.summary.stakedValidators).Count); | ||
| Assert.Equal(0L, (long)res.summary.totalStakedBalance); | ||
| Assert.Equal(0L, (long)res.summary.totalRewardsEarned); | ||
| Assert.NotNull(res.actualRewards.byValidator); | ||
| Assert.True(res.actualRewards.byValidator is DJsonArray); | ||
| Assert.Equal(0, ((DJsonArray)res.actualRewards.byValidator).Count); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestDelegationSummary_WithLegacyParameter() | ||
| { | ||
| var resLegacyTrue = await Client.GetJsonAsync($"/v1/rewards/delegators/{Settings.Baker}/summary?legacy=true"); | ||
| var resLegacyFalse = await Client.GetJsonAsync($"/v1/rewards/delegators/{Settings.Baker}/summary?legacy=false"); | ||
|
|
||
| Assert.True(resLegacyTrue is DJsonObject); | ||
| Assert.True(resLegacyFalse is DJsonObject); | ||
| Assert.NotNull(resLegacyTrue.summary); | ||
| Assert.NotNull(resLegacyFalse.summary); | ||
| Assert.NotNull(resLegacyTrue.actualRewards); | ||
| Assert.NotNull(resLegacyFalse.actualRewards); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestContractDelegationSummary() | ||
| { | ||
| dynamic res = await Client.GetJsonAsync($"/v1/rewards/delegators/{Settings.Originator}/summary"); | ||
|
|
||
| Assert.True(res is DJsonObject); | ||
| Assert.NotNull(res.summary); | ||
| Assert.NotNull(res.actualRewards); | ||
| Assert.NotNull(res.summary.isDelegating); | ||
| Assert.NotNull(res.summary.isStaking); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestSummary_RewardsByCycle_OrderAndStructure() | ||
| { | ||
| dynamic res = await Client.GetJsonAsync($"/v1/rewards/delegators/{Settings.Baker}/summary"); | ||
| var rewardsByCycle = res.summary.rewardsByCycle; | ||
| if (rewardsByCycle is DJsonArray arr && arr.Count > 0) | ||
| { | ||
| dynamic first = arr.First(); | ||
| Assert.NotNull(first.cycle); | ||
| Assert.NotNull(first.rewards); | ||
| Assert.NotNull(first.delegatedBalance); | ||
| Assert.NotNull(first.stakedBalance); | ||
| Assert.NotNull(first.validator); | ||
|
|
||
| if (arr.Count >= 2) | ||
| { | ||
| for (int i = 0; i < arr.Count - 1; i++) | ||
| { | ||
| int current = (int)((dynamic)arr.ElementAt(i)).cycle; | ||
| int next = (int)((dynamic)arr.ElementAt(i + 1)).cycle; | ||
| Assert.True(current >= next, $"rewardsByCycle should be descending: at index {i} cycle={current}, at {i + 1} cycle={next}"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestSummary_ActualRewards_AllStakingRewardEvents_Structure() | ||
| { | ||
| dynamic res = await Client.GetJsonAsync($"/v1/rewards/delegators/{Settings.Baker}/summary"); | ||
| Assert.NotNull(res.actualRewards); | ||
| var events = res.actualRewards.allStakingRewardEvents; | ||
| Assert.NotNull(events); | ||
| Assert.True(events is DJsonArray); | ||
| if (((DJsonArray)events).Count > 0) | ||
| { | ||
| dynamic ev = ((DJsonArray)events).First(); | ||
| Assert.NotNull(ev.cycle); | ||
| Assert.NotNull(ev.amount); | ||
| Assert.NotNull(ev.validatorAddress); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestSummary_ByValidator() | ||
| { | ||
| var validPaymentStatuses = new[] { "fully_paid", "pending", "underpaid", "overpaid" }; | ||
| var validCycleStatuses = new[] { "paid", "pending", "underpaid", "overpaid" }; | ||
|
|
||
| dynamic res = await Client.GetJsonAsync($"/v1/rewards/delegators/{Settings.Baker}/summary"); | ||
| Assert.NotNull(res.actualRewards); | ||
| var byValidator = res.actualRewards.byValidator; | ||
|
|
||
| if (byValidator is DJsonArray arr && arr.Count > 0) | ||
| { | ||
| dynamic first = arr.First(); | ||
| Assert.NotNull(first.address); | ||
| Assert.NotNull(first.paymentStatus); | ||
| Assert.NotNull(first.expectedDelegationRewards); | ||
| Assert.NotNull(first.actualDelegationPayouts); | ||
| Assert.NotNull(first.pendingDelegation); | ||
|
|
||
| foreach (dynamic v in arr) | ||
| { | ||
| string status = (string)v.paymentStatus; | ||
| Assert.NotNull(status); | ||
| Assert.Contains(status, validPaymentStatuses); | ||
|
|
||
| var delegationByCycle = v.delegationByCycle; | ||
| Assert.NotNull(delegationByCycle); | ||
| if (delegationByCycle is DJsonArray dcArr && dcArr.Count > 0) | ||
| { | ||
| foreach (dynamic dc in dcArr) | ||
| { | ||
| Assert.NotNull(dc.cycle); | ||
| Assert.NotNull(dc.expectedReward); | ||
| Assert.NotNull(dc.status); | ||
| Assert.Contains((string)dc.status, validCycleStatuses); | ||
| } | ||
| } | ||
|
|
||
| var stakingByCycle = v.stakingRewardsByCycle; | ||
| Assert.NotNull(stakingByCycle); | ||
| if (stakingByCycle is DJsonArray scArr && scArr.Count > 0) | ||
| { | ||
| dynamic sc = scArr.First(); | ||
| Assert.NotNull(sc.cycle); | ||
| Assert.NotNull(sc.amount); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| namespace Mvkt.Api.Models | ||
| { | ||
| /// <summary> | ||
| /// Actual rewards received compared to expected rewards | ||
| /// </summary> | ||
| public class ActualRewards | ||
| { | ||
| /// <summary> | ||
| /// Total actual rewards received | ||
| /// </summary> | ||
| public long TotalActualRewards { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Delegation payouts received | ||
| /// </summary> | ||
| public long DelegationPayouts { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Number of delegation payout transactions | ||
| /// </summary> | ||
| public int DelegationPayoutCount { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Staking rewards auto-restaked | ||
| /// </summary> | ||
| public long StakingRewardsRestaked { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Number of staking restake events | ||
| /// </summary> | ||
| public int StakingRestakeCount { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Expected delegation rewards | ||
| /// </summary> | ||
| public long ExpectedDelegationRewards { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Expected staking rewards | ||
| /// </summary> | ||
| public long ExpectedStakingRewards { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Expected total rewards | ||
| /// </summary> | ||
| public long ExpectedTotalRewards { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Pending delegation rewards not yet paid | ||
| /// </summary> | ||
| public long PendingDelegation { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Pending staking rewards | ||
| /// </summary> | ||
| public long PendingStaking { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Total pending rewards | ||
| /// </summary> | ||
| public long PendingTotal { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Reward tracking per validator | ||
| /// </summary> | ||
| public List<ValidatorRewardTracking> ByValidator { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// All staking reward events | ||
| /// </summary> | ||
| public List<StakingRewardEvent> AllStakingRewardEvents { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// First reward date (ISO 8601) | ||
| /// </summary> | ||
| public DateTime? FirstRewardDate { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Last reward date (ISO 8601) | ||
| /// </summary> | ||
| public DateTime? LastRewardDate { get; set; } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.