SHARE
TWEET

Auction

a guest Feb 19th, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pragma solidity >=0.4.22 <0.6.0;
  2.  
  3. contract Auction {
  4.    
  5.     address public owner;
  6.     uint public startBlock;
  7.     uint public endBlock;
  8.    
  9.     bool public canceled;
  10.     address public highestBidder;
  11.     mapping(address => uint256) public fundsByBidder;
  12.  
  13.     event LogBid(address bidder, uint bid, address highestBidder, uint highestBid);
  14.     event LogWithdrawal(address withdrawer, address withdrawalAccount, uint amount);
  15.     event LogCanceled();
  16.    
  17.     constructor (uint _startBlock, uint _endBlock) public {
  18.         //require(_startBlock <= _endBlock);
  19.         //(_startBlock > block.number);
  20.    
  21.         owner = msg.sender;
  22.         startBlock = _startBlock;
  23.         endBlock = _endBlock;
  24.     }
  25.    
  26.     modifier onlyRunning {
  27.         require(canceled == false);
  28.         require( (block.number > startBlock) && (block.number < endBlock) );
  29.         _;
  30.     }
  31.    
  32.     modifier onlyOwner() {
  33.         require(msg.sender == owner);
  34.         _;
  35.     }
  36.    
  37.     modifier onlyNotOwner {
  38.         require (msg.sender != owner);
  39.         _;
  40.     }
  41.    
  42.     modifier onlyAfterStart {
  43.         //require (block.number > startBlock);
  44.         _;
  45.     }
  46.    
  47.     modifier onlyBeforeEnd {
  48.         //require(block.number < endBlock);
  49.         _;
  50.     }
  51.    
  52.     modifier onlyNotCanceled {
  53.         require(canceled == false);
  54.         _;
  55.     }
  56.    
  57.     modifier onlyEndedOrCanceled {
  58.         require ( (block.number > endBlock) || (canceled == true) );
  59.         _;
  60.     }
  61.    
  62.     function placeBid()
  63.         public
  64.         payable
  65.         onlyAfterStart
  66.         onlyBeforeEnd
  67.         onlyNotCanceled
  68.         onlyNotOwner
  69.     {
  70.         // reject payments of 0 ETH
  71.         require (msg.value > 0);
  72.    
  73.         // calculate the user's total bid based on the current amount they've sent to the contract
  74.         // plus whatever has been sent with this transaction
  75.         uint newBid = fundsByBidder[msg.sender] + msg.value;
  76.    
  77.         // get the current highest bid
  78.         uint lastHighestBid = fundsByBidder[highestBidder];
  79.        
  80.         // if the user isn't even willing to overbid the highest bid, there's nothing for us
  81.         // to do except revert the transaction.
  82.         require (newBid > lastHighestBid);
  83.    
  84.         // update the user bid
  85.         fundsByBidder[msg.sender] = newBid;
  86.  
  87.         highestBidder = msg.sender;
  88.        
  89.         emit LogBid(msg.sender, newBid, highestBidder, lastHighestBid);
  90.     }
  91.  
  92.     function withdraw()
  93.         public
  94.         onlyEndedOrCanceled
  95.     {
  96.         address withdrawalAccount;
  97.         uint withdrawalAmount;
  98.    
  99.         if (canceled == true) {
  100.             // if the auction was canceled, everyone should simply be allowed to withdraw their funds
  101.             withdrawalAmount = fundsByBidder[msg.sender];
  102.    
  103.         } else {
  104.             // highest bidder won the auction, so he cannot withdraw his money
  105.             require(msg.sender != highestBidder);
  106.            
  107.             // the auction finished without being canceled
  108.             if (msg.sender == owner) {
  109.                 // the auction's owner should be allowed to withdraw the highestBid
  110.                 withdrawalAmount = fundsByBidder[highestBidder];
  111.             }
  112.             else {
  113.                 // anyone who participated but did not win the auction
  114.                 // should be allowed to withdraw
  115.                 // the full amount of their funds
  116.                 withdrawalAmount = fundsByBidder[msg.sender];
  117.             }
  118.         }
  119.    
  120.         require (withdrawalAmount > 0);
  121.    
  122.         fundsByBidder[withdrawalAccount] -= withdrawalAmount;
  123.    
  124.         // send the funds
  125.         msg.sender.transfer(withdrawalAmount);
  126.    
  127.         emit LogWithdrawal(msg.sender, withdrawalAccount, withdrawalAmount);
  128.     }
  129.    
  130.     function cancelAuction()
  131.         public
  132.         onlyOwner
  133.         onlyBeforeEnd
  134.         onlyNotCanceled
  135.     {
  136.         canceled = true;
  137.         emit LogCanceled();
  138.     }
  139. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top