Exemple #1
0
        public void ExecuteInsufficientBalanceError()
        {
            var shopState = _initialState.GetShopState();

            var sellerAvatarAddress = new PrivateKey().ToAddress();
            var sellerAgentAddress  = new PrivateKey().ToAddress();

            var(avatarState, agentState) = CreateAvatarState(sellerAgentAddress, sellerAvatarAddress);

            var equipment = ItemFactory.CreateItemUsable(
                _tableSheets.EquipmentItemSheet.First,
                Guid.NewGuid(),
                1);

            shopState.Register(new ShopItem(
                                   sellerAgentAddress,
                                   sellerAvatarAddress,
                                   Guid.NewGuid(),
                                   new FungibleAssetValue(_goldCurrencyState.Currency, 1, 0),
                                   100,
                                   equipment));

            var costume = ItemFactory.CreateCostume(
                _tableSheets.CostumeItemSheet.First,
                Guid.NewGuid());

            shopState.Register(new ShopItem(
                                   sellerAgentAddress,
                                   sellerAvatarAddress,
                                   Guid.NewGuid(),
                                   new FungibleAssetValue(_goldCurrencyState.Currency, 100, 0),
                                   100,
                                   costume));

            _initialState = _initialState
                            .SetState(Addresses.Shop, shopState.Serialize());
            shopState = _initialState.GetShopState();
            Assert.NotEmpty(shopState.Products);

            var products = shopState.Products.Values
                           .Select(p => new BuyMultiple.PurchaseInfo(
                                       p.ProductId,
                                       p.SellerAgentAddress,
                                       p.SellerAvatarAddress))
                           .ToList();

            Assert.NotEmpty(products);

            var balance = _initialState.GetBalance(_buyerAgentAddress, _goldCurrencyState.Currency);

            _initialState = _initialState.BurnAsset(_buyerAgentAddress, balance);

            var action = new BuyMultiple
            {
                buyerAvatarAddress = _buyerAvatarAddress,
                purchaseInfos      = products,
            };

            action.Execute(new ActionContext()
            {
                BlockIndex     = 0,
                PreviousStates = _initialState,
                Random         = new TestRandom(),
                Signer         = _buyerAgentAddress,
            });

            var results     = action.buyerResult.purchaseResults;
            var isAllFailed = results.Any(r => r.errorCode == BuyMultiple.ERROR_CODE_INSUFFICIENT_BALANCE);

            Assert.True(isAllFailed);
        }