-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathClaimStakeReward.cs
189 lines (169 loc) · 6.95 KB
/
ClaimStakeReward.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using System;
using System.Collections.Immutable;
using System.Globalization;
using System.Linq;
using Bencodex.Types;
using Lib9c;
using Lib9c.Abstractions;
using Libplanet.Action;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Types.Assets;
using Nekoyume.Action.Garages;
using Nekoyume.Extensions;
using Nekoyume.Helper;
using Nekoyume.Model.Item;
using Nekoyume.Model.Stake;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Nekoyume.Module.ValidatorDelegation;
using Nekoyume.TableData;
using Nekoyume.ValidatorDelegation;
using static Lib9c.SerializeKeys;
namespace Nekoyume.Action
{
/// <summary>
/// Hard forked at https://github.com/planetarium/lib9c/pull/2097
/// </summary>
[ActionType(ActionTypeText)]
public class ClaimStakeReward : GameAction, IClaimStakeReward, IClaimStakeRewardV1
{
private const string ActionTypeText = "claim_stake_reward9";
internal Address AvatarAddress { get; private set; }
Address IClaimStakeRewardV1.AvatarAddress => AvatarAddress;
public ClaimStakeReward(Address avatarAddress) : this()
{
AvatarAddress = avatarAddress;
}
public ClaimStakeReward()
{
}
protected override IImmutableDictionary<string, IValue> PlainValueInternal =>
ImmutableDictionary<string, IValue>.Empty
.Add(AvatarAddressKey, AvatarAddress.Serialize());
protected override void LoadPlainValueInternal(
IImmutableDictionary<string, IValue> plainValue)
{
AvatarAddress = plainValue[AvatarAddressKey].ToAddress();
}
public override IWorld Execute(IActionContext context)
{
GasTracer.UseGas(1);
var states = context.PreviousState;
var addressesHex = GetSignerAndOtherAddressesHex(context, AvatarAddress);
var stakeStateAddr = LegacyStakeState.DeriveAddress(context.Signer);
var validatorRepository = new ValidatorRepository(states, context);
var isValidator = validatorRepository.TryGetValidatorDelegatee(
context.Signer, out var _);
if (isValidator)
{
throw new InvalidOperationException(
"The validator cannot claim the stake reward.");
}
if (!states.TryGetStakeState(context.Signer, out var stakeStateV2))
{
throw new FailedLoadStateException(
ActionTypeText,
addressesHex,
typeof(LegacyStakeState),
stakeStateAddr);
}
if (stakeStateV2.StateVersion == 2)
{
if (!StakeStateUtils.TryMigrateV2ToV3(
context,
states,
StakeState.DeriveAddress(context.Signer),
stakeStateV2, out var result))
{
throw new InvalidOperationException(
"Failed to migrate stake state. Unexpected situation.");
}
states = result.Value.world;
stakeStateV2 = result.Value.newStakeState;
}
if (stakeStateV2.ClaimableBlockIndex > context.BlockIndex)
{
throw new RequiredBlockIndexException(
ActionTypeText,
addressesHex,
context.BlockIndex);
}
if (!states.TryGetAvatarState(
context.Signer,
AvatarAddress,
out var avatarState))
{
throw new FailedLoadStateException(
ActionTypeText,
addressesHex,
typeof(AvatarState),
AvatarAddress);
}
var sheets = states.GetSheets(sheetTuples: new[]
{
(
typeof(StakeRegularFixedRewardSheet),
stakeStateV2.Contract.StakeRegularFixedRewardSheetTableName
),
(
typeof(StakeRegularRewardSheet),
stakeStateV2.Contract.StakeRegularRewardSheetTableName
),
(typeof(ConsumableItemSheet), nameof(ConsumableItemSheet)),
(typeof(CostumeItemSheet), nameof(CostumeItemSheet)),
(typeof(EquipmentItemSheet), nameof(EquipmentItemSheet)),
(typeof(MaterialItemSheet), nameof(MaterialItemSheet)),
});
var stakeRegularFixedRewardSheet = sheets.GetSheet<StakeRegularFixedRewardSheet>();
var stakeRegularRewardSheet = sheets.GetSheet<StakeRegularRewardSheet>();
// NOTE:
var ncg = states.GetGoldCurrency();
var stakedNcg = states.GetBalance(stakeStateAddr, ncg);
var stakingLevel = Math.Min(
stakeRegularRewardSheet.FindLevelByStakedAmount(
context.Signer,
stakedNcg),
stakeRegularRewardSheet.Keys.Max());
var itemSheet = sheets.GetItemSheet();
// The first reward is given at the claimable block index.
var rewardSteps = stakeStateV2.ClaimableBlockIndex == context.BlockIndex
? 1
: 1 + (int)Math.DivRem(
context.BlockIndex - stakeStateV2.ClaimableBlockIndex,
stakeStateV2.Contract.RewardInterval,
out _);
// Fixed Reward
var random = context.GetRandom();
#pragma warning disable LAA1002
foreach (var pair in StakeRewardCalculator.CalculateFixedRewards(stakingLevel, random, stakeRegularFixedRewardSheet, itemSheet, rewardSteps))
#pragma warning restore LAA1002
{
avatarState.inventory.AddItem(pair.Key, pair.Value);
}
// Regular Reward
var (itemResult, favResult) = StakeRewardCalculator.CalculateRewards(ncg, stakedNcg, stakingLevel, rewardSteps,
stakeRegularRewardSheet, itemSheet, random);
#pragma warning disable LAA1002
foreach (var pair in itemResult)
#pragma warning restore LAA1002
{
avatarState.inventory.AddItem(pair.Key, pair.Value);
}
foreach (var fav in favResult)
{
var rewardCurrency = fav.Currency;
var recipient = Currencies.PickAddress(rewardCurrency, context.Signer, AvatarAddress);
states = states.MintAsset(context, recipient, fav);
}
// NOTE: update claimed block index.
stakeStateV2 = new StakeState(
stakeStateV2.Contract,
stakeStateV2.StartedBlockIndex,
context.BlockIndex);
return states
.SetLegacyState(stakeStateAddr, stakeStateV2.Serialize())
.SetAvatarState(AvatarAddress, avatarState);
}
}
}