Iโve been building an over-collateralized lending protocol in the
@buidlguidl SpeedrunEthereum challenge (ETH collateral, CORN debt).
Hereโs the reasoning behind my `borrowCorn()` function ๐
The key idea:
โข You can only borrow if your position stays above the minimum collateral ratio (120% in my case).
So:
โข collateralValue / debt >= 1.20
Implementation choices in the function:
1) Reject borrowAmount == 0
No pointless state writes.
2) Update debt FIRST:
s_userBorrowed[msg.sender] += borrowAmount;
3) Validate AFTER increasing debt:
_validatePosition(msg.sender);
4) Only if itโs safe, transfer CORN to the user.