Skip to content

Commit

Permalink
add supports_interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-fleming committed Nov 18, 2023
1 parent f2ecaf3 commit e812a87
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/introspection/src5.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ mod SRC5Component {
self.SRC5_supported_interfaces.write(interface_id, true);
}

// Registers the given interfaces as supported by the contract.
fn register_interfaces(ref self: ComponentState<TContractState>, mut interface_ids: Span<felt252>) {
loop {
if interface_ids.len() == 0 {
break;
}

let id = *interface_ids.pop_front().unwrap();
self.register_interface(id);
}
}

/// Deregisters the given interface as supported by the contract.
fn deregister_interface(ref self: ComponentState<TContractState>, interface_id: felt252) {
assert(interface_id != interface::ISRC5_ID, Errors::INVALID_ID);
Expand Down
26 changes: 26 additions & 0 deletions src/tests/introspection/test_src5.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use openzeppelin::introspection::src5::SRC5Component::InternalTrait;
use openzeppelin::tests::mocks::src5_mocks::DualCaseSRC5Mock;

const OTHER_ID: felt252 = 0x12345678;
const OTHER_ID_2: felt252 = 0x87654321;

fn STATE() -> DualCaseSRC5Mock::ContractState {
DualCaseSRC5Mock::contract_state_for_testing()
Expand Down Expand Up @@ -33,6 +34,31 @@ fn test_register_interface() {
assert(supports_new_interface, 'Should support new interface');
}

#[test]
#[available_gas(2000000)]
fn test_register_interfaces() {
let mut state = STATE();
let ids = array![OTHER_ID, OTHER_ID_2];
state.src5.register_interfaces(ids.span());

let supports_id = state.src5.supports_interface(OTHER_ID);
assert(supports_id, 'Should support new interface');

let supports_id_2 = state.src5.supports_interface(OTHER_ID_2);
assert(supports_id_2, 'Should support new interface');
}

#[test]
#[available_gas(2000000)]
fn test_register_interfaces_one_id() {
let mut state = STATE();
let ids = array![OTHER_ID];
state.src5.register_interfaces(ids.span());

let supports_id = state.src5.supports_interface(OTHER_ID);
assert(supports_id, 'Should support new interface');
}

#[test]
#[available_gas(2000000)]
fn test_deregister_interface() {
Expand Down

0 comments on commit e812a87

Please sign in to comment.