-
Notifications
You must be signed in to change notification settings - Fork 0
/
FundingPool.sol
161 lines (147 loc) · 5.91 KB
/
FundingPool.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.8.19;
import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {Constants} from "../Constants.sol";
import {IFundingPool} from "./interfaces/IFundingPool.sol";
import {ILoanProposalImpl} from "./interfaces/ILoanProposalImpl.sol";
import {ILoanProposalFactory} from "./interfaces/ILoanProposalFactory.sol";
import {Constants} from "../Constants.sol";
import {DataTypesPeerToPool} from "./DataTypesPeerToPool.sol";
import {Errors} from "../Errors.sol";
contract FundingPool is IFundingPool {
using SafeERC20 for IERC20Metadata;
address public immutable loanProposalFactory;
address public immutable depositToken;
mapping(address => uint256) public balanceOf;
mapping(address => uint256) public totalSubscribed;
mapping(address => bool) public totalSubscribedIsDeployed;
mapping(address => mapping(address => uint256)) public subscribedBalanceOf;
// note: earliest unsubscribe time is to prevent griefing accept loans through atomic flashborrow, deposit, subscribe, unsubscribe, and withdraw
mapping(address => mapping(address => uint256))
internal earliestUnsubscribe;
constructor(address _loanProposalFactory, address _depositToken) {
loanProposalFactory = _loanProposalFactory;
depositToken = _depositToken;
}
function deposit(uint256 amount, uint256 transferFee) external {
if (amount == 0) {
revert Errors.InvalidSendAmount();
}
uint256 preBal = IERC20Metadata(depositToken).balanceOf(address(this));
IERC20Metadata(depositToken).safeTransferFrom(
msg.sender,
address(this),
amount
);
uint256 postBal = IERC20Metadata(depositToken).balanceOf(address(this));
if ((postBal - preBal) != amount - transferFee) {
revert Errors.InvalidSendAmount();
}
balanceOf[msg.sender] += postBal - preBal;
}
function withdraw(uint256 amount) external {
if (amount == 0 || amount > balanceOf[msg.sender]) {
revert Errors.InvalidWithdrawAmount();
}
balanceOf[msg.sender] -= amount;
IERC20Metadata(depositToken).safeTransfer(msg.sender, amount);
}
function subscribe(address loanProposal, uint256 amount) external {
if (amount == 0) {
revert Errors.InvalidAmount();
}
if (
!ILoanProposalFactory(loanProposalFactory).isLoanProposal(
loanProposal
)
) {
revert Errors.UnregisteredLoanProposal();
}
if (!ILoanProposalImpl(loanProposal).canSubscribe()) {
revert Errors.NotInSubscriptionPhase();
}
if (amount > balanceOf[msg.sender]) {
revert Errors.InsufficientBalance();
}
DataTypesPeerToPool.LoanTerms memory loanTerms = ILoanProposalImpl(
loanProposal
).loanTerms();
if (amount + totalSubscribed[loanProposal] > loanTerms.maxLoanAmount) {
revert Errors.SubscriptionAmountTooHigh();
}
balanceOf[msg.sender] -= amount;
totalSubscribed[loanProposal] += amount;
subscribedBalanceOf[loanProposal][msg.sender] += amount;
earliestUnsubscribe[loanProposal][msg.sender] =
block.timestamp +
Constants.MIN_WAIT_UNTIL_EARLIEST_UNSUBSCRIBE;
emit Subscribed(loanProposal, amount);
}
function unsubscribe(address loanProposal, uint256 amount) external {
if (amount == 0) {
revert Errors.InvalidAmount();
}
if (
!ILoanProposalFactory(loanProposalFactory).isLoanProposal(
loanProposal
)
) {
revert Errors.UnregisteredLoanProposal();
}
if (!ILoanProposalImpl(loanProposal).canUnsubscribe()) {
revert Errors.NotInUnsubscriptionPhase();
}
if (amount > subscribedBalanceOf[loanProposal][msg.sender]) {
revert Errors.UnsubscriptionAmountTooLarge();
}
if (block.timestamp < earliestUnsubscribe[loanProposal][msg.sender]) {
revert Errors.BeforeEarliestUnsubscribe();
}
balanceOf[msg.sender] += amount;
totalSubscribed[loanProposal] -= amount;
subscribedBalanceOf[loanProposal][msg.sender] -= amount;
earliestUnsubscribe[loanProposal][msg.sender] = 0;
emit Unsubscribed(loanProposal, amount);
}
function executeLoanProposal(address loanProposal) external {
if (
!ILoanProposalFactory(loanProposalFactory).isLoanProposal(
loanProposal
)
) {
revert Errors.UnregisteredLoanProposal();
}
(
uint256 arrangerFee,
uint256 finalLoanAmount,
,
,
,
,
) = ILoanProposalImpl(loanProposal).dynamicData();
DataTypesPeerToPool.LoanTerms memory loanTerms = ILoanProposalImpl(
loanProposal
).loanTerms();
totalSubscribedIsDeployed[loanProposal] = true;
ILoanProposalImpl(loanProposal).checkAndupdateStatus();
IERC20Metadata(depositToken).safeTransfer(
loanTerms.borrower,
finalLoanAmount
);
(, , address arranger, , , ) = ILoanProposalImpl(loanProposal)
.staticData();
uint256 protocolFeeShare = (arrangerFee *
ILoanProposalFactory(loanProposalFactory).arrangerFeeSplit()) /
Constants.BASE;
IERC20Metadata(depositToken).safeTransfer(
arranger,
arrangerFee - protocolFeeShare
);
IERC20Metadata(depositToken).safeTransfer(
ILoanProposalFactory(loanProposalFactory).owner(),
protocolFeeShare
);
emit LoanProposalExecuted(loanProposal);
}
}