Skip to content

Instantly share code, notes, and snippets.

View sekrystal's full-sized avatar

Sam Krystal sekrystal

View GitHub Profile
@sekrystal
sekrystal / aavehf.sol
Created March 20, 2023 18:42
Defi Lending Concepts 2
pragma solidity ^0.8.13;
vars.healthFactor = calculateHealthFactorFromBalances(
vars.totalCollateralInETH,
vars.totalDebtInETH,
vars.avgLiquidationThreshold
);
// ...
@sekrystal
sekrystal / bark.sol
Created March 20, 2023 18:41
Defi Lending Concepts 2
pragma solidity ^0.8.13;
function bark(bytes32 ilk, address urn, address kpr) external returns (uint256 id) {
require(live == 1, "Dog/not-live");
(uint256 ink, uint256 art) = vat.urns(ilk, urn);
Ilk memory milk = ilks[ilk];
uint256 dart;
uint256 rate;
uint256 dust;
@sekrystal
sekrystal / bite.sol
Created March 20, 2023 18:40
Defi Lending Concepts 2
pragma solidity ^0.8.13;
function bite(bytes32 ilk, address urn) external returns (uint id) {
(, uint rate, uint spot) = vat.ilks(ilk);
(uint ink, uint art) = vat.urns(ilk, urn);
require(live == 1, "Cat/not-live");
require(spot > 0 && mul(ink, spot) < mul(art, rate), "Cat/not-unsafe");
@sekrystal
sekrystal / collateral.sol
Created March 20, 2023 18:39
Defi Lending Concepts 2
pragma solidity ^0.8.13;
uint internal constant collateralFactorMaxMantissa = 0.9e18; // 0.9
@sekrystal
sekrystal / acctliquidity.sol
Created March 20, 2023 18:38
Defi Lending Concepts 2
pragma solidity ^0.8.13;
struct AccountLiquidityLocalVars {
uint sumCollateral;
uint sumBorrowPlusEffects;
uint cTokenBalance;
uint borrowBalance;
uint exchangeRateMantissa;
uint oraclePriceMantissa;
Exp collateralFactor;
@sekrystal
sekrystal / etokenMintTransfer.sol
Created March 2, 2023 18:40
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);
@sekrystal
sekrystal / etoken.sol
Last active March 8, 2023 19:48
Defi Lending Concepts 1
pragma solidity ^0.8.13;
function deposit(uint subAccountId, uint amount) external nonReentrant {
(address underlying, AssetStorage storage assetStorage, address proxyAddr, address msgSender) = CALLER();
address account = getSubAccount(msgSender, subAccountId);
updateAverageLiquidity(account);
emit RequestDeposit(account, amount);
AssetCache memory assetCache = loadAssetCache(underlying, assetStorage);
@sekrystal
sekrystal / returnMintCtoken.sol
Last active March 8, 2023 19:47
Defi Lending Concepts 1
pragma solidity ^0.8.13;
function mintFresh(address minter, uint mintAmount) internal returns (uint, uint) {
uint allowed = comptroller.mintAllowed(address(this), minter, mintAmount);
if (allowed != 0) {
return (failOpaque(Error.COMPTROLLER_REJECTION, FailureInfo.MINT_COMPTROLLER_REJECTION, allowed), 0);
}
if (accrualBlockNumber != getBlockNumber()) {
return (fail(Error.MARKET_NOT_FRESH, FailureInfo.MINT_FRESHNESS_CHECK), 0);
@sekrystal
sekrystal / ctokenexchange.sol
Last active March 8, 2023 19:46
Defi Lending Concepts 1
pragma solidity ^0.8.13;
function exchangeRateStoredInternal() internal view returns (MathError, uint) {
uint _totalSupply = totalSupply;
if (_totalSupply == 0) {
return (MathError.NO_ERROR, initialExchangeRateMantissa);
} else {
uint totalCash = getCashPrior();
uint cashPlusBorrowsMinusReserves;
Exp memory exchangeRate;
pragma solidity ^0.8.13;
function getOverallBorrowRateInternal(
uint256 _totalBorrowsStable,
uint256 _totalBorrowsVariable,
uint256 _currentVariableBorrowRate,
uint256 _currentAverageStableBorrowRate
) internal pure returns (uint256) {
uint256 totalBorrows = _totalBorrowsStable.add(_totalBorrowsVariable);