vault: use custom operators to improve readability

This commit is contained in:
Mark Spanbroek 2025-01-23 10:19:41 +01:00
parent 1df6afb132
commit d9722d55fd
3 changed files with 21 additions and 13 deletions

View File

@ -3,16 +3,19 @@ pragma solidity 0.8.28;
type Timestamp is uint64; type Timestamp is uint64;
using {_lessThan as <} for Timestamp global;
using {_atMost as <=} for Timestamp global;
function _lessThan(Timestamp a, Timestamp b) pure returns (bool) {
return Timestamp.unwrap(a) < Timestamp.unwrap(b);
}
function _atMost(Timestamp a, Timestamp b) pure returns (bool) {
return Timestamp.unwrap(a) <= Timestamp.unwrap(b);
}
library Timestamps { library Timestamps {
function currentTime() internal view returns (Timestamp) { function currentTime() internal view returns (Timestamp) {
return Timestamp.wrap(uint64(block.timestamp)); return Timestamp.wrap(uint64(block.timestamp));
} }
function isAfter(Timestamp a, Timestamp b) internal pure returns (bool) {
return Timestamp.unwrap(a) > Timestamp.unwrap(b);
}
function isFuture(Timestamp timestamp) internal view returns (bool) {
return Timestamp.unwrap(timestamp) > block.timestamp;
}
} }

View File

@ -6,7 +6,12 @@ import "./Timestamps.sol";
type TokensPerSecond is int256; type TokensPerSecond is int256;
using {_negate as -} for TokensPerSecond global; using {_negate as -} for TokensPerSecond global;
using {_equals as ==} for TokensPerSecond global;
function _negate(TokensPerSecond rate) pure returns (TokensPerSecond) { function _negate(TokensPerSecond rate) pure returns (TokensPerSecond) {
return TokensPerSecond.wrap(-TokensPerSecond.unwrap(rate)); return TokensPerSecond.wrap(-TokensPerSecond.unwrap(rate));
} }
function _equals(TokensPerSecond a, TokensPerSecond b) pure returns (bool) {
return TokensPerSecond.unwrap(a) == TokensPerSecond.unwrap(b);
}

View File

@ -99,7 +99,7 @@ abstract contract VaultBase {
Context context, Context context,
Recipient recipient Recipient recipient
) internal { ) internal {
require(!_getLock(controller, context).expiry.isFuture(), Locked()); require(_getLock(controller, context).expiry <= Timestamps.currentTime(), Locked());
delete _locks[controller][context]; delete _locks[controller][context];
Balance memory balance = _getBalance(controller, context, recipient); Balance memory balance = _getBalance(controller, context, recipient);
uint256 amount = balance.available + balance.designated; uint256 amount = balance.available + balance.designated;
@ -155,7 +155,7 @@ abstract contract VaultBase {
Timestamp.unwrap(_getLock(controller, context).maximum) == 0, Timestamp.unwrap(_getLock(controller, context).maximum) == 0,
AlreadyLocked() AlreadyLocked()
); );
require(!expiry.isAfter(maximum), ExpiryPastMaximum()); require(expiry <= maximum, ExpiryPastMaximum());
_locks[controller][context] = Lock({expiry: expiry, maximum: maximum}); _locks[controller][context] = Lock({expiry: expiry, maximum: maximum});
} }
@ -165,9 +165,9 @@ abstract contract VaultBase {
Timestamp expiry Timestamp expiry
) internal { ) internal {
Lock memory previous = _getLock(controller, context); Lock memory previous = _getLock(controller, context);
require(previous.expiry.isFuture(), LockExpired()); require(Timestamps.currentTime() < previous.expiry, LockExpired());
require(!previous.expiry.isAfter(expiry), InvalidExpiry()); require(previous.expiry <= expiry, InvalidExpiry());
require(!expiry.isAfter(previous.maximum), ExpiryPastMaximum()); require(expiry <= previous.maximum, ExpiryPastMaximum());
_locks[controller][context].expiry = expiry; _locks[controller][context].expiry = expiry;
} }