Skip to content

Commit

Permalink
Count restaking when validating finalize_unstake
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed Mar 26, 2024
1 parent 1985051 commit 4bf7e00
Showing 1 changed file with 42 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,14 @@ public async Task Apply(Block block, JsonElement op, JsonElement content)
x.Cycle >= operation.FirstCycleUnstaked.Value &&
x.Cycle <= operation.LastCycleUnstaked.Value)
.SumAsync(x => x.Amount);

requestedAmount -= await Db.AutostakingOps
.Where(x =>
x.BakerId == baker.Id &&
x.Action == AutostakingAction.Restake &&
x.Cycle >= operation.FirstCycleUnstaked.Value &&
x.Cycle <= operation.LastCycleUnstaked.Value)
.SumAsync(x => x.Amount);
}

if (operation.Amount != requestedAmount)
Expand Down Expand Up @@ -413,25 +421,45 @@ public async Task Revert(Block block, StakingOperation operation)
.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)));

// 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();
if (sender is Data.Models.Delegate baker)
{
var autostakingOps = await Db.AutostakingOps
.AsNoTracking()
.Where(x =>
x.BakerId == baker.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(autostakingOps.Select(x => ((int?)x.BakerId, x.Amount)));
var autostakingOps2 = await Db.AutostakingOps
.AsNoTracking()
.Where(x =>
x.BakerId == baker.Id &&
x.Action == AutostakingAction.Restake &&
x.Cycle >= operation.FirstCycleUnstaked.Value &&
x.Cycle <= operation.LastCycleUnstaked.Value)
.OrderBy(x => x.Level)
.ThenBy(x => x.Id)
.ToListAsync();

unstakeOps = unstakeOps
.Concat(autostakingOps.Select(x => ((int?)x.BakerId, x.Amount)))
.Concat(autostakingOps2.Select(x => ((int?)x.BakerId, -x.Amount)));
}

if (unstakeOps.Any() && unstakeOps.Sum(x => x.Item2) != operation.Amount)
throw new NotImplementedException("Manual staking is not fully implemented in O2, because it's partially forbidden...");

foreach (var (prevBakerId, unstakedAmount) in unstakeOps)
{
if (sender.UnstakedBalance == 0 && unstakedAmount > 0)
if (sender.UnstakedBalance == 0 && unstakedAmount != 0)
sender.UnstakedBakerId = prevBakerId;

sender.UnstakedBalance += unstakedAmount;
Expand Down

0 comments on commit 4bf7e00

Please sign in to comment.