-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathApprovePledge.cs
69 lines (62 loc) · 2.2 KB
/
ApprovePledge.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
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Nekoyume.Action.Guild;
using Nekoyume.Extensions;
using Nekoyume.Model.Guild;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Nekoyume.Module.Guild;
namespace Nekoyume.Action
{
[ActionType(TypeIdentifier)]
public class ApprovePledge : ActionBase
{
public const string TypeIdentifier = "approve_pledge";
public ApprovePledge()
{
}
public Address PatronAddress;
public override IValue PlainValue =>
Dictionary.Empty
.Add("type_id", TypeIdentifier)
.Add("values", PatronAddress.Serialize());
public override void LoadPlainValue(IValue plainValue)
{
PatronAddress = ((Dictionary)plainValue)["values"].ToAddress();
}
public override IWorld Execute(IActionContext context)
{
GasTracer.UseGas(1);
Address signer = context.Signer;
var states = context.PreviousState;
var contractAddress = signer.GetPledgeAddress();
if (!states.TryGetLegacyState(contractAddress, out List contract))
{
throw new FailedLoadStateException("failed to find requested pledge.");
}
if (contract[0].ToAddress() != PatronAddress)
{
throw new InvalidAddressException("invalid patron address.");
}
if (contract[1].ToBoolean())
{
throw new AlreadyContractedException($"{signer} already contracted.");
}
var repository = new GuildRepository(states, context);
if (PatronAddress == MeadConfig.PatronAddress
&& repository.GetJoinedGuild(GuildConfig.PlanetariumGuildOwner) is { } guildAddress)
{
states = repository.JoinGuild(guildAddress, context.GetAgentAddress()).World;
}
return states.SetLegacyState(
contractAddress,
List.Empty
.Add(PatronAddress.Serialize())
.Add(true.Serialize())
.Add(contract[2])
);
}
}
}