English Auction
Complete the English auction contract.
A NFT (non fungible token) is sold on an English auction. An English auction starts at a minimum price set by the seller. Bidders compete for the ownership of NFT by bidding higher than the previous bidder. Auction ends in 7 days, at which point the highest bidder is the winner.
start()
This function is called by the seller
to start the auction.
Only the
seller
can call.It can only be called once.
Transfer the ownership of
nft
from theseller
to thiscontract
. Assume that theseller
has approved this contract to calltransferFrom
.Set
started
totrue
Set expiration date,
endAt
to 7 days in the future from the current timestamp.Emit the event
Start
bid()
bid()
This function is called by bidders to bid higher than the previous bidder.
Cannot call this function if the auction has not yet started.
Cannot call if auction has expired.
Amount of ETH sent to this function must be greater than the previous
highestBid
.The mapping
bids
stores the amount of ETH each bidder can withdraw. Use this mapping to update the amount of ETH the previous highest bidder can withdraw.Set
highestBidder
to caller.Set
highestBid
to the amount of ETH that was sent.Emit the event
Bid
logging the parameters caller and amount of ETH that was sent.
withdraw()
withdraw()
This function refunds ETH to caller. The amount of ETH the caller can withdraw is the sum of all bids the caller has sent which are no longer the current highest bid. This sum is stored in the mapping bids
.
Reset
bids
of caller to0
.Send appropriate amount of ETH to caller.
emit the event
Withdraw
with the parameters caller and amount of ETH that was withdrawn.
end()
end()
This function ends the auction, transferring ownership of NFT to the highest bidder and paying the seller.
This function cannot be called if the auction has not started or the auction is not expired.
Use the state variable
ended
to check that this function can only be called once.Set
ended
totrue
.Transfer the ownership of NFT to
highestBidder
and pay the seller.Emit the event
End
withhighestBidder
andhighestBid
.
Last updated
Was this helpful?