pragma solidity ^0.5.0; contract Auction { uint public startTime; uint public endTime; uint public startingPrice; uint tokenId; address payable owner; address payable[] public bidders; mapping(address => uint) public balances; // bidderAddress => bidAmount /// @dev Bid event emits when a participant makes a successful bid event NewBid(address indexed _bidder, uint indexed _bidValue,uint _time); /// @dev Refund event emits when a participant withdraws their locked ethers after auction ends. event Refund(address indexed _participant, uint indexed _amount, uint indexed _time); modifier NotOwner { require(msg.sender != owner,'owner cannot place bid'); _; } modifier onlyOwner { require(msg.sender == owner,'only owner can call'); _; } modifier isAuctionOn { require(now > startTime && now < endTime,'Auction has end'); _; } modifier isAuctionEnd { require(now > endTime ,'Auction has not ended yet'); _; } constructor(uint _duration, uint _startingPrice,uint _tokenId) public { tokenId = _tokenId; owner = msg.sender; startTime = now; endTime = startTime + _duration; startingPrice = _startingPrice; } /// @dev users can submit a bid by sending plain ethers in the contract. /// @return true on a successful bid function bid() external payable NotOwner isAuctionOn { require(!isContract(msg.sender),'only external accounts can participate'); require(msg.value > startingPrice,'Bid value must be greater than starting Price'); if(balances[msg.sender] == 0) { bidders.push(msg.sender); } balances[msg.sender] += msg.value; emit NewBid(msg.sender,balances[msg.sender],now); } /// @dev refunds ethers to non-winners. /// param _winner address of the winner(fetched from DB) function endAuction(address _winner) external onlyOwner isAuctionEnd { for (uint i = 0; i < bidders.length; i++){ if(bidders[i] != _winner) { bidders[i].transfer(balances[bidders[i]]); emit Refund(bidders[i], balances[bidders[i]], now); } } owner.transfer(address(this).balance); // send remaining to the owner } // // @dev withdraw ethers from this contract - only owner can call // function withdrawFunds() external onlyOwner isAuctionEnd returns (bool){ // owner.transfer(address(this).balance); // return true; // } function isContract(address account) internal view returns (bool) { // This method relies in extcodesize, which returns 0 for contracts in // construction, since the code is only stored at the end of the // constructor execution. // According to EIP-1052, 0x0 is the value returned for not-yet created accounts // and 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 is returned // for accounts without code, i.e. `keccak256('')` bytes32 codehash; bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470; // solhint-disable-next-line no-inline-assembly assembly { codehash := extcodehash(account) } return (codehash != 0x0 && codehash != accountHash); } /// @return true if auction is active function isActive() public view returns (bool) { return (now > startTime && now < endTime); } /// @dev gets the bid amount of users /// @param _user address of the user /// @return total bid amount function bidDetails(address _user) public view returns (uint) { require(balances[_user] != 0,"user has not placed any bid"); return balances[_user]; } /// @return ethers stored in contract balance function getContractBalance() public view returns (uint) { return address(this).balance; } /// @return an array of address of all the bidders. function getAllBidders() public view returns (address payable[] memory) { address payable[] memory _bidders = new address payable[](bidders.length); for(uint i = 0; i < bidders.length; i++) { _bidders[i] = bidders[i]; } return _bidders; } function getHighestEthBid() public view returns (address,uint) { address highestBider; uint highestBid; for(uint i = 0; i < bidders.length; i++) { if(highestBid > balances[bidders[i]]) break; else { highestBid = balances[bidders[i]]; highestBider = bidders[i]; } } return (highestBider,highestBid); } function totalBids() public view returns(uint) { return bidders.length; } }
0.5.11