Skip to content

Commit

Permalink
Fix finalize unstake
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed Mar 25, 2024
1 parent 6abef5d commit 3c8d6c7
Showing 1 changed file with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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...");

Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit 3c8d6c7

Please sign in to comment.