-
Notifications
You must be signed in to change notification settings - Fork 132
/
LibAavegotchi.sol
375 lines (329 loc) · 16.1 KB
/
LibAavegotchi.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// SPDX-License-Identifier: MIT
pragma solidity 0.8.1;
import {IERC20} from "../../shared/interfaces/IERC20.sol";
import {LibAppStorage, AavegotchiCollateralTypeInfo, AppStorage, Aavegotchi, ItemType, NUMERIC_TRAITS_NUM, EQUIPPED_WEARABLE_SLOTS, PORTAL_AAVEGOTCHIS_NUM} from "./LibAppStorage.sol";
import {LibERC20} from "../../shared/libraries/LibERC20.sol";
import {LibMeta} from "../../shared/libraries/LibMeta.sol";
import {IERC721} from "../../shared/interfaces/IERC721.sol";
import {LibERC721} from "../../shared/libraries/LibERC721.sol";
import {LibItems, ItemTypeIO} from "../libraries/LibItems.sol";
struct AavegotchiCollateralTypeIO {
address collateralType;
AavegotchiCollateralTypeInfo collateralTypeInfo;
}
struct AavegotchiInfo {
uint256 tokenId;
string name;
address owner;
uint256 randomNumber;
uint256 status;
int16[NUMERIC_TRAITS_NUM] numericTraits;
int16[NUMERIC_TRAITS_NUM] modifiedNumericTraits;
uint16[EQUIPPED_WEARABLE_SLOTS] equippedWearables;
address collateral;
address escrow;
uint256 stakedAmount;
uint256 minimumStake;
uint256 kinship; //The kinship value of this Aavegotchi. Default is 50.
uint256 lastInteracted;
uint256 experience; //How much XP this Aavegotchi has accrued. Begins at 0.
uint256 toNextLevel;
uint256 usedSkillPoints; //number of skill points used
uint256 level; //the current aavegotchi level
uint256 hauntId;
uint256 baseRarityScore;
uint256 modifiedRarityScore;
bool locked;
ItemTypeIO[] items;
}
struct PortalAavegotchiTraitsIO {
uint256 randomNumber;
int16[NUMERIC_TRAITS_NUM] numericTraits;
address collateralType;
uint256 minimumStake;
}
struct InternalPortalAavegotchiTraitsIO {
uint256 randomNumber;
int16[NUMERIC_TRAITS_NUM] numericTraits;
address collateralType;
uint256 minimumStake;
}
library LibAavegotchi {
uint8 constant STATUS_CLOSED_PORTAL = 0;
uint8 constant STATUS_VRF_PENDING = 1;
uint8 constant STATUS_OPEN_PORTAL = 2;
uint8 constant STATUS_AAVEGOTCHI = 3;
event AavegotchiInteract(uint256 indexed _tokenId, uint256 kinship);
function toNumericTraits(
uint256 _randomNumber,
int16[NUMERIC_TRAITS_NUM] memory _modifiers,
uint256 _hauntId
) internal pure returns (int16[NUMERIC_TRAITS_NUM] memory numericTraits_) {
if (_hauntId == 1) {
for (uint256 i; i < NUMERIC_TRAITS_NUM; i++) {
uint256 value = uint8(uint256(_randomNumber >> (i * 8)));
if (value > 99) {
value /= 2;
if (value > 99) {
value = uint256(keccak256(abi.encodePacked(_randomNumber, i))) % 100;
}
}
numericTraits_[i] = int16(int256(value)) + _modifiers[i];
}
} else {
for (uint256 i; i < NUMERIC_TRAITS_NUM; i++) {
uint256 value = uint8(uint256(_randomNumber >> (i * 8)));
if (value > 99) {
value = value - 100;
if (value > 99) {
value = uint256(keccak256(abi.encodePacked(_randomNumber, i))) % 100;
}
}
numericTraits_[i] = int16(int256(value)) + _modifiers[i];
}
}
}
function rarityMultiplier(int16[NUMERIC_TRAITS_NUM] memory _numericTraits) internal pure returns (uint256 multiplier) {
uint256 rarityScore = LibAavegotchi.baseRarityScore(_numericTraits);
if (rarityScore < 300) return 10;
else if (rarityScore >= 300 && rarityScore < 450) return 10;
else if (rarityScore >= 450 && rarityScore <= 525) return 25;
else if (rarityScore >= 526 && rarityScore <= 580) return 100;
else if (rarityScore >= 581) return 1000;
}
function singlePortalAavegotchiTraits(
uint256 _hauntId,
uint256 _randomNumber,
uint256 _option
) internal view returns (InternalPortalAavegotchiTraitsIO memory singlePortalAavegotchiTraits_) {
AppStorage storage s = LibAppStorage.diamondStorage();
uint256 randomNumberN = uint256(keccak256(abi.encodePacked(_randomNumber, _option)));
singlePortalAavegotchiTraits_.randomNumber = randomNumberN;
address collateralType = s.hauntCollateralTypes[_hauntId][randomNumberN % s.hauntCollateralTypes[_hauntId].length];
singlePortalAavegotchiTraits_.numericTraits = toNumericTraits(randomNumberN, s.collateralTypeInfo[collateralType].modifiers, _hauntId);
singlePortalAavegotchiTraits_.collateralType = collateralType;
AavegotchiCollateralTypeInfo memory collateralInfo = s.collateralTypeInfo[collateralType];
uint256 conversionRate = collateralInfo.conversionRate;
//Get rarity multiplier
uint256 multiplier = rarityMultiplier(singlePortalAavegotchiTraits_.numericTraits);
//First we get the base price of our collateral in terms of DAI
uint256 collateralDAIPrice = ((10**IERC20(collateralType).decimals()) / conversionRate);
//Then multiply by the rarity multiplier
singlePortalAavegotchiTraits_.minimumStake = collateralDAIPrice * multiplier;
}
function portalAavegotchiTraits(uint256 _tokenId)
internal
view
returns (PortalAavegotchiTraitsIO[PORTAL_AAVEGOTCHIS_NUM] memory portalAavegotchiTraits_)
{
AppStorage storage s = LibAppStorage.diamondStorage();
require(s.aavegotchis[_tokenId].status == LibAavegotchi.STATUS_OPEN_PORTAL, "AavegotchiFacet: Portal not open");
uint256 randomNumber = s.tokenIdToRandomNumber[_tokenId];
uint256 hauntId = s.aavegotchis[_tokenId].hauntId;
for (uint256 i; i < portalAavegotchiTraits_.length; i++) {
InternalPortalAavegotchiTraitsIO memory single = singlePortalAavegotchiTraits(hauntId, randomNumber, i);
portalAavegotchiTraits_[i].randomNumber = single.randomNumber;
portalAavegotchiTraits_[i].collateralType = single.collateralType;
portalAavegotchiTraits_[i].minimumStake = single.minimumStake;
portalAavegotchiTraits_[i].numericTraits = single.numericTraits;
}
}
function getAavegotchi(uint256 _tokenId) internal view returns (AavegotchiInfo memory aavegotchiInfo_) {
AppStorage storage s = LibAppStorage.diamondStorage();
aavegotchiInfo_.tokenId = _tokenId;
aavegotchiInfo_.owner = s.aavegotchis[_tokenId].owner;
aavegotchiInfo_.randomNumber = s.aavegotchis[_tokenId].randomNumber;
aavegotchiInfo_.status = s.aavegotchis[_tokenId].status;
aavegotchiInfo_.hauntId = s.aavegotchis[_tokenId].hauntId;
if (aavegotchiInfo_.status == STATUS_AAVEGOTCHI) {
aavegotchiInfo_.name = s.aavegotchis[_tokenId].name;
aavegotchiInfo_.equippedWearables = s.aavegotchis[_tokenId].equippedWearables;
aavegotchiInfo_.collateral = s.aavegotchis[_tokenId].collateralType;
aavegotchiInfo_.escrow = s.aavegotchis[_tokenId].escrow;
aavegotchiInfo_.stakedAmount = IERC20(aavegotchiInfo_.collateral).balanceOf(aavegotchiInfo_.escrow);
aavegotchiInfo_.minimumStake = s.aavegotchis[_tokenId].minimumStake;
aavegotchiInfo_.kinship = kinship(_tokenId);
aavegotchiInfo_.lastInteracted = s.aavegotchis[_tokenId].lastInteracted;
aavegotchiInfo_.experience = s.aavegotchis[_tokenId].experience;
aavegotchiInfo_.toNextLevel = xpUntilNextLevel(s.aavegotchis[_tokenId].experience);
aavegotchiInfo_.level = aavegotchiLevel(s.aavegotchis[_tokenId].experience);
aavegotchiInfo_.usedSkillPoints = s.aavegotchis[_tokenId].usedSkillPoints;
aavegotchiInfo_.numericTraits = s.aavegotchis[_tokenId].numericTraits;
aavegotchiInfo_.baseRarityScore = baseRarityScore(aavegotchiInfo_.numericTraits);
(aavegotchiInfo_.modifiedNumericTraits, aavegotchiInfo_.modifiedRarityScore) = modifiedTraitsAndRarityScore(_tokenId);
aavegotchiInfo_.locked = s.aavegotchis[_tokenId].locked;
aavegotchiInfo_.items = LibItems.itemBalancesOfTokenWithTypes(address(this), _tokenId);
}
}
//Only valid for claimed Aavegotchis
function modifiedTraitsAndRarityScore(uint256 _tokenId)
internal
view
returns (int16[NUMERIC_TRAITS_NUM] memory numericTraits_, uint256 rarityScore_)
{
AppStorage storage s = LibAppStorage.diamondStorage();
require(s.aavegotchis[_tokenId].status == STATUS_AAVEGOTCHI, "AavegotchiFacet: Must be claimed");
Aavegotchi storage aavegotchi = s.aavegotchis[_tokenId];
numericTraits_ = getNumericTraits(_tokenId);
uint256 wearableBonus;
for (uint256 slot; slot < EQUIPPED_WEARABLE_SLOTS; slot++) {
uint256 wearableId = aavegotchi.equippedWearables[slot];
if (wearableId == 0) {
continue;
}
ItemType storage itemType = s.itemTypes[wearableId];
//Add on trait modifiers
for (uint256 j; j < NUMERIC_TRAITS_NUM; j++) {
numericTraits_[j] += itemType.traitModifiers[j];
}
wearableBonus += itemType.rarityScoreModifier;
}
uint256 baseRarity = baseRarityScore(numericTraits_);
rarityScore_ = baseRarity + wearableBonus;
}
function getNumericTraits(uint256 _tokenId) internal view returns (int16[NUMERIC_TRAITS_NUM] memory numericTraits_) {
AppStorage storage s = LibAppStorage.diamondStorage();
//Check if trait boosts from consumables are still valid
int256 boostDecay = int256((block.timestamp - s.aavegotchis[_tokenId].lastTemporaryBoost) / 24 hours);
for (uint256 i; i < NUMERIC_TRAITS_NUM; i++) {
int256 number = s.aavegotchis[_tokenId].numericTraits[i];
int256 boost = s.aavegotchis[_tokenId].temporaryTraitBoosts[i];
if (boost > 0 && boost > boostDecay) {
number += boost - boostDecay;
} else if ((boost * -1) > boostDecay) {
number += boost + boostDecay;
}
numericTraits_[i] = int16(number);
}
}
function kinship(uint256 _tokenId) internal view returns (uint256 score_) {
AppStorage storage s = LibAppStorage.diamondStorage();
Aavegotchi storage aavegotchi = s.aavegotchis[_tokenId];
uint256 lastInteracted = aavegotchi.lastInteracted;
uint256 interactionCount = aavegotchi.interactionCount;
uint256 interval = block.timestamp - lastInteracted;
uint256 daysSinceInteraction = interval / 24 hours;
if (interactionCount > daysSinceInteraction) {
score_ = interactionCount - daysSinceInteraction;
}
}
function xpUntilNextLevel(uint256 _experience) internal pure returns (uint256 requiredXp_) {
uint256 currentLevel = aavegotchiLevel(_experience);
requiredXp_ = ((currentLevel**2) * 50) - _experience;
}
function aavegotchiLevel(uint256 _experience) internal pure returns (uint256 level_) {
if (_experience > 490050) {
return 99;
}
level_ = (sqrt(2 * _experience) / 10);
return level_ + 1;
}
function interact(uint256 _tokenId) internal returns (bool) {
AppStorage storage s = LibAppStorage.diamondStorage();
uint256 lastInteracted = s.aavegotchis[_tokenId].lastInteracted;
// if interacted less than 12 hours ago
if (block.timestamp < lastInteracted + 12 hours) {
return false;
}
uint256 interactionCount = s.aavegotchis[_tokenId].interactionCount;
uint256 interval = block.timestamp - lastInteracted;
uint256 daysSinceInteraction = interval / 1 days;
uint256 l_kinship;
if (interactionCount > daysSinceInteraction) {
l_kinship = interactionCount - daysSinceInteraction;
}
uint256 hateBonus;
if (l_kinship < 40) {
hateBonus = 2;
}
l_kinship += 1 + hateBonus;
s.aavegotchis[_tokenId].interactionCount = l_kinship;
s.aavegotchis[_tokenId].lastInteracted = uint40(block.timestamp);
emit AavegotchiInteract(_tokenId, l_kinship);
return true;
}
//Calculates the base rarity score, including collateral modifier
function baseRarityScore(int16[NUMERIC_TRAITS_NUM] memory _numericTraits) internal pure returns (uint256 _rarityScore) {
for (uint256 i; i < NUMERIC_TRAITS_NUM; i++) {
int256 number = _numericTraits[i];
if (number >= 50) {
_rarityScore += uint256(number) + 1;
} else {
_rarityScore += uint256(int256(100) - number);
}
}
}
// Need to ensure there is no overflow of _ghst
function purchase(address _from, uint256 _ghst) internal {
AppStorage storage s = LibAppStorage.diamondStorage();
//33% to burn address
uint256 burnShare = (_ghst * 33) / 100;
//17% to Pixelcraft wallet
uint256 companyShare = (_ghst * 17) / 100;
//40% to rarity farming rewards
uint256 rarityFarmShare = (_ghst * 2) / 5;
//10% to DAO
uint256 daoShare = (_ghst - burnShare - companyShare - rarityFarmShare);
// Using 0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF as burn address.
// GHST token contract does not allow transferring to address(0) address: https://etherscan.io/address/0x3F382DbD960E3a9bbCeaE22651E88158d2791550#code
address ghstContract = s.ghstContract;
LibERC20.transferFrom(ghstContract, _from, address(0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF), burnShare);
LibERC20.transferFrom(ghstContract, _from, s.pixelCraft, companyShare);
LibERC20.transferFrom(ghstContract, _from, s.rarityFarming, rarityFarmShare);
LibERC20.transferFrom(ghstContract, _from, s.dao, daoShare);
}
function sqrt(uint256 x) internal pure returns (uint256 y) {
uint256 z = (x + 1) / 2;
y = x;
while (z < y) {
y = z;
z = (x / z + z) / 2;
}
}
function validateAndLowerName(string memory _name) internal pure returns (string memory) {
bytes memory name = abi.encodePacked(_name);
uint256 len = name.length;
require(len != 0, "LibAavegotchi: name can't be 0 chars");
require(len < 26, "LibAavegotchi: name can't be greater than 25 characters");
uint256 char = uint256(uint8(name[0]));
require(char != 32, "LibAavegotchi: first char of name can't be a space");
char = uint256(uint8(name[len - 1]));
require(char != 32, "LibAavegotchi: last char of name can't be a space");
for (uint256 i; i < len; i++) {
char = uint256(uint8(name[i]));
require(char > 31 && char < 127, "LibAavegotchi: invalid character in Aavegotchi name.");
if (char < 91 && char > 64) {
name[i] = bytes1(uint8(char + 32));
}
}
return string(name);
}
// function addTokenToUser(address _to, uint256 _tokenId) internal {}
// function removeTokenFromUser(address _from, uint256 _tokenId) internal {}
function transfer(
address _from,
address _to,
uint256 _tokenId
) internal {
AppStorage storage s = LibAppStorage.diamondStorage();
// remove
uint256 index = s.ownerTokenIdIndexes[_from][_tokenId];
uint256 lastIndex = s.ownerTokenIds[_from].length - 1;
if (index != lastIndex) {
uint32 lastTokenId = s.ownerTokenIds[_from][lastIndex];
s.ownerTokenIds[_from][index] = lastTokenId;
s.ownerTokenIdIndexes[_from][lastTokenId] = index;
}
s.ownerTokenIds[_from].pop();
delete s.ownerTokenIdIndexes[_from][_tokenId];
if (s.approved[_tokenId] != address(0)) {
delete s.approved[_tokenId];
emit LibERC721.Approval(_from, address(0), _tokenId);
}
// add
s.aavegotchis[_tokenId].owner = _to;
s.ownerTokenIdIndexes[_to][_tokenId] = s.ownerTokenIds[_to].length;
s.ownerTokenIds[_to].push(uint32(_tokenId));
emit LibERC721.Transfer(_from, _to, _tokenId);
}
}