-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiTroveGetter.sol
120 lines (97 loc) · 3.59 KB
/
MultiTroveGetter.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
// SPDX-License-Identifier: MIT
pragma solidity 0.6.11;
pragma experimental ABIEncoderV2;
import "./TroveManager.sol";
import "./SortedTroves.sol";
/* Helper contract for grabbing Trove data for the front end. Not part of the core Liquity system. */
contract MultiTroveGetter {
struct CombinedTroveData {
address owner;
uint debt;
uint coll;
uint stake;
uint snapshotETH;
uint snapshotLUSDDebt;
}
TroveManager public troveManager; // XXX Troves missing from ITroveManager?
ISortedTroves public sortedTroves;
constructor(TroveManager _troveManager, ISortedTroves _sortedTroves) public {
troveManager = _troveManager;
sortedTroves = _sortedTroves;
}
function getMultipleSortedTroves(int _startIdx, uint _count)
external view returns (CombinedTroveData[] memory _troves)
{
uint startIdx;
bool descend;
if (_startIdx >= 0) {
startIdx = uint(_startIdx);
descend = true;
} else {
startIdx = uint(-(_startIdx + 1));
descend = false;
}
uint sortedTrovesSize = sortedTroves.getSize();
if (startIdx >= sortedTrovesSize) {
_troves = new CombinedTroveData[](0);
} else {
uint maxCount = sortedTrovesSize - startIdx;
if (_count > maxCount) {
_count = maxCount;
}
if (descend) {
_troves = _getMultipleSortedTrovesFromHead(startIdx, _count);
} else {
_troves = _getMultipleSortedTrovesFromTail(startIdx, _count);
}
}
}
function _getMultipleSortedTrovesFromHead(uint _startIdx, uint _count)
internal view returns (CombinedTroveData[] memory _troves)
{
address currentTroveowner = sortedTroves.getFirst();
for (uint idx = 0; idx < _startIdx; ++idx) {
currentTroveowner = sortedTroves.getNext(currentTroveowner);
}
_troves = new CombinedTroveData[](_count);
for (uint 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].snapshotLUSDDebt
) = troveManager.rewardSnapshots(currentTroveowner);
currentTroveowner = sortedTroves.getNext(currentTroveowner);
}
}
function _getMultipleSortedTrovesFromTail(uint _startIdx, uint _count)
internal view returns (CombinedTroveData[] memory _troves)
{
address currentTroveowner = sortedTroves.getLast();
for (uint idx = 0; idx < _startIdx; ++idx) {
currentTroveowner = sortedTroves.getPrev(currentTroveowner);
}
_troves = new CombinedTroveData[](_count);
for (uint 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].snapshotLUSDDebt
) = troveManager.rewardSnapshots(currentTroveowner);
currentTroveowner = sortedTroves.getPrev(currentTroveowner);
}
}
}