public void AuctionEnd_Transfer_Token_To_Owner_When_Auction_Ended_Without_Bid_Success()
        {
            SetupMessage(creator, 0);

            var store = new NFTAuctionStore(mContractState.Object);

            SetupBlock(101);

            SetupMessage(tokenOwner, 0);

            SetAuctionInfo(new AuctionInfo { Seller = tokenOwner, EndBlock = 100, StartingPrice = 100 });

            SetupSafeTransferToken(contract, tokenOwner, tokenId, TransferResult.Succeed(true));

            store.AuctionEnd(tokenContract, tokenId);

            store.GetAuctionInfo(tokenContract, tokenId)
                 .Should()
                 .Be(new AuctionInfo {
                    Seller = tokenOwner,
                    OnAuction = true,
                    EndBlock = 100, 
                    StartingPrice = 100,
                 });

            VerifyLog(new AuctionEndSucceedLog { Contract = tokenContract, TokenId = tokenId, HighestBid = 0, HighestBidder = Address.Zero });
        }
        public void OnNonFungibleTokenReceived_Owner_Sell_Token_Success()
        {
            SetupMessage(creator, 0);
            SetupBlock(1);
            var store = new NFTAuctionStore(mContractState.Object);

            SetupMessage(tokenContract, 0);
            SetupGetOwnerOfToken(TransferResult.Succeed(contract));
            
            var parameters = new AuctionParam { StartingPrice = 100, Duration = duration };

            var paramBytes = Array.Empty<byte>();

            mSerializer.Setup(s => s.ToStruct<AuctionParam>(paramBytes)).Returns(parameters);

            store.OnNonFungibleTokenReceived(tokenOwner, tokenOwner, tokenId, paramBytes)
                 .Should()
                 .BeTrue();

            store.GetAuctionInfo(tokenContract, tokenId)
                 .Should()
                 .Be(new AuctionInfo { Seller = tokenOwner, EndBlock = 101, StartingPrice = 100 });

            VerifyLog(new AuctionStartedLog
            {
                Contract = tokenContract,
                TokenId = tokenId,
                startingPrice = 100,
                Seller = tokenOwner,
                EndBlock = 101
            });

        }
        public void AuctionEnd_Refund_Token_Return_False_Fails()
        {
            SetupMessage(creator, 0);

            var store = new NFTAuctionStore(mContractState.Object);

            SetupBlock(101);

            SetupMessage(tokenOwner, 0);

            SetAuctionInfo(new AuctionInfo { Seller = tokenOwner, EndBlock = 100, StartingPrice = 100 });

            SetupSafeTransferToken(contract, tokenOwner, tokenId, TransferResult.Succeed(false));

            store.Invoking(m => m.AuctionEnd(tokenContract, tokenId))
                 .Should()
                 .Throw<SmartContractAssertException>()
                 .WithMessage("The token transfer failed.");
        }
        public void OnNonFungibleTokenReceived_GetOwner_Call_Raise_Exception_Fails()
        {
            SetupMessage(creator, 0);

            var store = new NFTAuctionStore(mContractState.Object);

            SetupGetOwnerOfToken(TransferResult.Failed());
            SetupMessage(tokenContract, 0);

            var parameters = new AuctionParam { StartingPrice = 100, Duration = duration };

            var paramBytes = Array.Empty<byte>();

            mSerializer.Setup(s => s.ToStruct<AuctionParam>(paramBytes)).Returns(parameters);

            store.Invoking(s => s.OnNonFungibleTokenReceived(tokenOwner, tokenOwner, tokenId, paramBytes))
                 .Should()
                 .Throw<SmartContractAssertException>()
                 .WithMessage("GetOwner method call failed.");
        }
        public void AuctionEnd_Success()
        {
            SetupMessage(creator, 0);

            var store = new NFTAuctionStore(mContractState.Object);

            SetupBlock(101);

            SetupMessage(tokenOwner, 0);

            SetAuctionInfo(new AuctionInfo
            {
                Seller = tokenOwner,
                HighestBid = 100,
                HighestBidder = buyer,
                OnAuction = false,
                EndBlock = 100,
                StartingPrice = 100
            });

            SetupSafeTransferToken(contract, buyer, tokenId, TransferResult.Succeed(true));

            SetupTransfer(tokenOwner, 100, TransferResult.Succeed());

            store.AuctionEnd(tokenContract, tokenId);

            store.GetAuctionInfo(tokenContract, tokenId)
                 .Should()
                 .Be(new AuctionInfo
                 {
                     Seller = tokenOwner,
                     HighestBid = 100,
                     HighestBidder = buyer,
                     OnAuction = true,
                     EndBlock = 100,
                     StartingPrice = 100,
                 });


            VerifyLog(new AuctionEndSucceedLog { Contract = tokenContract, TokenId = tokenId, HighestBid = 100, HighestBidder = buyer });
        }
        public void OnNonFungibleTokenReceived_Selling_Already_OnSale_Token_Fails()
        {
            SetupMessage(creator, 0);
            SetupBlock(1);
            var store = new NFTAuctionStore(mContractState.Object);

            SetupMessage(tokenContract, 0);
            SetupGetOwnerOfToken(TransferResult.Succeed(contract));
            
            var parameters = new AuctionParam { StartingPrice = 100, Duration = duration };

            var paramBytes = Array.Empty<byte>();

            mSerializer.Setup(s => s.ToStruct<AuctionParam>(paramBytes)).Returns(parameters);

            store.OnNonFungibleTokenReceived(tokenOwner, tokenOwner, tokenId, paramBytes);

            store.Invoking(s => s.OnNonFungibleTokenReceived(tokenOwner, tokenOwner, tokenId, paramBytes))
                 .Should()
                 .Throw<SmartContractAssertException>()
                 .WithMessage("The token is already on sale.");
        }
 private void SetupIsApprovedForAll(Address owner, Address delegator, TransferResult result)
 {
     mTransactionExecutor.Setup(m => m.Call(mContractState.Object, tokenContract, 0, "IsApprovedForAll", new object[] { owner, delegator }, 0))
                         .Returns(result);
 }
 private void SetupGetOwnerOfToken(TransferResult result)
 {
     mTransactionExecutor.Setup(m => m.Call(mContractState.Object, tokenContract, 0, "GetOwner", new object[] { tokenId }, 0))
                         .Returns(result);
 }
 private void SetupTransfer(Address to, ulong amount, TransferResult result)
 {
     mTransactionExecutor.Setup(m => m.Transfer(mContractState.Object, to, amount))
                         .Returns(result);
 }
 private void SetupSafeTransferToken(Address from, Address to, ulong tokenId, TransferResult result)
 {
     mTransactionExecutor.Setup(m => m.Call(mContractState.Object, tokenContract, 0, "SafeTransferFrom", new object[] { from, to, tokenId }, 0))
                         .Returns(result);
 }