Skip to content

Instantly share code, notes, and snippets.

@sekrystal
Last active March 8, 2023 19:48
Show Gist options
  • Save sekrystal/855e764fd9af8f10fb67653ec145abf5 to your computer and use it in GitHub Desktop.
Save sekrystal/855e764fd9af8f10fb67653ec145abf5 to your computer and use it in GitHub Desktop.
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);
if (amount == type(uint).max) {
amount = callBalanceOf(assetCache, msgSender);
}
amount = decodeExternalAmount(assetCache, amount);
uint amountTransferred = pullTokens(assetCache, msgSender, amount);
uint amountInternal;
unchecked {
assetCache.poolSize -= amountTransferred;
amountInternal = underlyingAmountToBalance(assetCache, amountTransferred);
assetCache.poolSize += amountTransferred;
}
increaseBalance(assetStorage, assetCache, proxyAddr, account, amountInternal);
if (assetStorage.users[account].owed != 0) checkLiquidity(account);
logAssetStatus(assetCache);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment