Withdraw
In order to withdraw some assets from the Protocol, User has to have some assets deposited.
User can make a request to the Withdraw method with the SRWA User Badge and the amount of assets that the User wants withdrawn.
Protocol will first verify if the withdraws for that particular pool are locked. If the withdrawing is allowed, next thing the Protocol will verify is if there is enough liquidity in the Pool for the withdraw. Protocol will then verify if the user has enough assets deposited for the amount that they want to withdraw as well as if the Users withdraw will exceed maximal withdraw percent for that particular Pool.
Protocol will then calculate the price of the SD Token, Users accrued interest if there's any, total deposit balance of SD Token and the asset and Users balance in SD Token. After that, Protocol will update Protocol deposit balance and Users deposit balance in SRWA User Badge and transfer withdrawn assets to the Users wallet.
User can withdraw their deposited assets that are not used as collateral for a loan at any time. Withdrawing process is similar to borrow in terms of the validations that are needed to pass for the operation to be successful. So the first step is to check if there is enough available liquidity that the user can withdraw.
In the next step, the protocol needs to check the maximal amount that the user can withdraw, which is users collateral divided by LTV:
withdrawable_amount = user_available_collateral / asset_ltv_ratio
Same as with the borrow, the max borrow percent is checked in the withdrawal operation, except that here it's taking users borrow balance into account:
max_borrow_percent = pool_deposit - user_borrow / pool_max_borrow_percent