Created
March 20, 2023 18:38
-
-
Save sekrystal/9a744d3805ceada74746214374cbd547 to your computer and use it in GitHub Desktop.
Defi Lending Concepts 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.8.13; | |
struct AccountLiquidityLocalVars { | |
uint sumCollateral; | |
uint sumBorrowPlusEffects; | |
uint cTokenBalance; | |
uint borrowBalance; | |
uint exchangeRateMantissa; | |
uint oraclePriceMantissa; | |
Exp collateralFactor; | |
Exp exchangeRate; | |
Exp oraclePrice; | |
Exp tokensToDenom; | |
} | |
// ... | |
function getHypotheticalAccountLiquidityInternal( | |
address account, | |
CToken cTokenModify, | |
uint redeemTokens, | |
uint borrowAmount) internal view returns (Error, uint, uint) { | |
AccountLiquidityLocalVars memory vars; | |
uint oErr; | |
CToken[] memory assets = accountAssets[account]; | |
for (uint i = 0; i < assets.length; i++) { | |
CToken asset = assets[i]; | |
(oErr, vars.cTokenBalance, vars.borrowBalance, vars.exchangeRateMantissa) = asset.getAccountSnapshot(account); | |
if (oErr != 0) { | |
return (Error.SNAPSHOT_ERROR, 0, 0); | |
} | |
vars.collateralFactor = Exp({mantissa: markets[address(asset)].collateralFactorMantissa}); | |
vars.exchangeRate = Exp({mantissa: vars.exchangeRateMantissa}); | |
vars.oraclePriceMantissa = oracle.getUnderlyingPrice(asset); | |
if (vars.oraclePriceMantissa == 0) { | |
return (Error.PRICE_ERROR, 0, 0); | |
} | |
vars.oraclePrice = Exp({mantissa: vars.oraclePriceMantissa}); | |
vars.tokensToDenom = mul_(mul_(vars.collateralFactor, vars.exchangeRate), vars.oraclePrice); | |
vars.sumCollateral = mul_ScalarTruncateAddUInt(vars.tokensToDenom, vars.cTokenBalance, vars.sumCollateral); | |
vars.sumBorrowPlusEffects = mul_ScalarTruncateAddUInt(vars.oraclePrice, vars.borrowBalance, vars.sumBorrowPlusEffects); | |
if (asset == cTokenModify) { | |
vars.sumBorrowPlusEffects = mul_ScalarTruncateAddUInt(vars.tokensToDenom, redeemTokens, vars.sumBorrowPlusEffects); | |
vars.sumBorrowPlusEffects = mul_ScalarTruncateAddUInt(vars.oraclePrice, borrowAmount, vars.sumBorrowPlusEffects); | |
} | |
} | |
if (vars.sumCollateral > vars.sumBorrowPlusEffects) { | |
return (Error.NO_ERROR, vars.sumCollateral - vars.sumBorrowPlusEffects, 0); | |
} else { | |
return (Error.NO_ERROR, 0, vars.sumBorrowPlusEffects - vars.sumCollateral); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment