Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Dec 27, 2024
1 parent d0f9f9d commit 7ce828b
Show file tree
Hide file tree
Showing 19 changed files with 158 additions and 540 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ public override IWorld Execute(IActionContext context)

var repository = new GuildRepository(world, context);
var unbondingSet = repository.GetUnbondingSet();
var unbondings = unbondingSet.UnbondingsToRelease(context.BlockIndex);
var unbondings = repository.UnbondingsToRelease(context.BlockIndex);

unbondings = unbondings.Select<IUnbonding, IUnbonding>(unbonding =>
{
switch (unbonding)
{
case UnbondLockIn unbondLockIn:
unbondLockIn = unbondLockIn.Release(context.BlockIndex, out var releasedFAV);
repository.Release(unbondLockIn, releasedFAV);
repository.SetUnbondLockIn(unbondLockIn);
repository.UpdateWorld(
Unstake(repository.World, context, unbondLockIn, releasedFAV));
Expand Down
36 changes: 21 additions & 15 deletions Lib9c/Delegation/Delegatee.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Nekoyume.Delegation
{
public abstract class Delegatee<TRepository, TDelegatee, TDelegator> : IDelegatee
public abstract class Delegatee<TRepository, TDelegatee, TDelegator>
where TRepository : DelegationRepository<TRepository, TDelegatee, TDelegator>
where TDelegatee : Delegatee<TRepository, TDelegatee, TDelegator>
where TDelegator : Delegator<TRepository, TDelegatee, TDelegator>
Expand Down Expand Up @@ -110,15 +110,6 @@ public BigInteger ShareFromFAV(FungibleAssetValue fav)
public FungibleAssetValue FAVFromShare(BigInteger share)
=> Metadata.FAVFromShare(share);

BigInteger IDelegatee.Bond(IDelegator delegator, FungibleAssetValue fav, long height)
=> Bond((TDelegator)delegator, fav, height);

FungibleAssetValue IDelegatee.Unbond(IDelegator delegator, BigInteger share, long height)
=> Unbond((TDelegator)delegator, share, height);

void IDelegatee.DistributeReward(IDelegator delegator, long height)
=> DistributeReward((TDelegator)delegator, height);

public void Jail(long releaseHeight)
{
Metadata.JailedUntil = releaseHeight;
Expand Down Expand Up @@ -331,6 +322,15 @@ record = record.AddLumpSumRewards(rewards);
}
}

// private IUnbonding GetUnbondingFromRef(
// UnbondingRef reference)
// => reference.UnbondingType switch
// {
// UnbondingType.UnbondLockIn => Repository.GetUnlimitedUnbondLockIn(reference.Address),
// UnbondingType.RebondGrace => Repository.GetUnlimitedRebondGrace(reference.Address),
// _ => throw new ArgumentException("Invalid unbonding type.")
// };

public virtual void Slash(BigInteger slashFactor, long infractionHeight, long height)
{
FungibleAssetValue slashed = TotalDelegated.DivRem(slashFactor, out var rem);
Expand All @@ -348,9 +348,18 @@ public virtual void Slash(BigInteger slashFactor, long infractionHeight, long he

foreach (var item in Metadata.UnbondingRefs)
{
var unbonding = UnbondingFactory.GetUnbondingFromRef(item, Repository);
var unbonding = Repository.GetUnbondingFromRef(item);

unbonding = unbonding.Slash(slashFactor, infractionHeight, height, out var slashedFAV);
unbonding = unbonding.Slash(slashFactor, infractionHeight, height, out var slashed1);

FungibleAssetValue? slashedFAV = null;
foreach (var (address, slashedEach) in slashed1)
{
var delegatee = Repository.GetDelegatee(address);
var delegator = Repository.GetDelegator(unbonding.DelegatorAddress);
delegatee.Unbond(delegator, delegatee.ShareFromFAV(slashedEach), height);
slashedFAV = slashedFAV.HasValue ? slashedFAV + slashedEach : slashedEach;
}

if (slashedFAV.HasValue)
{
Expand Down Expand Up @@ -390,9 +399,6 @@ public virtual void Slash(BigInteger slashFactor, long infractionHeight, long he
DelegationChanged?.Invoke(this, height);
}

void IDelegatee.Slash(BigInteger slashFactor, long infractionHeight, long height)
=> Slash(slashFactor, infractionHeight, height);

public void AddUnbondingRef(UnbondingRef reference)
=> Metadata.AddUnbondingRef(reference);

Expand Down
8 changes: 4 additions & 4 deletions Lib9c/Delegation/DelegateeMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace Nekoyume.Delegation
{
public class DelegateeMetadata : IDelegateeMetadata
public class DelegateeMetadata : IBencodable

Check warning on line 15 in Lib9c/Delegation/DelegateeMetadata.cs

View workflow job for this annotation

GitHub Actions / build-and-test (Release)

Implement 'IEquatable<DelegateeMetadata>'.

Check warning on line 15 in Lib9c/Delegation/DelegateeMetadata.cs

View workflow job for this annotation

GitHub Actions / build-js

Implement 'IEquatable<DelegateeMetadata>'.
{
private const string StateTypeName = "delegatee_metadata";
private const long StateVersion = 1;
Expand Down Expand Up @@ -91,7 +91,7 @@ public DelegateeMetadata(
-1L,
false,
ImmutableSortedSet<UnbondingRef>.Empty)
{
{
}

public DelegateeMetadata(
Expand Down Expand Up @@ -434,9 +434,9 @@ public virtual Address LumpSumRewardsRecordAddress(long height)
=> DelegationAddress.RewardBaseAddress(Address, height);

public override bool Equals(object? obj)
=> obj is IDelegateeMetadata other && Equals(other);
=> obj is DelegateeMetadata other && Equals(other);

public virtual bool Equals(IDelegateeMetadata? other)
public virtual bool Equals(DelegateeMetadata? other)
=> ReferenceEquals(this, other)
|| (other is DelegateeMetadata delegatee
&& (GetType() != delegatee.GetType())
Expand Down
79 changes: 42 additions & 37 deletions Lib9c/Delegation/DelegationRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#nullable enable
using System;
using System.Collections.Immutable;
using System.Linq;
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Action.State;
Expand All @@ -8,7 +11,7 @@

namespace Nekoyume.Delegation
{
public abstract class DelegationRepository<TRepository, TDelegatee, TDelegator> : IDelegationRepository
public abstract class DelegationRepository<TRepository, TDelegatee, TDelegator>
where TRepository : DelegationRepository<TRepository, TDelegatee, TDelegator>
where TDelegatee : Delegatee<TRepository, TDelegatee, TDelegator>
where TDelegator : Delegator<TRepository, TDelegatee, TDelegator>
Expand Down Expand Up @@ -172,15 +175,15 @@ public UnbondLockIn GetUnbondLockIn(TDelegatee delegatee, Address delegatorAddre
Address address = delegatee.UnbondLockInAddress(delegatorAddress);
IValue? value = unbondLockInAccount.GetState(address);
return value is IValue bencoded
? new UnbondLockIn(address, delegatee.MaxUnbondLockInEntries, bencoded, this)
: new UnbondLockIn(address, delegatee.MaxUnbondLockInEntries, delegatee.Address, delegatorAddress, this);
? new UnbondLockIn(address, delegatee.MaxUnbondLockInEntries, bencoded)
: new UnbondLockIn(address, delegatee.MaxUnbondLockInEntries, delegatee.Address, delegatorAddress);
}

public UnbondLockIn GetUnlimitedUnbondLockIn(Address address)
{
IValue? value = unbondLockInAccount.GetState(address);
return value is IValue bencoded
? new UnbondLockIn(address, int.MaxValue, bencoded, this)
? new UnbondLockIn(address, int.MaxValue, bencoded)
: throw new FailedLoadStateException("UnbondLockIn not found.");
}

Expand All @@ -189,22 +192,22 @@ public RebondGrace GetRebondGrace(TDelegatee delegatee, Address delegatorAddress
Address address = delegatee.RebondGraceAddress(delegatorAddress);
IValue? value = rebondGraceAccount.GetState(address);
return value is IValue bencoded
? new RebondGrace(address, delegatee.MaxRebondGraceEntries, bencoded, this)
: new RebondGrace(address, delegatee.MaxRebondGraceEntries, this);
? new RebondGrace(address, delegatee.MaxRebondGraceEntries, bencoded)
: new RebondGrace(address, delegatee.MaxRebondGraceEntries);
}

public RebondGrace GetUnlimitedRebondGrace(Address address)
{
IValue? value = rebondGraceAccount.GetState(address);
return value is IValue bencoded
? new RebondGrace(address, int.MaxValue, bencoded, this)
? new RebondGrace(address, int.MaxValue, bencoded)
: throw new FailedLoadStateException("RebondGrace not found.");
}

public UnbondingSet GetUnbondingSet()
=> unbondingSetAccount.GetState(UnbondingSet.Address) is IValue bencoded
? new UnbondingSet(bencoded, this)
: new UnbondingSet(this);
? new UnbondingSet(bencoded)
: new UnbondingSet();

/// <inheritdoc/>
public RewardBase? GetCurrentRewardBase(TDelegatee delegatee)
Expand Down Expand Up @@ -325,35 +328,37 @@ public virtual void UpdateWorld(IWorld world)
lumpSumRewardsRecordAccount = world.GetAccount(LumpSumRewardsRecordAccountAddress);
}

IDelegatee IDelegationRepository.GetDelegatee(Address address) => GetDelegatee(address);

IDelegator IDelegationRepository.GetDelegator(Address address) => GetDelegator(address);

Bond IDelegationRepository.GetBond(IDelegatee delegatee, Address delegatorAddress)
=> GetBond((TDelegatee)delegatee, delegatorAddress);

UnbondLockIn IDelegationRepository.GetUnbondLockIn(IDelegatee delegatee, Address delegatorAddress)
=> GetUnbondLockIn((TDelegatee)delegatee, delegatorAddress);

RebondGrace IDelegationRepository.GetRebondGrace(IDelegatee delegatee, Address delegatorAddress)
=> GetRebondGrace((TDelegatee)delegatee, delegatorAddress);

RewardBase? IDelegationRepository.GetCurrentRewardBase(IDelegatee delegatee)
=> GetCurrentRewardBase((TDelegatee)delegatee);

RewardBase? IDelegationRepository.GetRewardBase(IDelegatee delegatee, long height)
=> GetRewardBase((TDelegatee)delegatee, height);

LumpSumRewardsRecord? IDelegationRepository.GetCurrentLumpSumRewardsRecord(IDelegatee delegatee)
=> GetCurrentLumpSumRewardsRecord((TDelegatee)delegatee);

LumpSumRewardsRecord? IDelegationRepository.GetLumpSumRewardsRecord(IDelegatee delegatee, long height)
=> GetLumpSumRewardsRecord((TDelegatee)delegatee, height);
public ImmutableArray<IUnbonding> UnbondingsToRelease(long height)
{
var unbondingSet = GetUnbondingSet();
var UnbondingRefs = unbondingSet.UnbondingRefs;
return UnbondingRefs
.TakeWhile(kv => kv.Key <= height)
.SelectMany(kv => kv.Value)
.Select(unbondingRef => GetUnbondingFromRef(unbondingRef))
.ToImmutableArray();
}

void IDelegationRepository.SetDelegatee(IDelegatee delegatee)
=> SetDelegatee((TDelegatee)delegatee);
public void Release(UnbondLockIn unbondLockIn, FungibleAssetValue? releasedFAV)
{
if (releasedFAV.HasValue)
{
var delegateeMetadata = GetDelegateeMetadata(unbondLockIn.DelegateeAddress);
var delegatorMetadata = GetDelegatorMetadata(unbondLockIn.DelegatorAddress);
TransferAsset(
delegateeMetadata.DelegationPoolAddress,
delegatorMetadata.DelegationPoolAddress,
releasedFAV.Value);
}
}

void IDelegationRepository.SetDelegator(IDelegator delegator)
=> SetDelegator((TDelegator)delegator);
internal IUnbonding GetUnbondingFromRef(
UnbondingRef reference)
=> reference.UnbondingType switch
{
UnbondingType.UnbondLockIn => GetUnlimitedUnbondLockIn(reference.Address),
UnbondingType.RebondGrace => GetUnlimitedRebondGrace(reference.Address),
_ => throw new ArgumentException("Invalid unbonding type.")
};
}
}
22 changes: 1 addition & 21 deletions Lib9c/Delegation/Delegator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Nekoyume.Delegation
{
public abstract class Delegator<TRepository, TDelegatee, TDelegator> : IDelegator
public abstract class Delegator<TRepository, TDelegatee, TDelegator>
where TRepository : DelegationRepository<TRepository, TDelegatee, TDelegator>
where TDelegatee : Delegatee<TRepository, TDelegatee, TDelegator>
where TDelegator : Delegator<TRepository, TDelegatee, TDelegator>
Expand Down Expand Up @@ -80,10 +80,6 @@ public virtual void Delegate(
Repository.SetDelegator((TDelegator)this);
}

void IDelegator.Delegate(
IDelegatee delegatee, FungibleAssetValue fav, long height)
=> Delegate((TDelegatee)delegatee, fav, height);

public virtual void Undelegate(
TDelegatee delegatee, BigInteger share, long height)
{
Expand Down Expand Up @@ -123,11 +119,6 @@ public virtual void Undelegate(
Repository.SetDelegator((TDelegator)this);
}

void IDelegator.Undelegate(
IDelegatee delegatee, BigInteger share, long height)
=> Undelegate((TDelegatee)delegatee, share, height);


public virtual void Redelegate(
TDelegatee srcDelegatee, TDelegatee dstDelegatee, BigInteger share, long height)
{
Expand Down Expand Up @@ -173,10 +164,6 @@ public virtual void Redelegate(
Repository.SetDelegator((TDelegator)this);
}

void IDelegator.Redelegate(
IDelegatee srcDelegatee, IDelegatee dstDelegatee, BigInteger share, long height)
=> Redelegate((TDelegatee)srcDelegatee, (TDelegatee)dstDelegatee, share, height);

public void CancelUndelegate(
TDelegatee delegatee, FungibleAssetValue fav, long height)
{
Expand Down Expand Up @@ -214,19 +201,12 @@ public void CancelUndelegate(
Repository.SetDelegator((TDelegator)this);
}

void IDelegator.CancelUndelegate(
IDelegatee delegatee, FungibleAssetValue fav, long height)
=> CancelUndelegate((TDelegatee)delegatee, fav, height);

public void ClaimReward(
TDelegatee delegatee, long height)
{
delegatee.DistributeReward((TDelegator)this, height);
delegatee.StartNewRewardPeriod(height);
Repository.SetDelegator((TDelegator)this);
}

void IDelegator.ClaimReward(IDelegatee delegatee, long height)
=> ClaimReward((TDelegatee)delegatee, height);
}
}
6 changes: 3 additions & 3 deletions Lib9c/Delegation/DelegatorMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Nekoyume.Delegation
{
public class DelegatorMetadata : IDelegatorMetadata
public class DelegatorMetadata : IBencodable

Check warning on line 11 in Lib9c/Delegation/DelegatorMetadata.cs

View workflow job for this annotation

GitHub Actions / build-and-test (Release)

Implement 'IEquatable<DelegatorMetadata>'.

Check warning on line 11 in Lib9c/Delegation/DelegatorMetadata.cs

View workflow job for this annotation

GitHub Actions / build-js

Implement 'IEquatable<DelegatorMetadata>'.
{
private Address? _address;

Expand Down Expand Up @@ -95,9 +95,9 @@ public void RemoveDelegatee(Address delegatee)
}

public override bool Equals(object? obj)
=> obj is IDelegator other && Equals(other);
=> obj is DelegatorMetadata other && Equals(other);

public virtual bool Equals(IDelegator? other)
public virtual bool Equals(DelegatorMetadata? other)
=> ReferenceEquals(this, other)
|| (other is DelegatorMetadata delegator
&& GetType() != delegator.GetType()
Expand Down
Loading

0 comments on commit 7ce828b

Please sign in to comment.