|
| 1 | +namespace Lib9c.Tests.Action; |
| 2 | + |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Globalization; |
| 5 | +using System.Numerics; |
| 6 | +using Libplanet.Action.State; |
| 7 | +using Libplanet.Crypto; |
| 8 | +using Libplanet.Mocks; |
| 9 | +using Libplanet.Types.Assets; |
| 10 | +using Nekoyume.Action; |
| 11 | +using Nekoyume.Model.State; |
| 12 | +using Nekoyume.Module; |
| 13 | +using Xunit; |
| 14 | + |
| 15 | +public class MigrateFeeTest |
| 16 | +{ |
| 17 | + private readonly Currency _ncgCurrency; |
| 18 | + |
| 19 | + public MigrateFeeTest() |
| 20 | + { |
| 21 | +#pragma warning disable CS0618 |
| 22 | + _ncgCurrency = Currency.Legacy("NCG", 2, null); |
| 23 | +#pragma warning restore CS0618 |
| 24 | + } |
| 25 | + |
| 26 | + [Fact] |
| 27 | + public void Execute() |
| 28 | + { |
| 29 | + var admin = new Address("8d9f76aF8Dc5A812aCeA15d8bf56E2F790F47fd7"); |
| 30 | + var context = new ActionContext(); |
| 31 | + var state = new World(MockUtil.MockModernWorldState) |
| 32 | + .SetLegacyState(AdminState.Address, new AdminState(admin, 100).Serialize()) |
| 33 | + .SetLegacyState(GoldCurrencyState.Address, new GoldCurrencyState(_ncgCurrency).Serialize()); |
| 34 | + var recipient = new PrivateKey().Address; |
| 35 | + var transferData = new List<(Address sender, Address recipient, BigInteger amount)>(); |
| 36 | + var amount = FungibleAssetValue.Parse(_ncgCurrency, 0.1m.ToString(CultureInfo.InvariantCulture)); |
| 37 | + for (int i = 1; i < 10; i++) |
| 38 | + { |
| 39 | + var address = new PrivateKey().Address; |
| 40 | + var balance = 0.1m * i; |
| 41 | + var fav = FungibleAssetValue.Parse(_ncgCurrency, balance.ToString(CultureInfo.InvariantCulture)); |
| 42 | + state = state.MintAsset(context, address, fav); |
| 43 | + transferData.Add((address, recipient, amount.RawValue)); |
| 44 | + } |
| 45 | + |
| 46 | + var action = new MigrateFee |
| 47 | + { |
| 48 | + TransferData = transferData, |
| 49 | + Memo = "memo", |
| 50 | + }; |
| 51 | + |
| 52 | + var nextState = action.Execute(new ActionContext |
| 53 | + { |
| 54 | + BlockIndex = 1L, |
| 55 | + PreviousState = state, |
| 56 | + RandomSeed = 0, |
| 57 | + Signer = admin, |
| 58 | + }); |
| 59 | + |
| 60 | + foreach (var (sender, _, _) in transferData) |
| 61 | + { |
| 62 | + var prevBalance = state.GetBalance(sender, _ncgCurrency); |
| 63 | + Assert.Equal(prevBalance - amount, nextState.GetBalance(sender, _ncgCurrency)); |
| 64 | + } |
| 65 | + |
| 66 | + Assert.Equal(FungibleAssetValue.Parse(_ncgCurrency, "0.9"), nextState.GetBalance(recipient, _ncgCurrency)); |
| 67 | + } |
| 68 | + |
| 69 | + [Theory] |
| 70 | + [InlineData(true)] |
| 71 | + [InlineData(false)] |
| 72 | + public void PlainValue(bool memo) |
| 73 | + { |
| 74 | + var transferData = new List<(Address sender, Address recipient, BigInteger amount)>(); |
| 75 | + // 0.9 |
| 76 | + // 1.0 |
| 77 | + // 1.1 |
| 78 | + for (int i = 9; i < 12; i++) |
| 79 | + { |
| 80 | + var sender = new PrivateKey().Address; |
| 81 | + var recipient = new PrivateKey().Address; |
| 82 | + var amount = FungibleAssetValue.Parse(_ncgCurrency, (0.1m * i).ToString(CultureInfo.InvariantCulture)); |
| 83 | + transferData.Add((sender, recipient, amount.RawValue)); |
| 84 | + } |
| 85 | + |
| 86 | + var action = new MigrateFee |
| 87 | + { |
| 88 | + TransferData = transferData, |
| 89 | + Memo = memo ? "memo" : null, |
| 90 | + }; |
| 91 | + |
| 92 | + var des = new MigrateFee(); |
| 93 | + des.LoadPlainValue(action.PlainValue); |
| 94 | + |
| 95 | + for (int i = 0; i < action.TransferData.Count; i++) |
| 96 | + { |
| 97 | + var data = action.TransferData[i]; |
| 98 | + Assert.Equal(des.TransferData[i].sender, data.sender); |
| 99 | + Assert.Equal(des.TransferData[i].recipient, data.recipient); |
| 100 | + Assert.Equal(des.TransferData[i].amount, data.amount); |
| 101 | + } |
| 102 | + |
| 103 | + Assert.Equal(memo, !string.IsNullOrEmpty(des.Memo)); |
| 104 | + } |
| 105 | + |
| 106 | + [Fact] |
| 107 | + public void Execute_Throw_InsufficientBalanceException() |
| 108 | + { |
| 109 | + var admin = new Address("8d9f76aF8Dc5A812aCeA15d8bf56E2F790F47fd7"); |
| 110 | + var context = new ActionContext(); |
| 111 | + var state = new World(MockUtil.MockModernWorldState) |
| 112 | + .SetLegacyState(AdminState.Address, new AdminState(admin, 100).Serialize()) |
| 113 | + .SetLegacyState(GoldCurrencyState.Address, new GoldCurrencyState(_ncgCurrency).Serialize()); |
| 114 | + var recipient = new PrivateKey().Address; |
| 115 | + var transferData = new List<(Address sender, Address recipient, BigInteger amount)>(); |
| 116 | + var amount = 1 * _ncgCurrency; |
| 117 | + var address = new PrivateKey().Address; |
| 118 | + var balance = 0.1m; |
| 119 | + var fav = FungibleAssetValue.Parse(_ncgCurrency, balance.ToString(CultureInfo.InvariantCulture)); |
| 120 | + state = state.MintAsset(context, address, fav); |
| 121 | + transferData.Add((address, recipient, amount.RawValue)); |
| 122 | + |
| 123 | + var action = new MigrateFee |
| 124 | + { |
| 125 | + TransferData = transferData, |
| 126 | + Memo = "memo", |
| 127 | + }; |
| 128 | + |
| 129 | + Assert.Throws<InsufficientBalanceException>(() => action.Execute(new ActionContext |
| 130 | + { |
| 131 | + BlockIndex = 1L, |
| 132 | + PreviousState = state, |
| 133 | + RandomSeed = 0, |
| 134 | + Signer = admin, |
| 135 | + })); |
| 136 | + } |
| 137 | +} |
0 commit comments