-
Notifications
You must be signed in to change notification settings - Fork 0
/
Auction.sol
114 lines (74 loc) · 3.44 KB
/
Auction.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
pragma solidity ^0.4.17;
contract Auction {
// Data
//Structure to hold details of the item
struct Item {
uint itemId; // id of the item
uint[] itemTokens; //tokens bid in favor of the item
}
//Structure to hold the details of a persons
struct Person {
uint remainingTokens; // tokens remaining with bidder
uint personId; // it serves as tokenId as well
address addr;//address of the bidder
}
mapping(address => Person) tokenDetails; //address to person
Person [4] bidders;//Array containing 4 person objects
Item [3] public items;//Array containing 3 item objects
address[3] public winners;//Array for address of winners
address public beneficiary;//owner of the smart contract
uint bidderCount=0;//counter
//functions
function Auction() public payable{ //constructor
beneficiary = msg.sender;
uint[] memory emptyArray;
items[0] = Item({itemId:0,itemTokens:emptyArray});
items[1] = Item({itemId:1,itemTokens:emptyArray});
items[2] = Item({itemId:2,itemTokens: emptyArray});
}
function register() public payable{
bidders[bidderCount].personId = bidderCount;
bidders[bidderCount].addr = msg.sender;
bidders[bidderCount].remainingTokens = 5; // only 5 tokens
tokenDetails[msg.sender]=bidders[bidderCount];
bidderCount++;
}
function bid(uint _itemId, uint _count) public payable{
/*
Bids tokens to a particular item.
Arguments:
_itemId -- uint, id of the item
_count -- uint, count of tokens to bid for the item
*/
if (tokenDetails[msg.sender].remainingTokens < _count || tokenDetails[msg.sender].remainingTokens == 0 || _itemId > 2) revert();
uint balance= tokenDetails[msg.sender].remainingTokens - _count;
tokenDetails[msg.sender].remainingTokens=balance;
bidders[tokenDetails[msg.sender].personId].remainingTokens=balance;//updating the same balance in bidders map.
Item storage bidItem = items[_itemId];
for(uint i=0; i<_count;i++) {
bidItem.itemTokens.push(tokenDetails[msg.sender].personId);
}
}
modifier onlyOwner {
require(msg.sender == beneficiary);
_;
}
function revealWinners() public onlyOwner{
/*
Iterate over all the items present in the auction.
If at least on person has placed a bid, randomly select the winner */
for (uint id = 0; id < 3; id++) {
Item storage currentItem=items[id];
if(currentItem.itemTokens.length != 0){
// generate random# from block number
uint randomIndex = (block.number / currentItem.itemTokens.length)% currentItem.itemTokens.length;
// Obtain the winning tokenId
uint winnerId = currentItem.itemTokens[randomIndex];
winners[id] = bidders[winnerId].addr;
}
}
}
function getPersonDetails(uint id) public constant returns(uint,uint,address){
return (bidders[id].remainingTokens,bidders[id].personId,bidders[id].addr);
}
}