Skip to content

Commit

Permalink
refactor: Convert the repository class to a generic type
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Dec 26, 2024
1 parent 4a4e14a commit 7aa9b9d
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 82 deletions.
76 changes: 34 additions & 42 deletions Lib9c/Delegation/Delegatee.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

namespace Nekoyume.Delegation
{
public abstract class Delegatee<T, TSelf> : IDelegatee
where T : Delegator<TSelf, T>
where TSelf : Delegatee<T, TSelf>
public abstract class Delegatee<TRepository, TDelegatee, TDelegator> : IDelegatee
where TRepository : DelegationRepository<TRepository, TDelegatee, TDelegator>
where TDelegatee : Delegatee<TRepository, TDelegatee, TDelegator>
where TDelegator : Delegator<TRepository, TDelegatee, TDelegator>
{
public Delegatee(
Address address,
Expand All @@ -26,7 +27,7 @@ public Delegatee(
long unbondingPeriod,
int maxUnbondLockInEntries,
int maxRebondGraceEntries,
IDelegationRepository repository)
TRepository repository)
: this(
new DelegateeMetadata(
address,
Expand All @@ -46,12 +47,12 @@ public Delegatee(

public Delegatee(
Address address,
IDelegationRepository repository)
TRepository repository)
: this(repository.GetDelegateeMetadata(address), repository)
{
}

private Delegatee(DelegateeMetadata metadata, IDelegationRepository repository)
private Delegatee(DelegateeMetadata metadata, TRepository repository)
{
Metadata = metadata;
Repository = repository;
Expand All @@ -65,7 +66,7 @@ private Delegatee(DelegateeMetadata metadata, IDelegationRepository repository)

public DelegateeMetadata Metadata { get; }

public IDelegationRepository Repository { get; }
public TRepository Repository { get; }

public Address Address => Metadata.DelegateeAddress;

Expand Down Expand Up @@ -109,14 +110,14 @@ public BigInteger ShareFromFAV(FungibleAssetValue fav)
public FungibleAssetValue FAVFromShare(BigInteger share)
=> Metadata.FAVFromShare(share);

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

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

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

public void Jail(long releaseHeight)
{
Expand Down Expand Up @@ -200,7 +201,7 @@ public Address CurrentLumpSumRewardsRecordAddress()
public Address LumpSumRewardsRecordAddress(long height)
=> Metadata.LumpSumRewardsRecordAddress(height);

public virtual BigInteger Bond(T delegator, FungibleAssetValue fav, long height)
public virtual BigInteger Bond(TDelegator delegator, FungibleAssetValue fav, long height)
{
DistributeReward(delegator, height);

Expand All @@ -216,7 +217,7 @@ public virtual BigInteger Bond(T delegator, FungibleAssetValue fav, long height)
"Cannot bond to tombstoned delegatee.");
}

Bond bond = Repository.GetBond(this, delegator.Address);
Bond bond = Repository.GetBond((TDelegatee)this, delegator.Address);
BigInteger share = ShareFromFAV(fav);
bond = bond.AddShare(share);
Metadata.AddShare(share);
Expand All @@ -229,10 +230,7 @@ public virtual BigInteger Bond(T delegator, FungibleAssetValue fav, long height)
return share;
}

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

public FungibleAssetValue Unbond(T delegator, BigInteger share, long height)
public FungibleAssetValue Unbond(TDelegator delegator, BigInteger share, long height)
{
DistributeReward(delegator, height);
if (TotalShares.IsZero || TotalDelegated.RawValue.IsZero)
Expand All @@ -241,7 +239,7 @@ public FungibleAssetValue Unbond(T delegator, BigInteger share, long height)
"Cannot unbond without bonding.");
}

Bond bond = Repository!.GetBond(this, delegator.Address);
Bond bond = Repository!.GetBond((TDelegatee)this, delegator.Address);
FungibleAssetValue fav = FAVFromShare(share);
bond = bond.SubtractShare(share);
if (bond.Share.IsZero)
Expand All @@ -259,19 +257,16 @@ public FungibleAssetValue Unbond(T delegator, BigInteger share, long height)
return fav;
}

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

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

if (!share.IsZero && bond.LastDistributeHeight.HasValue)
{
if (Repository.GetCurrentRewardBase(this) is RewardBase rewardBase)
if (Repository.GetCurrentRewardBase((TDelegatee)this) is RewardBase rewardBase)
{
var lastRewardBase = Repository.GetRewardBase(this, bond.LastDistributeHeight.Value);
var lastRewardBase = Repository.GetRewardBase((TDelegatee)this, bond.LastDistributeHeight.Value);
TransferReward(delegator, share, rewardBase, lastRewardBase);
// TransferRemainders(newRecord);
}
Expand All @@ -297,13 +292,10 @@ IEnumerable<LumpSumRewardsRecord> lumpSumRewardsRecords
Repository.SetBond(bond);
}

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

public void CollectRewards(long height)
{
var rewards = RewardCurrencies.Select(c => Repository.GetBalance(RewardPoolAddress, c));
if (Repository.GetCurrentRewardBase(this) is RewardBase rewardBase)
if (Repository.GetCurrentRewardBase((TDelegatee)this) is RewardBase rewardBase)
{
rewardBase = rewardBase.AddRewards(rewards, TotalShares);

Expand All @@ -319,7 +311,7 @@ public void CollectRewards(long height)
}
else
{
LumpSumRewardsRecord record = Repository.GetCurrentLumpSumRewardsRecord(this)
LumpSumRewardsRecord record = Repository.GetCurrentLumpSumRewardsRecord((TDelegatee)this)
?? new LumpSumRewardsRecord(
CurrentLumpSumRewardsRecordAddress(),
height,
Expand All @@ -334,7 +326,7 @@ record = record.AddLumpSumRewards(rewards);
Repository.TransferAsset(RewardPoolAddress, record.Address, rewardsEach);
}
}

Repository.SetLumpSumRewardsRecord(record);
}
}
Expand Down Expand Up @@ -436,10 +428,10 @@ public void StartNewRewardPeriod(long height)
MigrateLumpSumRewardsRecords();

RewardBase newRewardBase;
if (Repository.GetCurrentRewardBase(this) is RewardBase rewardBase)
if (Repository.GetCurrentRewardBase((TDelegatee)this) is RewardBase rewardBase)
{
newRewardBase = rewardBase.UpdateSigFig(TotalShares);
if (Repository.GetRewardBase(this, height) is not null)
if (Repository.GetRewardBase((TDelegatee)this, height) is not null)
{
Repository.SetRewardBase(newRewardBase);
return;
Expand Down Expand Up @@ -469,7 +461,7 @@ private List<LumpSumRewardsRecord> GetLumpSumRewardsRecords(long? lastRewardHeig
{
List<LumpSumRewardsRecord> records = new();
if (lastRewardHeight is null
|| !(Repository.GetCurrentLumpSumRewardsRecord(this) is LumpSumRewardsRecord record))
|| !(Repository.GetCurrentLumpSumRewardsRecord((TDelegatee)this) is LumpSumRewardsRecord record))
{
return records;
}
Expand All @@ -483,15 +475,15 @@ private List<LumpSumRewardsRecord> GetLumpSumRewardsRecords(long? lastRewardHeig
break;
}

record = Repository.GetLumpSumRewardsRecord(this, lastStartHeight)
record = Repository.GetLumpSumRewardsRecord((TDelegatee)this, lastStartHeight)
?? throw new InvalidOperationException(
$"Lump sum rewards record for #{lastStartHeight} is missing");
}

return records;
}

private void TransferReward(T delegator, BigInteger share, LumpSumRewardsRecord record)
private void TransferReward(TDelegator delegator, BigInteger share, LumpSumRewardsRecord record)
{
ImmutableSortedDictionary<Currency, FungibleAssetValue> reward = record.RewardsDuringPeriod(share);
foreach (var rewardEach in reward)
Expand All @@ -504,7 +496,7 @@ private void TransferReward(T delegator, BigInteger share, LumpSumRewardsRecord
}

private void TransferReward(
T delegator,
TDelegator delegator,
BigInteger share,
RewardBase currentRewardBase,
RewardBase? lastRewardBase)
Expand Down Expand Up @@ -549,7 +541,7 @@ private void MigrateLumpSumRewardsRecords()
var growSize = 100;
var capacity = 5000;
List<LumpSumRewardsRecord> records = new(capacity);
if (!(Repository.GetCurrentLumpSumRewardsRecord(this) is LumpSumRewardsRecord record))
if (!(Repository.GetCurrentLumpSumRewardsRecord((TDelegatee)this) is LumpSumRewardsRecord record))
{
return;
}
Expand All @@ -563,7 +555,7 @@ private void MigrateLumpSumRewardsRecords()
}

records.Add(record);
record = Repository.GetLumpSumRewardsRecord(this, lastStartHeight)
record = Repository.GetLumpSumRewardsRecord((TDelegatee)this, lastStartHeight)
?? throw new InvalidOperationException(
$"Lump sum rewards record for #{lastStartHeight} is missing");
}
Expand All @@ -583,7 +575,7 @@ record = Repository.GetLumpSumRewardsRecord(this, lastStartHeight)
else
{
var newRewardBase = rewardBase.UpdateSigFig(recordEach.TotalShares);
if (Repository.GetRewardBase(this, recordEach.StartHeight) is not null)
if (Repository.GetRewardBase((TDelegatee)this, recordEach.StartHeight) is not null)
{
Repository.SetRewardBase(newRewardBase);
}
Expand Down
58 changes: 46 additions & 12 deletions Lib9c/Delegation/DelegationRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

namespace Nekoyume.Delegation
{
public abstract class DelegationRepository : IDelegationRepository
public abstract class DelegationRepository<TRepository, TDelegatee, TDelegator> : IDelegationRepository
where TRepository : DelegationRepository<TRepository, TDelegatee, TDelegator>
where TDelegatee : Delegatee<TRepository, TDelegatee, TDelegator>
where TDelegator : Delegator<TRepository, TDelegatee, TDelegator>
{
protected IWorld previousWorld;
protected IAccount delegateeAccount;
Expand Down Expand Up @@ -129,13 +132,13 @@ public DelegationRepository(
/// </summary>
public Address LumpSumRewardsRecordAccountAddress { get; }

public abstract IDelegatee GetDelegatee(Address address);
public abstract TDelegatee GetDelegatee(Address address);

public abstract IDelegator GetDelegator(Address address);
public abstract TDelegator GetDelegator(Address address);

public abstract void SetDelegatee(IDelegatee delegatee);
public abstract void SetDelegatee(TDelegatee delegatee);

public abstract void SetDelegator(IDelegator delegator);
public abstract void SetDelegator(TDelegator delegator);

public DelegateeMetadata GetDelegateeMetadata(Address delegateeAddress)
{
Expand All @@ -155,7 +158,7 @@ public DelegatorMetadata GetDelegatorMetadata(Address delegatorAddress)
: throw new FailedLoadStateException("DelegatorMetadata not found.");
}

public Bond GetBond(IDelegatee delegatee, Address delegatorAddress)
public Bond GetBond(TDelegatee delegatee, Address delegatorAddress)
{
Address address = delegatee.BondAddress(delegatorAddress);
IValue? value = bondAccount.GetState(address);
Expand All @@ -164,7 +167,7 @@ public Bond GetBond(IDelegatee delegatee, Address delegatorAddress)
: new Bond(address);
}

public UnbondLockIn GetUnbondLockIn(IDelegatee delegatee, Address delegatorAddress)
public UnbondLockIn GetUnbondLockIn(TDelegatee delegatee, Address delegatorAddress)
{
Address address = delegatee.UnbondLockInAddress(delegatorAddress);
IValue? value = unbondLockInAccount.GetState(address);
Expand All @@ -181,7 +184,7 @@ public UnbondLockIn GetUnlimitedUnbondLockIn(Address address)
: throw new FailedLoadStateException("UnbondLockIn not found.");
}

public RebondGrace GetRebondGrace(IDelegatee delegatee, Address delegatorAddress)
public RebondGrace GetRebondGrace(TDelegatee delegatee, Address delegatorAddress)
{
Address address = delegatee.RebondGraceAddress(delegatorAddress);
IValue? value = rebondGraceAccount.GetState(address);
Expand All @@ -204,7 +207,7 @@ public UnbondingSet GetUnbondingSet()
: new UnbondingSet(this);

/// <inheritdoc/>
public RewardBase? GetCurrentRewardBase(IDelegatee delegatee)
public RewardBase? GetCurrentRewardBase(TDelegatee delegatee)
{
Address address = delegatee.CurrentRewardBaseAddress();
IValue? value = rewardBaseAccount.GetState(address);
Expand All @@ -214,7 +217,7 @@ public UnbondingSet GetUnbondingSet()
}

/// <inheritdoc/>
public RewardBase? GetRewardBase(IDelegatee delegatee, long height)
public RewardBase? GetRewardBase(TDelegatee delegatee, long height)
{
Address address = delegatee.RewardBaseAddress(height);
IValue? value = rewardBaseAccount.GetState(address);
Expand All @@ -223,7 +226,7 @@ public UnbondingSet GetUnbondingSet()
: null;
}

public LumpSumRewardsRecord? GetLumpSumRewardsRecord(IDelegatee delegatee, long height)
public LumpSumRewardsRecord? GetLumpSumRewardsRecord(TDelegatee delegatee, long height)
{
Address address = delegatee.LumpSumRewardsRecordAddress(height);
IValue? value = lumpSumRewardsRecordAccount.GetState(address);
Expand All @@ -232,7 +235,7 @@ public UnbondingSet GetUnbondingSet()
: null;
}

public LumpSumRewardsRecord? GetCurrentLumpSumRewardsRecord(IDelegatee delegatee)
public LumpSumRewardsRecord? GetCurrentLumpSumRewardsRecord(TDelegatee delegatee)
{
Address address = delegatee.CurrentLumpSumRewardsRecordAddress();
IValue? value = lumpSumRewardsRecordAccount.GetState(address);
Expand Down Expand Up @@ -321,5 +324,36 @@ public virtual void UpdateWorld(IWorld world)
rewardBaseAccount = world.GetAccount(RewardBaseAccountAddress);
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);

void IDelegationRepository.SetDelegatee(IDelegatee delegatee)
=> SetDelegatee((TDelegatee)delegatee);

void IDelegationRepository.SetDelegator(IDelegator delegator)
=> SetDelegator((TDelegator)delegator);
}
}
Loading

0 comments on commit 7aa9b9d

Please sign in to comment.