经典智能合约之智能拍卖 | 您所在的位置:网站首页 › 沉底钓铅坠重量选择 › 经典智能合约之智能拍卖 |
实现一个简易的拍卖合约
角色分析:4类角色(拍卖师actioneer,委托人seller,竞买人bidder,买受人buyer) 功能分析:拍卖的基本原则是价高者得,在设定的拍卖时限内,出价最高者最终获得拍卖得标物(提示:在数据结构上,不用太复杂的设计,只需能记录当前最高价的竞买人及其金额、拍卖结束时间即可。) 竞拍功能:竞买人可以多次出价,价格必须高于当前记录的最高价,并将最高价和竞买人替换。 竞拍结束:竞拍结束时,宣布胜利者。 状态变量定义和初始化定义变量:委托人、拍卖师、买受人、竞拍结束标志、时间限制、竞拍最高金额。(提示:合约调用者为拍卖师,如果涉及到转账的角色需要用address payable,构造函数可以初始化拍卖师[即msg.sender]、拍卖时限、竞拍结束标志,竞买人在程序中选择不同account,根据account和msg.value更新买受人和最高金额) 竞拍功能只要竞拍未结束都可以发起竞拍(提示:从调用信息窗口选择买家,输入竞拍价格,用msg.sender和msg.value获得买家和价格信息) 但需要满足以下条件:竞拍未结束;竞拍时的价格一定要比之前的价格高;竞拍在时限内; 当竞拍被允许时,需要退钱给上一个买家,并替换原有的最高价格和买受人。 结束竞拍判断是否超过时限,并且是第一次执行本操作,确认通过后,转账给委托人。 合约代码 pragma solidity ^0.8.4; contract auction { uint auctionEndTime; address public auctioneer; address public buyer; address payable public seller; uint public auctionAmount; // Allowed withdrawals of previous bids mapping(address => uint) pendingReturns; // A sign to judge whether the auction is over // Set to true at the end, disallows any change. bool ended; // Events that will be emitted on changes. event auctionAmountIncreased(address bidder, uint amount); event AuctionEnded(address winner, uint amount); // Errors that describe failures. /// The auction has already ended. error AuctionAlreadyEnded(); /// There is already a higher or equal bid. error BidNotHighEnough(uint auctionAmount); /// The auction has not ended yet. error AuctionNotYetEnded(); /// The function auctionEnd has already been called. error AuctionEndAlreadyCalled(); constructor( uint biddingTime, address payable sellerAddress ) { seller = sellerAddress; auctionEndTime = block.timestamp + biddingTime; auctioneer = msg.sender; } /// Bid function function bid() external payable { // Revert the call if the bidding period is over. if (block.timestamp > auctionEndTime) revert AuctionAlreadyEnded(); // Return money if the bid is not high enough if (msg.value |
CopyRight 2018-2019 实验室设备网 版权所有 |