Skip to content

Commit e774cc9

Browse files
committed
getHealthScores
1 parent 7f5eae8 commit e774cc9

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

src/MaglevLens.sol

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
pragma solidity ^0.8.13;
33

44
import {IEVC} from "evc/interfaces/IEthereumVaultConnector.sol";
5-
import {IEVault, IERC20} from "evk/EVault/IEVault.sol";
5+
import {IEVault, IERC20, IRiskManager} from "evk/EVault/IEVault.sol";
66
import {RPow} from "evk/EVault/shared/lib/RPow.sol";
77
import {IEulerSwapRegistry} from "euler-swap/interfaces/IEulerSwapRegistry.sol";
88
import {IEulerSwap} from "euler-swap/interfaces/IEulerSwap.sol";
99

1010
contract MaglevLens {
11-
// Packed: underlying asset (address), decimals (uint8), symbol (variable)
11+
//// Vault queries
1212

13+
/// @dev Packed output: underlying asset (address), decimals (uint8), symbol (variable)
1314
function vaultsStatic(address[] calldata vaults) external view returns (bytes[] memory output) {
1415
unchecked {
1516
output = new bytes[](vaults.length);
@@ -187,6 +188,8 @@ contract MaglevLens {
187188
}
188189
}
189190

191+
//// EulerSwap queries
192+
190193
struct EulerSwapData {
191194
address addr;
192195
IEulerSwap.StaticParams sParams;
@@ -266,4 +269,48 @@ contract MaglevLens {
266269
require(reserve0 >= reserve0Min && reserve0 <= reserve0Max, AssertEulerSwapReservesFailure());
267270
require(reserve1 >= reserve1Min && reserve1 <= reserve1Max, AssertEulerSwapReservesFailure());
268271
}
272+
273+
//// Liquidity Queries
274+
275+
error MultipleControllers();
276+
277+
/// Packed health score: controller (address), health (uint32), error (bool)
278+
/// @dev Health scores are 1e6 scale.
279+
function getHealthScores(address evc, address[] calldata addrs) external view returns (uint256[] memory healths) {
280+
healths = new uint256[](addrs.length);
281+
282+
for (uint256 i = 0; i < addrs.length; ++i) {
283+
address addr = addrs[i];
284+
285+
address controller;
286+
uint32 health;
287+
uint8 errorFlag;
288+
289+
{
290+
address[] memory controllers = IEVC(evc).getControllers(addr);
291+
require(controllers.length < 2, MultipleControllers());
292+
if (controllers.length == 1) controller = controllers[0];
293+
}
294+
295+
if (controller != address(0)) {
296+
(bool success, bytes memory data) = controller.staticcall(abi.encodeCall(IRiskManager.accountLiquidity, (addr, true)));
297+
298+
if (success) {
299+
(uint256 collateralValue, uint256 liabilityValue) = abi.decode(data, (uint256, uint256));
300+
301+
if (liabilityValue == 0) {
302+
health = type(uint32).max;
303+
} else {
304+
uint256 h = 1e6 * collateralValue / liabilityValue;
305+
if (h > type(uint32).max) h = type(uint32).max;
306+
health = uint32(h);
307+
}
308+
} else {
309+
errorFlag = 1;
310+
}
311+
}
312+
313+
healths[i] = (uint160(controller) << 40) | (health << 32) | errorFlag;
314+
}
315+
}
269316
}

0 commit comments

Comments
 (0)