Skip to content

Commit

Permalink
feat: remove source (#60)
Browse files Browse the repository at this point in the history
* feat: remove source

* fix: spell

* feat: tests

* fix: remove print

* feat: test

* fix: event

* fix: remove duplicate

* fix: new syntax + removing all debug::PrintTrait

* fix: remaining printTrait

---------

Co-authored-by: 0xevolve <Artevolve@yahoo.com>
  • Loading branch information
JordyRo1 and EvolveArt authored Nov 14, 2023
1 parent 8cfc977 commit adbfa2e
Show file tree
Hide file tree
Showing 15 changed files with 241 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ version = "1.0.0"

[dependencies]
alexandria_math = { git = "https://github.com/keep-starknet-strange/alexandria.git", rev = "46c8d8ab9e3bfb68b70a29b3246f809cd8bf70e4" }
alexandria_storage = { git = "https://github.com/keep-starknet-strange/alexandria.git", rev = "a3052ff" }
alexandria_storage = { git = "https://github.com/keep-starknet-strange/alexandria.git", rev = "92c3c1b4ac35a4a56c14abe992814581aee875a8" }
alexandria_data_structures = { git = "https://github.com/keep-starknet-strange/alexandria.git", rev = "46c8d8ab9e3bfb68b70a29b3246f809cd8bf70e4" }
alexandria_sorting = { git = "https://github.com/keep-starknet-strange/alexandria.git", rev = "46c8d8ab9e3bfb68b70a29b3246f809cd8bf70e4" }
cubit = { git = "https://github.com/influenceth/cubit", rev = "2ccb2536dffa3f15ebd38b755c1be65fde1eab0c" }
Expand Down
3 changes: 1 addition & 2 deletions src/compute_engines/summary_stats/summary_stats.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use starknet::ContractAddress;
use pragma::entry::structs::{DataType, AggregationMode};
use debug::PrintTrait;
use cubit::f128::types::fixed::{FixedTrait, ONE_u128};
#[starknet::interface]
trait ISummaryStatsABI<TContractState> {
Expand Down Expand Up @@ -43,7 +42,7 @@ mod SummaryStats {
use pragma::operations::time_series::structs::TickElem;
use pragma::operations::time_series::metrics::{volatility, mean, twap};
use pragma::operations::time_series::scaler::scale_data;
use super::{FixedTrait, ONE_u128, PrintTrait, ISummaryStatsABI};
use super::{FixedTrait, ONE_u128, ISummaryStatsABI};
const SCALED_ARR_SIZE: u32 = 30;
#[storage]
struct Storage {
Expand Down
1 change: 0 additions & 1 deletion src/compute_engines/yield_curve/yield_curve.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ mod YieldCurve {
DataType, AggregationMode, PragmaPricesResponse, PossibleEntries, SpotEntry, FutureEntry,
GenericEntry
};
use debug::PrintTrait;
use starknet::get_caller_address;
use pragma::admin::admin::Ownable;

Expand Down
2 changes: 0 additions & 2 deletions src/entry/entry.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,6 @@ mod Entry {
//-----------------------------------------------
// Tests

use debug::PrintTrait;

#[test]
#[available_gas(100000000)]
fn test_aggregate_entries_median() {
Expand Down
1 change: 0 additions & 1 deletion src/operations/time_series/convert.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use alexandria_math::pow;
use traits::Into;
use debug::PrintTrait;


const MAX_POWER: u128 = 10000000000000000000000000000000;
Expand Down
1 change: 0 additions & 1 deletion src/operations/time_series/metrics.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use cubit::f128::types::fixed::{
use array::{ArrayTrait, SpanTrait};
use traits::{Into, TryInto};
use option::OptionTrait;
use debug::PrintTrait;
use box::BoxTrait;

const ONE_YEAR_IN_SECONDS: u128 = 31536000_u128;
Expand Down
1 change: 0 additions & 1 deletion src/operations/time_series/scaler.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use zeroable::Zeroable;
use traits::Into;
use option::OptionTrait;
use box::BoxTrait;
use debug::PrintTrait;

use cubit::f128::types::fixed::{FixedTrait, Fixed, FixedPrint, ONE_u128};

Expand Down
140 changes: 139 additions & 1 deletion src/oracle/oracle.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ trait IOracleABI<TContractState> {
fn set_checkpoints(
ref self: TContractState, data_types: Span<DataType>, aggregation_mode: AggregationMode
);
fn remove_source(ref self: TContractState, source: felt252, data_type: DataType) -> bool;
fn set_sources_threshold(ref self: TContractState, threshold: u32);
fn upgrade(ref self: TContractState, impl_hash: ClassHash);
}
Expand Down Expand Up @@ -192,7 +193,6 @@ mod Oracle {

use cmp::{max, min};
use option::OptionTrait;
use debug::PrintTrait;
const BACKWARD_TIMESTAMP_BUFFER: u64 = 600; // 10 minutes
const FORWARD_TIMESTAMP_BUFFER: u64 = 120; // 2 minutes

Expand Down Expand Up @@ -261,6 +261,12 @@ mod Oracle {
}


fn clean<T>(ref self: List<T>) {
self.len = 0;
Store::write(self.address_domain, self.base, self.len);
}


#[derive(Drop, starknet::Event)]
struct UpdatedPublisherRegistryAddress {
old_publisher_registry_address: ContractAddress,
Expand Down Expand Up @@ -1589,6 +1595,138 @@ mod Oracle {
return ();
}

// @notice remove a source for a given data type(DataType)
// @dev can be called only by the admin
// @dev need to also call remove_source_for_all_publishers on the publisher registry contract
// @param source: the source to be removed
// @param data_type: an enum of DataType (e.g : DataType::SpotEntry(ASSET_ID))
fn remove_source(ref self: ContractState, source: felt252, data_type: DataType) -> bool {
OracleInternal::assert_only_admin();
match data_type {
DataType::SpotEntry(pair_id) => {
let sources_len = self.oracle_sources_len_storage.read((pair_id, SPOT, 0));
let mut cur_idx = 0;
let is_in_storage: bool = loop {
if (cur_idx == sources_len) {
break false;
}
let cur_source = self
.oracle_sources_storage
.read((pair_id, SPOT, cur_idx, 0));
if (source == cur_source) {
break true;
}
cur_idx += 1;
};
if (!is_in_storage) {
assert(false, 'Source not found');
return false;
}
if (cur_idx == sources_len - 1) {
self.oracle_sources_len_storage.write((pair_id, SPOT, 0), sources_len - 1);
self.oracle_sources_storage.write((pair_id, SPOT, sources_len - 1, 0), 0);
} else {
let last_source = self
.oracle_sources_storage
.read((pair_id, SPOT, sources_len - 1, 0));
self.oracle_sources_storage.write((pair_id, SPOT, cur_idx, 0), last_source);
self.oracle_sources_storage.write((pair_id, SPOT, sources_len - 1, 0), 0);
self.oracle_sources_len_storage.write((pair_id, SPOT, 0), sources_len - 1);
}
let mut publishers_list = self
.oracle_list_of_publishers_for_sources_storage
.read((source, SPOT, pair_id));
publishers_list.clean();
return true;
},
DataType::FutureEntry((
pair_id, expiration_timestamp
)) => {
let sources_len = self
.oracle_sources_len_storage
.read((pair_id, FUTURE, expiration_timestamp));
let mut cur_idx = 0;
let is_in_storage: bool = loop {
if (cur_idx == sources_len) {
break false;
}
let cur_source = self
.oracle_sources_storage
.read((pair_id, SPOT, cur_idx, 0));
if (source == cur_source) {
break true;
}
cur_idx += 1;
};
if (!is_in_storage) {
assert(false, 'Source not found');
return false;
}
if (cur_idx == sources_len - 1) {
self
.oracle_sources_len_storage
.write((pair_id, FUTURE, expiration_timestamp), sources_len - 1);
self
.oracle_sources_storage
.write((pair_id, FUTURE, sources_len - 1, expiration_timestamp), 0);
} else {
let last_source = self
.oracle_sources_storage
.read((pair_id, FUTURE, sources_len - 1, expiration_timestamp));
self
.oracle_sources_storage
.write((pair_id, FUTURE, cur_idx, expiration_timestamp), last_source);
self
.oracle_sources_storage
.write((pair_id, FUTURE, sources_len - 1, expiration_timestamp), 0);
self
.oracle_sources_len_storage
.write((pair_id, FUTURE, expiration_timestamp), sources_len - 1);
}
let mut publishers_list = self
.oracle_list_of_publishers_for_sources_storage
.read((source, FUTURE, pair_id));
publishers_list.clean();
return true;
},
DataType::GenericEntry(key) => {
let sources_len = self.oracle_sources_len_storage.read((key, GENERIC, 0));
let mut cur_idx = 0;
let is_in_storage: bool = loop {
if (cur_idx == sources_len) {
break false;
}
let cur_source = self
.oracle_sources_storage
.read((key, GENERIC, cur_idx, 0));
if (source == cur_source) {
break true;
}
cur_idx += 1;
};
if (!is_in_storage) {
assert(false, 'Source not found');
return false;
}
if (cur_idx == sources_len - 1) {
self.oracle_sources_len_storage.write((key, GENERIC, 0), sources_len - 1);
self.oracle_sources_storage.write((key, GENERIC, sources_len - 1, 0), 0);
} else {
let last_source = self
.oracle_sources_storage
.read((key, GENERIC, sources_len - 1, 0));
self.oracle_sources_storage.write((key, GENERIC, cur_idx, 0), last_source);
self.oracle_sources_storage.write((key, GENERIC, sources_len - 1, 0), 0);
self.oracle_sources_len_storage.write((key, GENERIC, 0), sources_len - 1);
}
let mut publishers_list = self
.oracle_list_of_publishers_for_sources_storage
.read((source, GENERIC, key));
publishers_list.clean();
return true;
}
}
}

// @notice set a new checkpoint for a given data type and and aggregation mode
// @param data_type: an enum of DataType (e.g : DataType::SpotEntry(ASSET_ID) or DataType::FutureEntry((ASSSET_ID, expiration_timestamp)))
Expand Down
36 changes: 34 additions & 2 deletions src/publisher_registry/publisher_registry.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ trait IPublisherRegistryABI<TContractState> {
ref self: TContractState, publisher: felt252, sources: Span<felt252>
);
fn remove_source_for_publisher(ref self: TContractState, publisher: felt252, source: felt252);
fn remove_source_for_all_publishers(ref self: TContractState, source: felt252);

fn can_publish_source(self: @TContractState, publisher: felt252, source: felt252) -> bool;
fn get_publisher_address(self: @TContractState, publisher: felt252) -> ContractAddress;
fn set_admin_address(ref self: TContractState, new_admin_address: ContractAddress);
Expand All @@ -36,7 +38,6 @@ mod PublisherRegistry {
use traits::TryInto;
use pragma::admin::admin::Ownable;
use super::IPublisherRegistryABI;
use debug::PrintTrait;

#[storage]
struct Storage {
Expand Down Expand Up @@ -84,15 +85,22 @@ mod PublisherRegistry {
new_address: ContractAddress
}

#[derive(Drop, starknet::Event)]
struct DeletedSource {
source: felt252,
}

#[derive(Drop, starknet::Event)]
#[event]
enum Event {
RegisteredPublisher: RegisteredPublisher,
UpdatedPublisherAddress: UpdatedPublisherAddress,
RemovedPublisher: RemovedPublisher,
TransferOwnership: TransferOwnership
TransferOwnership: TransferOwnership,
DeletedSource: DeletedSource
}


#[external(v0)]
impl PublisherRegistryImpl of IPublisherRegistryABI<ContractState> {
// @notice add a publisher to the registry
Expand Down Expand Up @@ -233,6 +241,7 @@ mod PublisherRegistry {
}

// @notice remove a source for a given publisher
// @dev can be called only by the admin
// @param the publisher for which a source needs to be removed
// @param source : the source that needs to be removed for the publisher
fn remove_source_for_publisher(
Expand Down Expand Up @@ -260,6 +269,28 @@ mod PublisherRegistry {
self.publishers_sources.write((publisher, cur_idx - 1), 0);
self.publishers_sources.write((publisher, source_idx), last_source);
}
self.emit(Event::DeletedSource(DeletedSource { source, }));
}

// @notice remove a given source for all the publishers
// @dev can be called only by admin
// @param source the source to consider
fn remove_source_for_all_publishers(ref self: ContractState, source: felt252) {
let mut publishers = IPublisherRegistryABI::get_all_publishers(@self);
assert_only_admin();
loop {
match publishers.pop_front() {
Option::Some(publisher) => {
IPublisherRegistryABI::remove_source_for_publisher(
ref self, publisher, source
);
},
Option::None(_) => {
break ();
}
};
};
self.emit(Event::DeletedSource(DeletedSource { source, }));
}

// @notice checks whether a publisher can publish for a certain source or not
Expand Down Expand Up @@ -353,6 +384,7 @@ mod PublisherRegistry {
assert(caller == admin, 'Admin: unauthorized');
}


// @notice retrieve all the publishers
// @dev recursive function
// @param index : current input index, should be set to 0
Expand Down
1 change: 0 additions & 1 deletion src/randomness/randomness.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ mod Randomness {
};
use pragma::admin::admin::Ownable;
use poseidon::poseidon_hash_span;
use debug::PrintTrait;

use array::{ArrayTrait, SpanTrait};
use traits::{TryInto, Into};
Expand Down
Loading

0 comments on commit adbfa2e

Please sign in to comment.