mirror of
https://github.com/status-im/codex-contracts-eth.git
synced 2025-01-31 02:35:36 +00:00
vault: use custom operators to improve readability
This commit is contained in:
parent
1df6afb132
commit
d9722d55fd
@ -3,16 +3,19 @@ pragma solidity 0.8.28;
|
||||
|
||||
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 {
|
||||
function currentTime() internal view returns (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;
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,12 @@ import "./Timestamps.sol";
|
||||
type TokensPerSecond is int256;
|
||||
|
||||
using {_negate as -} for TokensPerSecond global;
|
||||
using {_equals as ==} for TokensPerSecond global;
|
||||
|
||||
function _negate(TokensPerSecond rate) pure returns (TokensPerSecond) {
|
||||
return TokensPerSecond.wrap(-TokensPerSecond.unwrap(rate));
|
||||
}
|
||||
|
||||
function _equals(TokensPerSecond a, TokensPerSecond b) pure returns (bool) {
|
||||
return TokensPerSecond.unwrap(a) == TokensPerSecond.unwrap(b);
|
||||
}
|
||||
|
@ -99,7 +99,7 @@ abstract contract VaultBase {
|
||||
Context context,
|
||||
Recipient recipient
|
||||
) internal {
|
||||
require(!_getLock(controller, context).expiry.isFuture(), Locked());
|
||||
require(_getLock(controller, context).expiry <= Timestamps.currentTime(), Locked());
|
||||
delete _locks[controller][context];
|
||||
Balance memory balance = _getBalance(controller, context, recipient);
|
||||
uint256 amount = balance.available + balance.designated;
|
||||
@ -155,7 +155,7 @@ abstract contract VaultBase {
|
||||
Timestamp.unwrap(_getLock(controller, context).maximum) == 0,
|
||||
AlreadyLocked()
|
||||
);
|
||||
require(!expiry.isAfter(maximum), ExpiryPastMaximum());
|
||||
require(expiry <= maximum, ExpiryPastMaximum());
|
||||
_locks[controller][context] = Lock({expiry: expiry, maximum: maximum});
|
||||
}
|
||||
|
||||
@ -165,9 +165,9 @@ abstract contract VaultBase {
|
||||
Timestamp expiry
|
||||
) internal {
|
||||
Lock memory previous = _getLock(controller, context);
|
||||
require(previous.expiry.isFuture(), LockExpired());
|
||||
require(!previous.expiry.isAfter(expiry), InvalidExpiry());
|
||||
require(!expiry.isAfter(previous.maximum), ExpiryPastMaximum());
|
||||
require(Timestamps.currentTime() < previous.expiry, LockExpired());
|
||||
require(previous.expiry <= expiry, InvalidExpiry());
|
||||
require(expiry <= previous.maximum, ExpiryPastMaximum());
|
||||
_locks[controller][context].expiry = expiry;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user