Skip to content

Instantly share code, notes, and snippets.

@sekrystal
Created March 2, 2023 18:40
Show Gist options
  • Save sekrystal/ec151c91240f568c79d06cae6a760652 to your computer and use it in GitHub Desktop.
Save sekrystal/ec151c91240f568c79d06cae6a760652 to your computer and use it in GitHub Desktop.
Defi Lending Concepts
pragma solidity ^0.8.13;
function computeExchangeRate(AssetCache memory assetCache) private pure returns (uint) {
uint totalAssets = assetCache.poolSize + (assetCache.totalBorrows / INTERNAL_DEBT_PRECISION);
if (totalAssets == 0 || assetCache.totalBalances == 0) return 1e18;
return totalAssets * 1e18 / assetCache.totalBalances;
}
function underlyingAmountToBalance(AssetCache memory assetCache, uint amount) internal pure returns (uint) {
uint exchangeRate = computeExchangeRate(assetCache);
return amount * 1e18 / exchangeRate;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment