Esempio n. 1
0
        public void Bidder_can_withdraw_their_bid_but_currently_at_least_2_bids_before_they_can()
        {
            var contract = new Auction(smartContractState, 1);

            var message = ((TestMessage)smartContractState.Message);

            message.Value  = 200;
            message.Sender = BidderOne;
            contract.Bid();
            message.Value = 300;
            contract.Bid();

            contract.Withdraw();
        }
Esempio n. 2
0
        public void Bidder_cant_bid_lower_amount()
        {
            var contract = new Auction(smartContractState, 1);

            var message = ((TestMessage)smartContractState.Message);

            message.Value  = 200;
            message.Sender = BidderOne;
            contract.Bid();
            message.Value = 100;
            Action bid = () => contract.Bid();

            bid.Should().Throw <Exception>()
            .WithMessage("Condition inside 'Assert' call was false.");
        }
Esempio n. 3
0
        public void Bidder_cant_bid_lower_amount()
        {
            var contract = new Auction(smartContractState, 1);

            var message = ((TestMessage)smartContractState.Message);

            message.Value  = 200;
            message.Sender = BidderOne;
            contract.Bid();
            message.Value = 100;
            Action bid = () => contract.Bid();

            bid.Should().Throw <SmartContractAssertException>()
            .WithMessage("Assert failed.");
        }
Esempio n. 4
0
        public void TestBidding()
        {
            const ulong duration = 20;
            var         contract = new Auction(this.smartContractState, duration);

            ((TestMessage)this.smartContractState.Message).Value = 100;
            Assert.Equal(default(Address), this.smartContractState.PersistentState.GetAddress("HighestBidder"));
            Assert.Equal(0uL, this.smartContractState.PersistentState.GetUInt64("HighestBid"));

            contract.Bid();
            Assert.Equal(this.TestAddress, this.smartContractState.PersistentState.GetAddress("HighestBidder"));
            Assert.Equal(100uL, this.smartContractState.PersistentState.GetUInt64("HighestBid"));

            ((TestMessage)this.smartContractState.Message).Value = 90;
            Assert.ThrowsAny <Exception>(() => contract.Bid());
        }
Esempio n. 5
0
        public void TestBidding()
        {
            const ulong duration = 20;
            var         contract = new Auction(smartContractState, duration);

            Assert.IsNull(smartContractState.PersistentState.GetObject <Address>("HighestBidder").Value);
            Assert.AreEqual(0uL, smartContractState.PersistentState.GetObject <ulong>("HighestBid"));

            ((TestMessage)smartContractState.Message).Value = 100;
            contract.Bid();
            Assert.IsNotNull(smartContractState.PersistentState.GetObject <Address>("HighestBidder").Value);
            Assert.AreEqual(100uL, smartContractState.PersistentState.GetObject <ulong>("HighestBid"));

            ((TestMessage)smartContractState.Message).Value = 90;
            Assert.ThrowsException <Exception>(() => contract.Bid());
        }
Esempio n. 6
0
        public void Bidder_can_outbid_themselves()
        {
            var contract = new Auction(smartContractState, 1);

            var message = ((TestMessage)smartContractState.Message);

            message.Value  = 200;
            message.Sender = BidderOne;
            contract.Bid();
            message.Value = 250;
            contract.Bid();
            message.Value = 300;
            contract.Bid();

            contract.HighestBidder.Should().Be(BidderOne);
            contract.HighestBid.Should().Be(300ul);
        }
Esempio n. 7
0
        public void Bidder_can_outbid_themselves()
        {
            var contract = new Auction(smartContractState, 1);

            var message = ((TestMessage)smartContractState.Message);

            message.Value  = 200;
            message.Sender = BidderOne;
            contract.Bid();
            message.Value = 250;
            contract.Bid();
            message.Value = 300;
            contract.Bid();

            AssertionExtensions.Should((object)contract.HighestBidder).Be(BidderOne);
            AssertionExtensions.Should((ulong)contract.HighestBid).Be(300ul);
        }
Esempio n. 8
0
        public void Second_bidder_higher_than_first_becomes_highest_bid()
        {
            var contract = new Auction(smartContractState, 20);

            var message = ((TestMessage)smartContractState.Message);

            message.Value  = 100;
            message.Sender = BidderOne;
            contract.Bid();

            message.Value  = 200;
            message.Sender = BidderTwo;
            contract.Bid();

            Assert.Equal(BidderTwo, contract.HighestBidder);
            Assert.Equal(200uL, smartContractState.PersistentState.GetUInt64("HighestBid"));
        }
        public void TestBidding()
        {
            var auction = new Auction(SmartContractState, Duration);

            Assert.IsNull(SmartContractState.PersistentState.GetAddress("HighestBidder").Value);
            Assert.AreEqual(0uL, SmartContractState.PersistentState.GetUInt64("HighestBid"));

            ((TestMessage)SmartContractState.Message).Value = 100;

            auction.Bid();

            Assert.IsNotNull(SmartContractState.PersistentState.GetAddress("HighestBidder").Value);
            Assert.AreEqual(100uL, SmartContractState.PersistentState.GetUInt64("HighestBid"));

            ((TestMessage)SmartContractState.Message).Value = 90;

            Assert.ThrowsException <SmartContractAssertException>(() => auction.Bid());
        }
Esempio n. 10
0
        public void Bid_after_end_block_fails()
        {
            var contract = new Auction(smartContractState, 0);

            var message = ((TestMessage)smartContractState.Message);

            message.Value  = 200;
            message.Sender = BidderTwo;

            var exception = Assert.Throws <Exception>(() => contract.Bid());

            Assert.Equal("Condition inside 'Assert' call was false.", exception.Message);
        }
Esempio n. 11
0
        public void BidsHigherAndReportsBiddingWhenNewPriceArrives()
        {
            int            price     = 1001;
            int            increment = 25;
            int            bid       = price + increment;
            SniperSnapshot state     = new SniperSnapshot(ITEM_ID, price, bid, SniperState.Bidding);

            sniper.CurrentPrice(price, increment, PriceSource.FromOtherBidder);

            A.CallTo(() => sniperListener.SniperStateChanged(A <SniperSnapshot> .That.Matches(ss => ss.Equals(state))))
            .MustHaveHappened(Repeated.AtLeast.Once);
            A.CallTo(() => auction.Bid(price + increment)).MustHaveHappened(Repeated.Exactly.Once);
        }
Esempio n. 12
0
        public void Bid_after_end_block_fails()
        {
            var contract = new Auction(smartContractState, 0);

            var message = ((TestMessage)smartContractState.Message);

            message.Value  = 200;
            message.Sender = BidderTwo;

            var exception = Assert.Throws <SmartContractAssertException>(() => contract.Bid());

            Assert.Equal("Assert failed.", exception.Message);
        }
Esempio n. 13
0
        Auction CreateSuccessfulAuction(Auction.ItemCategory itemCategory = Auction.ItemCategory.Misc, decimal amount = anyStartingPrice + 1.00M)
        {
            Auction auction = CreateDefaultAuction();

            auction.Category = itemCategory;
            auction.OnStart();
            decimal bidAmount = amount;

            auction.Bid(CreateDefaultBidder(), bidAmount, out Auction.BidStatus status);
            auction.OnClose();

            return(auction);
        }
Esempio n. 14
0
        public void Bidder_cant_withdraw_bid_if_only_one_bid()
        {
            var contract = new Auction(smartContractState, 1);
            var message  = ((TestMessage)smartContractState.Message);

            message.Value  = 200;
            message.Sender = BidderOne;
            contract.Bid();

            Action withdraw = () => contract.Withdraw();

            withdraw.Should().Throw <Exception>()
            .WithMessage("Condition inside 'Assert' call was false.");
        }
Esempio n. 15
0
        public void TestOffHoursShunt()
        {
            var     offHours = HoursFactory.GetHours(true);
            Auction auction  = CreateDefaultAuction();

            auction.OffHours = new OffHoursShunt();
            auction.OnStart();
            auction.Bid(CreateDefaultBidder(), 10000M, out Auction.BidStatus bidStatus);
            auction.OnClose();

            string expectedLog = CreateAuctionLog(auction);

            Assert.IsTrue(NotificationFactory.OffHoursLogger.FindMessage(expectedLog), "Expected auction log: " + expectedLog);
        }
Esempio n. 16
0
        public void TestUserCanNotBidIfLoggedOut()
        {
            User user = CreateDefaultUser();

            user.LoggedIn = false;

            Auction auction = CreateDefaultAuction();

            auction.OnStart();

            auction.Bid(user, anyStartingPrice + 1.0M, out Auction.BidStatus status);

            Assert.AreEqual(Auction.BidStatus.UserNotLoggedIn, status, "User should not be able to place a bid if not logged in.");
        }
        public void CurrentPrice(int price, int increment, PriceSource priceSource)
        {
            switch (priceSource)
            {
            case PriceSource.FromSniper:
                _snapshot = _snapshot.Winning(price);
                break;

            case PriceSource.FromOtherBidder:
                int bid = price + increment;
                _auction.Bid(bid);
                _snapshot = _snapshot.Bidding(price, bid);
                break;
            }
            NotifyChange();
        }
Esempio n. 18
0
        public void First_bid_becomes_highest_bid()
        {
            var contract = new Auction(smartContractState, 20);

            contract.HighestBidder.Should().Be(default(Address));
            contract.HighestBid.Should().Be(0uL);

            var message = ((TestMessage)smartContractState.Message);

            message.Value  = 100;
            message.Sender = BidderOne;
            contract.Bid();

            contract.HighestBidder.Should().Be(BidderOne);
            contract.HighestBid.Should().Be(100ul);
        }
Esempio n. 19
0
        public void EndAuction_attempt_after_end_block_succeeds()
        {
            var contract = new Auction(smartContractState, 1);

            var message = ((TestMessage)smartContractState.Message);

            message.Value  = 200;
            message.Sender = BidderOne;
            contract.Bid();

            smartContractState.Block.GetType().GetProperty("Number")
            .SetValue(smartContractState.Block, contract.EndBlock);

            contract.AuctionEnd();
            Assert.True(contract.HasEnded);
            this.transactionExecutor.Received().TransferFunds(smartContractState, ContractOwnerAddress, 200ul, null);
        }
Esempio n. 20
0
        public void TestNotificationHighestBidder()
        {
            Auction auction = CreateDefaultAuction();

            auction.OnStart();
            decimal bidAmount = anyStartingPrice + 1.00M;

            auction.Bid(CreateDefaultBidder(), bidAmount, out Auction.BidStatus status);

            auction.OnClose();

            string expectedMessage = "Congratulations! You won an auction for a " + auction.ItemDesc + " from " + anyUserName + " for " + bidAmount.ToString("C2") + ".";

            string message = auction.PostOffice.FindEmail(auction.HighestBidder.UserEmail, expectedMessage);

            Assert.IsFalse(string.IsNullOrEmpty(message), "Expected: " + expectedMessage);
        }
Esempio n. 21
0
        public void TestUserCanBidOnAuction()
        {
            User user = CreateDefaultUser();

            user.LoggedIn = true;

            Auction auction = CreateDefaultAuction();

            auction.OnStart();

            // user will bid 1 dollar over starting price
            decimal bidAmount = auction.StartingPrice + 1.00M;
            bool    success   = auction.Bid(user, bidAmount, out Auction.BidStatus status);

            Assert.IsTrue(success, "Bid was not successful." + status.ToString());
            Assert.AreEqual(auction.CurrentPrice, bidAmount);
            Assert.AreEqual(auction.HighestBidder.UserName, user.UserName);
        }
        public void TestScenario_TwoBidders_VerifyBalances()
        {
            // Setup
            var auction = new Auction(SmartContractState, Duration);

            var bidderA = (Address)"bidderAAddress";
            var bidderB = (Address)"bidderBAddress";

            BlockchainBalances[ContractAddress]      = 0;
            BlockchainBalances[ContractOwnerAddress] = 0;
            BlockchainBalances[bidderA] = 13;
            BlockchainBalances[bidderB] = 13;

            ulong currentSimulatedBlockNumber = ContractDeployBlockNumber;

            // Bidder A bids 10, is highest (and only) bid
            currentSimulatedBlockNumber++;
            SetBlock(currentSimulatedBlockNumber);
            MockContractMethodCall(sender: bidderA, value: 10u);
            auction.Bid();
            // Bidder B bids 11, is new highest bid
            currentSimulatedBlockNumber++;
            SetBlock(currentSimulatedBlockNumber);
            MockContractMethodCall(sender: bidderB, value: 11u);
            auction.Bid();
            // Bidder A withdraws failed bid
            currentSimulatedBlockNumber++;
            SetBlock(currentSimulatedBlockNumber);
            MockContractMethodCall(sender: bidderA, value: 0u);
            auction.Withdraw();
            // Bidder A bids 12, is new highest bid
            currentSimulatedBlockNumber++;
            SetBlock(currentSimulatedBlockNumber);
            MockContractMethodCall(sender: bidderA, value: 12u);
            auction.Bid();
            // AuctionEnd called by contract owner after end block has passed
            currentSimulatedBlockNumber = currentSimulatedBlockNumber + Duration;
            SetBlock(currentSimulatedBlockNumber);
            MockContractMethodCall(sender: ContractOwnerAddress, value: 0u);
            auction.AuctionEnd();

            // Verify end balances
            var expectedBlockchainBalances = new Dictionary <Address, ulong> {
                { ContractAddress, 11 },
                { ContractOwnerAddress, 12 },
                { bidderA, 1 },
                { bidderB, 2 }
            };

            var expectedReturnBalances = new Dictionary <Address, ulong> {
                { bidderA, 0 },
                { bidderB, 11 }
            };

            foreach (var k in expectedBlockchainBalances.Keys)
            {
                Assert.IsTrue(BlockchainBalances[k] == expectedBlockchainBalances[k]);
            }

            foreach (var k in expectedReturnBalances.Keys)
            {
                Assert.IsTrue(auction.ReturnBalances[k] == expectedReturnBalances[k]);
            }

            // Sanity check
            Assert.AreEqual(SumDictionary(BlockchainBalances), SumDictionary(expectedBlockchainBalances));
        }
Esempio n. 23
0
        public void InteractiveAuction(GameObject game)
        {
            foreach (var player in game.Players)
            {
                if (player.CanBuy)
                {
                    Console.WriteLine("You can bid for a power station. Your options are 1-4:");
                    foreach (var station in game.AuctionHouse.CurrentMarket)
                    {
                        ColoredConsoleWrite(GetConsoleColor(station.Type), GetShortName(station.Type));
                        Console.WriteLine($" Val: {station.Value}, Res: {station.Resource} Pow: {station.Power}");
                    }

                    string option = Console.ReadLine();
                    try
                    {
                        var stationPick = Convert.ToInt32(option);
                        Console.WriteLine("Whats the starting bid");
                        var startingBid = Convert.ToInt32(Console.ReadLine());


                        var newAuction = new Auction(new Bid(player, startingBid), game.AuctionHouse.CurrentMarket.ElementAt(stationPick));

                        var eligiblePlayers = new LinkedList <Player>();
                        foreach (var item in game.Players)
                        {
                            if (item.CanBuy)
                            {
                                eligiblePlayers.AddLast(item);
                            }
                        }

                        var currentPlayer = eligiblePlayers.First;
                        while (eligiblePlayers.Count != 1)
                        {
                            Console.WriteLine($"Its {currentPlayer.Value.Name} turn. you can bid or pass");

                            option = Console.ReadLine();
                            if (option != "x")
                            {
                                int bidValue = Convert.ToInt32(option);
                                if (bidValue > newAuction.CurrentBid.Value)
                                {
                                    newAuction.Bid(new Bid(currentPlayer.Value, bidValue));
                                }
                                else
                                {
                                    Console.Write("No a valid bid. You forfeit and pass.");
                                    eligiblePlayers.Remove(currentPlayer);
                                }
                            }
                            else
                            {
                                Console.WriteLine("You pass.");
                                eligiblePlayers.Remove(currentPlayer);
                            }

                            currentPlayer = currentPlayer.Next ?? eligiblePlayers.First;
                        }
                    }
                    // skipped
                    catch (Exception e)
                    {
                        player.CanBuy = false;
                    }
                }
            }
        }