Facebook
From Anshul Chaudhary, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 186
  1. pragma solidity ^0.4.17;
  2. contract Auction {
  3.  
  4.     // Data
  5.     //Structure to hold details of the item
  6.     struct Item {
  7.         uint itemId; // id of the item
  8.         uint[] itemTokens;  //tokens bid in favor of the item
  9.  
  10.     }
  11.  
  12.    //Structure to hold the details of a persons
  13.     struct Person {
  14.         uint remainingTokens; // tokens remaining with bidder
  15.         uint personId; // it serves as tokenId as well
  16.         address addr;//address of the bidder
  17.     }
  18.  
  19.     mapping(address => Person) tokenDetails; //address to person
  20.     Person [4] bidders;//Array containing 4 person objects
  21.  
  22.     Item [3] public items;//Array containing 3 item objects
  23.     address[3] public winners;//Array for address of winners
  24.     address public beneficiary;//owner of the smart contract
  25.  
  26.     uint bidderCount=0;//counter
  27.  
  28.     //functions
  29.  
  30.     function Auction() public payable{    //constructor
  31.  
  32.         //Part 1 Task 1. Initialize beneficiary with address of smart contract’s owner
  33.         //Hint. In the constructor,"msg.sender" is the address of the owner.
  34.         // ** Start code here. 1 line approximately. **/
  35.         beneficiary = msg.sender;
  36.           //** End code here. **/
  37.         uint[] memory emptyArray;
  38.         items[0] = Item({itemId:0,itemTokens:emptyArray});
  39.  
  40.         //Part 1 Task 2. Initialize two items with at index 1 and 2.
  41.         // ** Start code here. 2 lines approximately. **/
  42.        uint[] memory emptyArray1;
  43.         items[1] = Item({itemId:1,itemTokens:emptyArray1});
  44.         uint[] memory emptyArray2;
  45.         items[2] = Item({itemId:2,itemTokens:emptyArray2});
  46.         //** End code here**/
  47.     }
  48.  
  49.  
  50.     function register() public payable{
  51.  
  52.  
  53.         bidders[bidderCount].personId = bidderCount;
  54.  
  55.         //Part 1 Task 3. Initialize the address of the bidder
  56.         /*Hint. Here the bidders[bidderCount].addr should be initialized with address of the registrant.*/
  57.  
  58.         // ** Start code here. 1 line approximately. **/
  59.         bidders[bidderCount].addr = msg.sender;
  60.         //** End code here. **
  61.  
  62.         bidders[bidderCount].remainingTokens = 5; // only 5 tokens
  63.         tokenDetails[msg.sender]=bidders[bidderCount];
  64.         bidderCount++;
  65.     }
  66.  
  67.     function bid(uint _itemId, uint _count) public payable{
  68.         /*
  69.             Bids tokens to a particular item.
  70.             Arguments:
  71.             _itemId -- uint, id of the item
  72.             _count -- uint, count of tokens to bid for the item
  73.         */
  74.  
  75.         /*
  76.         Part 1 Task 4. Implement the three conditions below.
  77.             4.1 If the number of tokens remaining with the bidder is < count of tokens bidded, revert.
  78.             4.2 If there are no tokens remaining with the bidder, revert.
  79.             4.3 If the id of the item for which bid is placed, is greater than 2, revert.
  80.  
  81.         Hint: "tokenDetails[msg.sender].remainingTokens" gives the details of the number of tokens remaining with the bidder.
  82.         */
  83.  
  84.         // ** Start code here. 2 lines approximately. **/
  85.          if ( (tokenDetails[msg.sender].remainingTokens < _count) ||
  86.              (tokenDetails[msg.sender].remainingTokens <= 0)
  87.             ) { revert(); }
  88.         if (_itemId > items.length - 1) { revert(); }
  89.  
  90.         //** End code here. **
  91.  
  92.         /*Part 1 Task 5. Decrement the remainingTokens by the number of tokens bid and store the value in balance variable.
  93.         Hint. "tokenDetails[msg.sender].remainingTokens" should be decremented by "_count". */
  94.  
  95.         // ** Start code here. 1 line approximately. **
  96.         uint balance = tokenDetails[msg.sender].remainingTokens - _count;
  97.         //** End code here. **
  98.  
  99.         tokenDetails[msg.sender].remainingTokens=balance;
  100.         bidders[tokenDetails[msg.sender].personId].remainingTokens=balance;//updating the same balance in bidders map.
  101.  
  102.         Item storage bidItem = items[_itemId];
  103.         for(uint i=0; i<_count;i++) {
  104.             bidItem.itemTokens.push(tokenDetails[msg.sender].personId);
  105.         }
  106.     }
  107.  
  108.     // Part 2 Task 1. Create a modifier named "onlyOwner" to ensure that only owner is allowed to reveal winners
  109.     //Hint : Use require to validate if "msg.sender" is equal to the "beneficiary".
  110.     modifier onlyOwner {
  111.         // ** Start code here. 2 lines approximately. **
  112.          require (msg.sender == beneficiary);
  113.         _;
  114.  
  115.         //** End code here. **
  116.     }
  117.  
  118.  
  119.     function revealWinners() public onlyOwner{
  120.  
  121.         /*
  122.             Iterate over all the items present in the auction.
  123.             If at least on person has placed a bid, randomly select          the winner */
  124.  
  125.         for (uint id = 0; id < 3; id++) {
  126.             Item storage currentItem=items[id];
  127.             if(currentItem.itemTokens.length != 0){
  128.             // generate random# from block number
  129.             uint randomIndex = (block.number / currentItem.itemTokens.length)% currentItem.itemTokens.length;
  130.             // Obtain the winning tokenId
  131.  
  132.             uint winnerId = currentItem.itemTokens[randomIndex];
  133.  
  134.             /* Part 1 Task 6. Assign the winners.
  135.             Hint." bidders[winnerId] " will give you the person object with the winnerId.
  136.             you need to assign the address of the person obtained above to winners[id] */
  137.  
  138.             // ** Start coding here *** 1 line approximately.
  139.             winners[id] = bidders[winnerId].addr;
  140.  
  141.             //** end code here*
  142.  
  143.             }
  144.         }
  145.     }
  146.  
  147.   //Miscellaneous methods: Below methods are used to assist Grading. Please DONOT CHANGE THEM.
  148.     function getPersonDetails(uint id) public constant returns(uint,uint,address){
  149.         return (bidders[id].remainingTokens,bidders[id].personId,bidders[id].addr);
  150.     }
  151.  
  152. }
  153.