Skip to content

Commit 3a581fc

Browse files
authored
feat: system time (#189)
1 parent 7f60ff0 commit 3a581fc

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

workspace/apps/perpetuals/contracts/src/core/core.cairo

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ pub mod Core {
1919
};
2020
use perpetuals::core::errors::{
2121
AMOUNT_OVERFLOW, FORCED_WAIT_REQUIRED, INVALID_ZERO_TIMEOUT, LENGTH_MISMATCH,
22-
ORDER_IS_NOT_EXPIRED, TRADE_ASSET_NOT_SYNTHETIC, TRANSFER_FAILED,
22+
NON_MONOTONIC_TIME, ORDER_IS_NOT_EXPIRED, STALE_TIME, TRADE_ASSET_NOT_SYNTHETIC,
23+
TRANSFER_FAILED,
2324
};
2425
use perpetuals::core::events;
2526
use perpetuals::core::interface::{ICore, Settlement};
@@ -161,6 +162,7 @@ pub mod Core {
161162
forced_action_timelock: TimeDelta,
162163
// Cost for executing forced actions.
163164
premium_cost: u64,
165+
system_time: Timestamp,
164166
}
165167

166168
#[event]
@@ -953,6 +955,41 @@ pub mod Core {
953955
},
954956
);
955957
}
958+
959+
/// Updates the system time stored in the contract.
960+
///
961+
/// Validations:
962+
/// - The contract must not be paused.
963+
/// - Only the operator can call this function.
964+
/// - The operator_nonce must be valid.
965+
/// - The new system time must be strictly greater than the current system time.
966+
/// - The new system time must not exceed the current Starknet block timestamp.
967+
///
968+
/// Execution:
969+
/// - Updates the system time.
970+
/// - Emits no events.
971+
fn update_system_time(
972+
ref self: ContractState, operator_nonce: u64, new_timestamp: Timestamp,
973+
) {
974+
self.pausable.assert_not_paused();
975+
self.operator_nonce.use_checked_nonce(:operator_nonce);
976+
977+
// The new system time must be strictly greater than the current system time.
978+
let current_system_time = self.system_time.read();
979+
assert(new_timestamp > current_system_time, NON_MONOTONIC_TIME);
980+
981+
// The new system time must not exceed the current Starknet block timestamp.
982+
let now = Time::now();
983+
assert(new_timestamp <= now, STALE_TIME);
984+
985+
// Update the system time.
986+
self.system_time.write(new_timestamp);
987+
}
988+
989+
/// Returns the current system time stored in the contract.
990+
fn get_system_time(self: @ContractState) -> Timestamp {
991+
self.system_time.read()
992+
}
956993
}
957994

958995
#[generate_trait]

workspace/apps/perpetuals/contracts/src/core/errors.cairo

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ pub const TRANSFER_FAILED: felt252 = 'TRANSFER_FAILED';
2525
pub const SAME_BASE_QUOTE_ASSET_IDS: felt252 = 'SAME_BASE_QUOTE_ASSET_IDS';
2626
pub const ORDER_IS_NOT_EXPIRED: felt252 = 'ORDER_IS_NOT_EXPIRED';
2727
pub const LENGTH_MISMATCH: felt252 = 'LENGTH_MISMATCH';
28+
pub const NON_MONOTONIC_TIME: felt252 = 'NON_MONOTONIC_TIME';
29+
pub const STALE_TIME: felt252 = 'STALE_TIME';
30+
2831

2932
pub fn fulfillment_exceeded_err(position_id: PositionId) -> ByteArray {
3033
format!("FULFILLMENT_EXCEEDED position_id: {:?}", position_id)

workspace/apps/perpetuals/contracts/src/core/interface.cairo

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,6 @@ pub trait ICore<TContractState> {
166166
order_b: Order,
167167
);
168168
fn forced_trade(ref self: TContractState, order_a: Order, order_b: Order);
169+
fn update_system_time(ref self: TContractState, operator_nonce: u64, new_timestamp: Timestamp);
170+
fn get_system_time(self: @TContractState) -> Timestamp;
169171
}

0 commit comments

Comments
 (0)