@gearbox-protocol/periphery-v3 1.7.0-next.93 → 1.7.0-next.94
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// Gearbox Protocol. Generalized leverage for DeFi protocols
|
|
3
|
+
// (c) Gearbox Foundation, 2025.
|
|
4
|
+
pragma solidity ^0.8.23;
|
|
5
|
+
|
|
6
|
+
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
|
7
|
+
import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";
|
|
8
|
+
|
|
9
|
+
import {IAddressProvider} from "@gearbox-protocol/permissionless/contracts/interfaces/IAddressProvider.sol";
|
|
10
|
+
import {IMarketConfigurator} from "@gearbox-protocol/permissionless/contracts/interfaces/IMarketConfigurator.sol";
|
|
11
|
+
import {IContractsRegister} from "@gearbox-protocol/permissionless/contracts/interfaces/IContractsRegister.sol";
|
|
12
|
+
import {IMarketConfiguratorFactory} from
|
|
13
|
+
"@gearbox-protocol/permissionless/contracts/interfaces/IMarketConfiguratorFactory.sol";
|
|
14
|
+
import {
|
|
15
|
+
AP_MARKET_CONFIGURATOR_FACTORY,
|
|
16
|
+
NO_VERSION_CONTROL
|
|
17
|
+
} from "@gearbox-protocol/permissionless/contracts/libraries/ContractLiterals.sol";
|
|
18
|
+
|
|
19
|
+
import {ICreditAccountV3} from "@gearbox-protocol/core-v3/contracts/interfaces/ICreditAccountV3.sol";
|
|
20
|
+
import {ICreditManagerV3} from "@gearbox-protocol/core-v3/contracts/interfaces/ICreditManagerV3.sol";
|
|
21
|
+
import {IPoolV3} from "@gearbox-protocol/core-v3/contracts/interfaces/IPoolV3.sol";
|
|
22
|
+
|
|
23
|
+
import {AP_DEFILLAMA_COMPRESSOR} from "../libraries/Literals.sol";
|
|
24
|
+
|
|
25
|
+
import {DefillamaCreditAccountData, DefillamaTokenInfo, DefillamaPoolData} from "../types/Defillama.sol";
|
|
26
|
+
import {IDefillamaCompressor} from "../interfaces/IDefillamaCompressor.sol";
|
|
27
|
+
|
|
28
|
+
/// @title Credit account compressor
|
|
29
|
+
/// @notice Allows to fetch data on all credit accounts matching certain criteria in an efficient manner
|
|
30
|
+
/// @dev The contract is not gas optimized and is thus not recommended for on-chain use
|
|
31
|
+
/// @dev Querying functions try to process as many accounts as possible and stop when they get close to gas limit
|
|
32
|
+
contract DefillamaCompressor is IDefillamaCompressor {
|
|
33
|
+
struct Pool {
|
|
34
|
+
address addr;
|
|
35
|
+
address configurator;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/// @notice Contract version
|
|
39
|
+
uint256 public constant override version = 3_10;
|
|
40
|
+
|
|
41
|
+
/// @notice Contract type
|
|
42
|
+
bytes32 public constant override contractType = AP_DEFILLAMA_COMPRESSOR;
|
|
43
|
+
|
|
44
|
+
address public immutable addressProvider;
|
|
45
|
+
|
|
46
|
+
/// @notice Constructor
|
|
47
|
+
/// @param addressProvider_ Address provider contract address
|
|
48
|
+
constructor(address addressProvider_) {
|
|
49
|
+
addressProvider = addressProvider_;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function getPools(address[] memory configurators) external view returns (DefillamaPoolData[] memory pools) {
|
|
53
|
+
return _getPools(configurators);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function getPools() external view returns (DefillamaPoolData[] memory pools) {
|
|
57
|
+
address mcFactory =
|
|
58
|
+
IAddressProvider(addressProvider).getAddressOrRevert(AP_MARKET_CONFIGURATOR_FACTORY, NO_VERSION_CONTROL);
|
|
59
|
+
address[] memory configurators = IMarketConfiguratorFactory(mcFactory).getMarketConfigurators();
|
|
60
|
+
return _getPools(configurators);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function getCreditManagers(address[] memory configurators)
|
|
64
|
+
external
|
|
65
|
+
view
|
|
66
|
+
returns (address[] memory creditManagers)
|
|
67
|
+
{
|
|
68
|
+
return _getCreditManagers(configurators);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function getCreditManagers() external view returns (address[] memory creditManagers) {
|
|
72
|
+
address mcFactory =
|
|
73
|
+
IAddressProvider(addressProvider).getAddressOrRevert(AP_MARKET_CONFIGURATOR_FACTORY, NO_VERSION_CONTROL);
|
|
74
|
+
address[] memory configurators = IMarketConfiguratorFactory(mcFactory).getMarketConfigurators();
|
|
75
|
+
return _getCreditManagers(configurators);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/// @notice Returns data for credit accounts in a given `creditManager`
|
|
79
|
+
function getCreditAccounts(address creditManager)
|
|
80
|
+
external
|
|
81
|
+
view
|
|
82
|
+
returns (DefillamaCreditAccountData[] memory data)
|
|
83
|
+
{
|
|
84
|
+
return _getCreditAccounts(creditManager, 0, type(uint256).max);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/// @dev Same as above but with limit and offset
|
|
88
|
+
function getCreditAccounts(address creditManager, uint256 offset, uint256 limit)
|
|
89
|
+
external
|
|
90
|
+
view
|
|
91
|
+
returns (DefillamaCreditAccountData[] memory data)
|
|
92
|
+
{
|
|
93
|
+
return _getCreditAccounts(creditManager, offset, limit);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// --------- //
|
|
97
|
+
// INTERNALS //
|
|
98
|
+
// --------- //
|
|
99
|
+
|
|
100
|
+
function _getCreditAccounts(address creditManager, uint256 offset, uint256 limit)
|
|
101
|
+
internal
|
|
102
|
+
view
|
|
103
|
+
returns (DefillamaCreditAccountData[] memory data)
|
|
104
|
+
{
|
|
105
|
+
uint256 len = ICreditManagerV3(creditManager).creditAccountsLen();
|
|
106
|
+
|
|
107
|
+
if (offset >= len) {
|
|
108
|
+
return new DefillamaCreditAccountData[](0);
|
|
109
|
+
}
|
|
110
|
+
address[] memory creditAccounts =
|
|
111
|
+
ICreditManagerV3(creditManager).creditAccounts(offset, Math.min(len - offset, limit));
|
|
112
|
+
|
|
113
|
+
data = new DefillamaCreditAccountData[](creditAccounts.length);
|
|
114
|
+
for (uint256 i; i < creditAccounts.length; ++i) {
|
|
115
|
+
data[i] = _getCreditAccountData(creditAccounts[i], creditManager);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/// @dev Data loading implementation
|
|
120
|
+
function _getCreditAccountData(address creditAccount, address creditManager)
|
|
121
|
+
internal
|
|
122
|
+
view
|
|
123
|
+
returns (DefillamaCreditAccountData memory data)
|
|
124
|
+
{
|
|
125
|
+
data.creditAccount = creditAccount;
|
|
126
|
+
|
|
127
|
+
(uint256 debt,,,, uint256 enabledTokensMask,,,) =
|
|
128
|
+
ICreditManagerV3(creditManager).creditAccountInfo(creditAccount);
|
|
129
|
+
data.debt = debt;
|
|
130
|
+
// ignore tokens on zero-debt accounts
|
|
131
|
+
if (debt == 0) {
|
|
132
|
+
return data;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
uint256 maxTokens = ICreditManagerV3(creditManager).collateralTokensCount();
|
|
136
|
+
|
|
137
|
+
// the function is called for every account, so allocating an array of size `maxTokens` and trimming it
|
|
138
|
+
// might cause issues with memory expansion and we must count the precise number of tokens in advance
|
|
139
|
+
uint256 numTokens;
|
|
140
|
+
uint256 returnedTokensMask;
|
|
141
|
+
for (uint256 k; k < maxTokens; ++k) {
|
|
142
|
+
uint256 mask = 1 << k;
|
|
143
|
+
if (enabledTokensMask & mask == 0) {
|
|
144
|
+
address token = ICreditManagerV3(creditManager).getTokenByMask(mask);
|
|
145
|
+
try IERC20(token).balanceOf(creditAccount) returns (uint256 balance) {
|
|
146
|
+
if (balance <= 1) continue;
|
|
147
|
+
} catch {
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
++numTokens;
|
|
152
|
+
returnedTokensMask |= mask;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
data.tokens = new DefillamaTokenInfo[](numTokens);
|
|
156
|
+
uint256 i;
|
|
157
|
+
while (returnedTokensMask != 0) {
|
|
158
|
+
uint256 mask = returnedTokensMask & uint256(-int256(returnedTokensMask));
|
|
159
|
+
address token = ICreditManagerV3(creditManager).getTokenByMask(mask);
|
|
160
|
+
data.tokens[i].token = token;
|
|
161
|
+
|
|
162
|
+
try IERC20(token).balanceOf(creditAccount) returns (uint256 balance) {
|
|
163
|
+
data.tokens[i].balance = balance;
|
|
164
|
+
} catch {}
|
|
165
|
+
|
|
166
|
+
returnedTokensMask ^= mask;
|
|
167
|
+
++i;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function _getPools(address[] memory configurators) internal view returns (DefillamaPoolData[] memory pools) {
|
|
172
|
+
Pool[] memory _pools = _getPoolsInternal(configurators);
|
|
173
|
+
pools = new DefillamaPoolData[](_pools.length);
|
|
174
|
+
for (uint256 i; i < _pools.length; ++i) {
|
|
175
|
+
Pool memory pool = _pools[i];
|
|
176
|
+
pools[i] = DefillamaPoolData({
|
|
177
|
+
pool: pool.addr,
|
|
178
|
+
underlying: IPoolV3(pool.addr).asset(),
|
|
179
|
+
totalBorrowed: IPoolV3(pool.addr).totalBorrowed(),
|
|
180
|
+
availableLiquidity: IPoolV3(pool.addr).availableLiquidity()
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function _getCreditManagers(address[] memory configurators)
|
|
186
|
+
internal
|
|
187
|
+
view
|
|
188
|
+
returns (address[] memory creditManagers)
|
|
189
|
+
{
|
|
190
|
+
Pool[] memory pools = _getPoolsInternal(configurators);
|
|
191
|
+
|
|
192
|
+
// rough estimate of maximum number of credit managers
|
|
193
|
+
uint256 max;
|
|
194
|
+
for (uint256 i; i < pools.length; ++i) {
|
|
195
|
+
address cr = IMarketConfigurator(pools[i].configurator).contractsRegister();
|
|
196
|
+
max += IContractsRegister(cr).getCreditManagers(pools[i].addr).length;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// allocate the array with maximum potentially needed size (total number of credit managers can be assumed
|
|
200
|
+
// to be relatively small and the function is only called once, so memory expansion cost is not an issue)
|
|
201
|
+
creditManagers = new address[](max);
|
|
202
|
+
uint256 num;
|
|
203
|
+
for (uint256 i; i < pools.length; ++i) {
|
|
204
|
+
address cr = IMarketConfigurator(pools[i].configurator).contractsRegister();
|
|
205
|
+
address[] memory managers = IContractsRegister(cr).getCreditManagers(pools[i].addr);
|
|
206
|
+
for (uint256 j; j < managers.length; ++j) {
|
|
207
|
+
creditManagers[num++] = managers[j];
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
// trim the array to its actual size
|
|
211
|
+
assembly {
|
|
212
|
+
mstore(creditManagers, num)
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function _getPoolsInternal(address[] memory configurators) internal view returns (Pool[] memory pools) {
|
|
217
|
+
uint256 numPools;
|
|
218
|
+
for (uint256 i; i < configurators.length; ++i) {
|
|
219
|
+
address contractsRegister = IMarketConfigurator(configurators[i]).contractsRegister();
|
|
220
|
+
numPools += IContractsRegister(contractsRegister).getPools().length;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
pools = new Pool[](numPools);
|
|
224
|
+
uint256 num;
|
|
225
|
+
|
|
226
|
+
for (uint256 i; i < configurators.length; ++i) {
|
|
227
|
+
address contractsRegister = IMarketConfigurator(configurators[i]).contractsRegister();
|
|
228
|
+
address[] memory poolsMC = IContractsRegister(contractsRegister).getPools();
|
|
229
|
+
for (uint256 j; j < poolsMC.length; ++j) {
|
|
230
|
+
address pool = poolsMC[j];
|
|
231
|
+
|
|
232
|
+
pools[num++] = Pool({addr: pool, configurator: configurators[i]});
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// Gearbox Protocol. Generalized leverage for DeFi protocols
|
|
3
|
+
// (c) Gearbox Foundation, 2025.
|
|
4
|
+
pragma solidity ^0.8.23;
|
|
5
|
+
|
|
6
|
+
import {IVersion} from "@gearbox-protocol/core-v3/contracts/interfaces/base/IVersion.sol";
|
|
7
|
+
import {DefillamaCreditAccountData, DefillamaPoolData} from "../types/Defillama.sol";
|
|
8
|
+
|
|
9
|
+
/// @title Compressor contract for Defillama
|
|
10
|
+
interface IDefillamaCompressor is IVersion {
|
|
11
|
+
/// @notice Returns data for pools in given configurators
|
|
12
|
+
function getPools(address[] memory configurators) external view returns (DefillamaPoolData[] memory pools);
|
|
13
|
+
|
|
14
|
+
/// @notice Returns data for pools in all configurators found in market configurator factory
|
|
15
|
+
function getPools() external view returns (DefillamaPoolData[] memory pools);
|
|
16
|
+
|
|
17
|
+
/// @notice Returns credit managers in given configurators
|
|
18
|
+
function getCreditManagers(address[] memory configurators)
|
|
19
|
+
external
|
|
20
|
+
view
|
|
21
|
+
returns (address[] memory creditManagers);
|
|
22
|
+
|
|
23
|
+
/// @notice Returns credit managers in all configurators found in market configurator factory
|
|
24
|
+
function getCreditManagers() external view returns (address[] memory creditManagers);
|
|
25
|
+
|
|
26
|
+
/// @notice Returns data for credit accounts in a given `creditManager`
|
|
27
|
+
function getCreditAccounts(address creditManager)
|
|
28
|
+
external
|
|
29
|
+
view
|
|
30
|
+
returns (DefillamaCreditAccountData[] memory data);
|
|
31
|
+
|
|
32
|
+
/// @dev Same as above but with offset and limit
|
|
33
|
+
function getCreditAccounts(address creditManager, uint256 offset, uint256 limit)
|
|
34
|
+
external
|
|
35
|
+
view
|
|
36
|
+
returns (DefillamaCreditAccountData[] memory data);
|
|
37
|
+
}
|
|
@@ -11,3 +11,4 @@ bytes32 constant AP_PERIPHERY_COMPRESSOR = "GLOBAL::PERIPHERY_COMPRESSOR";
|
|
|
11
11
|
bytes32 constant AP_PRICE_FEED_COMPRESSOR = "GLOBAL::PRICE_FEED_COMPRESSOR";
|
|
12
12
|
bytes32 constant AP_REWARDS_COMPRESSOR = "GLOBAL::REWARDS_COMPRESSOR";
|
|
13
13
|
bytes32 constant AP_TOKEN_COMPRESSOR = "GLOBAL::TOKEN_COMPRESSOR";
|
|
14
|
+
bytes32 constant AP_DEFILLAMA_COMPRESSOR = "GLOBAL::DEFILLAMA_COMPRESSOR";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// Gearbox Protocol. Generalized leverage for DeFi protocols
|
|
3
|
+
// (c) Gearbox Foundation, 2025.
|
|
4
|
+
pragma solidity ^0.8.23;
|
|
5
|
+
|
|
6
|
+
struct DefillamaCreditAccountData {
|
|
7
|
+
address creditAccount;
|
|
8
|
+
uint256 debt;
|
|
9
|
+
DefillamaTokenInfo[] tokens;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
struct DefillamaTokenInfo {
|
|
13
|
+
address token;
|
|
14
|
+
uint256 balance;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
struct DefillamaPoolData {
|
|
18
|
+
address pool;
|
|
19
|
+
address underlying;
|
|
20
|
+
uint256 availableLiquidity;
|
|
21
|
+
uint256 totalBorrowed;
|
|
22
|
+
}
|
package/package.json
CHANGED