Skip to content

Commit c66e0f6

Browse files
feat: uint64 timestamps and fmt
1 parent 6fc357d commit c66e0f6

File tree

10 files changed

+142
-306
lines changed

10 files changed

+142
-306
lines changed

foundry.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ gas_reports = ["BasedAppManager"]
1010
[fmt]
1111
bracket_spacing = false
1212
int_types = "long"
13-
line_length = 120
14-
multiline_func_header = "params_first"
13+
line_length = 130
14+
multiline_func_header = "attributes_first"
1515
number_underscore = "thousands"
1616
quote_style = "double"
1717
tab_width = 4

src/BasedAppManager.sol

Lines changed: 47 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,7 @@ import {IBasedAppManager} from "./interfaces/IBasedAppManager.sol";
5353
* Marco Tabasco
5454
* Riccardo Persiani
5555
*/
56-
contract BasedAppManager is
57-
Initializable,
58-
OwnableUpgradeable,
59-
UUPSUpgradeable,
60-
ReentrancyGuardTransient,
61-
IBasedAppManager
62-
{
56+
contract BasedAppManager is Initializable, OwnableUpgradeable, UUPSUpgradeable, ReentrancyGuardTransient, IBasedAppManager {
6357
using SafeERC20 for IERC20;
6458

6559
uint32 public constant MAX_PERCENTAGE = 1e4;
@@ -117,8 +111,7 @@ contract BasedAppManager is
117111
* @notice Tracks obligation percentages for a strategy based on specific bApps and tokens.
118112
* @dev Uses a hash of the bApp and token to map the obligation percentage for the strategy.
119113
*/
120-
mapping(uint256 strategyId => mapping(address bApp => mapping(address token => ICore.Obligation))) public
121-
obligations;
114+
mapping(uint256 strategyId => mapping(address bApp => mapping(address token => ICore.Obligation))) public obligations;
122115
/**
123116
* @notice Tracks unallocated tokens in a strategy.
124117
* @dev Count the number of bApps that have one obligation set for the token.
@@ -161,9 +154,7 @@ contract BasedAppManager is
161154

162155
/// @notice Allow the function to be called only by the strategy owner
163156
/// @param strategyId The ID of the strategy
164-
modifier onlyStrategyOwner(
165-
uint256 strategyId
166-
) {
157+
modifier onlyStrategyOwner(uint256 strategyId) {
167158
if (strategies[strategyId].owner != msg.sender) {
168159
revert ICore.InvalidStrategyOwner(msg.sender, strategies[strategyId].owner);
169160
}
@@ -172,18 +163,14 @@ contract BasedAppManager is
172163

173164
/// @notice Allow the function to be called only by the bApp owner
174165
/// @param bApp The address of the bApp
175-
modifier onlyBAppOwner(
176-
address bApp
177-
) {
166+
modifier onlyBAppOwner(address bApp) {
178167
if (bAppOwners[bApp] != msg.sender) revert ICore.InvalidBAppOwner(msg.sender, bAppOwners[bApp]);
179168
_;
180169
}
181170

182171
/// @notice Defines who can authorize the upgrade
183172
/// @param newImplementation The address of the new implementation
184-
function _authorizeUpgrade(
185-
address newImplementation
186-
) internal override onlyOwner {}
173+
function _authorizeUpgrade(address newImplementation) internal override onlyOwner {}
187174

188175
// *****************************************
189176
// ** Section: Delegate Validator Balance **
@@ -228,9 +215,7 @@ contract BasedAppManager is
228215

229216
/// @notice Removes delegation from an account.
230217
/// @param account The address of the account whose delegation is being removed.
231-
function removeDelegatedBalance(
232-
address account
233-
) external {
218+
function removeDelegatedBalance(address account) external {
234219
uint32 percentage = delegations[msg.sender][account];
235220
if (percentage == 0) revert ICore.DelegationDoesNotExist();
236221

@@ -276,11 +261,10 @@ contract BasedAppManager is
276261
/// @param bApp The address of the bApp
277262
/// @param tokens The list of tokens to add
278263
/// @param sharedRiskLevels The shared risk levels of the tokens
279-
function addTokensToBApp(
280-
address bApp,
281-
address[] calldata tokens,
282-
uint32[] calldata sharedRiskLevels
283-
) external onlyBAppOwner(bApp) {
264+
function addTokensToBApp(address bApp, address[] calldata tokens, uint32[] calldata sharedRiskLevels)
265+
external
266+
onlyBAppOwner(bApp)
267+
{
284268
if (tokens.length == 0) revert ICore.EmptyTokenList();
285269
_addNewTokens(bApp, tokens, sharedRiskLevels);
286270
emit BAppTokensCreated(bApp, tokens, sharedRiskLevels);
@@ -290,11 +274,10 @@ contract BasedAppManager is
290274
/// @param bApp The address of the bApp
291275
/// @param tokens The list of tokens to update
292276
/// @param sharedRiskLevels The shared risk levels of the tokens
293-
function updateBAppTokens(
294-
address bApp,
295-
address[] calldata tokens,
296-
uint32[] calldata sharedRiskLevels
297-
) external onlyBAppOwner(bApp) {
277+
function updateBAppTokens(address bApp, address[] calldata tokens, uint32[] calldata sharedRiskLevels)
278+
external
279+
onlyBAppOwner(bApp)
280+
{
298281
if (tokens.length == 0) revert ICore.EmptyTokenList();
299282
_validateArraysLength(tokens, sharedRiskLevels);
300283
for (uint256 i = 0; i < tokens.length; i++) {
@@ -314,9 +297,7 @@ contract BasedAppManager is
314297

315298
/// @notice Function to create a new Strategy
316299
/// @return strategyId The ID of the new Strategy
317-
function createStrategy(
318-
uint32 fee
319-
) external returns (uint256 strategyId) {
300+
function createStrategy(uint32 fee) external returns (uint256 strategyId) {
320301
if (fee > MAX_PERCENTAGE) revert ICore.InvalidStrategyFee();
321302

322303
strategyId = ++_strategyCounter;
@@ -371,9 +352,7 @@ contract BasedAppManager is
371352

372353
/// @notice Deposit ETH into the strategy
373354
/// @param strategyId The ID of the strategy
374-
function depositETH(
375-
uint256 strategyId
376-
) external payable {
355+
function depositETH(uint256 strategyId) external payable {
377356
if (msg.value == 0) revert ICore.InvalidAmount();
378357

379358
strategyTokenBalances[strategyId][msg.sender][ETH_ADDRESS] += msg.value;
@@ -425,11 +404,9 @@ contract BasedAppManager is
425404
ICore.WithdrawalRequest storage request = withdrawalRequests[strategyId][msg.sender][address(token)];
426405

427406
request.amount = amount;
428-
request.requestTime = block.timestamp;
407+
request.requestTime = uint64(block.timestamp);
429408

430-
emit StrategyWithdrawalProposed(
431-
strategyId, msg.sender, address(token), amount, block.timestamp + WITHDRAWAL_TIMELOCK_PERIOD
432-
);
409+
emit StrategyWithdrawalProposed(strategyId, msg.sender, address(token), amount);
433410
}
434411

435412
/// @notice Finalize the ERC20 withdrawal after the timelock period has passed.
@@ -461,18 +438,14 @@ contract BasedAppManager is
461438
ICore.WithdrawalRequest storage request = withdrawalRequests[strategyId][msg.sender][ETH_ADDRESS];
462439

463440
request.amount = amount;
464-
request.requestTime = block.timestamp;
441+
request.requestTime = uint64(block.timestamp);
465442

466-
emit StrategyWithdrawalProposed(
467-
strategyId, msg.sender, ETH_ADDRESS, amount, block.timestamp + WITHDRAWAL_TIMELOCK_PERIOD
468-
);
443+
emit StrategyWithdrawalProposed(strategyId, msg.sender, ETH_ADDRESS, amount);
469444
}
470445

471446
/// @notice Finalize the ETH withdrawal after the timelock period has passed.
472447
/// @param strategyId The ID of the strategy.
473-
function finalizeWithdrawalETH(
474-
uint256 strategyId
475-
) external nonReentrant {
448+
function finalizeWithdrawalETH(uint256 strategyId) external nonReentrant {
476449
ICore.WithdrawalRequest storage request = withdrawalRequests[strategyId][msg.sender][ETH_ADDRESS];
477450
uint256 requestTime = request.requestTime;
478451

@@ -493,12 +466,10 @@ contract BasedAppManager is
493466
/// @param bApp The address of the bApp
494467
/// @param token The address of the token
495468
/// @param obligationPercentage The obligation percentage
496-
function createObligation(
497-
uint256 strategyId,
498-
address bApp,
499-
address token,
500-
uint32 obligationPercentage
501-
) external onlyStrategyOwner(strategyId) {
469+
function createObligation(uint256 strategyId, address bApp, address token, uint32 obligationPercentage)
470+
external
471+
onlyStrategyOwner(strategyId)
472+
{
502473
if (accountBAppStrategy[msg.sender][bApp] != strategyId) revert ICore.BAppNotOptedIn();
503474

504475
_createSingleObligation(strategyId, bApp, token, obligationPercentage);
@@ -510,12 +481,10 @@ contract BasedAppManager is
510481
/// @param token The address of the token
511482
/// @param obligationPercentage The obligation percentage
512483
/// @dev The used tokens counter cannot be decreased as the fast update can only bring the percentage up
513-
function fastUpdateObligation(
514-
uint256 strategyId,
515-
address bApp,
516-
address token,
517-
uint32 obligationPercentage
518-
) external onlyStrategyOwner(strategyId) {
484+
function fastUpdateObligation(uint256 strategyId, address bApp, address token, uint32 obligationPercentage)
485+
external
486+
onlyStrategyOwner(strategyId)
487+
{
519488
if (obligationPercentage <= obligations[strategyId][bApp][token].percentage) revert ICore.InvalidPercentage();
520489

521490
_validateObligationUpdateInput(strategyId, bApp, token, obligationPercentage);
@@ -528,33 +497,25 @@ contract BasedAppManager is
528497
/// @param strategyId The ID of the strategy.
529498
/// @param token The ERC20 token address.
530499
/// @param obligationPercentage The new percentage of the obligation
531-
function proposeUpdateObligation(
532-
uint256 strategyId,
533-
address bApp,
534-
address token,
535-
uint32 obligationPercentage
536-
) external onlyStrategyOwner(strategyId) {
500+
function proposeUpdateObligation(uint256 strategyId, address bApp, address token, uint32 obligationPercentage)
501+
external
502+
onlyStrategyOwner(strategyId)
503+
{
537504
_validateObligationUpdateInput(strategyId, bApp, token, obligationPercentage);
538505

539506
ICore.ObligationRequest storage request = obligationRequests[strategyId][bApp][token];
540507

541508
request.percentage = obligationPercentage;
542-
request.requestTime = block.timestamp;
509+
request.requestTime = uint64(block.timestamp);
543510

544-
emit ObligationUpdateProposed(
545-
strategyId, bApp, address(token), obligationPercentage, request.requestTime + OBLIGATION_TIMELOCK_PERIOD
546-
);
511+
emit ObligationUpdateProposed(strategyId, bApp, address(token), obligationPercentage);
547512
}
548513

549514
/// @notice Finalize the withdrawal after the timelock period has passed.
550515
/// @param strategyId The ID of the strategy.
551516
/// @param bApp The address of the bApp.
552517
/// @param token The ERC20 token address.
553-
function finalizeUpdateObligation(
554-
uint256 strategyId,
555-
address bApp,
556-
address token
557-
) external onlyStrategyOwner(strategyId) {
518+
function finalizeUpdateObligation(uint256 strategyId, address bApp, address token) external onlyStrategyOwner(strategyId) {
558519
ICore.ObligationRequest storage request = obligationRequests[strategyId][bApp][address(token)];
559520
uint256 requestTime = request.requestTime;
560521
uint32 percentage = request.percentage;
@@ -586,16 +547,14 @@ contract BasedAppManager is
586547
if (proposedFee == fee) revert ICore.FeeAlreadySet();
587548

588549
strategy.feeProposed = proposedFee;
589-
strategy.feeRequestTime = block.timestamp;
550+
strategy.feeRequestTime = uint64(block.timestamp);
590551

591-
emit StrategyFeeUpdateProposed(strategyId, msg.sender, proposedFee, fee, strategy.feeRequestTime);
552+
emit StrategyFeeUpdateProposed(strategyId, msg.sender, proposedFee, fee);
592553
}
593554

594555
/// @notice Finalize the fee update for a strategy
595556
/// @param strategyId The ID of the strategy
596-
function finalizeFeeUpdate(
597-
uint256 strategyId
598-
) external onlyStrategyOwner(strategyId) {
557+
function finalizeFeeUpdate(uint256 strategyId) external onlyStrategyOwner(strategyId) {
599558
ICore.Strategy storage strategy = strategies[strategyId];
600559

601560
uint256 feeRequestTime = strategy.feeRequestTime;
@@ -624,9 +583,7 @@ contract BasedAppManager is
624583

625584
/// @notice Internal function to validate the token and shared risk level
626585
/// @param token The token address to be validated
627-
function _validateTokenInput(
628-
address token
629-
) internal pure {
586+
function _validateTokenInput(address token) internal pure {
630587
if (token == address(0)) revert ICore.ZeroAddressNotAllowed();
631588
}
632589

@@ -673,12 +630,7 @@ contract BasedAppManager is
673630
/// @param bApp The address of the bApp
674631
/// @param token The address of the token
675632
/// @param obligationPercentage The obligation percentage
676-
function _createSingleObligation(
677-
uint256 strategyId,
678-
address bApp,
679-
address token,
680-
uint32 obligationPercentage
681-
) private {
633+
function _createSingleObligation(uint256 strategyId, address bApp, address token, uint32 obligationPercentage) private {
682634
if (!bAppTokens[bApp][token].isSet) revert ICore.TokenNoTSupportedByBApp(token);
683635
if (obligationPercentage > MAX_PERCENTAGE) revert ICore.InvalidPercentage();
684636
if (obligations[strategyId][bApp][token].isSet) revert ICore.ObligationAlreadySet();
@@ -698,12 +650,10 @@ contract BasedAppManager is
698650
/// @param bApp The address of the bApp
699651
/// @param token The address of the token
700652
/// @param obligationPercentage The obligation percentage
701-
function _validateObligationUpdateInput(
702-
uint256 strategyId,
703-
address bApp,
704-
address token,
705-
uint32 obligationPercentage
706-
) private view {
653+
function _validateObligationUpdateInput(uint256 strategyId, address bApp, address token, uint32 obligationPercentage)
654+
private
655+
view
656+
{
707657
if (accountBAppStrategy[msg.sender][bApp] != strategyId) revert ICore.BAppNotOptedIn();
708658
if (obligationPercentage > MAX_PERCENTAGE) revert ICore.InvalidPercentage();
709659
if (obligationPercentage == obligations[strategyId][bApp][token].percentage) {
@@ -728,8 +678,8 @@ contract BasedAppManager is
728678
/// @param timelockPeriod The timelock period
729679
/// @param expireTime The expire time
730680
function _checkTimelocks(uint256 requestTime, uint256 timelockPeriod, uint256 expireTime) private view {
731-
if (block.timestamp < requestTime + timelockPeriod) revert ICore.TimelockNotElapsed();
732-
if (block.timestamp > requestTime + timelockPeriod + expireTime) {
681+
if (uint64(block.timestamp) < requestTime + timelockPeriod) revert ICore.TimelockNotElapsed();
682+
if (uint64(block.timestamp) > requestTime + timelockPeriod + expireTime) {
733683
revert ICore.RequestTimeExpired();
734684
}
735685
}

0 commit comments

Comments
 (0)