diff --git a/src/account.cairo b/src/account.cairo deleted file mode 100644 index b3f26c82..00000000 --- a/src/account.cairo +++ /dev/null @@ -1,2 +0,0 @@ -mod account; -mod interface; diff --git a/src/account/interface.cairo b/src/account/interface.cairo deleted file mode 100644 index 5c77f797..00000000 --- a/src/account/interface.cairo +++ /dev/null @@ -1,21 +0,0 @@ -use array::ArrayTrait; -use array::SpanTrait; -use starknet::ContractAddress; - -const IACCOUNT_ID: u32 = 0xa66bd575_u32; -const ERC1271_VALIDATED: u32 = 0x1626ba7e_u32; - -#[derive(Serde, Drop)] -struct Call { - to: ContractAddress, - selector: felt252, - calldata: Array -} - -trait IAccount { - fn __execute__(calls: Array) -> Array>; - fn __validate__(calls: Array) -> felt252; - fn __validate_declare__(class_hash: felt252) -> felt252; - fn is_valid_signature(message: felt252, signature: Array) -> u32; - fn supports_interface(interface_id: u32) -> bool; -} diff --git a/src/entry/entry.cairo b/src/entry/entry.cairo index dd46346a..23babb69 100644 --- a/src/entry/entry.cairo +++ b/src/entry/entry.cairo @@ -107,11 +107,10 @@ mod Entry { index = index + 1; } } + // @notice returns the median value from an entries array - // @param entries_len: length of entries array - // @param entries: pointer to first Entry in array + // @param entries: array of entries to aggregate // @return value: the median value from the array of entries - fn entries_median< T, impl TCopy: Copy, @@ -138,6 +137,10 @@ mod Entry { (median_entry_1 + median_entry_2) / (2.into()) } } + + // @notice Returns the mean value from an entries array + // @param entries: entries array to aggregate + // @return value: the mean value from the array of entries fn entries_mean, impl TCopy: Copy, impl TDrop: Drop>( entries: @Array ) -> u256 { diff --git a/src/entry/structs.cairo b/src/entry/structs.cairo index fe03ede9..c97f5948 100644 --- a/src/entry/structs.cairo +++ b/src/entry/structs.cairo @@ -82,7 +82,7 @@ struct FutureEntryStorage { /// /// * `Spot` - Spot price /// * `Future` - Future price -/// * `Generic` - Generic price +/// * `Option` - Option price #[derive(Drop, Copy, Serde)] enum DataType { SpotEntry: felt252, @@ -98,7 +98,7 @@ enum PossibleEntryStorage { } #[derive(Drop, Copy, Serde)] -enum simpleDataType { +enum SimpleDataType { SpotEntry: (), FutureEntry: (), // OptionEntry: (), @@ -142,6 +142,7 @@ struct Checkpoint { aggregation_mode: AggregationMode, num_sources_aggregated: u32, } + #[derive(Serde, Drop, Copy)] struct PragmaPricesResponse { price: u256, @@ -154,5 +155,6 @@ struct PragmaPricesResponse { #[derive(Serde, Drop, Copy)] enum AggregationMode { Median: (), + // Mean: (), Error: (), } diff --git a/src/introspection.cairo b/src/introspection.cairo deleted file mode 100644 index 4391460f..00000000 --- a/src/introspection.cairo +++ /dev/null @@ -1 +0,0 @@ -mod erc165; diff --git a/src/introspection/erc165.cairo b/src/introspection/erc165.cairo deleted file mode 100644 index 4a009e36..00000000 --- a/src/introspection/erc165.cairo +++ /dev/null @@ -1,45 +0,0 @@ -// TAKEN FROM : https://github.com/OpenZeppelin/cairo-contracts/blob/cairo-1/src/openzeppelin/introspection/erc165.cairo -// CREDITS TO OZ TEAM - -const IERC165_ID: u32 = 0x01ffc9a7_u32; -const INVALID_ID: u32 = 0xffffffff_u32; - - -#[starknet::interface] -trait IERC165 { - fn supports_interface(self: @TContractState, interface_id: u32) -> bool; -} - -#[starknet::contract] -mod ERC165 { - use super::IERC165_ID; - use super::INVALID_ID; - use super::IERC165; - - #[storage] - struct Storage { - supported_interfaces: LegacyMap - } - - impl ERC165 of IERC165 { - fn supports_interface(self: @ContractState, interface_id: u32) -> bool { - if interface_id == IERC165_ID { - return true; - } - self.supported_interfaces.read(interface_id) - } - } - - - #[internal] - fn register_interface(ref self: ContractState, interface_id: u32) { - assert(interface_id != INVALID_ID, 'Invalid id'); - self.supported_interfaces.write(interface_id, true); - } - - #[internal] - fn deregister_interface(ref self: ContractState, interface_id: u32) { - assert(interface_id != IERC165_ID, 'Invalid id'); - self.supported_interfaces.write(interface_id, false); - } -} diff --git a/src/lib.cairo b/src/lib.cairo index 2c474aa2..98e98a69 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -1,7 +1,5 @@ -// mod account; mod admin; mod entry; -mod introspection; mod operations; mod oracle; mod publisher_registry; diff --git a/src/operations/tests.cairo b/src/operations/tests.cairo deleted file mode 100644 index 51a376da..00000000 --- a/src/operations/tests.cairo +++ /dev/null @@ -1,2 +0,0 @@ -#[cfg(test)] -mod test_oracle; diff --git a/src/operations/time_series/metrics.cairo b/src/operations/time_series/metrics.cairo index f3c9026f..8e84d673 100644 --- a/src/operations/time_series/metrics.cairo +++ b/src/operations/time_series/metrics.cairo @@ -6,14 +6,16 @@ use array::{ArrayTrait, SpanTrait}; use traits::{Into, TryInto}; use option::OptionTrait; use box::BoxTrait; + const ONE_YEAR_IN_SECONDS: u128 = 31536000_u128; + #[derive(Copy, Drop)] enum Operations { SUBSTRACTION: (), MULTIPLICATION: (), } - +/// Returns an array of `u128` from `TickElem` array fn extract_value(tick_arr: Span) -> Array { let mut output = ArrayTrait::::new(); let mut cur_idx = 0; @@ -28,6 +30,7 @@ fn extract_value(tick_arr: Span) -> Array { output } +/// Sum the values of an array of `TickElem` fn sum_tick_array(tick_arr: Span) -> u128 { let mut output = 0; let mut cur_idx = 0; @@ -42,6 +45,7 @@ fn sum_tick_array(tick_arr: Span) -> u128 { output } +/// Sum the elements of an array of `u128` fn sum_array(tick_arr: Span) -> u128 { let mut output = 0; let mut cur_idx = 0; @@ -56,7 +60,7 @@ fn sum_array(tick_arr: Span) -> u128 { output } - +/// Computes the mean of a `TickElem` array fn mean(tick_arr: Span) -> u128 { let sum_ = sum_tick_array(tick_arr); let felt_count: felt252 = tick_arr.len().into(); @@ -64,6 +68,7 @@ fn mean(tick_arr: Span) -> u128 { sum_ / count } +/// Computes the variance of a `TickElem` array fn variance(tick_arr: Span) -> u128 { let arr_ = extract_value(tick_arr); let arr_len = arr_.len(); @@ -81,6 +86,8 @@ fn variance(tick_arr: Span) -> u128 { return variance_; } +/// Computes the standard deviation of a `TickElem` array +/// Calls `variance` and computes the squared root fn standard_deviation(arr: Span) -> Fixed { let variance_ = variance(arr); let fixed_variance_ = FixedTrait::new(variance_, false); @@ -88,6 +95,7 @@ fn standard_deviation(arr: Span) -> Fixed { std } +/// Compute the volatility of a `TickElem` array fn volatility(arr: Span) -> Fixed { let _volatility_sum = _sum_volatility(arr); let arr_len = arr.len(); @@ -127,6 +135,8 @@ fn _sum_volatility(arr: Span) -> Fixed { sum } +/// Computes a result array given two arrays and one operation +/// e.g : [1, 2, 3] + [1, 2, 3] = [2, 4, 6] fn pairwise_1D(operation: Operations, x_len: u32, x: Span, y: Span) -> Span { let mut cur_idx: u32 = 0; let mut output = ArrayTrait::::new(); @@ -157,6 +167,7 @@ fn pairwise_1D(operation: Operations, x_len: u32, x: Span, y: Span) output.span() } +/// Fills an array with one `value` fn fill_1d(arr_len: u32, value: u128) -> Array { let mut cur_idx = 0; let mut output = ArrayTrait::new(); diff --git a/src/operations/time_series/scaler.cairo b/src/operations/time_series/scaler.cairo index 774483f4..c69fd414 100644 --- a/src/operations/time_series/scaler.cairo +++ b/src/operations/time_series/scaler.cairo @@ -11,6 +11,8 @@ fn calculate_slope(x1: i129, x2: i129, y1: i129, y2: i129) -> i129 { (y2 - y1) / (x2 - x1) } +/// Scales an array of `TickElem` by returning an array of `TickElem` with `num_intervals` elements +/// Takes a start and end tick as an input. fn scale_data( start_tick: u64, end_tick: u64, tick_array: Span, num_intervals: u32 ) -> Array { diff --git a/src/operations/time_series/structs.cairo b/src/operations/time_series/structs.cairo index 6d662ef2..fb065f73 100644 --- a/src/operations/time_series/structs.cairo +++ b/src/operations/time_series/structs.cairo @@ -1,12 +1,12 @@ use array::ArrayTrait; use integer::u32; + #[derive(Drop, Copy)] struct TickElem { tick: u64, value: u128 } - struct List { length: u32, size: u32, diff --git a/src/oracle/library.cairo b/src/oracle/library.cairo deleted file mode 100644 index e39426d5..00000000 --- a/src/oracle/library.cairo +++ /dev/null @@ -1,862 +0,0 @@ -// // #[contract] -// // mod Library { -// // use starknet::get_caller_address; -// // use zeroable::Zeroable; -// // use cmp::{max, min}; -// // use entry::contracts::entry::Entry; -// // use option::OptionTrait; -// // use array::{ArrayTrait, SpanTrait}; -// // use traits::Into; -// // use traits::TryInto; -// // use box::BoxTrait; -// // use result::{ResultTrait, ResultTraitImpl}; -// // use entry::contracts::structs::{ -// // BaseEntry, SpotEntry, Currency, Pair, DataType, PragmaPricesResponse, Checkpoint, -// // USD_CURRENCY_ID, SPOT, FUTURE, OPTION, PossibleEntryStorage, FutureEntry, OptionEntry, -// // simpleDataType, SpotEntryStorage, FutureEntryStorage, AggregationMode, PossibleEntries, -// // ArrayEntry -// // }; - -// // use oracle::business_logic::oracleInterface::IOracle; -// // use pragma::bits_manipulation::bits_manipulation::{ -// // actual_set_element_at, actual_get_element_at -// // }; -// // use pragma::time_series::convert::convert_via_usd; -// // use admin::contracts::Admin::Admin; -// // use serde::{Serde}; -// // use serde::deserialize_array_helper; -// // use serde::serialize_array_helper; -// // use starknet::{StorageAccess, StorageBaseAddress, SyscallResult}; -// // use starknet::{ -// // storage_read_syscall, storage_write_syscall, storage_address_from_base_and_offset, -// // storage_access::storage_base_address_from_felt252 -// // }; -// // use starknet::{ContractAddress, Felt252TryIntoContractAddress}; -// // use starknet::{get_block_timestamp}; -// // use publisher_registry::contracts::publisher_registry::{ -// // IPublisherRegistryABIDispatcher, IPublisherRegistryABIDispatcherTrait -// // }; -// // const BACKWARD_TIMESTAMP_BUFFER: u64 = 7800; // 2 hours and 10 minutes - -// // //Structure - -// struct Storage { -// //oracle controller address storage, contractAddress -// oracle_controller_address_storage: ContractAddress, -// // oracle publisher registry address, ContractAddres -// oracle_publisher_registry_address_storage: ContractAddress, -// //oracle pair storage, legacy map between the pair_id and the pair in question (no need to specify the data type here). -// oracle_pairs_storage: LegacyMap::, -// //oracle_pair_id_storage, legacy Map between (quote_currency_id, base_currency_id) and the pair_id -// oracle_pair_id_storage: LegacyMap::<(felt252, felt252), felt252>, -// //oracle_currencies_storage, legacy Map between (currency_id) and the currency -// oracle_currencies_storage: LegacyMap::, -// //oralce_sources_storage, legacyMap between (pair_id ,(SPOT/FUTURES/OPTIONS), index, expiration_timestamp ) and the source -// oracle_sources_storage: LegacyMap::<(felt252, felt252, u64, u64), felt252>, -// //oracle_sources_len_storage, legacyMap between (pair_id ,(SPOT/FUTURES/OPTIONS), expiration_timestamp) and the len of the sources array -// oracle_sources_len_storage: LegacyMap::<(felt252, felt252, u64), u64>, -// //oracle_data_entry_storage, legacyMap between (pair_id, (SPOT/FUTURES/OPTIONS), source, expiration_timestamp (0 for SPOT)) -// oracle_data_entry_storage: LegacyMap::<(felt252, felt252, felt252, u64), u256>, -// //oracle_checkpoints, legacyMap between, (pair_id, (SPOT/FUTURES/OPTIONS), index, expiration_timestamp (0 for SPOT)) asociated to a checkpoint -// oracle_checkpoints: LegacyMap::<(felt252, felt252, u64, u64), Checkpoint>, -// //oracle_checkpoint_index, legacyMap between (pair_id, (SPOT/FUTURES/OPTIONS), expiration_timestamp (0 for SPOT)) and the index of the last checkpoint -// oracle_checkpoint_index: LegacyMap::<(felt252, felt252, u64), u64>, -// oracle_sources_threshold_storage: u32, -// } -// impl SpanSerde< -// T, impl TSerde: Serde, impl TCopy: Copy, impl TDrop: Drop -// > of Serde> { -// fn serialize(self: @Span, ref output: Array) { -// (*self).len().serialize(ref output); -// serialize_array_helper(*self, ref output); -// } -// fn deserialize(ref serialized: Span) -> Option> { -// let length = *serialized.pop_front()?; -// let mut arr = ArrayTrait::new(); -// Option::Some(deserialize_array_helper(ref serialized, arr, length)?.span()) -// } -// } - -// // #[event] -// // fn UpdatedPublisherRegistryAddress( -// // old_publisher_registry_address: ContractAddress, -// // new_publisher_registry_address: ContractAddress -// // ) {} - -// // #[event] -// // fn SubmittedSpotEntry(spot_entry: SpotEntry) {} - -// // #[event] -// // fn SubmittedFutureEntry(future_entry: FutureEntry) {} - -// // #[event] -// // fn SubmittedOptionEntry(option_entry: OptionEntry) {} - -// // #[event] -// // fn SubmittedCurrency(currency: Currency) {} - -// // #[event] -// // fn UpdatedCurrency(currency: Currency) {} - -// // #[event] -// // fn SubmittedPair(pair: Pair) {} - -// // #[event] -// // fn CheckpointSpotEntry(pair_id: felt252) {} - -// // #[event] -// // fn CheckpointFutureEntry(pair_id: felt252, expiration_timestamp: u64) {} - -// // fn initializer( -// // publisher_registry_address: ContractAddress, currencies: Span, pairs: Span -// // ) { -// // oracle_publisher_registry_address_storage::write(publisher_registry_address); -// // _set_keys_currencies(currencies, 0); -// // _set_keys_pairs(pairs); -// // } - -// // // -// // //Getters -// // // - -// // fn get_decimals(data_type: DataType) -> u32 { -// // let currency = match data_type { -// // DataType::SpotEntry(pair_id) => { -// // let pair = oracle_pairs_storage::read(pair_id); -// // oracle_currencies_storage::read(pair.base_currency_id) -// // }, -// // DataType::FutureEntry(( -// // pair_id, expiration_timestamp -// // )) => { -// // let pair = oracle_pairs_storage::read(pair_id); -// // oracle_currencies_storage::read(pair.base_currency_id) -// // }, -// // // DataType::OptionEntry((pair_id, expiration_timestamp)) => {} -// // }; -// // currency.decimals -// // } - -// // fn get_data( -// // data_type: DataType, aggregation_mode: AggregationMode, sources: Span -// // ) -> PragmaPricesResponse { -// // let mut entries = ArrayTrait::::new(); -// // let (entries, entries_len, last_updated_timestamp) = get_data_entries(data_type, sources); -// // if (entries_len == 0) { -// // return PragmaPricesResponse { -// // price: 0, -// // decimals: 0, -// // last_updated_timestamp: 0, -// // num_sources_aggregated: 0, -// // expiration_timestamp: Option::Some(0), -// // }; -// // } -// // let filtered_entries: ArrayEntry = filter_data_array(data_type, @entries); - -// // //TODO change timestmap -// // impl CheckpointStorageAccess of StorageAccess { -// // fn read(address_domain: u32, base: StorageBaseAddress) -> SyscallResult { -// // let timestamp_base = storage_base_address_from_felt252( -// // storage_address_from_base_and_offset(base, 0_u8).into() -// // ); -// // let timestamp: u64 = StorageAccess::::read(address_domain, timestamp_base)? -// // .try_into() -// // .unwrap(); - -// // fn get_data_with_USD_hop( -// // base_currency_id: felt252, -// // quote_currency_id: felt252, -// // aggregation_mode: AggregationMode, -// // typeof: simpleDataType, -// // expiration_timestamp: Option -// // ) -> PragmaPricesResponse { -// // let mut sources = ArrayTrait::::new().span(); -// // let base_pair_id = oracle_pair_id_storage::read((base_currency_id, USD_CURRENCY_ID)); -// // let quote_pair_id = oracle_pair_id_storage::read((quote_currency_id, USD_CURRENCY_ID)); -// // let (base_data_type, quote_data_type, currency) = match typeof { -// // simpleDataType::SpotEntry(()) => { -// // ( -// // DataType::SpotEntry(base_pair_id), -// // DataType::SpotEntry(quote_pair_id), -// // oracle_currencies_storage::read(quote_currency_id) -// // ) -// // }, -// // simpleDataType::FutureEntry(()) => { -// // match expiration_timestamp { -// // Option::Some(expiration) => { -// // let base_dt = DataType::FutureEntry((base_pair_id, expiration)); -// // let quote_dt = DataType::FutureEntry((quote_pair_id, expiration)); -// // (base_dt, quote_dt, oracle_currencies_storage::read(quote_currency_id)) -// // }, -// // Option::None(_) => { -// // // Handle case where Future data type was provided without an expiration timestamp -// // assert(1 == 1, 'Requires expiration timestamp'); -// // ( -// // DataType::FutureEntry((base_pair_id, 0)), -// // DataType::FutureEntry((quote_pair_id, 0)), -// // oracle_currencies_storage::read(quote_currency_id) -// // ) -// // } -// // } -// // }, -// // }; -// // let basePPR: PragmaPricesResponse = get_data(base_data_type, aggregation_mode, sources); -// // let quotePPR: PragmaPricesResponse = get_data(quote_data_type, aggregation_mode, sources); -// // let decimals = min(get_decimals(base_data_type), get_decimals(quote_data_type)); -// // let rebased_value = convert_via_usd(basePPR.price, quotePPR.price, decimals); -// // let last_updated_timestamp = max( -// // quotePPR.last_updated_timestamp, basePPR.last_updated_timestamp -// // ); -// // let num_sources_aggregated = max( -// // quotePPR.num_sources_aggregated, basePPR.num_sources_aggregated -// // ); -// // PragmaPricesResponse { -// // price: rebased_value, -// // decimals: decimals, -// // last_updated_timestamp: last_updated_timestamp, -// // num_sources_aggregated: num_sources_aggregated, -// // expiration_timestamp: expiration_timestamp, -// // } -// // } - -// // fn get_data_entry(data_type: DataType, source: felt252) -> PossibleEntries { -// // let _entry = match data_type { -// // DataType::SpotEntry(pair_id) => { -// // oracle_data_entry_storage::read((pair_id, source, SPOT, 0)) -// // }, -// // DataType::FutureEntry(( -// // pair_id, expiration_timestamp -// // )) => { -// // oracle_data_entry_storage::read((pair_id, source, FUTURE, expiration_timestamp)) -// // }, -// // }; -// // let u256_timestamp = actual_get_element_at(_entry, 0, 31); -// // let timestamp: u64 = u256_timestamp.try_into().unwrap().try_into().unwrap(); -// // let volume = actual_get_element_at(_entry, 32, 42); -// // let price = actual_get_element_at(_entry, 75, 128); -// // match data_type { -// // DataType::SpotEntry(pair_id) => { -// // PossibleEntries::Spot( -// // SpotEntry { -// // base: BaseEntry { -// // timestamp: timestamp, source: source, publisher: 0 -// // }, pair_id: pair_id, price: price, volume: volume -// // } -// // ) -// // }, -// // DataType::FutureEntry(( -// // pair_id, expiration_timestamp -// // )) => { -// // PossibleEntries::Future( -// // FutureEntry { -// // base: BaseEntry { -// // timestamp: timestamp, source: source, publisher: 0 -// // }, -// // pair_id: pair_id, -// // price: price, -// // volume: volume, -// // expiration_timestamp: expiration_timestamp -// // } -// // ) -// // }, -// // } -// // } - -// // // fn get_data_entry(data_type: DataType, source: felt252) -> T { -// // // let _entry = match data_type { -// // // DataType::SpotEntry(pair_id) => { -// // // oracle_data_entry_storage::read((pair_id, source, SPOT, 0)) -// // // }, -// // // DataType::FutureEntry(( -// // // pair_id, expiration_timestamp -// // // )) => { -// // // oracle_data_entry_storage::read((pair_id, source, FUTURE, expiration_timestamp)) -// // // }, -// // // }; -// // // let timestamp = actual_get_element_at(_entry.timestamp__volume__price, 0, 31); -// // // let volume = actual_get_element_at(_entry.timestamp__volume__price, 32, 42); -// // // let price = actual_get_element_at(_entry.timestamp__volume__price, 75, 128); -// // // match data_type { -// // // DataType::SpotEntry(pair_id) => { -// // // return SpotEntry { -// // // base: BaseEntry { -// // // timestamp: timestamp, source: source, publisher: 0 -// // // }, pair_id: pair_id, price: price, volume: volume -// // // }; -// // // }, -// // // DataType::FutureEntry(( -// // // pair_id, expiration_timestamp -// // // )) => { -// // // return FutureEntry { -// // // base: BaseEntry { -// // // timestamp: timestamp, source: source, publisher: 0 -// // // }, -// // // pair_id: pair_id, -// // // price: price, -// // // volume: volume, -// // // expiration_timestamp: expiration_timestamp -// // // }; -// // // }, -// // // } -// // // } - -// // fn get_admin_address() -> ContractAddress { -// // return Admin::get_admin_address(); -// // } - -// // fn get_publisher_registry_address() -> ContractAddress { -// // let publisher_registry_address = oracle_publisher_registry_address_storage::read(); -// // return publisher_registry_address; -// // } - -// // fn get_latest_checkpoint_index(data_type: DataType, aggregation_mode: AggregationMode) -> u64 { -// // let checkpoint_index = match data_type { -// // DataType::SpotEntry(pair_id) => { -// // oracle_checkpoint_index::read((pair_id, SPOT, 0)) -// // }, -// // DataType::FutureEntry(( -// // pair_id, expiration_timestamp -// // )) => { -// // oracle_checkpoint_index::read((pair_id, FUTURE, expiration_timestamp)) -// // }, -// // }; -// // return checkpoint_index; -// // } - -// // fn get_data_entries( -// // data_type: DataType, sources: Span -// // ) -> (Array, u32, u64) { -// // let last_updated_timestamp = get_latest_entry_timestamp(data_type, sources); -// // let current_timestamp: u64 = get_block_timestamp(); -// // let conservative_current_timestamp = min(last_updated_timestamp, current_timestamp); -// // let (entries, entries_len) = get_all_entries( -// // data_type, sources, conservative_current_timestamp -// // ); -// // return (entries, entries_len, last_updated_timestamp); -// // } - -// // fn get_sources_threshold() -> u32 { -// // oracle_sources_threshold_storage::read() -// // } -// // fn get_latest_checkpoint(data_type: DataType, aggregation_mode: AggregationMode) -> Checkpoint { -// // let cur_idx = get_latest_checkpoint_index(data_type, aggregation_mode); -// // let latest_checkpoint = match data_type { -// // DataType::SpotEntry(pair_id) => { -// // oracle_checkpoints::read((pair_id, SPOT, cur_idx, 0)) -// // }, -// // DataType::FutureEntry(( -// // pair_id, expiration_timestamp -// // )) => { -// // oracle_checkpoints::read((pair_id, FUTURE, cur_idx, expiration_timestamp)) -// // }, -// // }; -// // return latest_checkpoint; -// // } - -// // // -// // //Setters -// // // - -// // fn publish_data(new_entry: PossibleEntries) { -// // match new_entry { -// // PossibleEntries::Spot(spot_entry) => { -// // validate_sender_for_source(spot_entry); -// // let entry: PossibleEntries = get_data_entry( -// // DataType::SpotEntry(spot_entry.pair_id), spot_entry.base.source -// // ); -// // match entry { -// // PossibleEntries::Spot(spot) => { -// // validate_data_timestamp(new_entry, spot); -// // SubmittedSpotEntry(spot_entry); -// // let conv_timestamp: u256 = u256 { -// // low: spot_entry.base.timestamp.into(), high: 0 -// // }; -// // let element = actual_set_element_at(0, 0, 31, conv_timestamp); -// // let element = actual_set_element_at(element, 32, 42, spot_entry.volume); -// // let element = actual_set_element_at(element, 75, 128, spot_entry.price); -// // let spot_entry_storage = SpotEntryStorage { -// // timestamp__volume__price: element -// // }; -// // oracle_data_entry_storage::write( -// // (spot_entry.pair_id, SPOT, spot_entry.base.source, 0), element -// // ); -// // }, -// // PossibleEntries::Future(_) => { -// // assert(1 == 1, 'Failed fetching spot entry'); -// // }, -// // } -// // }, -// // PossibleEntries::Future(future_entry) => { -// // validate_sender_for_source(future_entry); -// // let entry: PossibleEntries = get_data_entry( -// // DataType::FutureEntry( -// // (future_entry.pair_id, future_entry.expiration_timestamp) -// // ), -// // future_entry.base.source -// // ); -// // match entry { -// // PossibleEntries::Spot(_) => { -// // assert(1 == 1, 'Failed fetching future entry'); -// // }, -// // PossibleEntries::Future(future) => { -// // validate_data_timestamp::(new_entry, future); -// // SubmittedFutureEntry(future_entry); -// // let conv_timestamp: u256 = u256 { -// // low: future_entry.base.timestamp.into(), high: 0 -// // }; -// // let element = actual_set_element_at(0, 0, 31, conv_timestamp); -// // let element = actual_set_element_at(element, 32, 42, future_entry.volume); -// // let element = actual_set_element_at(element, 75, 128, future_entry.price); -// // let future_entry_storage = FutureEntryStorage { -// // timestamp__volume__price: element -// // }; -// // oracle_data_entry_storage::write( -// // ( -// // future_entry.pair_id, -// // FUTURE, -// // future_entry.base.source, -// // future_entry.expiration_timestamp -// // ), -// // element -// // ); -// // }, -// // } -// // }, -// // } -// // return (); -// // } -// // fn update_publisher_registry_address(new_publisher_registry_address: ContractAddress) { -// // let old_publisher_registry_address = oracle_publisher_registry_address_storage::read(); -// // oracle_publisher_registry_address_storage::write(new_publisher_registry_address); -// // UpdatedPublisherRegistryAddress( -// // old_publisher_registry_address, new_publisher_registry_address -// // ); -// // return (); -// // } - -// // fn add_currency(currency: Currency) { -// // let existing_currency = oracle_currencies_storage::read(currency.id); -// // assert(existing_currency.id == 0, 'Currency already exists for key'); -// // SubmittedCurrency(currency); -// // oracle_currencies_storage::write(currency.id, currency); -// // return (); -// // } - -// // fn update_currency(currency: Currency, typeof: felt252) { -// // oracle_currencies_storage::write(currency.id, currency); -// // UpdatedCurrency(currency); -// // return (); -// // } - -// // fn set_checkpoint(data_type: DataType, aggregation_mode: AggregationMode) { -// // let mut sources = ArrayTrait::::new().span(); -// // let priceResponse = get_data(data_type, aggregation_mode, sources); -// // let sources_threshold = oracle_sources_threshold_storage::read(); -// // let cur_checkpoint = get_latest_checkpoint(data_type, aggregation_mode); -// // let timestamp: u64 = get_block_timestamp(); -// // if (sources_threshold < priceResponse.num_sources_aggregated -// // & (cur_checkpoint.timestamp + 1) < timestamp) { -// // let new_checkpoint = Checkpoint { -// // timestamp: timestamp, -// // value: priceResponse.price, -// // aggregation_mode: aggregation_mode, -// // num_sources_aggregated: priceResponse.num_sources_aggregated -// // }; -// // match data_type { -// // DataType::SpotEntry(pair_id) => { -// // let cur_idx = oracle_checkpoint_index::read((pair_id, SPOT, 0)); -// // oracle_checkpoints::write((pair_id, SPOT, cur_idx, 0), new_checkpoint); -// // oracle_checkpoint_index::write((pair_id, SPOT, 0), cur_idx + 1); -// // CheckpointSpotEntry(pair_id); -// // }, -// // DataType::FutureEntry(( -// // pair_id, expiration_timestamp -// // )) => { -// // let cur_idx = oracle_checkpoint_index::read( -// // (pair_id, FUTURE, expiration_timestamp) -// // ); -// // oracle_checkpoints::write( -// // (pair_id, FUTURE, cur_idx, expiration_timestamp), new_checkpoint -// // ); -// // oracle_checkpoint_index::write( -// // (pair_id, FUTURE, expiration_timestamp), cur_idx + 1 -// // ); -// // CheckpointFutureEntry(pair_id, expiration_timestamp); -// // }, -// // } -// // } -// // return (); -// // } - -// // fn set_checkpoints(data_types: Span, aggregation_mode: AggregationMode) { -// // let mut cur_idx: u32 = 0; -// // loop { -// // if (cur_idx == data_types.len()) { -// // break (); -// // } -// // let data_type: DataType = *data_types.get(cur_idx).unwrap().unbox(); -// // set_checkpoint(data_type, aggregation_mode); -// // cur_idx += 1; -// // } -// // } - -// // fn get_decimals_for_currency(currency_id: felt252, typeof: felt252) -> u32 { -// // let key_currency = oracle_currencies_storage::read(currency_id); -// // if (key_currency.id == 0) { -// // return 0; -// // } -// // key_currency.decimals -// // } - -// // // -// // //Guard -// // // -// // fn only_oracle_controller() { -// // let caller_address = get_caller_address(); -// // let oracle_controller_address = oracle_controller_address_storage::read(); -// // if (oracle_controller_address.is_zero()) { -// // return (); -// // } -// // assert(caller_address == oracle_controller_address, 'Needs to be oracle controller'); -// // return (); -// // } - -// // // -// // //Internal -// // // -// // fn build_sources_array(data_type: DataType, ref sources: Array, idx: u64) { -// // match data_type { -// // DataType::SpotEntry(pair_id) => { -// // let new_source = oracle_sources_storage::read((pair_id, SPOT, idx, 0)); -// // sources.append(new_source); -// // }, -// // DataType::FutureEntry(( -// // pair_id, expiration_timestamp -// // )) => { -// // let new_source = oracle_sources_storage::read( -// // (pair_id, FUTURE, idx, expiration_timestamp) -// // ); -// // sources.append(new_source); -// // } -// // } -// // } - -// // fn get_latest_entry_timestamp(data_type: DataType, sources: Span) -> u64 { -// // let mut cur_idx = 0; -// // let mut latest_timestamp = 0; -// // loop { -// // if (cur_idx >= sources.len()) { -// // break (); -// // } -// // let source: felt252 = *sources.get(cur_idx).unwrap().unbox(); -// // let entry: PossibleEntries = get_data_entry(data_type, source); -// // match entry { -// // PossibleEntries::Spot(spot_entry) => { -// // if spot_entry.base.timestamp > latest_timestamp { -// // latest_timestamp = spot_entry.base.timestamp; -// // } -// // }, -// // PossibleEntries::Future(future_entry) => { -// // if future_entry.base.timestamp > latest_timestamp { -// // latest_timestamp = future_entry.base.timestamp; -// // } -// // } -// // } -// // cur_idx += 1; -// // }; -// // return latest_timestamp; -// // } - -// // fn build_entries_array( -// // data_type: DataType, -// // sources: Span, -// // ref entries: Array, -// // latest_timestamp: u64 -// // ) { -// // let mut cur_idx = 0; -// // loop { -// // if (cur_idx >= sources.len()) { -// // break (); -// // } -// // let source: felt252 = *sources.get(cur_idx).unwrap().unbox(); -// // let g_entry: PossibleEntries = get_data_entry(data_type, source); -// // match g_entry { -// // PossibleEntries::Spot(spot_entry) => { -// // let is_entry_not_initialized: bool = spot_entry.get_base_timestamp() == 0; -// // let condition: bool = is_entry_not_initialized -// // & (spot_entry -// // .get_base_timestamp() < (latest_timestamp - BACKWARD_TIMESTAMP_BUFFER)); -// // if !condition { -// // entries.append(PossibleEntries::Spot(spot_entry)); -// // } -// // }, -// // PossibleEntries::Future(future_entry) => { -// // let is_entry_not_initialized: bool = future_entry.get_base_timestamp() == 0; -// // let condition: bool = is_entry_not_initialized -// // & (future_entry -// // .get_base_timestamp() < (latest_timestamp - BACKWARD_TIMESTAMP_BUFFER)); -// // if !condition { -// // entries.append(PossibleEntries::Future(future_entry)); -// // } -// // } -// // }; - -// // cur_idx += 1; -// // }; -// // return (); -// // } - -// // fn get_checkpoint_by_index(data_type: DataType, checkpoint_index: u64) -> Checkpoint { -// // let checkpoint = match data_type { -// // DataType::SpotEntry(pair_id) => { -// // oracle_checkpoints::read((pair_id, SPOT, checkpoint_index, 0)) -// // }, -// // DataType::FutureEntry(( -// // pair_id, expiration_timestamp -// // )) => { -// // oracle_checkpoints::read((pair_id, FUTURE, checkpoint_index, expiration_timestamp)) -// // }, -// // }; -// // return checkpoint; -// // } - -// // fn validate_sender_for_source, impl TDrop: Drop>( -// // _entry: T -// // ) { -// // let publisher_registry_address = get_publisher_registry_address(); -// // let publisher_registry_dispatcher = IPublisherRegistryABIDispatcher { -// // contract_address: publisher_registry_address -// // }; -// // let publisher_address = publisher_registry_dispatcher -// // .get_publisher_address(_entry.get_base_entry().publisher); - -// // let _can_publish_source = publisher_registry_dispatcher -// // .can_publish_source(_entry.get_base_entry().publisher, _entry.get_base_entry().source); -// // //CHECK IF THIS VERIFICATION WORKS -// // let caller_address = get_caller_address(); -// // assert(publisher_address.is_zero(), 'Publisher is not registered'); -// // assert(!caller_address.is_zero(), 'Caller must not be zero address'); -// // assert(caller_address == publisher_address, 'Transaction not from publisher'); -// // assert(_can_publish_source == true, 'Not allowed for source'); -// // return (); -// // } - -// // fn get_all_sources(data_type: DataType) -> Array { -// // let mut sources = ArrayTrait::::new(); -// // match data_type { -// // DataType::SpotEntry(pair_id) => { -// // let source_len = oracle_sources_len_storage::read((pair_id, SPOT, 0)); -// // build_sources_array(data_type, ref sources, source_len); -// // return sources; -// // }, -// // DataType::FutureEntry(( -// // pair_id, expiration_timestamp -// // )) => { -// // let source_len = oracle_sources_len_storage::read( -// // (pair_id, FUTURE, expiration_timestamp) -// // ); -// // build_sources_array(data_type, ref sources, source_len); -// // return sources; -// // }, -// // } -// // } - -// // fn get_all_entries( -// // data_type: DataType, sources: Span, max_timestamp: u64 -// // ) -> (Array, u32) { -// // let mut entries = ArrayTrait::::new(); -// // if (sources.len() == 0) { -// // let all_sources = get_all_sources(data_type).span(); -// // build_entries_array(data_type, all_sources, ref entries, max_timestamp); -// // (entries, entries.len()) -// // } else { -// // build_entries_array(data_type, sources, ref entries, max_timestamp); -// // (entries, entries.len()) -// // } -// // } - -// // fn filter_data_array(data_type: DataType, data: @Array) -> ArrayEntry { -// // match data_type { -// // DataType::SpotEntry(pair_id) => { -// // let mut cur_idx = 0; -// // let mut spot_entries = ArrayTrait::::new(); -// // loop { -// // if (cur_idx >= data.len()) { -// // break (); -// // } -// // let entry = *data.at(cur_idx); -// // match entry { -// // PossibleEntries::Spot(spot_entry) => { -// // spot_entries.append(spot_entry); -// // }, -// // PossibleEntries::Future(_) => { -// // assert(false, 'Invalid entry type'); -// // } -// // } -// // cur_idx = cur_idx + 1; -// // }; -// // ArrayEntry::SpotEntry(spot_entries) -// // }, -// // DataType::FutureEntry(( -// // pair_id, expiration_timestamp -// // )) => { -// // let mut cur_idx = 0; -// // let mut future_entries = ArrayTrait::::new(); -// // loop { -// // if (cur_idx >= data.len()) { -// // break (); -// // } -// // let entry = *data.at(cur_idx); -// // match entry { -// // PossibleEntries::Spot(_) => { -// // assert(false, 'Invalid entry type'); -// // }, -// // PossibleEntries::Future(future_entry) => { -// // future_entries.append(future_entry); -// // } -// // } -// // cur_idx = cur_idx + 1; -// // }; -// // ArrayEntry::FutureEntry(future_entries) -// // } -// // } -// // } -// // fn validate_data_timestamp, impl TDrop: Drop>( -// // new_entry: PossibleEntries, last_entry: T -// // ) { -// // match new_entry { -// // PossibleEntries::Spot(spot_entry) => { -// // assert( -// // spot_entry.get_base_timestamp() > last_entry.get_base_timestamp(), -// // 'Existing entry is more recent' -// // ); -// // if (last_entry.get_base_timestamp() == 0) { -// // let sources_len = oracle_sources_len_storage::read( -// // (spot_entry.pair_id, SPOT, 0) -// // ); -// // oracle_sources_storage::write( -// // (spot_entry.pair_id, SPOT, sources_len, 0), -// // spot_entry.get_base_entry().source -// // ); -// // oracle_sources_len_storage::write( -// // (spot_entry.pair_id, SPOT, 0), sources_len + 1 -// // ); -// // } -// // }, -// // PossibleEntries::Future(future_entry) => { -// // assert( -// // future_entry.get_base_timestamp() > last_entry.get_base_timestamp(), -// // 'Existing entry is more recent' -// // ); -// // if (last_entry.get_base_timestamp() == 0) { -// // let sources_len = oracle_sources_len_storage::read( -// // (future_entry.pair_id, FUTURE, future_entry.expiration_timestamp) -// // ); -// // oracle_sources_storage::write( -// // ( -// // future_entry.pair_id, -// // FUTURE, -// // sources_len, -// // future_entry.expiration_timestamp -// // ), -// // future_entry.get_base_entry().source -// // ); -// // oracle_sources_len_storage::write( -// // (future_entry.pair_id, FUTURE, future_entry.expiration_timestamp), -// // sources_len + 1 -// // ); -// // } -// // }, -// // // PossibleEntries::OptionEntry(option_entry) => {} -// // } -// // return (); -// // } - -// // fn add_pair(pair: Pair) { -// // let check_pair = oracle_pairs_storage::read(pair.id); -// // assert(check_pair.id == 0, 'Pair with this key registered'); -// // SubmittedPair(pair); -// // oracle_pairs_storage::write(pair.id, pair); -// // oracle_pair_id_storage::write((pair.quote_currency_id, pair.base_currency_id), pair.id); -// // return (); -// // } - -// // fn _set_keys_currencies(key_currencies: Span, idx: usize) { -// // let mut idx: u32 = 0; -// // loop { -// // if (idx == key_currencies.len()) { -// // break (); -// // } - -// // let key_currency = *key_currencies.get(idx).unwrap().unbox(); -// // oracle_currencies_storage::write(key_currency.id, key_currency); -// // idx = idx + 1; -// // }; -// // return (); -// // } - -// // fn set_sources_threshold(threshold: u32) { -// // oracle_sources_threshold_storage::write(threshold); -// // return (); -// // } - -// // fn find_startpoint( -// // data_type: DataType, aggregation_mode: AggregationMode, timestamp: u64 -// // ) -> u64 { -// // let last_checkpoint_index = get_latest_checkpoint_index(data_type, aggregation_mode); -// // let latest_checkpoint_index = get_latest_checkpoint_index(data_type, aggregation_mode); -// // let cp = get_checkpoint_by_index(data_type, latest_checkpoint_index - 1); -// // let first_cp = get_checkpoint_by_index(data_type, 0); -// // if (cp.timestamp < timestamp) { -// // return latest_checkpoint_index - 1; -// // } - -// // if (timestamp < first_cp.timestamp) { -// // return 0; -// // } -// // let startpoint = _binary_search(data_type, 0, latest_checkpoint_index, timestamp); -// // return startpoint; -// // } - -// // fn _binary_search(data_type: DataType, low: u64, high: u64, target: u64) -> u64 { -// // let midpoint = (low + high) / 2; - -// // if (high == low) { -// // return midpoint; -// // } - -// // if ((high + 1) <= low) { -// // return low - 1; -// // } - -// // let cp = get_checkpoint_by_index(data_type, midpoint); -// // let timestamp = cp.timestamp; - -// // if (timestamp == target) { -// // return midpoint; -// // } - -// // if (target <= timestamp) { -// // return _binary_search(data_type, low, midpoint - 1, target); -// // } else { -// // return _binary_search(data_type, midpoint + 1, high, target); -// // } -// // } - -// // fn _set_keys_pairs(key_pairs: Span) { -// // let mut idx: u32 = 0; -// // loop { -// // if (idx >= key_pairs.len()) { -// // break (); -// // } -// // let key_pair = *key_pairs.get(idx).unwrap().unbox(); -// // oracle_pairs_storage::write(key_pair.id, key_pair); -// // oracle_pair_id_storage::write( -// // (key_pair.quote_currency_id, key_pair.base_currency_id), key_pair.id -// // ); -// // idx = idx + 1; -// // }; -// // return (); -// // } -// // } - - diff --git a/src/oracle/oracle.cairo b/src/oracle/oracle.cairo index dc80f39b..c38f5383 100644 --- a/src/oracle/oracle.cairo +++ b/src/oracle/oracle.cairo @@ -1,28 +1,24 @@ use pragma::entry::structs::{ BaseEntry, SpotEntry, Currency, Pair, DataType, PragmaPricesResponse, Checkpoint, USD_CURRENCY_ID, SPOT, FUTURE, OPTION, PossibleEntryStorage, FutureEntry, OptionEntry, - simpleDataType, SpotEntryStorage, FutureEntryStorage, AggregationMode, PossibleEntries, + SimpleDataType, SpotEntryStorage, FutureEntryStorage, AggregationMode, PossibleEntries, ArrayEntry }; -use serde::{Serde}; -// use serde::deserialize_array_helper; -// use serde::serialize_array_helper; -use starknet::{StorageAccess, StorageBaseAddress, SyscallResult}; +use pragma::oracle::oracleInterface::IOracle; +use pragma::admin::admin::Admin; +use pragma::upgradeable::upgradeable::Upgradeable; +use serde::Serde; use starknet::{ storage_read_syscall, storage_write_syscall, storage_address_from_base_and_offset, - storage_access::storage_base_address_from_felt252 + storage_access::storage_base_address_from_felt252, StorageAccess, StorageBaseAddress, + SyscallResult, ContractAddress, get_caller_address }; -use traits::Into; -use traits::TryInto; -use box::BoxTrait; -use result::{ResultTrait, ResultTraitImpl}; -use pragma::oracle::oracleInterface::IOracle; -use starknet::{ContractAddress, get_caller_address}; -use array::{ArrayTrait}; -use pragma::admin::admin::Admin; use starknet::class_hash::ClassHash; +use traits::{Into, TryInto}; +use result::{ResultTrait, ResultTraitImpl}; +use box::BoxTrait; +use array::ArrayTrait; use zeroable::Zeroable; -use pragma::upgradeable::upgradeable::Upgradeable; #[starknet::interface] trait IOracleABI { @@ -69,7 +65,7 @@ trait IOracleABI { base_currency_id: felt252, quote_currency_id: felt252, aggregation_mode: AggregationMode, - typeof: simpleDataType, + typeof: SimpleDataType, expiration_timestamp: Option:: ) -> PragmaPricesResponse; @@ -154,7 +150,7 @@ trait IPragmaABI { base_currency_id: felt252, quote_currency_id: felt252, aggregation_mode: AggregationMode, - typeof: simpleDataType, + typeof: SimpleDataType, expiration_timestamp: Option:: ) -> PragmaPricesResponse; @@ -166,46 +162,33 @@ trait IPragmaABI { #[starknet::contract] mod Oracle { - use starknet::get_caller_address; - use zeroable::Zeroable; - use cmp::{max, min}; - use pragma::entry::entry::Entry; - use option::OptionTrait; - use array::{ArrayTrait, SpanTrait}; - use traits::Into; - use traits::TryInto; - use box::BoxTrait; - use result::{ResultTrait, ResultTraitImpl}; - use pragma::entry::structs::{ + use super::{ BaseEntry, SpotEntry, Currency, Pair, DataType, PragmaPricesResponse, Checkpoint, USD_CURRENCY_ID, SPOT, FUTURE, OPTION, PossibleEntryStorage, FutureEntry, OptionEntry, - simpleDataType, SpotEntryStorage, FutureEntryStorage, AggregationMode, PossibleEntries, - ArrayEntry + SimpleDataType, SpotEntryStorage, FutureEntryStorage, AggregationMode, PossibleEntries, + ArrayEntry, IOracle, Admin, Upgradeable, Serde, storage_read_syscall, storage_write_syscall, + storage_address_from_base_and_offset, storage_base_address_from_felt252, StorageAccess, + StorageBaseAddress, SyscallResult, ContractAddress, get_caller_address, ClassHash, Into, + TryInto, ResultTrait, ResultTraitImpl, BoxTrait, ArrayTrait, Zeroable }; - use pragma::oracle::oracleInterface::IOracle; + use pragma::entry::entry::Entry; use pragma::operations::bits_manipulation::bits_manipulation::{ actual_set_element_at, actual_get_element_at }; - use pragma::upgradeable::upgradeable::Upgradeable; use pragma::operations::time_series::convert::convert_via_usd; - use pragma::admin::admin::Admin; - use serde::{Serde}; - // use serde::deserialize_array_helper; - // use serde::serialize_array_helper; - use starknet::{StorageAccess, StorageBaseAddress, SyscallResult}; - use starknet::{ - storage_read_syscall, storage_write_syscall, storage_address_from_base_and_offset, - storage_access::storage_base_address_from_felt252 - }; - use starknet::{ContractAddress, Felt252TryIntoContractAddress}; - use starknet::{get_block_timestamp}; use pragma::publisher_registry::publisher_registry::{ IPublisherRegistryABIDispatcher, IPublisherRegistryABIDispatcherTrait }; - use starknet::ClassHash; - use hash::LegacyHash; + + use starknet::{get_block_timestamp, Felt252TryIntoContractAddress}; + + use cmp::{max, min}; + use option::OptionTrait; + use array::SpanTrait; + const BACKWARD_TIMESTAMP_BUFFER: u64 = 7800; // 2 hours and 10 minutes + #[storage] struct Storage { //oracle controller address storage, contractAddress @@ -231,40 +214,8 @@ mod Oracle { oracle_sources_threshold_storage: u32, } - // impl SpanSerde< - // T, impl TSerde: Serde, impl TCopy: Copy, impl TDrop: Drop - // > of Serde> { - // fn serialize(self: @Span, ref output: Array) { - // (*self).len().serialize(ref output); - // serialize_array_helper(*self, ref output); - // } - // fn deserialize(ref serialized: Span) -> Option> { - // let length = *serialized.pop_front()?; - // let mut arr = ArrayTrait::new(); - // Option::Some(deserialize_array_helper(ref serialized, arr, length)?.span()) - // } - // } - - //ORACLE DATA ENTRY STORAGE -> CHECK - trait workingEntry { - fn process(self: @T) -> felt252; - } - impl SworkingEntryImpl of workingEntry { - fn process(self: @SpotEntry) -> felt252 { - return (SPOT); - } - } - impl FworkingEntryImpl of workingEntry { - fn process(self: @FutureEntry) -> felt252 { - return (FUTURE); - } - } - impl OworkingEntryImpl of workingEntry { - fn process(self: @OptionEntry) -> felt252 { - return (OPTION); - } - } - + /// DataType should implement this trait + /// If it has a `base_entry` field defined by `BaseEntry` struct trait hasBaseEntry { fn get_base_entry(self: @T) -> BaseEntry; fn get_base_timestamp(self: @T) -> u64; @@ -298,6 +249,8 @@ mod Oracle { } } + /// DataType should implement this trait + /// If it has a `price` field defined in `self` trait HasPrice { fn get_price(self: @T) -> u256; } @@ -882,7 +835,7 @@ mod Oracle { base_currency_id: felt252, quote_currency_id: felt252, aggregation_mode: AggregationMode, - typeof: simpleDataType, + typeof: SimpleDataType, expiration_timestamp: Option ) -> PragmaPricesResponse { let mut sources = ArrayTrait::::new().span(); @@ -893,14 +846,14 @@ mod Oracle { .oracle_pair_id_storage .read((quote_currency_id, USD_CURRENCY_ID)); let (base_data_type, quote_data_type, currency) = match typeof { - simpleDataType::SpotEntry(()) => { + SimpleDataType::SpotEntry(()) => { ( DataType::SpotEntry(base_pair_id), DataType::SpotEntry(quote_pair_id), self.oracle_currencies_storage.read(quote_currency_id) ) }, - simpleDataType::FutureEntry(()) => { + SimpleDataType::FutureEntry(()) => { match expiration_timestamp { Option::Some(expiration) => { let base_dt = DataType::FutureEntry((base_pair_id, expiration)); diff --git a/src/oracle/oracleInterface.cairo b/src/oracle/oracleInterface.cairo index 880d5ad7..f09c25e0 100644 --- a/src/oracle/oracleInterface.cairo +++ b/src/oracle/oracleInterface.cairo @@ -1,6 +1,6 @@ use pragma::entry::structs::{ BaseEntry, SpotEntry, Currency, Pair, DataType, PragmaPricesResponse, Checkpoint, - simpleDataType, AggregationMode, PossibleEntries + SimpleDataType, AggregationMode, PossibleEntries }; use array::{ArrayTrait, SpanTrait}; use starknet::{ContractAddress, ClassHash}; @@ -48,7 +48,7 @@ trait IOracle { base_currency_id: felt252, quote_currency_id: felt252, aggregation_mode: AggregationMode, - typeof: simpleDataType, + typeof: SimpleDataType, expiration_timestamp: Option:: ) -> PragmaPricesResponse; fn get_publisher_registry_address(self: @TContractState) -> ContractAddress; diff --git a/src/oracle/pragmaInterface.cairo b/src/oracle/pragmaInterface.cairo deleted file mode 100644 index 48babf96..00000000 --- a/src/oracle/pragmaInterface.cairo +++ /dev/null @@ -1,93 +0,0 @@ -use entry::contracts::structs::{ - BaseEntry, SpotEntry, Currency, Pair, DataType, PragmaPricesResponse, Checkpoint, -}; -use array::ArrayTrait; -trait IPragmaOracle { - /// Get info about some data e.g spot, future, generic - /// Queried data should implement the `Query` trait - /// - /// # Arguments - /// - /// * `aggregation_mode` - Aggregation mode to use for the price - /// * `data_type` - Type of the data to get - /// - /// # Returns - /// - /// * `price` - Price of the pair - /// * `decimals` - Number of decimals of the price - /// * `last_updated_timestamp` - Timestamp of the most recent update, UTC epoch - /// * `num_sources_aggregated` - Number of sources aggregated into this price - fn get_data(data_type: DataType, aggregation_mode: felt252) -> PragmaPricesResponse; - /// Get a specific data entry e.g spot, future, generic - /// - /// # Arguments - /// - /// * `source` - UTF-8 encoded uppercased string, e.g. "GEMINI" - /// * `data_type` - Type of the price to get - /// * `expiration_timestamp` - Expiration timestamp if applicable (Futures) - /// - /// # Returns - /// - /// * `T` - Data entry - fn get_data_entry(source: felt252, data_type: DataType) -> T; - /// Get the median of some data e.g spot, future, generic - /// - /// # Arguments - /// - /// * `data_type` - Type of the data to get - /// * `expiration_timestamp` - Expiration timestamp if applicable (Futures) - /// - /// # Returns - /// - /// * `price` - Median price of the pair - fn get_data_median(data_type: DataType, ) -> PragmaPricesResponse; - ///Get the data entries for a specific data type for given sources - /// - /// # Arguments - /// - /// * `expiration_timestamp` - Expiration timestamp if applicable (Futures) - /// * `data_type` - Type of the data to get - /// * `sources` - Array of sources to get data for - /// - /// # Returns - /// - /// * `Array::` - Array of data entries - fn get_data_entries_for_sources( - expiration_timestamp: Option::, data_type: DataType, sources: @Array - ) -> Array::; - ///Get the data entries for a specific data type for all the sources - /// - /// # Arguments - /// - /// * `data_type` - Type of the data to get - /// * `sources` - Array of sources to get data for - /// * `aggregation_mode` - Aggregation mode to use for the price - /// - /// # Returns - /// - /// * `Array::` - Array of data entries - fn get_data_entries(data_type: DataType, aggregation_mode: felt252) -> Array::; - /// Get the number of decimals of some data e.g spot, future, generic - /// # Arguments - /// - /// * `data_type` - Type of the data to get - /// - /// # Returns - /// - /// felt252 - Number of decimals of for the given data - fn get_data_decimals(data_type: DataType) -> felt252; - /// Get the last updated timestamp of some data e.g spot, future, generic - /// - /// # Arguments - /// - /// * `data_type` - Type of the data to get - /// * `timestamp` - the timestamp to get the last updated timestamp before - /// - /// # Returns - /// - /// * `felt252` - Timestamp of the most recent update, before the given timestamp, UTC epoch - /// * `Checkpoint` - The checkpoint associated with the timestamp - fn get_last_data_checkpoint_before( - timestamp: felt252, data_type: DataType - ) -> (Checkpoint, felt252); -}