Skip to content

Commit c7c6562

Browse files
committed
fix: vesting contract gas optimize
1 parent 2b5a272 commit c7c6562

1 file changed

Lines changed: 18 additions & 21 deletions

File tree

src/tokenDistribution/vesting/Vesting.sol

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -85,34 +85,30 @@ contract Vesting is IVesting, Initializable {
8585
}
8686

8787
/// @notice Returns the total amount of unlocked tokens
88-
function totalUnlocked() public view returns (uint256) {
88+
function totalUnlocked() external view returns (uint256) {
8989
return _computeUnlocked(_initialTotalLocked);
9090
}
9191

9292
/// @notice Returns the total amount of locked tokens
9393
function totalLocked() external view returns (uint256) {
94-
// TODO: заменить на
95-
// uint256 initialTotalLocked = _initialTotalLocked;
96-
// return initialTotalLocked - _computeUnlocked(initialTotalLocked);
97-
return _initialTotalLocked - totalUnlocked();
94+
uint256 initialTotalLocked = _initialTotalLocked;
95+
return initialTotalLocked - _computeUnlocked(initialTotalLocked);
9896
}
9997

10098
/// @notice Returns the amount of unlocked tokens for a specific account
101-
function unlockedOf(address account) public view returns (uint256) {
99+
function unlockedOf(address account) external view returns (uint256) {
102100
return _computeUnlocked(_initialLocked[account]);
103101
}
104102

105103
/// @notice Returns the amount of locked tokens for a specific account
106104
function lockedOf(address account) external view returns (uint256) {
107-
// TODO: заменить на
108-
// uint256 initialLocked = _initialLocked[account];
109-
// return initialLocked - _computeUnlocked(initialLocked);
110-
return _initialLocked[account] - unlockedOf(account);
105+
uint256 initialLocked = _initialLocked[account];
106+
return initialLocked - _computeUnlocked(initialLocked);
111107
}
112108

113109
/// @notice Returns the available amount of unlocked tokens that can be claimed by a specific account
114110
function availableToClaim(address account) public view returns (uint256) {
115-
return unlockedOf(account) - _released[account];
111+
return _computeUnlocked(_initialLocked[account]) - _released[account];
116112
}
117113

118114
// endregion
@@ -174,6 +170,7 @@ contract Vesting is IVesting, Initializable {
174170
revert ZeroBeneficiaries();
175171
}
176172

173+
uint256 initialTotalLocked = 0;
177174
for (uint256 i = 0; i < beneficiaries.length; i++) {
178175
Beneficiary memory beneficiary = beneficiaries[i];
179176

@@ -189,15 +186,16 @@ contract Vesting is IVesting, Initializable {
189186
revert DuplicateBeneficiary(beneficiary.account);
190187
}
191188

189+
initialTotalLocked += beneficiary.amount;
192190
_initialLocked[beneficiary.account] = beneficiary.amount;
193-
// TODO: переделать на чтение из memory и последующая запись в storage
194-
_initialTotalLocked += beneficiary.amount;
195191
}
196192

197-
if (_initialTotalLocked != baseToken.balanceOf(address(this))) {
193+
if (initialTotalLocked != baseToken.balanceOf(address(this))) {
198194
revert IncorrectAmountOfBeneficiaries();
199195
}
200196

197+
_initialTotalLocked = initialTotalLocked;
198+
201199
emit BeneficiariesInitialized(beneficiaries);
202200
}
203201

@@ -207,16 +205,15 @@ contract Vesting is IVesting, Initializable {
207205
* @return unlockedAmount The total amount of unlocked tokens
208206
*/
209207
function _computeUnlocked(uint256 initialLockedAmount) private view returns (uint256 unlockedAmount) {
210-
// TODO: добавить _schedule в memory
211-
if (block.timestamp < _schedule.startTime) {
208+
Schedule memory schedule = _schedule;
209+
210+
if (block.timestamp < schedule.startTime) {
212211
return 0;
213212
}
214213

215-
uint256 startTime = _schedule.startTime;
216-
Period[] memory periods = _schedule.periods;
217-
218-
for (uint256 i = 0; i < periods.length; i++) {
219-
Period memory period = _schedule.periods[i];
214+
uint256 startTime = schedule.startTime;
215+
for (uint256 i = 0; i < schedule.periods.length; i++) {
216+
Period memory period = schedule.periods[i];
220217

221218
uint256 endTime = period.endTime;
222219
uint256 portion = period.portion;

0 commit comments

Comments
 (0)