-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMultiTroveGetter.sol
More file actions
125 lines (103 loc) · 3.66 KB
/
Copy pathMultiTroveGetter.sol
File metadata and controls
125 lines (103 loc) · 3.66 KB
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
// SPDX-License-Identifier: MIT
pragma solidity 0.6.11;
pragma experimental ABIEncoderV2;
import "./TroveManager.sol";
import "./SortedTroves.sol";
import "./MultiTroveGetterStorage.sol";
/** Helper contract for grabbing Trove data for the front end. Not part of the core Zero system. */
contract MultiTroveGetter is MultiTroveGetterStorage {
struct CombinedTroveData {
address owner;
uint256 debt;
uint256 coll;
uint256 stake;
uint256 snapshotETH;
uint256 snapshotZUSDDebt;
}
function setAddresses(TroveManager _troveManager, ISortedTroves _sortedTroves)
public
onlyOwner
{
troveManager = _troveManager;
sortedTroves = _sortedTroves;
}
function getMultipleSortedTroves(int256 _startIdx, uint256 _count)
external
view
returns (CombinedTroveData[] memory _troves)
{
uint256 startIdx;
bool descend;
if (_startIdx >= 0) {
startIdx = uint256(_startIdx);
descend = true;
} else {
startIdx = uint256(-(_startIdx + 1));
descend = false;
}
uint256 sortedTrovesSize = sortedTroves.getSize();
if (startIdx >= sortedTrovesSize) {
_troves = new CombinedTroveData[](0);
} else {
uint256 maxCount = sortedTrovesSize - startIdx;
if (_count > maxCount) {
_count = maxCount;
}
if (descend) {
_troves = _getMultipleSortedTrovesFromHead(startIdx, _count);
} else {
_troves = _getMultipleSortedTrovesFromTail(startIdx, _count);
}
}
}
function _getMultipleSortedTrovesFromHead(uint256 _startIdx, uint256 _count)
internal
view
returns (CombinedTroveData[] memory _troves)
{
address currentTroveowner = sortedTroves.getFirst();
for (uint256 idx = 0; idx < _startIdx; ++idx) {
currentTroveowner = sortedTroves.getNext(currentTroveowner);
}
_troves = new CombinedTroveData[](_count);
for (uint256 idx = 0; idx < _count; ++idx) {
_troves[idx].owner = currentTroveowner;
(
_troves[idx].debt,
_troves[idx].coll,
_troves[idx].stake,
/* status */
/* arrayIndex */
,
) = troveManager.Troves(currentTroveowner);
(_troves[idx].snapshotETH, _troves[idx].snapshotZUSDDebt) = troveManager
.rewardSnapshots(currentTroveowner);
currentTroveowner = sortedTroves.getNext(currentTroveowner);
}
}
function _getMultipleSortedTrovesFromTail(uint256 _startIdx, uint256 _count)
internal
view
returns (CombinedTroveData[] memory _troves)
{
address currentTroveowner = sortedTroves.getLast();
for (uint256 idx = 0; idx < _startIdx; ++idx) {
currentTroveowner = sortedTroves.getPrev(currentTroveowner);
}
_troves = new CombinedTroveData[](_count);
for (uint256 idx = 0; idx < _count; ++idx) {
_troves[idx].owner = currentTroveowner;
(
_troves[idx].debt,
_troves[idx].coll,
_troves[idx].stake,
/* status */
/* arrayIndex */
,
) = troveManager.Troves(currentTroveowner);
(_troves[idx].snapshotETH, _troves[idx].snapshotZUSDDebt) = troveManager
.rewardSnapshots(currentTroveowner);
currentTroveowner = sortedTroves.getPrev(currentTroveowner);
}
}
}