Skip to content

Commit

Permalink
Merge pull request planetarium#2108 from boscohyun/improve-claim-stak…
Browse files Browse the repository at this point in the history
…e-reward8

Improve claim stake reward8
  • Loading branch information
boscohyun authored Aug 29, 2023
2 parents 7fffe0b + 210b3b0 commit 849e443
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 2 deletions.
207 changes: 207 additions & 0 deletions .Lib9c.Tests/Action/ClaimStakeRewardTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ namespace Lib9c.Tests.Action
public class ClaimStakeRewardTest
{
private const string AgentAddressHex = "0x0000000001000000000100000000010000000001";

// VALUE: 6_692_400L
// - receive v1 reward * 1
// - receive v2(w/o currency) reward * 4
// - receive v2(w/ currency) reward * 14
// - receive v3 reward * n
private const long BlockIndexForTest =
StakeState.StakeRewardSheetV3Index -
((StakeState.StakeRewardSheetV3Index - StakeState.StakeRewardSheetV2Index) / StakeState.RewardInterval + 1) *
StakeState.RewardInterval;

private readonly Address _agentAddr = new Address(AgentAddressHex);
private readonly Address _avatarAddr;
private readonly IAccountStateDelta _initialStatesWithAvatarStateV1;
Expand Down Expand Up @@ -140,6 +151,7 @@ public void Serialization()
0L
)]
// stake before currency as reward, non prev.
// receive v2(w/o currency) * 2, receive v2(w/ currency). check GARAGE.
[InlineData(
StakeState.CurrencyAsRewardStartIndex - StakeState.RewardInterval * 2,
10_000_000L,
Expand All @@ -153,6 +165,7 @@ public void Serialization()
100_000L
)]
// stake before currency as reward, prev.
// receive v2(w/o currency), receive v2(w/ currency). check GARAGE.
[InlineData(
StakeState.CurrencyAsRewardStartIndex - StakeState.RewardInterval * 2,
10_000_000L,
Expand All @@ -165,6 +178,200 @@ public void Serialization()
"GARAGE",
100_000L
)]
// stake before v3(crystal), non prev. receive v2. check CRYSTAL.
[InlineData(
StakeState.StakeRewardSheetV3Index - 1,
500L,
null,
StakeState.StakeRewardSheetV3Index - 1 + StakeState.RewardInterval,
125,
2,
0,
AgentAddressHex,
"CRYSTAL",
0L
)]
// stake after v3(crystal), non prev. receive v3. check CRYSTAL.
[InlineData(
StakeState.StakeRewardSheetV3Index,
500L,
null,
StakeState.StakeRewardSheetV3Index + StakeState.RewardInterval,
125,
2,
0,
AgentAddressHex,
"CRYSTAL",
5_000L
)]
// stake before v3(crystal), non prev. receive v2 * 2, receive v3. check CRYSTAL.
[InlineData(
StakeState.StakeRewardSheetV3Index - StakeState.RewardInterval * 2,
10_000_000L,
null,
StakeState.StakeRewardSheetV3Index + StakeState.RewardInterval,
35_000_000,
175_006,
11_665,
AgentAddressHex,
"CRYSTAL",
1_000_000_000L
)]
// stake before v3(crystal), prev. receive v2, receive v3. check CRYSTAL.
[InlineData(
StakeState.StakeRewardSheetV3Index - StakeState.RewardInterval * 2,
10_000_000L,
StakeState.StakeRewardSheetV3Index - StakeState.RewardInterval,
StakeState.StakeRewardSheetV3Index + StakeState.RewardInterval,
30_000_000,
150_004,
9_999,
AgentAddressHex,
"CRYSTAL",
1_000_000_000L
)]
// stake after v3(crystal), non prev. receive v2 * 2, receive v3. check CRYSTAL.
[InlineData(
StakeState.StakeRewardSheetV3Index,
10_000_000L,
null,
StakeState.StakeRewardSheetV3Index + StakeState.RewardInterval * 3,
75_000_000,
375_006,
24_999,
AgentAddressHex,
"CRYSTAL",
3_000_000_000L
)]
// stake after v3(crystal), prev. receive v2, receive v3. check CRYSTAL.
[InlineData(
StakeState.StakeRewardSheetV3Index,
10_000_000L,
StakeState.StakeRewardSheetV3Index + StakeState.RewardInterval,
StakeState.StakeRewardSheetV3Index + StakeState.RewardInterval * 3,
50_000_000,
250_004,
16_666,
AgentAddressHex,
"CRYSTAL",
2_000_000_000L
)]
// stake before v2(w/o currency), non prev.
// receive v1.
[InlineData(
BlockIndexForTest,
500L,
null,
BlockIndexForTest + StakeState.RewardInterval,
62,
2,
0,
null,
null,
0L
)]
// stake before v2(w/o currency), non prev.
// receive v1, do not receive v2(w/o currency).
[InlineData(
BlockIndexForTest,
500L,
null,
StakeState.StakeRewardSheetV2Index + StakeState.RewardInterval - 1,
62,
2,
0,
null,
null,
0L
)]
// stake before v2(w/o currency), non prev.
// receive v1, receive v2(w/o currency).
[InlineData(
BlockIndexForTest,
500L,
null,
StakeState.StakeRewardSheetV2Index + StakeState.RewardInterval * 2 - 1,
187,
4,
0,
null,
null,
0L
)]
// stake before v2(w/o currency), non prev.
// receive v1, receive v2(w/o currency) * 3, do not receive v2(w/ currency).
[InlineData(
BlockIndexForTest,
500L,
null,
StakeState.CurrencyAsRewardStartIndex + StakeState.RewardInterval - 1,
562,
10,
0,
null,
null,
0L
)]
// stake before v2(w/o currency), non prev.
// receive v1, receive v2(w/o currency) * 3, receive v2(w/ currency).
// check GARAGE is 0 when stake 500.
[InlineData(
BlockIndexForTest,
500L,
null,
StakeState.CurrencyAsRewardStartIndex + StakeState.RewardInterval * 2 - 1,
687,
12,
0,
AgentAddressHex,
"GARAGE",
0L
)]
// stake before v2(w/o currency), non prev.
// receive v1, receive v2(w/o currency) * 3, receive v2(w/ currency).
// check GARAGE is 100,000 when stake 10,000,000.
[InlineData(
BlockIndexForTest,
10_000_000L,
null,
StakeState.CurrencyAsRewardStartIndex + StakeState.RewardInterval * 2 - 1,
27_000_000,
137_512,
9_996,
AgentAddressHex,
"GARAGE",
100_000L
)]
// stake before v2(w/o currency), non prev.
// receive v1, receive v2(w/o currency) * 3, receive v2(w/ currency) * ???, no receive v3.
// check CRYSTAL is 0.
[InlineData(
BlockIndexForTest,
500L,
null,
StakeState.StakeRewardSheetV3Index + StakeState.RewardInterval - 1,
2_312,
38,
0,
AgentAddressHex,
"CRYSTAL",
0L
)]
// stake before v2(w/o currency), non prev.
// receive v1, receive v2(w/o currency) * 3, receive v2(w/ currency) * ???, receive v3.
// check CRYSTAL is ???.
[InlineData(
BlockIndexForTest,
500L,
null,
StakeState.StakeRewardSheetV3Index + StakeState.RewardInterval * 2 - 1,
2_437,
40,
0,
AgentAddressHex,
"CRYSTAL",
5_000L
)]
public void Execute_Success(
long startedBlockIndex,
long stakeAmount,
Expand Down
11 changes: 11 additions & 0 deletions Lib9c/Action/ClaimStakeReward.cs
Original file line number Diff line number Diff line change
Expand Up @@ -437,24 +437,35 @@ private IAccountStateDelta ProcessReward(
continue;
}

// NOTE: prepare reward currency.
Currency rewardCurrency;
// NOTE: this line covers the reward.CurrencyTicker is following cases:
// - Currencies.Crystal.Ticker
// - Currencies.Garage.Ticker
// - lower case is starting with "rune_" or "runestone_"
// - lower case is starting with "soulstone_"
try
{
rewardCurrency =
Currencies.GetMinterlessCurrency(reward.CurrencyTicker);
}
// NOTE: throw exception if reward.CurrencyTicker is null or empty.
catch (ArgumentNullException)
{
throw;
}
// NOTE: handle the case that reward.CurrencyTicker isn't covered by
// Currencies.GetMinterlessCurrency().
catch (ArgumentException)
{
// NOTE: throw exception if reward.CurrencyDecimalPlaces is null.
if (reward.CurrencyDecimalPlaces is null)
{
throw new ArgumentException(
$"Decimal places of {reward.CurrencyTicker} is null");
}

// NOTE: new currency is created as uncapped currency.
rewardCurrency = Currency.Uncapped(
reward.CurrencyTicker,
Convert.ToByte(reward.CurrencyDecimalPlaces.Value),
Expand Down
2 changes: 1 addition & 1 deletion Lib9c/Action/ClaimStakeReward7.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Nekoyume.Action
public class ClaimStakeReward7 : GameAction, IClaimStakeReward, IClaimStakeRewardV1
{
private const string ActionTypeText = "claim_stake_reward7";
public const long ObsoleteBlockIndex = ActionObsoleteConfig.V200062ObsoleteIndex;
public const long ObsoleteBlockIndex = ActionObsoleteConfig.V200063ObsoleteIndex;

/// <summary>
/// This is the version 1 of the stake reward sheet.
Expand Down
2 changes: 1 addition & 1 deletion Lib9c/ActionObsoleteConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static class ActionObsoleteConfig

public const long V200061ObsoleteIndex = 7_660_000L;

public const long V200062ObsoleteIndex = 7_700_000L;
public const long V200063ObsoleteIndex = 7_700_000L;

// While v200020, the action obsolete wasn't work well.
// So other previous `V*ObsoletedIndex`s lost its meaning and
Expand Down
11 changes: 11 additions & 0 deletions Lib9c/Currencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ public static class Currencies

public static readonly Currency Mead = Currency.Legacy("Mead", 18, null);

/// <summary>
/// Covers the reward.CurrencyTicker is following cases:
/// - Currencies.Crystal.Ticker
/// - Currencies.Garage.Ticker
/// - lower case is starting with "rune_" or "runestone_"
/// - lower case is starting with "soulstone_"
/// </summary>
/// <param name="ticker"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public static Currency GetMinterlessCurrency(string? ticker)
{
if (string.IsNullOrEmpty(ticker))
Expand Down

0 comments on commit 849e443

Please sign in to comment.