-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathCreatePledge.cs
101 lines (92 loc) · 3.68 KB
/
CreatePledge.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
using System.Collections.Generic;
using System.Linq;
using Bencodex.Types;
using Lib9c;
using Libplanet.Action;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Nekoyume.Model.Guild;
using Nekoyume.Model.State;
using Nekoyume.Module;
namespace Nekoyume.Action
{
using Extensions;
using Module.Guild;
/// <summary>
/// Admin action for pledge contract
/// </summary>
[ActionType(TypeIdentifier)]
public class CreatePledge : ActionBase
{
public const string TypeIdentifier = "create_pledge";
public Address PatronAddress;
public int Mead;
public IEnumerable<(Address, Address)> AgentAddresses;
public override IValue PlainValue =>
Dictionary.Empty
.Add("type_id", TypeIdentifier)
.Add("values", List.Empty
.Add(PatronAddress.Serialize())
.Add(Mead)
.Add(new List(AgentAddresses.Select(tuple =>
List.Empty
.Add(tuple.Item1.Serialize())
.Add(tuple.Item2.Serialize())
))));
public override void LoadPlainValue(IValue plainValue)
{
var list = (List)((Dictionary)plainValue)["values"];
PatronAddress = list[0].ToAddress();
Mead = (Integer)list[1];
var serialized = (List)list[2];
var agentAddresses = new List<(Address, Address)>();
foreach (var value in serialized)
{
var innerList = (List)value;
var agentAddress = innerList[0].ToAddress();
var pledgeAddress = innerList[1].ToAddress();
agentAddresses.Add((agentAddress, pledgeAddress));
}
AgentAddresses = agentAddresses;
}
public override IWorld Execute(IActionContext context)
{
GasTracer.UseGas(1);
CheckPermission(context);
var states = context.PreviousState;
var mead = Mead * Currencies.Mead;
var contractList = List.Empty
.Add(PatronAddress.Serialize())
.Add(true.Serialize())
.Add(Mead.Serialize());
// migration for planetarium guild
var repository = new GuildRepository(states, context);
var planetariumGuildOwner = Nekoyume.Action.Guild.GuildConfig.PlanetariumGuildOwner;
if (repository.GetJoinedGuild(planetariumGuildOwner) is null)
{
var random = context.GetRandom();
var guildAddr = new Nekoyume.TypedAddress.GuildAddress(random.GenerateAddress());
var guild = new Model.Guild.Guild(guildAddr, planetariumGuildOwner,
context.Miner, repository);
repository.SetGuild(guild);
repository = repository.JoinGuild(guildAddr, planetariumGuildOwner);
}
var guildAddress =
(Nekoyume.TypedAddress.GuildAddress)
repository.GetJoinedGuild(planetariumGuildOwner)!;
foreach (var (agentAddress, pledgeAddress) in AgentAddresses)
{
if (PatronAddress == MeadConfig.PatronAddress)
{
repository = repository.JoinGuild(guildAddress,
new Nekoyume.TypedAddress.AgentAddress(agentAddress));
}
repository.UpdateWorld(repository.World
.TransferAsset(context, PatronAddress, agentAddress, mead)
.SetLegacyState(pledgeAddress, contractList)
);
}
return repository.World;
}
}
}