from backend import *
from basement import *

from auction import Auction
from clockauction import ClockAuction
# @title Reverse auction modified for siring
# @notice We omit a fallback function to prevent accidental sends to this contract.
class SiringClockAuction(ClockAuction):
    # Delegate constructor
    def __init__(self, _nftAddr: address, _cut: uint256):
        # @dev Sanity check that allows us to ensure that we are pointing to the
        #  right auction in our setSiringAuctionAddress() call.
        ClockAuction.__init__(self, _nftAddr, _cut)
        self.isSiringClockAuction = True

    # @dev Creates and begins a new auction. Since this function is wrapped,
    # require sender to be KittyCore contract.
    # @param _tokenId - ID of token to auction, sender must be owner.
    # @param _startingPrice - Price of item (in wei) at beginning of auction.
    # @param _endingPrice - Price of item (in wei) at end of auction.
    # @param _duration - Length of auction (in seconds).
    # @param _seller - Seller, if not the message sender
    def createAuction(self, _tokenId: uint256, _startingPrice: uint256,
        _endingPrice: uint256, _duration: uint256, _seller: address):
        # Sanity check that no inputs overflow how many bits we've allocated
        # to store them in the auction struct.
        require(_startingPrice == uint256(uint128(_startingPrice)))
        require(_endingPrice == uint256(uint128(_endingPrice)))
        require(_duration == uint256(uint64(_duration)))

        require(msg.sender == address(self.nonFungibleContract))
        self._escrow(_seller, _tokenId)
        auction = Auction(
            _seller,
            uint128(_startingPrice),
            uint128(_endingPrice),
            uint64(_duration),
            uint64(now)
        )
        self._addAuction(_tokenId, auction)

    # @dev Places a bid for siring. Requires the sender
    # is the KittyCore contract because all bid methods
    # should be wrapped. Also returns the kitty to the
    # seller rather than the winner.
    def bid(self, _tokenId: uint256):
        require(msg.sender == address(self.nonFungibleContract))
        seller = self.tokenIdToAuction[_tokenId].seller
        # _bid checks that token ID is valid and will throw if bid fails
        self._bid(_tokenId, msg.value)
        # We transfer the kitty back to the seller, the winner will get
        # the offspring
        self._transfer(seller, _tokenId)
