Skip to content
This repository has been archived by the owner on Nov 11, 2024. It is now read-only.

docs: fix typo (n-06) #36

Merged
merged 2 commits into from
Nov 11, 2024
Merged

docs: fix typo (n-06) #36

merged 2 commits into from
Nov 11, 2024

Conversation

glihm
Copy link
Contributor

@glihm glihm commented Nov 5, 2024

A transaction hash showing the behavior on Sepolia:
0xee9e5900239cf8d9c7f5cb5e67c814edcdd6e90d6eceefa342ffb5304ff892

Some code to reproduce (on the Cairo test runner, it runs as expected):

#[starknet::contract]
mod c1 {
    use starknet::ClassHash;

    #[storage]
    struct Storage {}
    
    #[event]
    #[derive(Drop, starknet::Event)]
    pub enum Event {
        Syscalled: Syscalled,
    }

    #[derive(Drop, starknet::Event)]
    pub struct Syscalled {
        pub success: bool,
    }

    #[abi(per_item)]
    #[generate_trait]
    impl Impl1 of Impl1Trait {
        #[external(v0)]
        fn call_f2(ref self: ContractState, class_hash: ClassHash) {
            let success = match starknet::syscalls::library_call_syscall(
                class_hash, selector!("f2"), [].span(),
            ) {
                Result::Ok(_) => true,
                Result::Err(_) => false,
            };

            self.emit(Syscalled { success });
        }
    }
}

#[starknet::contract]
mod c2 {
    #[storage]
    struct Storage {}

    #[abi(per_item)]
    #[generate_trait]
    impl Impl1 of Impl1Trait {
        #[external(v0)]
        fn f2(ref self: ContractState) {            
        }
    }    
}

#[starknet::contract]
mod c3 {
    #[storage]
    struct Storage {}
}

#[cfg(test)]
mod tests {
    use super::{c1, c2, c3};
    use starknet::ContractAddress;

    type TestClassHash = felt252;

    fn deploy_contract(class_hash: TestClassHash) -> ContractAddress {
        let salt = core::testing::get_available_gas();

        let (addr, _) = starknet::syscalls::deploy_syscall(
            class_hash.try_into().unwrap(),
            salt.into(),
            [].span(),
            true
        )
            .unwrap();
    
        addr
    }

    #[test]
    fn success() {
        let c1 = deploy_contract(c1::TEST_CLASS_HASH);

        let _ = starknet::syscalls::call_contract_syscall(
            c1,
            selector!("call_f2"),
            [c2::TEST_CLASS_HASH.try_into().unwrap()].span(),
        )
            .expect('call_f2 on c1 should succeed');

        let event = starknet::testing::pop_log::<c1::Event>(c1).expect('c1 should emit Syscalled');

        if let c1::Event::Syscalled(syscalled) = event {
            assert!(syscalled.success);
        } else {
            panic!("event should be Syscalled");
        }
    }

    #[test]
    fn fail() {
        let c1 = deploy_contract(c1::TEST_CLASS_HASH);

        let _ = starknet::syscalls::call_contract_syscall(
            c1,
            selector!("call_f2"),
            [c3::TEST_CLASS_HASH.try_into().unwrap()].span(),
        )
            .expect('call_f2 on c1 should fail');

        let event = starknet::testing::pop_log::<c1::Event>(c1).expect('c1 should emit Syscalled');

        if let c1::Event::Syscalled(syscalled) = event {
            assert!(!syscalled.success);
        } else {
            panic!("event should be Syscalled");
        }
    }
}

@glihm glihm merged commit f6ab047 into main Nov 11, 2024
4 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant