-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDegenRank.sol
executable file
·96 lines (88 loc) · 3.25 KB
/
DegenRank.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.17;
import {SingleRanking} from "src/lib/SingleRanking.sol";
import {BitMapsUpgradeable} from "@openzeppelin/contracts-upgradeable/utils/structs/BitMapsUpgradeable.sol";
library DegenRank {
using SingleRanking for SingleRanking.Data;
using BitMapsUpgradeable for BitMapsUpgradeable.BitMap;
function _enterScoreRank(
SingleRanking.Data storage _scoreRank,
SingleRanking.Data storage _tributeRank,
BitMapsUpgradeable.BitMap storage _isTopHundredScore,
mapping(uint256 => uint256) storage _oldStakeAmounts,
uint256 tokenId,
uint256 value
) external {
if (value == 0) {
return;
}
// only when length is larger than 100, remove
if (SingleRanking.length(_scoreRank) >= 100) {
uint256 minValue = _scoreRank.getNthValue(99);
// get the 100th value and compare, if new value is smaller, nothing happen
if (value <= minValue) {
return;
}
// remove the smallest in the score rank
uint256 tokenIdWithMinmalScore = _scoreRank.get(99, 1)[0];
_scoreRank.remove(tokenIdWithMinmalScore, minValue);
// also remove it from tvl rank
_isTopHundredScore.unset(tokenIdWithMinmalScore);
_exitTvlRank(
_tributeRank,
_oldStakeAmounts,
tokenIdWithMinmalScore
);
}
// add to score rank
_scoreRank.add(tokenId, value);
// can enter the tvl rank
_isTopHundredScore.set(tokenId);
// Enter as a very small value, just ensure it's not zero and pass check
// it doesn't matter too much as really stake has decimal with 18.
// General value woule be much larger than 1
_enterTvlRank(
_tributeRank,
_isTopHundredScore,
_oldStakeAmounts,
tokenId,
1
);
}
/**
* @dev set a new value in tree, only save top x largest value
* @param value new value enters in the tree
*/
function _enterTvlRank(
SingleRanking.Data storage _tributeRank,
BitMapsUpgradeable.BitMap storage _isTopHundredScore,
mapping(uint256 => uint256) storage _oldStakeAmounts,
uint256 tokenId,
uint256 value
) public {
// if it's not one hundred score, nothing happens
if (!_isTopHundredScore.get(tokenId)) {
return;
}
// remove old value from the rank, keep one token Id only one value
if (_oldStakeAmounts[tokenId] != 0) {
_tributeRank.remove(tokenId, _oldStakeAmounts[tokenId]);
}
_tributeRank.add(tokenId, value);
_oldStakeAmounts[tokenId] = value;
}
/**
* @dev if the tokenId's value is zero, it exits the ranking
* @param tokenId pool tokenId
*/
function _exitTvlRank(
SingleRanking.Data storage _tributeRank,
mapping(uint256 => uint256) storage _oldStakeAmounts,
uint256 tokenId
) internal {
if (_oldStakeAmounts[tokenId] != 0) {
_tributeRank.remove(tokenId, _oldStakeAmounts[tokenId]);
delete _oldStakeAmounts[tokenId];
}
}
}