forked from CryptoGifts/cryptosanta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCryptoSanta.sol
163 lines (118 loc) · 4.18 KB
/
CryptoSanta.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
pragma solidity ^0.4.17;
import 'zeppelin-solidity/contracts/ownership/Ownable.sol';
import 'zeppelin-solidity/contracts/math/SafeMath.sol';
import './BasicNFT.sol';
contract CryptoSanta is Ownable, BasicNFT {
using SafeMath for uint;
string public standard = 'ERC721';
string public name = 'CRYPTO SANTA GIFT';
string public symbol = 'GIFT';
uint8 public decimals = 0;
uint public nextGiftIndexToAssign = 1;
uint private _founderTokensAmount = 0;
uint private _founderTokensMax = 1000;
mapping(uint => bool) public isFounderToken;
// address where funds are collected
address public wallet;
// how many token units a buyer gets per wei
uint256 public giftPriceWei;
uint256 public endTime;
mapping(uint => uint8) public tokenType;
mapping(uint => bool) public tokenUsed;
mapping(address => address) public refAddresses;
mapping(uint8 => string) public tokenTypeImage;
uint8[] _giftsTypesRarity;
struct Collection {
uint8 rewardType;
uint8[] requiredTypes;
}
Collection[] _giftsCollection;
event GiftCreated(address indexed from, address indexed to, uint tokenId);
function CryptoSanta(uint256 _giftPriceWei, address _wallet, uint _endTime) {
require(_wallet != address(0));
require(_giftPriceWei > 0);
giftPriceWei = _giftPriceWei;
wallet = _wallet;
endTime = _endTime;
}
function sendGift(address to, uint8 giftsCount, address ref) public payable {
require(endTime > now);
require(giftsCount > 0);
require(giftsCount <= 15);
uint giftsPrice = giftPriceWei.mul(giftsCount);
require(msg.value >= giftsPrice);
uint giftWei = msg.value.sub(giftsPrice);
uint8 lastGiftType = 0;
for (uint8 i = 0; i < giftsCount; i++) {
lastGiftType = _getGiftType();
_createGift(msg.sender, to, lastGiftType);
}
if (ref != address(0)) {
if (refAddresses[msg.sender] == address(0)) {
refAddresses[msg.sender] = ref;
_createGift(msg.sender, ref, lastGiftType);
}
}
// forward funds
wallet.transfer(giftsPrice);
if (giftWei > 0) {
to.transfer(giftWei);
}
}
function craftGift(uint8 collectionIndex, uint[] tokenIds) public {
Collection storage c = _giftsCollection[collectionIndex];
require(tokenIds.length == c.requiredTypes.length);
for (uint8 i = 0; i < tokenIds.length; i++) {
uint tokenId = tokenIds[i];
assert(tokenOwner[tokenId] == msg.sender);
assert(!tokenUsed[tokenId]);
assert(tokenType[tokenId] == c.requiredTypes[i]);
tokenUsed[tokenIds[i]] = true;
}
_createGift(address(0), msg.sender, c.rewardType);
}
function getTokenImage(uint tokenId) public constant returns (string image) {
uint8 giftType = tokenType[tokenId];
return tokenTypeImage[giftType];
}
function setGiftPrice(uint256 price) public onlyOwner {
giftPriceWei = price;
}
function setGiftTypesRarity(uint8[] giftsTypesRarity) public onlyOwner {
require(_giftsTypesRarity.length == 0);
_giftsTypesRarity = giftsTypesRarity;
}
function addGiftCollection(uint8 rewardType, uint8[] requiredTypes) public onlyOwner {
_giftsCollection.push(Collection(rewardType, requiredTypes));
}
function setTokenTypeImage(uint8 giftType, string image) public onlyOwner {
tokenTypeImage[giftType] = image;
}
function createFounderGift(address to, uint8 giftsCount) public onlyOwner {
for (uint8 i = 0; i < giftsCount; i++) {
assert(_founderTokensAmount <= _founderTokensMax);
isFounderToken[nextGiftIndexToAssign] = true;
_createGift(address(0), to, _getGiftType());
_founderTokensAmount++;
}
}
function setEndTime(uint _endTime) public onlyOwner {
endTime = _endTime;
}
function _createGift(address from, address to, uint8 giftType) internal {
uint tokenId = nextGiftIndexToAssign;
nextGiftIndexToAssign = nextGiftIndexToAssign.add(1);
totalTokens = totalTokens.add(1);
_addTokenTo(to, tokenId);
tokenType[tokenId] = giftType;
GiftCreated(from, to, tokenId);
}
function _randomGen(uint min, uint max) internal constant returns (uint randomNumber) {
return min + uint(keccak256(block.blockhash(block.number - 1), nextGiftIndexToAssign)) % (max - min);
}
function _getGiftType() internal returns (uint8 randomNumber){
uint n = _giftsTypesRarity.length;
uint rnd = _randomGen(0, n);
return _giftsTypesRarity[rnd];
}
}