Skip to content

Commit

Permalink
feat(dispatcher_with_tests): Added tests for events
Browse files Browse the repository at this point in the history
  • Loading branch information
akhercha committed Sep 16, 2024
1 parent e07d7ee commit 068049c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 17 deletions.
8 changes: 5 additions & 3 deletions cairo/crates/pragma_dispatcher/src/contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ pub mod PragmaDispatcher {
hyperlane::HyperlaneMessageId, pragma_oracle::SummaryStatsComputation,
};
use pragma_feed_types::{FeedId};
use pragma_feeds_registry::{IPragmaFeedRegistryDispatcher, IPragmaFeedRegistryDispatcherTrait};
use pragma_feeds_registry::{
IPragmaFeedsRegistryDispatcher, IPragmaFeedsRegistryDispatcherTrait
};
use pragma_lib::abi::{
IPragmaABIDispatcher, IPragmaABIDispatcherTrait, ISummaryStatsABIDispatcher,
ISummaryStatsABIDispatcherTrait
Expand Down Expand Up @@ -129,15 +131,15 @@ pub mod PragmaDispatcher {
impl PragmaFeedsRegistryWrapper of IPragmaFeedsRegistryWrapper<ContractState> {
/// Calls feed_exists from the Pragma Feeds Registry contract.
fn call_feed_exists(self: @ContractState, feed_id: FeedId) -> bool {
let registry_dispatcher = IPragmaFeedRegistryDispatcher {
let registry_dispatcher = IPragmaFeedsRegistryDispatcher {
contract_address: self.pragma_feed_registry_address.read()
};
registry_dispatcher.feed_exists(feed_id)
}

/// Calls get_all_feeds from the Pragma Feeds Registry contract.
fn call_get_all_feeds(self: @ContractState) -> Array<FeedId> {
let registry_dispatcher = IPragmaFeedRegistryDispatcher {
let registry_dispatcher = IPragmaFeedsRegistryDispatcher {
contract_address: self.pragma_feed_registry_address.read()
};
registry_dispatcher.get_all_feeds()
Expand Down
8 changes: 4 additions & 4 deletions cairo/crates/pragma_feeds_registry/src/contract.cairo
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#[starknet::contract]
pub mod PragmaFeedRegistry {
pub mod PragmaFeedsRegistry {
use core::num::traits::Zero;
use core::panic_with_felt252;
use openzeppelin::access::ownable::OwnableComponent;
use openzeppelin::upgrades::{UpgradeableComponent, interface::IUpgradeable};
use pragma_feed_types::{FeedId, FeedTrait};
use pragma_feeds_registry::errors;
use pragma_feeds_registry::interface::IPragmaFeedRegistry;
use pragma_feeds_registry::interface::IPragmaFeedsRegistry;
use starknet::{ClassHash, ContractAddress, get_caller_address};

// ================== COMPONENTS ==================
Expand Down Expand Up @@ -52,7 +52,7 @@ pub mod PragmaFeedRegistry {

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
pub enum Event {
#[flat]
OwnableEvent: OwnableComponent::Event,
#[flat]
Expand All @@ -73,7 +73,7 @@ pub mod PragmaFeedRegistry {
// ================== PUBLIC ABI ==================

#[abi(embed_v0)]
impl PragmaFeedRegistry of IPragmaFeedRegistry<ContractState> {
impl PragmaFeedsRegistry of IPragmaFeedsRegistry<ContractState> {
/// Adds the [feed_id] into the Registry.
///
/// Panics if:
Expand Down
2 changes: 1 addition & 1 deletion cairo/crates/pragma_feeds_registry/src/interface.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use pragma_feed_types::FeedId;

#[starknet::interface]
pub trait IPragmaFeedRegistry<TContractState> {
pub trait IPragmaFeedsRegistry<TContractState> {
/// Adds the [feed_id] into the Registry.
fn add_feed(ref self: TContractState, feed_id: FeedId);
/// Removes the [feed_id] from the Registry.
Expand Down
2 changes: 1 addition & 1 deletion cairo/crates/pragma_feeds_registry/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ pub mod interface;


pub use interface::{
IPragmaFeedRegistry, IPragmaFeedRegistryDispatcher, IPragmaFeedRegistryDispatcherTrait
IPragmaFeedsRegistry, IPragmaFeedsRegistryDispatcher, IPragmaFeedsRegistryDispatcherTrait
};
43 changes: 35 additions & 8 deletions cairo/crates/pragma_feeds_registry/tests/test_contract.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use pragma_feed_types::FeedId;
use pragma_feeds_registry::{IPragmaFeedRegistryDispatcher, IPragmaFeedRegistryDispatcherTrait};
use snforge_std::{declare, ContractClassTrait, start_prank, stop_prank, CheatTarget};
use pragma_feeds_registry::contract::PragmaFeedsRegistry;
use pragma_feeds_registry::{IPragmaFeedsRegistryDispatcher, IPragmaFeedsRegistryDispatcherTrait};
use snforge_std::{
declare, ContractClassTrait, start_prank, stop_prank, CheatTarget, spy_events, SpyOn,
EventAssertions
};
use starknet::{ContractAddress, contract_address_const};

/// Returns the mock owner
Expand All @@ -11,24 +15,35 @@ fn owner() -> ContractAddress {
/// Deploys the Pragma Feeds Registry contract and returns:
/// * the deployed contract address
/// * the registry dispatcher
fn deploy_pragma_registry() -> (ContractAddress, IPragmaFeedRegistryDispatcher) {
// Deploy contract
let contract = declare("PragmaFeedRegistry").unwrap();
fn deploy_pragma_registry() -> (ContractAddress, IPragmaFeedsRegistryDispatcher) {
let contract = declare("PragmaFeedsRegistry").unwrap();
let (contract_address, _) = contract.deploy(@array![owner().into()]).unwrap();
let dispatcher = IPragmaFeedRegistryDispatcher { contract_address };
let dispatcher = IPragmaFeedsRegistryDispatcher { contract_address };
start_prank(CheatTarget::One(contract_address), owner());
(contract_address, dispatcher)
}

#[test]
fn test_add_feed() {
let (_, registry) = deploy_pragma_registry();
let (registry_address, registry) = deploy_pragma_registry();
let mut spy = spy_events(SpyOn::One(registry_address));

let feed_id: FeedId = 0x456;

registry.add_feed(feed_id);

assert!(registry.feed_exists(feed_id), "Feed should exist");
spy
.assert_emitted(
@array![
(
registry_address,
PragmaFeedsRegistry::Event::NewFeedId(
PragmaFeedsRegistry::NewFeedId { sender: owner(), feed_id: 0x456, }
)
)
]
);
}

#[test]
Expand All @@ -43,13 +58,25 @@ fn test_add_duplicate_feed() {

#[test]
fn test_remove_feed() {
let (_, registry) = deploy_pragma_registry();
let (registry_address, registry) = deploy_pragma_registry();
let mut spy = spy_events(SpyOn::One(registry_address));

let feed_id: FeedId = 0x456;
registry.add_feed(feed_id);
registry.remove_feed(feed_id);

assert!(!registry.feed_exists(feed_id), "Feed should not exist");
spy
.assert_emitted(
@array![
(
registry_address,
PragmaFeedsRegistry::Event::RemovedFeedId(
PragmaFeedsRegistry::RemovedFeedId { sender: owner(), feed_id: 0x456, }
)
)
]
);
}

#[test]
Expand Down

0 comments on commit 068049c

Please sign in to comment.