diff --git a/Tzkt.Sync/Protocols/Handlers/Proto18/Commits/Operations/StakingCommit.cs b/Tzkt.Sync/Protocols/Handlers/Proto18/Commits/Operations/StakingCommit.cs index 7f63bd2f..8421f892 100644 --- a/Tzkt.Sync/Protocols/Handlers/Proto18/Commits/Operations/StakingCommit.cs +++ b/Tzkt.Sync/Protocols/Handlers/Proto18/Commits/Operations/StakingCommit.cs @@ -261,6 +261,18 @@ public async Task Apply(Block block, JsonElement op, JsonElement content) x.Level <= endLevel) .SumAsync(x => x.UnstakedBalance + x.UnstakedRewards); + // TODO: review in P + if (sender is Data.Models.Delegate baker) + { + requestedAmount += await Db.AutostakingOps + .Where(x => + x.BakerId == baker.Id && + x.Action == AutostakingAction.Unstake && + x.Cycle >= operation.FirstCycleUnstaked.Value && + x.Cycle <= operation.LastCycleUnstaked.Value) + .SumAsync(x => x.Amount); + } + if (operation.Amount != requestedAmount) throw new NotImplementedException("Slashing of unstaked deposits cannot be implemented due to bugs in Oxford. Let's wait for fixes..."); @@ -401,8 +413,21 @@ public async Task Revert(Block block, StakingOperation operation) .ThenBy(x => x.Id) .ToListAsync(); + // TODO: review in P + var autostakingOps = await Db.AutostakingOps + .AsNoTracking() + .Where(x => + x.BakerId == sender.Id && + x.Action == AutostakingAction.Unstake && + x.Cycle >= operation.FirstCycleUnstaked.Value && + x.Cycle <= operation.LastCycleUnstaked.Value) + .OrderBy(x => x.Level) + .ThenBy(x => x.Id) + .ToListAsync(); + var unstakeOps = stakingOps.Select(x => (x.BakerId, x.Amount.Value)) - .Concat(delegationOps.Select(x => (x.PrevDelegateId, x.UnstakedBalance.Value + x.UnstakedRewards.Value))); + .Concat(delegationOps.Select(x => (x.PrevDelegateId, x.UnstakedBalance.Value + x.UnstakedRewards.Value))) + .Concat(autostakingOps.Select(x => ((int?)x.BakerId, x.Amount))); foreach (var (prevBakerId, unstakedAmount) in unstakeOps) {