-
Notifications
You must be signed in to change notification settings - Fork 48
/
TransferAsset.cs
229 lines (200 loc) · 8.03 KB
/
TransferAsset.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using Bencodex;
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Types.Assets;
using Nekoyume.Model.State;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using Lib9c.Abstractions;
using Nekoyume.Model.Stake;
using Nekoyume.Module;
using Serilog;
namespace Nekoyume.Action
{
/// <summary>
/// Hard forked at https://github.com/planetarium/lib9c/pull/2143
/// Updated at https://github.com/planetarium/lib9c/pull/2143
/// </summary>
[Serializable]
[ActionType(TypeIdentifier)]
public class TransferAsset : ActionBase, ISerializable, ITransferAsset, ITransferAssetV1
{
private const int MemoMaxLength = 80;
public const string TypeIdentifier = "transfer_asset5";
public TransferAsset()
{
}
public TransferAsset(Address sender, Address recipient, FungibleAssetValue amount, string memo = null)
{
Sender = sender;
Recipient = recipient;
Amount = amount;
CheckMemoLength(memo);
Memo = memo;
}
protected TransferAsset(SerializationInfo info, StreamingContext context)
{
var rawBytes = (byte[])info.GetValue("serialized", typeof(byte[]));
Dictionary pv = (Dictionary) new Codec().Decode(rawBytes);
LoadPlainValue(pv);
}
public Address Sender { get; private set; }
public Address Recipient { get; private set; }
public FungibleAssetValue Amount { get; private set; }
public string Memo { get; private set; }
Address ITransferAssetV1.Sender => Sender;
Address ITransferAssetV1.Recipient => Recipient;
FungibleAssetValue ITransferAssetV1.Amount => Amount;
string ITransferAssetV1.Memo => Memo;
public override IValue PlainValue
{
get
{
IEnumerable<KeyValuePair<IKey, IValue>> pairs = new[]
{
new KeyValuePair<IKey, IValue>((Text) "sender", Sender.Serialize()),
new KeyValuePair<IKey, IValue>((Text) "recipient", Recipient.Serialize()),
new KeyValuePair<IKey, IValue>((Text) "amount", Amount.Serialize()),
};
if (!(Memo is null))
{
pairs = pairs.Append(new KeyValuePair<IKey, IValue>((Text) "memo", Memo.Serialize()));
}
return Dictionary.Empty
.Add("type_id", TypeIdentifier)
.Add("values", new Dictionary(pairs));
}
}
public override IWorld Execute(IActionContext context)
{
context.UseGas(4);
Address signer = context.Signer;
var state = context.PreviousState;
var addressesHex = GetSignerAndOtherAddressesHex(context, signer);
var started = DateTimeOffset.UtcNow;
Log.Debug("{AddressesHex}TransferAsset5 exec started", addressesHex);
if (Sender != signer)
{
throw new InvalidTransferSignerException(signer, Sender, Recipient);
}
if (Sender == Recipient)
{
throw new InvalidTransferRecipientException(Sender, Recipient);
}
Currency currency = Amount.Currency;
if (!(currency.Minters is null) &&
(currency.Minters.Contains(Sender) || currency.Minters.Contains(Recipient)))
{
throw new InvalidTransferMinterException(
currency.Minters,
Sender,
Recipient
);
}
TransferAsset3.CheckCrystalSender(currency, context.BlockIndex, Sender);
ThrowIfStakeState(state, Recipient);
var ended = DateTimeOffset.UtcNow;
Log.Debug("{AddressesHex}TransferAsset5 Total Executed Time: {Elapsed}", addressesHex, ended - started);
return state.TransferAsset(context, Sender, Recipient, Amount);
}
public static void ThrowIfStakeState(IWorld state, Address recipient)
{
if (state.TryGetLegacyState(recipient, out IValue serializedStakeState))
{
bool isStakeStateOrMonsterCollectionState;
if (serializedStakeState is Dictionary dictionary)
{
try
{
_ = new StakeState(dictionary);
isStakeStateOrMonsterCollectionState = true;
}
catch (Exception)
{
isStakeStateOrMonsterCollectionState = false;
}
if (isStakeStateOrMonsterCollectionState)
{
throw new ArgumentException(
"You can't send assets to staking state.",
nameof(recipient));
}
try
{
_ = new MonsterCollectionState0(dictionary);
isStakeStateOrMonsterCollectionState = true;
}
catch (Exception)
{
isStakeStateOrMonsterCollectionState = false;
}
if (isStakeStateOrMonsterCollectionState)
{
throw new ArgumentException(
"You can't send assets to staking state.",
nameof(recipient));
}
try
{
_ = new MonsterCollectionState(dictionary);
isStakeStateOrMonsterCollectionState = true;
}
catch (Exception)
{
isStakeStateOrMonsterCollectionState = false;
}
if (isStakeStateOrMonsterCollectionState)
{
throw new ArgumentException(
"You can't send assets to staking state.",
nameof(recipient));
}
}
if (serializedStakeState is List serializedStakeStateV2)
{
try
{
_ = new StakeStateV2(serializedStakeStateV2);
isStakeStateOrMonsterCollectionState = true;
}
catch (Exception)
{
isStakeStateOrMonsterCollectionState = false;
}
if (isStakeStateOrMonsterCollectionState)
{
throw new ArgumentException(
"You can't send assets to staking state.",
nameof(recipient));
}
}
}
}
public override void LoadPlainValue(IValue plainValue)
{
var asDict = (Dictionary)((Dictionary)plainValue)["values"];
Sender = asDict["sender"].ToAddress();
Recipient = asDict["recipient"].ToAddress();
Amount = asDict["amount"].ToFungibleAssetValue();
Memo = asDict.TryGetValue((Text) "memo", out IValue memo) ? memo.ToDotnetString() : null;
CheckMemoLength(Memo);
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("serialized", new Codec().Encode(PlainValue));
}
private void CheckMemoLength(string memo)
{
if (memo?.Length > MemoMaxLength)
{
string msg = $"The length of the memo, {memo.Length}, " +
$"is overflowed than the max length, {MemoMaxLength}.";
throw new MemoLengthOverflowException(msg);
}
}
}
}