@@ -39,7 +39,7 @@ contract JITpilot {
3939 uint256 currentLTV;
4040 uint256 swapFees;
4141 int256 netInterest;
42- uint256 depositValue;
42+ int256 depositValue;
4343 }
4444
4545 // LP data structure
@@ -156,7 +156,7 @@ contract JITpilot {
156156
157157 // Calculate current Yield
158158 uint256 currentYield = 0 ;
159- uint256 swapApy = currentData.swapFees / currentData.depositValue;
159+ uint256 swapApy = uint256 ( int256 ( currentData.swapFees) * 1e18 / currentData.depositValue) ;
160160 if (currentData.depositValue > 0 ) {
161161 if (currentData.netInterest >= 0 ) {
162162 currentYield = (uint256 (currentData.netInterest) * PRECISION) + swapApy;
@@ -287,37 +287,13 @@ contract JITpilot {
287287
288288 BlockData memory blockData;
289289
290- blockData.allowedLTV = 0 ;
291- blockData.currentLTV = 0 ;
292- blockData.swapFees = 0 ;
293- blockData.netInterest = 0 ;
294- blockData.depositValue = 0 ;
295-
296290 // uint256 swapFees = 0;
297291 uint256 supplyApyTotal = 0 ;
298292 uint256 borrowApyTotal = 0 ;
299293
300- // We only consider the EulerSwap vaults as collateral for this account, even if
301- // they have other enabled collaterals. This may differ from Euler's own UI, but it's
302- // more accurate for our purposes.
303- // uint256 collateralValue0 = getCollateralValue(lp, vault0);
304- // uint256 collateralValue1 = getCollateralValue(lp, vault1);
305- uint256 collateralValueTotal =
306- getCollateralValue (lp, eulerSwapData.params.vault0) + getCollateralValue (lp, eulerSwapData.params.vault1);
307-
308- // get supply APY data using the MaglevLens contract
309- IMaglevLens maglevLens = IMaglevLens (MaglevLens);
310- address [] memory collateralVaults = new address [](2 );
311- collateralVaults[0 ] = eulerSwapData.params.vault0;
312- collateralVaults[1 ] = eulerSwapData.params.vault1;
313- IMaglevLens.VaultGlobal[] memory collateralVaultsGlobal = maglevLens.vaultsGlobal (collateralVaults);
314- // packed2: shares (160), supply APY (48), borrow APY (48)
315- for (uint256 i; i < collateralVaultsGlobal.length ; ++ i) {
316- uint256 supplyApy = uint256 ((collateralVaultsGlobal[i].packed2 << (256 - 96 )) >> (256 - 48 ));
317- supplyApyTotal += supplyApy * getCollateralValue (lp, collateralVaults[i]) / collateralValueTotal;
318- console.log ("Supply APY for vault " , collateralVaults[i], "is " , supplyApy);
319- console.log ("Supply APY total is " , supplyApyTotal);
320- }
294+ (uint256 collateralValueTotal , uint256 debtValue ) = getDepositValue (lp);
295+ blockData.depositValue = int256 (collateralValueTotal) - int256 (debtValue);
296+ supplyApyTotal = getSupplyApy (lp);
321297
322298 // get the currently enabled controller vault (i.e. the debt vault)
323299 address controllerVault = getCurrentControllerVault (lp);
@@ -327,41 +303,42 @@ contract JITpilot {
327303 console.log ("doesn't have controller vault: " , controllerVault);
328304 blockData.allowedLTV = 0 ;
329305 blockData.currentLTV = 0 ;
330- // If there is no debt, there is no looping or leverage
331- blockData.depositValue = collateralValueTotal;
306+ // If there is no debt, there is no looping or leverage, so interest is just supplyAPY
332307 blockData.netInterest = int256 (supplyApyTotal);
333308 } else {
334309 console.log ("has controller vault: " , controllerVault);
335310 // Figure out which vault is the collateralVault
336311 address collateralVault = (controllerVault == eulerSwapData.params.vault0)
337312 ? eulerSwapData.params.vault1
338313 : eulerSwapData.params.vault0;
339- uint256 debtValue = getDebtValue (lp, controllerVault);
340314 console.log ("debtValue: " , debtValue);
341315 blockData.allowedLTV = uint256 (IEVault (controllerVault).LTVLiquidation (collateralVault)) * 1e18 / 1e4 ;
342316 blockData.currentLTV = debtValue * 1e18 / collateralValueTotal;
343317 console.log ("currentLTV: " , blockData.currentLTV);
344318
345319 address [] memory controllerVaultArray = new address [](1 );
346320 controllerVaultArray[0 ] = controllerVault;
347- IMaglevLens.VaultGlobal[] memory controllerVaultsGlobal = maglevLens.vaultsGlobal (controllerVaultArray);
321+ IMaglevLens.VaultGlobal[] memory controllerVaultsGlobal =
322+ IMaglevLens (MaglevLens).vaultsGlobal (controllerVaultArray);
348323 // borrow APY is on the last 48 bits. Shift left then right to extract it.
349324 uint256 borrowApy = uint256 ((controllerVaultsGlobal[0 ].packed2 << (256 - 48 )) >> (256 - 48 ));
350325 borrowApyTotal = borrowApy;
351-
352- // The EVC already ensures that liabilityValue is less than collateralValueTotal, so this is safe
353- blockData.depositValue = collateralValueTotal - debtValue;
354326 if (supplyApyTotal * collateralValueTotal < borrowApyTotal * debtValue) {
355327 // avoid overflow
356- blockData.netInterest = - int256 ((borrowApyTotal * debtValue - supplyApyTotal * collateralValueTotal) / blockData.depositValue)
357- * 1e9 ;
328+ blockData.netInterest = - int256 (
329+ (borrowApyTotal * debtValue - supplyApyTotal * collateralValueTotal)
330+ / uint256 (blockData.depositValue)
331+ ) * 1e9 ;
358332 } else {
359333 blockData.netInterest = int256 (
360- (supplyApyTotal * collateralValueTotal - borrowApyTotal * debtValue) / blockData.depositValue
334+ (supplyApyTotal * collateralValueTotal - borrowApyTotal * debtValue)
335+ / uint256 (blockData.depositValue)
361336 ) * 1e9 ;
362337 }
363338 }
364339
340+ blockData.depositValue = int256 (collateralValueTotal) - int256 (debtValue);
341+
365342 // get LP's liquidity status in the controller vault, with regards to liquidation
366343 // (
367344 // address[] memory collateralVaults,
@@ -416,6 +393,8 @@ contract JITpilot {
416393 uint256 outLimit01;
417394 uint256 inLimit10;
418395 uint256 outLimit10;
396+ uint16 borrowLTV01;
397+ uint16 borrowLTV10;
419398 }
420399
421400 function getEulerSwapData (address poolAddr ) internal view returns (EulerSwapData memory output ) {
@@ -432,6 +411,9 @@ contract JITpilot {
432411 output.asset1 = asset1;
433412 (output.inLimit01, output.outLimit01) = pool.getLimits (asset0, asset1);
434413 (output.inLimit10, output.outLimit10) = pool.getLimits (asset1, asset0);
414+ // fetch borrow LTVs. These will be used to calculate reserves for rebalancing
415+ output.borrowLTV01 = IEVault (output.params.vault0).LTVBorrow (output.params.vault1);
416+ output.borrowLTV10 = IEVault (output.params.vault1).LTVBorrow (output.params.vault0);
435417 }
436418 /**
437419 * @dev Rebalance LP position (placeholder - to be implemented later)
@@ -578,4 +560,57 @@ contract JITpilot {
578560 function getData (address lp ) external view returns (BlockData memory ) {
579561 return fetchData (lp);
580562 }
563+
564+ /**
565+ * @dev Get the deposit value of an LP
566+ * @param lp LP address
567+ * @return collateralValueTotal Collateral value of the LP
568+ * @return debtValue Debt value of the LP
569+ */
570+ function getDepositValue (address lp )
571+ internal
572+ view
573+ virtual
574+ returns (uint256 collateralValueTotal , uint256 debtValue )
575+ {
576+ address poolAddr = IEulerSwapFactory (EulerSwapFactory).poolByEulerAccount (lp);
577+ IEulerSwap.Params memory eulerSwapParams = IEulerSwap (poolAddr).getParams ();
578+
579+ collateralValueTotal =
580+ getCollateralValue (lp, eulerSwapParams.vault0) + getCollateralValue (lp, eulerSwapParams.vault1);
581+ debtValue = getDebtValue (lp, eulerSwapParams.vault0) + getDebtValue (lp, eulerSwapParams.vault1);
582+
583+ return (collateralValueTotal, debtValue);
584+ }
585+
586+ /**
587+ * @dev Get the supply APY of an LP
588+ * @param lp LP address
589+ * @return supplyApyTotal Supply APY of the LP, weighted by collateral value
590+ */
591+ function getSupplyApy (address lp ) internal view virtual returns (uint256 supplyApyTotal ) {
592+ address poolAddr = IEulerSwapFactory (EulerSwapFactory).poolByEulerAccount (lp);
593+ IEulerSwap.Params memory eulerSwapParams = IEulerSwap (poolAddr).getParams ();
594+
595+ // get supply APY data using the MaglevLens contract
596+ IMaglevLens maglevLens = IMaglevLens (MaglevLens);
597+ address [] memory collateralVaults = new address [](2 );
598+ collateralVaults[0 ] = eulerSwapParams.vault0;
599+ collateralVaults[1 ] = eulerSwapParams.vault1;
600+ IMaglevLens.VaultGlobal[] memory collateralVaultsGlobal = maglevLens.vaultsGlobal (collateralVaults);
601+ // packed2: shares (160), supply APY (48), borrow APY (48)
602+ uint256 collateralValueTotal;
603+ for (uint256 i; i < collateralVaultsGlobal.length ; ++ i) {
604+ uint256 supplyApy = uint256 ((collateralVaultsGlobal[i].packed2 << (256 - 96 )) >> (256 - 48 ));
605+ uint256 collateralValue = getCollateralValue (lp, collateralVaults[i]);
606+ supplyApyTotal += supplyApy * collateralValue;
607+ collateralValueTotal += collateralValue;
608+ console.log ("Supply APY for vault " , collateralVaults[i], "is " , supplyApy);
609+ console.log ("Supply APY total is " , supplyApyTotal);
610+ }
611+
612+ supplyApyTotal = supplyApyTotal / collateralValueTotal;
613+
614+ return supplyApyTotal;
615+ }
581616}
0 commit comments