Skip to content

Commit

Permalink
Merge pull request #2998 from planetarium/bugfix/create-pledge-guild-…
Browse files Browse the repository at this point in the history
…migration

Fix missing guild migration in CreatePledge
  • Loading branch information
ipdae authored Nov 13, 2024
2 parents e8917e2 + bf57f41 commit 88397f3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
38 changes: 29 additions & 9 deletions .Lib9c.Tests/Action/CreatePledgeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Lib9c.Tests.Action
using Nekoyume.Action;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Nekoyume.Module.Guild;
using Serilog;
using Xunit;
using Xunit.Abstractions;
Expand All @@ -24,25 +25,29 @@ public CreatePledgeTest(ITestOutputHelper outputHelper)
}

[Theory]
[InlineData(true, null)]
[InlineData(false, typeof(PermissionDeniedException))]
public void Execute(bool admin, Type exc)
[InlineData(true, false, null)]
[InlineData(true, true, null)]
[InlineData(false, false, typeof(PermissionDeniedException))]
public void Execute(bool admin, bool plPatron, Type exc)
{
var adminAddress = new PrivateKey().Address;
var poolAddress = new PrivateKey().Address;
var adminState = new AdminState(adminAddress, 150L);
var patronAddress = new PrivateKey().Address;
var patronAddress = plPatron
? MeadConfig.PatronAddress
: new PrivateKey().Address;
var mead = Currencies.Mead;
var agentAddress = new PrivateKey().Address;
var pledgeAddress = agentAddress.GetPledgeAddress();
var pledgedAddress = new PrivateKey().Address;
var pledgeAddress = pledgedAddress.GetPledgeAddress();
var agentAddress = new Nekoyume.TypedAddress.AgentAddress(pledgedAddress);
var context = new ActionContext();
var states = new World(MockUtil.MockModernWorldState)
.SetLegacyState(Addresses.Admin, adminState.Serialize())
.MintAsset(context, patronAddress, 4 * 500 * mead);

var agentAddresses = new List<(Address, Address)>
{
(agentAddress, pledgeAddress),
(pledgedAddress, pledgeAddress),
};
for (var i = 0; i < 499; i++)
{
Expand All @@ -67,9 +72,24 @@ public void Execute(bool admin, Type exc)
if (exc is null)
{
var nextState = action.Execute(actionContext);

Assert.Equal(0 * mead, nextState.GetBalance(patronAddress, mead));
Assert.Equal(4 * mead, nextState.GetBalance(agentAddress, mead));
Assert.Equal(4 * mead, nextState.GetBalance(pledgedAddress, mead));

var planetariumGuildOwner = Nekoyume.Action.Guild.GuildConfig.PlanetariumGuildOwner;
var guildAddress = nextState.GetJoinedGuild(planetariumGuildOwner);
Assert.NotNull(guildAddress);
Assert.True(nextState.TryGetGuild(guildAddress.Value, out var guild));
Assert.Equal(planetariumGuildOwner, guild.GuildMasterAddress);
if (!plPatron)
{
Assert.Null(nextState.GetJoinedGuild(agentAddress));
}
else
{
var joinedGuildAddress = Assert.IsType<Nekoyume.TypedAddress.GuildAddress>(nextState.GetJoinedGuild(agentAddress));
Assert.True(nextState.TryGetGuild(joinedGuildAddress, out var joinedGuild));
Assert.Equal(Nekoyume.Action.Guild.GuildConfig.PlanetariumGuildOwner, joinedGuild.GuildMasterAddress);
}
}
else
{
Expand Down
19 changes: 19 additions & 0 deletions Lib9c/Action/CreatePledge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@

namespace Nekoyume.Action
{
using Extensions;
using Module.Guild;

/// <summary>
/// Admin action for pledge contract
/// </summary>
[ActionType(TypeIdentifier)]
public class CreatePledge : ActionBase
{
Expand Down Expand Up @@ -56,8 +62,21 @@ public override IWorld Execute(IActionContext context)
.Add(PatronAddress.Serialize())
.Add(true.Serialize())
.Add(Mead.Serialize());
// migration for planetarium guild
var planetariumGuildOwner = Nekoyume.Action.Guild.GuildConfig.PlanetariumGuildOwner;
if (states.GetJoinedGuild(planetariumGuildOwner) is null)
{
var random = context.GetRandom();
var guildAddress = new Nekoyume.TypedAddress.GuildAddress(random.GenerateAddress());
states = states.MakeGuild(guildAddress, new Nekoyume.TypedAddress.AgentAddress(planetariumGuildOwner));
}
foreach (var (agentAddress, pledgeAddress) in AgentAddresses)
{
if (PatronAddress == MeadConfig.PatronAddress && states.GetJoinedGuild(planetariumGuildOwner) is { } guildAddress)
{
states = states.JoinGuild(guildAddress, new Nekoyume.TypedAddress.AgentAddress(agentAddress));
}

states = states
.TransferAsset(context, PatronAddress, agentAddress, mead)
.SetLegacyState(pledgeAddress, contractList);
Expand Down

0 comments on commit 88397f3

Please sign in to comment.