Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Dec 30, 2024
1 parent 9f4134c commit a167055
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 51 deletions.
28 changes: 13 additions & 15 deletions Lib9c/Delegation/Delegatee.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,23 @@ private Delegatee(DelegateeMetadata metadata, TRepository repository)

public FungibleAssetValue FAVFromShare(BigInteger share) => Metadata.FAVFromShare(share);

public BigInteger Bond(TDelegator delegator, FungibleAssetValue fav, long height)
public Bond Bond(TDelegator delegator, FungibleAssetValue fav, long height)
{
var delegationCurrency = Metadata.DelegationCurrency;
DistributeReward(delegator, height);

if (!fav.Currency.Equals(delegationCurrency))
{
throw new InvalidOperationException(
"Cannot bond with invalid currency.");
throw new InvalidOperationException("Cannot bond with invalid currency.");
}

if (Metadata.Tombstoned)
{
throw new InvalidOperationException(
"Cannot bond to tombstoned delegatee.");
throw new InvalidOperationException("Cannot bond to tombstoned delegatee.");
}

Bond bond = Repository.GetBond((TDelegatee)this, delegator.Address);
BigInteger share = ShareFromFAV(fav);
var bond = Repository.GetBond((TDelegatee)this, delegator.Address);
var share = ShareFromFAV(fav);
bond = bond.AddShare(share);
Metadata.AddShare(share);
Metadata.AddDelegatedFAV(fav);
Expand All @@ -101,10 +99,10 @@ public BigInteger Bond(TDelegator delegator, FungibleAssetValue fav, long height
Repository.SetDelegatee((TDelegatee)this);
DelegationChanged?.Invoke(this, height);

return share;
return bond;
}

public FungibleAssetValue Unbond(TDelegator delegator, BigInteger share, long height)
public Bond Unbond(TDelegator delegator, BigInteger share, long height)
{
DistributeReward(delegator, height);
if (Metadata.TotalShares.IsZero || Metadata.TotalDelegatedFAV.RawValue.IsZero)
Expand All @@ -113,8 +111,8 @@ public FungibleAssetValue Unbond(TDelegator delegator, BigInteger share, long he
"Cannot unbond without bonding.");
}

Bond bond = Repository.GetBond((TDelegatee)this, delegator.Address);
FungibleAssetValue fav = FAVFromShare(share);
var bond = Repository.GetBond((TDelegatee)this, delegator.Address);
var fav = FAVFromShare(share);
bond = bond.SubtractShare(share);
if (bond.Share.IsZero)
{
Expand All @@ -128,26 +126,26 @@ public FungibleAssetValue Unbond(TDelegator delegator, BigInteger share, long he
Repository.SetDelegatee((TDelegatee)this);
DelegationChanged?.Invoke(this, height);

return fav;
return bond;
}

public void DistributeReward(TDelegator delegator, long height)
{
Bond bond = Repository.GetBond((TDelegatee)this, delegator.Address);
BigInteger share = bond.Share;

if (!share.IsZero && bond.LastDistributeHeight.HasValue)
if (!share.IsZero && bond.LastDistributeHeight is { } lastDistributeHeight)
{
if (Repository.GetCurrentRewardBase((TDelegatee)this) is RewardBase rewardBase)
{
var lastRewardBase = Repository.GetRewardBase((TDelegatee)this, bond.LastDistributeHeight.Value);
var lastRewardBase = Repository.GetRewardBase((TDelegatee)this, lastDistributeHeight);
TransferReward(delegator, share, rewardBase, lastRewardBase);
// TransferRemainders(newRecord);
}
else
{
IEnumerable<LumpSumRewardsRecord> lumpSumRewardsRecords
= GetLumpSumRewardsRecords(bond.LastDistributeHeight);
= GetLumpSumRewardsRecords(lastDistributeHeight);

foreach (LumpSumRewardsRecord record in lumpSumRewardsRecords)
{
Expand Down
10 changes: 4 additions & 6 deletions Lib9c/Delegation/DelegationRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,17 @@ public DelegatorMetadata GetDelegatorMetadata(Address delegatorAddress)
public Bond GetBond(TDelegatee delegatee, Address delegatorAddress)
{
var delegateeMetadata = delegatee.Metadata;
Address address = delegateeMetadata.BondAddress(delegatorAddress);
IValue? value = bondAccount.GetState(address);
return value is IValue bencoded
? new Bond(address, bencoded)
: new Bond(address);
var bondAddress = delegateeMetadata.BondAddress(delegatorAddress);
return bondAccount.GetState(bondAddress) is { } bencoded
? new Bond(bondAddress, bencoded) : new Bond(bondAddress);
}

public UnbondLockIn GetUnbondLockIn(TDelegatee delegatee, Address delegatorAddress)
{
var delegateeMetadata = delegatee.Metadata;
var maxUnbondLockInEntries = delegateeMetadata.MaxUnbondLockInEntries;
Address address = delegateeMetadata.UnbondLockInAddress(delegatorAddress);
IValue? value = unbondLockInAccount.GetState(address);
var maxUnbondLockInEntries = delegateeMetadata.MaxUnbondLockInEntries;
return value is IValue bencoded
? new UnbondLockIn(address, maxUnbondLockInEntries, bencoded)
: new UnbondLockIn(address, maxUnbondLockInEntries, delegatee.Address, delegatorAddress);
Expand Down
33 changes: 19 additions & 14 deletions Lib9c/Delegation/Delegator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,23 @@ private Delegator(DelegatorMetadata metadata, TRepository repository)

public Address Address => Metadata.DelegatorAddress;

public virtual void Delegate(
TDelegatee delegatee, FungibleAssetValue fav, long height)
public virtual void Delegate(TDelegatee delegatee, FungibleAssetValue fav, long height)
{
if (fav.Sign <= 0)
{
throw new ArgumentOutOfRangeException(
nameof(fav), fav, "Fungible asset value must be positive.");
}

if (delegatee.Metadata.Tombstoned)
var delegateeMetadata = delegatee.Metadata;
if (delegateeMetadata.Tombstoned)
{
throw new InvalidOperationException("Delegatee is tombstoned.");
}

delegatee.Bond((TDelegator)this, fav, height);
Metadata.AddDelegatee(delegatee.Address);
Repository.TransferAsset(Metadata.DelegationPoolAddress, delegatee.Metadata.DelegationPoolAddress, fav);
Repository.TransferAsset(Metadata.DelegationPoolAddress, delegateeMetadata.DelegationPoolAddress, fav);
Repository.SetDelegator((TDelegator)this);
}

Expand All @@ -91,11 +91,15 @@ public virtual void Undelegate(
}

var delegateeMetadata = delegatee.Metadata;
FungibleAssetValue fav = delegatee.Unbond((TDelegator)this, share, height);
unbondLockIn = unbondLockIn.LockIn(
fav, height, height + delegatee.Metadata.UnbondingPeriod);

if (Repository.GetBond(delegatee, Address).Share.IsZero)
var unbondingPeriod = delegatee.Metadata.UnbondingPeriod;
var expireHeight = height + unbondingPeriod;
var oldFav = delegatee.Metadata.TotalDelegatedFAV;
var bond = delegatee.Unbond((TDelegator)this, share, height);
var newFav = delegatee.Metadata.TotalDelegatedFAV;
var fav = oldFav - newFav;

unbondLockIn = unbondLockIn.LockIn(fav, height, expireHeight);
if (bond.Share.IsZero)
{
Metadata.RemoveDelegatee(delegatee.Address);
}
Expand Down Expand Up @@ -129,17 +133,18 @@ public virtual void Redelegate(
}

var srcDelegateeMetadata = srcDelegatee.Metadata;
FungibleAssetValue fav = srcDelegatee.Unbond(
(TDelegator)this, share, height);
dstDelegatee.Bond(
(TDelegator)this, fav, height);
var oldFav = srcDelegateeMetadata.TotalDelegatedFAV;
var srcBond = srcDelegatee.Unbond((TDelegator)this, share, height);
var newFav = srcDelegateeMetadata.TotalDelegatedFAV;
var fav = oldFav - newFav;
var dstBond = dstDelegatee.Bond((TDelegator)this, fav, height);
RebondGrace srcRebondGrace = Repository.GetRebondGrace(srcDelegatee, Address).Grace(
dstDelegatee.Address,
fav,
height,
height + srcDelegatee.Metadata.UnbondingPeriod);

if (Repository.GetBond(srcDelegatee, Address).Share.IsZero)
if (srcBond.Share.IsZero)
{
Metadata.RemoveDelegatee(srcDelegatee.Address);
}
Expand Down
10 changes: 5 additions & 5 deletions Lib9c/Delegation/UnbondingEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ private UnbondingEntry(List bencoded)
}

public UnbondingEntry(
Address unbondeeAddress,
FungibleAssetValue initialUnbondingFAV,
FungibleAssetValue unbondingFAV,
long creationHeight,
long expireHeight)
Address unbondeeAddress,
FungibleAssetValue initialUnbondingFAV,
FungibleAssetValue unbondingFAV,
long creationHeight,
long expireHeight)
{
if (initialUnbondingFAV.Sign <= 0)
{
Expand Down
9 changes: 5 additions & 4 deletions Lib9c/Delegation/UnbondingRef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

namespace Nekoyume.Delegation
{
public class UnbondingRef : IEquatable<UnbondingRef>, IComparable<UnbondingRef>, IComparable, IBencodable
public sealed class UnbondingRef
: IEquatable<UnbondingRef>, IComparable<UnbondingRef>, IComparable, IBencodable
{
public UnbondingRef(Address address, UnbondingType unbondingType)
{
Expand All @@ -32,10 +33,10 @@ public UnbondingRef(List list)
.Add(Address.Bencoded)
.Add((int)UnbondingType);

public static bool operator ==(UnbondingRef? left, UnbondingRef? right)
=> left?.Equals(right) ?? right is null;
public static bool operator ==(UnbondingRef left, UnbondingRef right)
=> left.Equals(right);

public static bool operator !=(UnbondingRef? left, UnbondingRef? right)
public static bool operator !=(UnbondingRef left, UnbondingRef right)
=> !(left == right);

public static bool operator <(UnbondingRef left, UnbondingRef right)
Expand Down
7 changes: 0 additions & 7 deletions Lib9c/Delegation/UnbondingSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,6 @@ public List Bencoded

public bool IsEmpty => UnbondingRefs.IsEmpty;

// public ImmutableArray<IUnbonding> UnbondingsToRelease(long height)
// => UnbondingRefs
// .TakeWhile(kv => kv.Key <= height)
// .SelectMany(kv => kv.Value)
// .Select(unbondingRef => UnbondingFactory.GetUnbondingFromRef(unbondingRef))
// .ToImmutableArray();

public UnbondingSet SetUnbondings(IEnumerable<IUnbonding> unbondings)
{
UnbondingSet result = this;
Expand Down

0 comments on commit a167055

Please sign in to comment.