Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. pragma solidity ^0.4.17;
  2. contract Auction {
  3.  
  4. modifier onlyOwner {
  5. require(msg.sender == beneficiary);
  6. _;
  7. }
  8.  
  9. // Data
  10. //Structure to hold details of the item
  11. struct Item {
  12. uint itemId; // id of the item
  13. uint[] itemTokens; //tokens bid in favor of the item
  14. }
  15.  
  16. //Structure to hold the details of a persons
  17. struct Person {
  18. uint remainingTokens; // tokens remaining with bidder
  19. uint personId; // it serves as tokenId as well
  20. address addr; // address of the bidder
  21. }
  22.  
  23. mapping(address => Person) tokenDetails; //address to person
  24. Person [4] bidders; //Array containing 4 person objects
  25.  
  26. Item [3] public items; //Array containing 3 item objects
  27. address[3] public winners; //Array for address of winners
  28. address public beneficiary; //owner of the smart contract
  29.  
  30. uint bidderCount=0; //counter
  31.  
  32. //functions
  33.  
  34. function Auction() public payable{ //constructor
  35.  
  36. //Part 1 Task 1. Initialize beneficiary with address of smart contract’s owner
  37. //Hint. In the constructor,"msg.sender" is the address of the owner.
  38. // ** Start code here. 1 line approximately. **/
  39. beneficiary = msg.sender;
  40. // ** End code here. **/
  41. uint[] memory emptyArray;
  42. items[0] = Item({itemId:0,itemTokens:emptyArray});
  43.  
  44. //Part 1 Task 2. Initialize two items at index 1 and 2.
  45. // ** Start code here. 2 lines approximately. **/
  46. items[1] = Item({itemId:1,itemTokens:emptyArray});
  47. items[2] = Item({itemId:2,itemTokens:emptyArray});
  48. // ** End code here**/
  49. }
  50.  
  51. function register() public payable{
  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 bid, 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. Hint: "tokenDetails[msg.sender].remainingTokens" gives the details of the number of tokens remaining with the bidder.
  81. */
  82.  
  83. // ** Start code here. 2 lines approximately. **/
  84. if (tokenDetails[msg.sender].remainingTokens < _count || tokenDetails[msg.sender].remainingTokens == 0) { revert(); }
  85. if (_itemId > 2) { revert(); }
  86. //** End code here. **
  87.  
  88. /*
  89. Part 1 Task 5. Decrement the remainingTokens by the number of tokens bid
  90. Hint. "tokenDetails[msg.sender].remainingTokens" should be decremented by "_count".
  91. */
  92.  
  93. // ** Start code here. 1 line approximately. **
  94. tokenDetails[msg.sender].remainingTokens -= _count;
  95. // ** End code here. **
  96.  
  97. bidders[tokenDetails[msg.sender].personId].remainingTokens=
  98. tokenDetails[msg.sender].remainingTokens; // updating the same balance in bidders map.
  99. Item storage bidItem = items[_itemId];
  100. for(uint i=0; i<_count;i++) {
  101. bidItem.itemTokens.push(tokenDetails[msg.sender].personId);
  102. }
  103. }
  104.  
  105. function revealWinners() onlyOwner public {
  106. /*
  107. Iterate over all the items present in the auction.
  108. If at least on person has placed a bid, randomly select the winner
  109. */
  110.  
  111. for (uint id = 0; id < 3; id++) {
  112. Item storage currentItem=items[id];
  113. if(currentItem.itemTokens.length != 0){
  114. // generate random# from block number
  115. uint randomIndex = (block.number / currentItem.itemTokens.length)% currentItem.itemTokens.length;
  116. // Obtain the winning tokenId
  117. uint winnerId = currentItem.itemTokens[randomIndex];
  118.  
  119. /*
  120. Part 1 Task 6. Assign the winners.
  121. Hint." bidders[winnerId] " will give you the person object with the winnerId. you need to assign the address of the person obtained above to winners[id]
  122. */
  123.  
  124. // ** Start coding here *** 1 line approximately.
  125. winners[id] = bidders[winnerId].addr;
  126. //** end code here*
  127. }
  128. }
  129. }
  130.  
  131. // Miscellaneous methods: Below methods are used to assist Grading. Please DO NOT CHANGE THEM.
  132. function getPersonDetails(uint id) public constant
  133. returns(uint,uint,address){
  134. return (bidders[id].remainingTokens,bidders[id].personId,bidders[id].addr);
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement