-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCommitmentManager.sol
136 lines (116 loc) · 4.65 KB
/
CommitmentManager.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
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.18;
import "../interfaces/IFundToken.sol";
/**
* @title CommitmentManager
* @notice This contract is responsible for managing commitments in a fund context.
*/
contract CommitmentManager {
// Structure to store commitment data.
struct Commit {
uint256 amount;
CommitState status;
}
// Define constants for different commitment statuses.
enum CommitState {
COMMIT_NONE,
COMMIT_PENDING,
COMMIT_APPROVED,
COMMIT_CANCELLED,
COMMIT_REJECTED,
COMMIT_BLOCKED
}
// Declare public state variables.
uint256 public totalInPendingLpCommits;
uint256 public totalCommittedGp;
uint256 public totalCommittedLp;
// Mapping to track commitments from addresses.
mapping(address => Commit) public lpCommitments;
address[] public gpAccounts;
mapping(address => Commit) public gpCommitments;
address[] public lpAccounts;
// Event declarations.
event LpCommitmentAdded(address indexed account, uint256 amount);
event GpCommitmentAdded(address indexed account, uint256 amount);
event LpCommitmentCancelled(address indexed account);
event LpCommitmentApproved(address indexed account, uint256 amount);
event LpCommitmentRejected(address indexed account);
/* ========== READ-ONLY FUNCTIONS ========== */
/**
* @dev Returns the total committed amount for both GPs and LPs.
* @return The total committed amount.
*/
function totalCommitted() public view returns (uint256) {
return totalCommittedGp + totalCommittedLp;
}
/* ========== INTERNAL ========== */
/**
* @dev Adds a new GP commitment.
* @param account Address of the account.
* @param amount Commitment amount.
*/
function _addGpCommitment(address account, uint256 amount) internal {
totalCommittedGp += amount;
gpCommitments[account] = Commit(amount, CommitState.COMMIT_APPROVED);
gpAccounts.push(account);
emit GpCommitmentAdded(account, amount);
}
/**
* @dev Sets an LP commitment for an account. If the commitment already exists,
* it updates the amount and resets its status.
* @param account Address of the account.
* @param amount Commitment amount.
*/
function _setLpCommitment(address account, uint256 amount) internal {
Commit storage commit = lpCommitments[account];
if (commit.status != CommitState.COMMIT_NONE && commit.status != CommitState.COMMIT_BLOCKED) {
totalInPendingLpCommits -= commit.amount;
}
if (commit.status == CommitState.COMMIT_NONE) {
lpCommitments[account] = Commit(amount, CommitState.COMMIT_PENDING);
lpAccounts.push(account);
} else {
commit.amount = amount;
commit.status = CommitState.COMMIT_PENDING;
}
totalInPendingLpCommits += amount;
emit LpCommitmentAdded(account, amount);
}
/**
* @dev Cancels an existing LP commitment.
* @param account Address of the account.
*/
function _cancelLpCommitment(address account) internal {
Commit storage commit = lpCommitments[account];
require(commit.status == CommitState.COMMIT_PENDING, "Too late to cancel");
commit.status = CommitState.COMMIT_CANCELLED;
totalInPendingLpCommits -= commit.amount;
emit LpCommitmentCancelled(account);
}
/**
* @dev Approves multiple LP commitments.
* @param accounts Array of account addresses to approve.
*/
function _approveLpCommitments(address[] calldata accounts, IFundToken lpCommitToken, uint256 price) internal {
for (uint256 i = 0; i < accounts.length; i++) {
Commit storage commit = lpCommitments[accounts[i]];
require(commit.status == CommitState.COMMIT_PENDING, "Commit not allowed");
commit.status = CommitState.COMMIT_APPROVED;
totalCommittedLp += commit.amount;
lpCommitToken.mint(accounts[i], commit.amount / price);
emit LpCommitmentApproved(accounts[i], commit.amount);
}
}
/**
* @dev Rejects multiple LP commitments.
* @param accounts Array of account addresses to reject.
*/
function _rejectLpCommitments(address[] calldata accounts) internal {
for (uint256 i = 0; i < accounts.length; i++) {
Commit storage commit = lpCommitments[accounts[i]];
require(commit.status == CommitState.COMMIT_PENDING, "Commitment must be pending");
commit.status = CommitState.COMMIT_REJECTED;
emit LpCommitmentRejected(accounts[i]);
}
}
}