Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions contracts/predictify-hybrid/src/oracles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,16 @@ impl<'a> ReflectorOracleClient<'a> {
res
}

/// Get TWAP (Time-Weighted Average Price) for an asset
/// Get TWAP (Time-Weighted Average Price) for an asset.
///
/// Uses an intra-transaction cache keyed by (asset, records) to avoid
/// duplicate oracle calls within the same transaction. The cache lives
/// in temporary storage and is automatically discarded when the
/// transaction ends.
///
/// When `force_refresh` is `true` the cache is bypassed and a fresh
/// oracle call is made; the result still updates the cache so subsequent
/// calls in the same transaction benefit from it.
pub fn twap(&self, asset: ReflectorAsset, records: u32, force_refresh: bool) -> Option<i128> {
// Build a cache key unique to this transaction
let cache_key: (Symbol, Val, Val) = (
Expand All @@ -652,10 +661,13 @@ impl<'a> ReflectorOracleClient<'a> {
records.into_val(self.env),
);
// Attempt to read from temporary storage (per-transaction cache)
if let Some(cached) = self.env.storage().temporary().get::<_, Option<i128>>(&cache_key) {
return cached;
// only when the caller hasn't requested a forced refresh.
if !force_refresh {
if let Some(cached) = self.env.storage().temporary().get::<_, Option<i128>>(&cache_key) {
return cached;
}
}
// Not cached; perform contract call
// Not cached (or force_refresh requested); perform contract call
let args = vec![
self.env,
asset.into_val(self.env),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl MockReflectorOracle {
}

#[test]
fn test_reflector_twap_cache() {
fn test_reflector_twap_cache_within_transaction() {
let env = Env::default();

// Register the mock oracle contract
Expand Down Expand Up @@ -53,3 +53,28 @@ fn test_reflector_twap_cache() {
assert_eq!(res4, Some(100_000_000));
assert_eq!(mock_client.get_calls(), 2);
}

#[test]
fn test_twap_cache_resets_between_transactions() {
// Transaction 1
let env1 = Env::default();
let mock_id1 = env1.register_contract(None, MockReflectorOracle);
let mock_client1 = MockReflectorOracleClient::new(&env1, &mock_id1);
let client1 = ReflectorOracleClient::new(&env1, mock_id1.clone());
let asset = ReflectorAsset::Other(Symbol::new(&env1, "BTC"));

let val1 = client1.twap(asset.clone(), 5, false);
assert_eq!(val1, Some(100_000_000));
assert_eq!(mock_client1.get_calls(), 1);

// Transaction 2 β€” fresh Env, cache must not persist
let env2 = Env::default();
let mock_id2 = env2.register_contract(None, MockReflectorOracle);
let mock_client2 = MockReflectorOracleClient::new(&env2, &mock_id2);
let client2 = ReflectorOracleClient::new(&env2, mock_id2.clone());

let val2 = client2.twap(asset, 5, false);
assert_eq!(val2, Some(100_000_000));
// In a new transaction the cache is empty, so the mock must be called again.
assert_eq!(mock_client2.get_calls(), 1);
}

This file was deleted.