-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SVR-301] 상품 구매 액션 추가 #159
Merged
Atralupus
merged 2 commits into
not-blond-beard:main
from
Atralupus:feat/buy-trade-good
Apr 14, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
namespace Savor22b.Tests.Action; | ||
|
||
using System; | ||
using System.Collections.Immutable; | ||
using Libplanet; | ||
using Libplanet.Assets; | ||
using Libplanet.State; | ||
using Savor22b.Action; | ||
using Savor22b.States; | ||
using Savor22b.States.Trade; | ||
using Xunit; | ||
|
||
public class BuyTradeGoodTests : ActionTests | ||
{ | ||
public BuyTradeGoodTests() { } | ||
|
||
[Fact] | ||
public void Execute_Success() | ||
{ | ||
var (stateDelta, productId) = CreatePresetStateDelta(); | ||
stateDelta = stateDelta.MintAsset( | ||
SignerAddress(), | ||
FungibleAssetValue.Parse(Currencies.KeyCurrency, "10") | ||
); | ||
|
||
var action = new BuyTradeGoodAction( | ||
productId | ||
); | ||
|
||
stateDelta = action.Execute( | ||
new DummyActionContext | ||
{ | ||
PreviousStates = stateDelta, | ||
Signer = SignerAddress(), | ||
Random = random, | ||
Rehearsal = false, | ||
BlockIndex = 1, | ||
} | ||
); | ||
|
||
var afterTradeInventoryState = DeriveTradeInventoryStateDelta(stateDelta); | ||
var afterRootState = DeriveRootStateFromAccountStateDelta(stateDelta); | ||
|
||
Assert.Empty(afterTradeInventoryState.TradeGoods); | ||
Assert.True(afterRootState.InventoryState.RefrigeratorStateList.Count == 1); | ||
} | ||
|
||
[Fact] | ||
public void Execute_Fail_InsufficientBalance() | ||
{ | ||
var (stateDelta, productId) = CreatePresetStateDelta(); | ||
|
||
var action = new BuyTradeGoodAction( | ||
productId | ||
); | ||
|
||
Assert.Throws<InsufficientBalanceException>(() => | ||
{ | ||
action.Execute( | ||
new DummyActionContext | ||
{ | ||
PreviousStates = stateDelta, | ||
Signer = SignerAddress(), | ||
Random = random, | ||
Rehearsal = false, | ||
BlockIndex = 1, | ||
} | ||
); | ||
}); | ||
} | ||
|
||
private (IAccountStateDelta, Guid) CreatePresetStateDelta() | ||
{ | ||
IAccountStateDelta state = new DummyState(); | ||
Address signerAddress = SignerAddress(); | ||
|
||
var rootStateEncoded = state.GetState(signerAddress); | ||
RootState rootState = rootStateEncoded is Bencodex.Types.Dictionary bdict | ||
? new RootState(bdict) | ||
: new RootState(); | ||
TradeInventoryState tradeInventoryState = state.GetState(TradeInventoryState.StateAddress) is Bencodex.Types.Dictionary tradeInventoryStateEncoded | ||
? new TradeInventoryState(tradeInventoryStateEncoded) | ||
: new TradeInventoryState(); | ||
|
||
var foodProductId = Guid.NewGuid(); | ||
|
||
var food = RefrigeratorState.CreateFood( | ||
Guid.NewGuid(), | ||
1, | ||
"D", | ||
1, | ||
1, | ||
1, | ||
1, | ||
1, | ||
ImmutableList<Guid>.Empty | ||
); | ||
|
||
tradeInventoryState = tradeInventoryState.RegisterGood( | ||
new FoodGoodState( | ||
signerAddress, | ||
foodProductId, | ||
FungibleAssetValue.Parse(Currencies.KeyCurrency, "10"), | ||
food) | ||
); | ||
state = state.SetState(TradeInventoryState.StateAddress, tradeInventoryState.Serialize()); | ||
return (state.SetState(signerAddress, rootState.Serialize()), foodProductId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
namespace Savor22b.Action; | ||
|
||
using System; | ||
using System.Collections.Immutable; | ||
using Bencodex.Types; | ||
using Libplanet.Action; | ||
using Libplanet.Headless.Extensions; | ||
using Libplanet.State; | ||
using Savor22b.States; | ||
using Savor22b.States.Trade; | ||
using Savor22b.Action.Exceptions; | ||
|
||
[ActionType(nameof(BuyTradeGoodAction))] | ||
public class BuyTradeGoodAction : SVRAction | ||
{ | ||
public BuyTradeGoodAction() { } | ||
|
||
public BuyTradeGoodAction(Guid productId) | ||
{ | ||
ProductId = productId; | ||
} | ||
|
||
public Guid ProductId; | ||
|
||
protected override IImmutableDictionary<string, IValue> PlainValueInternal => | ||
new Dictionary<string, IValue>() | ||
{ | ||
[nameof(ProductId)] = ProductId.Serialize(), | ||
}.ToImmutableDictionary(); | ||
|
||
protected override void LoadPlainValueInternal(IImmutableDictionary<string, IValue> plainValue) | ||
{ | ||
ProductId = plainValue[nameof(ProductId)].ToGuid(); | ||
} | ||
|
||
public override IAccountStateDelta Execute(IActionContext ctx) | ||
{ | ||
if (ctx.Rehearsal) | ||
{ | ||
return ctx.PreviousStates; | ||
} | ||
|
||
IAccountStateDelta states = ctx.PreviousStates; | ||
|
||
RootState rootState = states.GetState(ctx.Signer) is Dictionary rootStateEncoded | ||
? new RootState(rootStateEncoded) | ||
: new RootState(); | ||
TradeInventoryState tradeInventoryState = states.GetState(TradeInventoryState.StateAddress) is Dictionary tradeInventoryStateEncoded | ||
? new TradeInventoryState(tradeInventoryStateEncoded) | ||
: new TradeInventoryState(); | ||
|
||
var inventoryState = rootState.InventoryState; | ||
|
||
var good = tradeInventoryState.TradeGoods.First(g => g.Key == ProductId); | ||
|
||
states = states.TransferAsset( | ||
ctx.Signer, | ||
good.Value.SellerAddress, | ||
good.Value.Price, | ||
allowNegativeBalance: false | ||
); | ||
|
||
tradeInventoryState.TradeGoods.Remove(good.Key); | ||
|
||
switch (good.Value) | ||
{ | ||
case FoodGoodState foodGoodState: | ||
inventoryState = inventoryState.AddRefrigeratorItem(foodGoodState.Food); | ||
break; | ||
case ItemsGoodState itemsGoodState: | ||
foreach (var itemState in itemsGoodState.Items) | ||
{ | ||
inventoryState = inventoryState.AddItem(itemState); | ||
} | ||
|
||
break; | ||
default: | ||
throw new InvalidValueException("Food or Items required"); | ||
} | ||
|
||
rootState.SetInventoryState(inventoryState); | ||
states = states.SetState(TradeInventoryState.StateAddress, tradeInventoryState.Serialize()); | ||
return states.SetState(ctx.Signer, rootState.Serialize()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
복붙이 넘 티나요
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
쉿