-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWaifuDistribution.sol
181 lines (157 loc) · 5.27 KB
/
WaifuDistribution.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
pragma solidity ^0.4.24;
import "./libs/openzeppelin-solidity/contracts/token/ERC721/ERC721Full.sol";
contract WaifuDistribution is ERC721Full{
struct Bid {
bytes32 blindedBid;
uint deposit;
}
mapping(uint=>mapping(address => Bid[])) public bids;
mapping(address => uint) public pendingReturns;
mapping(uint=>address) public highestBidder;
mapping(uint=>uint) public highestBid;
uint public creationTime;
string baseURL="https://api.waifuchain.moe?waifu=";
constructor() ERC721Full("WaifuChain", "WAIFU") public {
creationTime=now;
}
event NewBid(uint waifuIndex);
/// Place a blinded bid with `_blindedBid` = keccak256(value,
/// fake, secret).
/// The sent ether is only refunded if the bid is correctly
/// revealed in the revealing phase. The bid is valid if the
/// ether sent together with the bid is at least "value" and
/// "fake" is not true. Setting "fake" to true and sending
/// not the exact amount are ways to hide the real bid but
/// still make the required deposit. The same address can
/// place multiple bids.
function bid(uint _tokenId, bytes32 _blindedBid)
public
payable
{
(uint min, uint max)=_waifuIndexRangeByDay(now);
require(_tokenId>=min && _tokenId<=max);
bids[_tokenId][msg.sender].push(Bid({
blindedBid: _blindedBid,
deposit: msg.value
}));
emit NewBid(_tokenId);
}
function _waifuIndexRangeByDay(uint time) view internal returns (uint, uint){
uint256 day=(time-creationTime)/(1 days);
assert(day>=0);
uint256 month=day/30;
require(month<=3);
uint min;
uint max;
if(month==3){
min=(8+4+2)*30+day%30;
max=min;
}
else if(month==2){
min=(8+4)*30+2*(day%30);
max=min+1;
}
else if(month==1){
min=(8)*30+4*(day%30);
max=min+3;
}
else if(month==0){
min=8*(day%30);
max=min+7;
}
return (min, max);
}
/// Reveal your blinded bids. You will get a refund for all
/// correctly blinded invalid bids and for all bids except for
/// the totally highest.
function reveal(
uint _tokenId,
uint[] _values,
bool[] _fake,
bytes32[] _secret
)
public
{
(uint min, uint max)=_waifuIndexRangeByDay(now - 1 days);
require(_tokenId>=min && _tokenId<=max);
uint length = bids[_tokenId][msg.sender].length;
require(_values.length == length);
require(_fake.length == length);
require(_secret.length == length);
uint refund;
for (uint i = 0; i < length; i++) {
Bid storage bid = bids[_tokenId][msg.sender][i];
(uint value, bool fake, bytes32 secret) =
(_values[i], _fake[i], _secret[i]);
if (bid.blindedBid != keccak256(value, fake, secret)) {
// Bid was not actually revealed.
// Do not refund deposit.
continue;
}
refund += bid.deposit;
if (!fake && bid.deposit >= value) {
if (placeBid(_tokenId, msg.sender, value))
refund -= value;
}
// Make it impossible for the sender to re-claim
// the same deposit.
bid.blindedBid = bytes32(0);
}
msg.sender.transfer(refund);
}
// This is an "internal" function which means that it
// can only be called from the contract itself (or from
// derived contracts).
function placeBid(uint _tokenId, address bidder, uint value) internal
returns (bool success)
{
if (value <= highestBid[_tokenId]) {
return false;
}
if (highestBidder[_tokenId] != 0) {
// Refund the previously highest bidder.
pendingReturns[highestBidder[_tokenId]] += highestBid[_tokenId];
}
highestBid[_tokenId] = value;
highestBidder[_tokenId] = bidder;
return true;
}
function withdraw() public {
uint amount = pendingReturns[msg.sender];
if (amount > 0) {
// It is important to set this to zero because the recipient
// can call this function again as part of the receiving call
// before `transfer` returns (see the remark above about
// conditions -> effects -> interaction).
pendingReturns[msg.sender] = 0;
msg.sender.transfer(amount);
}
}
//Copied from Oraclize
function uint2str(uint i) internal pure returns (string){
if (i == 0) return "0";
uint j = i;
uint length;
while (j != 0){
length++;
j /= 10;
}
bytes memory bstr = new bytes(length);
uint k = length - 1;
while (i != 0){
bstr[k--] = byte(48 + i % 10);
i /= 10;
}
return string(bstr);
}
function tokenURI(uint256 tokenId) external view returns (string) {
return string(abi.encodePacked(baseURL, uint2str(tokenId)));
}
function claimWaifu(uint waifuIndex) external{
require(msg.sender==highestBidder[waifuIndex]);
(uint min,)=_waifuIndexRangeByDay(now - 1 days);
require(waifuIndex<min);
highestBidder[waifuIndex]=address(0);
_mint(msg.sender, waifuIndex);
}
}