-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathWithdraw.sol
68 lines (57 loc) · 2.17 KB
/
Withdraw.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;
import {State} from "@src/SizeStorage.sol";
import {DepositTokenLibrary} from "@src/libraries/DepositTokenLibrary.sol";
import {Math} from "@src/libraries/Math.sol";
import {Errors} from "@src/libraries/Errors.sol";
import {Events} from "@src/libraries/Events.sol";
struct WithdrawParams {
// The token to withdraw
address token;
// The amount to withdraw
// The actual withdrawn amount is capped to the sender's balance
uint256 amount;
// The account to withdraw the tokens to
address to;
}
/// @title Withdraw
/// @custom:security-contact [email protected]
/// @author Size (https://size.credit/)
/// @notice Contains the logic for withdrawing tokens from the protocol
library Withdraw {
using DepositTokenLibrary for State;
function validateWithdraw(State storage state, WithdrawParams calldata params) external view {
// validte msg.sender
// N/A
// validate token
if (
params.token != address(state.data.underlyingCollateralToken)
&& params.token != address(state.data.underlyingBorrowToken)
) {
revert Errors.INVALID_TOKEN(params.token);
}
// validate amount
if (params.amount == 0) {
revert Errors.NULL_AMOUNT();
}
// validate to
if (params.to == address(0)) {
revert Errors.NULL_ADDRESS();
}
}
function executeWithdraw(State storage state, WithdrawParams calldata params) public {
uint256 amount;
if (params.token == address(state.data.underlyingBorrowToken)) {
amount = Math.min(params.amount, state.data.borrowAToken.balanceOf(msg.sender));
if (amount > 0) {
state.withdrawUnderlyingTokenFromVariablePool(msg.sender, params.to, amount);
}
} else {
amount = Math.min(params.amount, state.data.collateralToken.balanceOf(msg.sender));
if (amount > 0) {
state.withdrawUnderlyingCollateralToken(msg.sender, params.to, amount);
}
}
emit Events.Withdraw(params.token, params.to, amount);
}
}