Skip to content

Commit

Permalink
Merge pull request #2083 from boscohyun/feat/claim_stake_reward7
Browse files Browse the repository at this point in the history
feat: introduce "claim_stake_reward7" as `ClaimStakeReward`
  • Loading branch information
boscohyun authored Aug 22, 2023
2 parents 69b958e + f922a96 commit 63d49b6
Show file tree
Hide file tree
Showing 10 changed files with 834 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .Lib9c.Tests/Action/ClaimStakeReward3Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public ClaimStakeReward3Test(ITestOutputHelper outputHelper)
{
{
nameof(StakeRegularRewardSheet),
ClaimStakeReward.V2.StakeRegularRewardSheetCsv
ClaimStakeReward6.V2.StakeRegularRewardSheetCsv
},
});
_ncg = _initialStatesWithAvatarStateV1.GetGoldCurrency();
Expand Down
2 changes: 1 addition & 1 deletion .Lib9c.Tests/Action/ClaimStakeReward4Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public ClaimStakeReward4Test(ITestOutputHelper outputHelper)
{
{
nameof(StakeRegularRewardSheet),
ClaimStakeReward.V2.StakeRegularRewardSheetCsv
ClaimStakeReward6.V2.StakeRegularRewardSheetCsv
},
});
_ncg = _initialStatesWithAvatarStateV1.GetGoldCurrency();
Expand Down
2 changes: 1 addition & 1 deletion .Lib9c.Tests/Action/ClaimStakeReward5Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public ClaimStakeReward5Test(ITestOutputHelper outputHelper)
{
{
nameof(StakeRegularRewardSheet),
ClaimStakeReward.V2.StakeRegularRewardSheetCsv
ClaimStakeReward6.V2.StakeRegularRewardSheetCsv
},
});
_ncg = _initialStatesWithAvatarStateV1.GetGoldCurrency();
Expand Down
319 changes: 319 additions & 0 deletions .Lib9c.Tests/Action/ClaimStakeReward6Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,319 @@
#nullable enable

namespace Lib9c.Tests.Action
{
using System.Linq;
using Lib9c.Tests.Util;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Types.Assets;
using Nekoyume.Action;
using Nekoyume.Helper;
using Nekoyume.Model.State;
using Serilog;
using Xunit;
using Xunit.Abstractions;

public class ClaimStakeReward6Test
{
private const string AgentAddressHex = "0x0000000001000000000100000000010000000001";
private readonly Address _agentAddr = new Address(AgentAddressHex);
private readonly Address _avatarAddr;
private readonly IAccountStateDelta _initialStatesWithAvatarStateV1;
private readonly IAccountStateDelta _initialStatesWithAvatarStateV2;
private readonly Currency _ncg;

public ClaimStakeReward6Test(ITestOutputHelper outputHelper)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.TestOutput(outputHelper)
.CreateLogger();
(
_,
_,
_avatarAddr,
_initialStatesWithAvatarStateV1,
_initialStatesWithAvatarStateV2) = InitializeUtil.InitializeStates(
agentAddr: _agentAddr);
_ncg = _initialStatesWithAvatarStateV1.GetGoldCurrency();
}

[Fact]
public void Serialization()
{
var action = new ClaimStakeReward6(_avatarAddr);
var deserialized = new ClaimStakeReward6();
deserialized.LoadPlainValue(action.PlainValue);
Assert.Equal(action.AvatarAddress, deserialized.AvatarAddress);
}

[Theory]
[InlineData(
ClaimStakeReward2.ObsoletedIndex,
100L,
null,
ClaimStakeReward2.ObsoletedIndex + StakeState.LockupInterval,
40,
4,
0,
null,
null,
0L
)]
[InlineData(
ClaimStakeReward2.ObsoletedIndex,
6000L,
null,
ClaimStakeReward2.ObsoletedIndex + StakeState.LockupInterval,
4800,
36,
4,
null,
null,
0L
)]
// Calculate rune start from hard fork index
[InlineData(
0L,
6000L,
0L,
ClaimStakeReward2.ObsoletedIndex + StakeState.LockupInterval,
136800,
1026,
3,
null,
null,
0L
)]
// Stake reward v2
// Stake before v2, prev. receive v1, receive v1 & v2
[InlineData(
StakeState.StakeRewardSheetV2Index - StakeState.RewardInterval * 2,
50L,
StakeState.StakeRewardSheetV2Index - StakeState.RewardInterval,
StakeState.StakeRewardSheetV2Index + 1,
5,
1,
0,
null,
null,
0L
)]
// Stake before v2, prev. receive v2, receive v2
[InlineData(
StakeState.StakeRewardSheetV2Index - StakeState.RewardInterval,
50L,
StakeState.StakeRewardSheetV2Index,
StakeState.StakeRewardSheetV2Index + StakeState.RewardInterval,
5,
1,
0,
null,
null,
0L
)]
// Stake after v2, no prev. receive, receive v2
[InlineData(
StakeState.StakeRewardSheetV2Index,
6000L,
null,
StakeState.StakeRewardSheetV2Index + StakeState.RewardInterval,
1200,
9,
1,
null,
null,
0L
)]
// stake after v2, prev. receive v2, receive v2
[InlineData(
StakeState.StakeRewardSheetV2Index,
50L,
StakeState.StakeRewardSheetV2Index + StakeState.RewardInterval,
StakeState.StakeRewardSheetV2Index + StakeState.RewardInterval * 2,
5,
1,
0,
null,
null,
0L
)]
// stake before currency as reward, non prev.
[InlineData(
StakeState.CurrencyAsRewardStartIndex - StakeState.RewardInterval * 2,
10_000_000L,
null,
StakeState.CurrencyAsRewardStartIndex + StakeState.RewardInterval,
3_000_000,
37_506,
4_998,
AgentAddressHex,
"GARAGE",
100_000L
)]
// stake before currency as reward, prev.
[InlineData(
StakeState.CurrencyAsRewardStartIndex - StakeState.RewardInterval * 2,
10_000_000L,
StakeState.CurrencyAsRewardStartIndex - StakeState.RewardInterval,
StakeState.CurrencyAsRewardStartIndex + StakeState.RewardInterval,
2_000_000,
25_004,
3_332,
AgentAddressHex,
"GARAGE",
100_000L
)]
// test tx(c46cf83c46bc106372015a5020d6b9f15dc733819a6dc3fde37d9b5625fc3d93)
[InlineData(
7_009_561L,
10_000_000L,
7_110_390L,
7_160_778L,
1_000_000,
12_502,
1_666,
null,
null,
0)]
// test tx(3baf5904b8499975a27d3873e58953ef8d0aa740318e99b2fe6a85428c9eb7aa)
[InlineData(
5_350_456L,
500_000L,
7_576_016L,
7_625_216L,
100_000,
627,
83,
null,
null,
0)]
public void Execute_Success(
long startedBlockIndex,
long stakeAmount,
long? previousRewardReceiveIndex,
long blockIndex,
int expectedHourglass,
int expectedApStone,
int expectedRune,
string expectedCurrencyAddrHex,
string expectedCurrencyTicker,
long expectedCurrencyAmount)
{
Execute(
_initialStatesWithAvatarStateV1,
_agentAddr,
_avatarAddr,
startedBlockIndex,
stakeAmount,
previousRewardReceiveIndex,
blockIndex,
expectedHourglass,
expectedApStone,
expectedRune,
expectedCurrencyAddrHex,
expectedCurrencyTicker,
expectedCurrencyAmount);

Execute(
_initialStatesWithAvatarStateV2,
_agentAddr,
_avatarAddr,
startedBlockIndex,
stakeAmount,
previousRewardReceiveIndex,
blockIndex,
expectedHourglass,
expectedApStone,
expectedRune,
expectedCurrencyAddrHex,
expectedCurrencyTicker,
expectedCurrencyAmount);
}

private void Execute(
IAccountStateDelta prevState,
Address agentAddr,
Address avatarAddr,
long startedBlockIndex,
long stakeAmount,
long? previousRewardReceiveIndex,
long blockIndex,
int expectedHourglass,
int expectedApStone,
int expectedRune,
string expectedCurrencyAddrHex,
string expectedCurrencyTicker,
long expectedCurrencyAmount)
{
var context = new ActionContext();
var stakeStateAddr = StakeState.DeriveAddress(agentAddr);
var initialStakeState = new StakeState(stakeStateAddr, startedBlockIndex);
if (!(previousRewardReceiveIndex is null))
{
initialStakeState.Claim((long)previousRewardReceiveIndex);
}

prevState = prevState
.SetState(stakeStateAddr, initialStakeState.Serialize())
.MintAsset(context, stakeStateAddr, _ncg * stakeAmount);

var action = new ClaimStakeReward6(avatarAddr);
var states = action.Execute(new ActionContext
{
PreviousState = prevState,
Signer = agentAddr,
BlockIndex = blockIndex,
});

var avatarState = states.GetAvatarStateV2(avatarAddr);
if (expectedHourglass > 0)
{
Assert.Equal(
expectedHourglass,
avatarState.inventory.Items.First(x => x.item.Id == 400000).count);
}
else
{
Assert.DoesNotContain(avatarState.inventory.Items, x => x.item.Id == 400000);
}

if (expectedApStone > 0)
{
Assert.Equal(
expectedApStone,
avatarState.inventory.Items.First(x => x.item.Id == 500000).count);
}
else
{
Assert.DoesNotContain(avatarState.inventory.Items, x => x.item.Id == 500000);
}

if (expectedRune > 0)
{
Assert.Equal(
expectedRune * RuneHelper.StakeRune,
states.GetBalance(avatarAddr, RuneHelper.StakeRune));
}
else
{
Assert.Equal(
0 * RuneHelper.StakeRune,
states.GetBalance(avatarAddr, RuneHelper.StakeRune));
}

if (!string.IsNullOrEmpty(expectedCurrencyAddrHex))
{
var addr = new Address(expectedCurrencyAddrHex);
var currency = Currencies.GetMinterlessCurrency(expectedCurrencyTicker);
Assert.Equal(
expectedCurrencyAmount * currency,
states.GetBalance(addr, currency));
}

Assert.True(states.TryGetStakeState(agentAddr, out StakeState stakeState));
Assert.Equal(blockIndex, stakeState.ReceivedBlockIndex);
}
}
}
24 changes: 6 additions & 18 deletions .Lib9c.Tests/Action/ClaimStakeRewardTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ public void Serialization()
6000L,
null,
StakeState.StakeRewardSheetV2Index + StakeState.RewardInterval,
1200,
9,
3000,
17,
1,
null,
null,
Expand All @@ -145,8 +145,8 @@ public void Serialization()
10_000_000L,
null,
StakeState.CurrencyAsRewardStartIndex + StakeState.RewardInterval,
3_000_000,
37_506,
15_000_000,
75_006,
4_998,
AgentAddressHex,
"GARAGE",
Expand All @@ -158,25 +158,13 @@ public void Serialization()
10_000_000L,
StakeState.CurrencyAsRewardStartIndex - StakeState.RewardInterval,
StakeState.CurrencyAsRewardStartIndex + StakeState.RewardInterval,
2_000_000,
25_004,
10_000_000,
50_004,
3_332,
AgentAddressHex,
"GARAGE",
100_000L
)]
// test tx(c46cf83c46bc106372015a5020d6b9f15dc733819a6dc3fde37d9b5625fc3d93)
[InlineData(
7_009_561L,
10_000_000L,
7_110_390L,
7_160_778L,
1_000_000,
12_502,
1_666,
null,
null,
0)]
public void Execute_Success(
long startedBlockIndex,
long stakeAmount,
Expand Down
Loading

0 comments on commit 63d49b6

Please sign in to comment.