1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| ### 六、2 完整实现
功能完整拍卖: contract AuctionSystem { string itemDescription; address[] bidders; } Auction[] public auctions; mapping(uint256 => mapping(address => uint256)) public pendingReturns; event AuctionCreated( uint256 indexed auctionId, address indexed seller, string itemDescription, uint256 startingPrice, uint256 endTime ); event BidPlaced( address indexed bidder, uint256 amount ); event AuctionEnded( address indexed winner, ); function createAuction( string memory itemDescription, uint256 duration ) public returns (uint256) { uint256 auctionId = auctions.length; auctions.push(); Auction storage auction = auctions[auctionId]; auction.seller = msg.sender; auction.itemDescription = itemDescription; auction.startingPrice = startingPrice; auction.endTime = block.timestamp + duration; auction.ended = false; emit AuctionCreated(auctionId, msg.sender, itemDescription, startingPrice, auction.endTime); return auctionId; } function bid(uint256 auctionId) public payable { require(block.timestamp < auction.endTime, "Auction ended"); require(!auction.ended, "Auction already ended"); require(msg.value > auction.highestBid, "Bid too low"); require(msg.value >= auction.startingPrice, "Bid below starting price"); if (auction.highestBidder != address(0)) { pendingReturns[auctionId][auction.highestBidder] += auction.highestBid; } if (auction.bids[msg.sender] == 0) { auction.bidders.push(msg.sender); } auction.bids[msg.sender] += msg.value; auction.highestBid = msg.value; auction.highestBidder = msg.sender; emit BidPlaced(auctionId, msg.sender, msg.value); } function withdraw(uint256 auctionId) public returns (bool) { uint256 amount = pendingReturns[auctionId][msg.sender]; pendingReturns[auctionId][msg.sender] = 0; } } function endAuction(uint256 auctionId) public { require(block.timestamp >= auction.endTime, "Auction not ended"); auction.ended = true; emit AuctionEnded(auctionId, auction.highestBidder, auction.highestBid); payable(auction.seller).transfer(auction.highestBid); } function getAuction(uint256 auctionId) public view returns ( address seller, uint256 highestBid, address highestBidder, uint256 endTime, bool ended ) { return ( auction.seller, auction.itemDescription, auction.startingPrice, auction.highestBid, auction.highestBidder, auction.endTime, auction.ended ); } function getBidders(uint256 auctionId) public view returns (address[] memory) { return auctions[auctionId].bidders; } function getBid(uint256 auctionId, address bidder) public view returns (uint256) { return auctions[auctionId].bids[bidder]; }
|