ink_analyzer_ir/
contract_ref.rs

1//! ink! contract ref IR.
2
3use ra_ap_syntax::ast;
4
5use crate::Message;
6
7/// An ink! contract ref.
8#[ink_analyzer_macro::entity(macro_kind = ContractRef)]
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct ContractRef {
11    // ASTNode type.
12    ast: ast::Trait,
13    // ink! messages.
14    messages: Vec<Message>,
15}
16
17impl_ast_type_trait!(ContractRef, IsInkTrait);
18
19impl_has_ink_environment!(ContractRef, Env);
20
21impl ContractRef {
22    /// Returns the specified ABI (if any).
23    pub fn abi(&self) -> Option<String> {
24        self.abi_arg()?.value()?.as_string()
25    }
26
27    impl_pub_ink_arg_getter!(abi_arg, Abi, abi);
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33    use crate::test_utils::*;
34    use crate::traits::{HasInkEnvironment, InkEntity, IsInkTrait};
35    use test_utils::quote_as_str;
36
37    #[test]
38    fn cast_works() {
39        let node = parse_first_syntax_node(quote_as_str! {
40            #[ink::contract_ref(abi="sol", env=crate::MyEnvironment)]
41            pub trait MyTrait {
42                #[ink(message)]
43                fn my_message(&self);
44
45                #[ink(message)]
46                fn my_message_mut(&mut self);
47            }
48
49            #[derive(Clone)]
50            pub struct MyEnvironment;
51
52            impl ink::env::Environment for MyEnvironment {
53                const NATIVE_TO_ETH_RATIO: u32 = 100_000_000;
54
55                type AccountId = [u8; 16];
56                type Balance = u128;
57                type Hash = [u8; 32];
58                type Timestamp = u64;
59                type BlockNumber = u32;
60                type EventRecord = ();
61            }
62        });
63
64        let contract_ref = ContractRef::cast(node).unwrap();
65
66        // `abi` argument exists.
67        assert!(contract_ref.abi_arg().is_some());
68
69        // `abi` argument value.
70        assert_eq!(contract_ref.abi().as_deref(), Some("sol"));
71
72        // `env` argument exists.
73        assert!(contract_ref.env_arg().is_some());
74
75        // `environment` ADT is returned.
76        assert!(contract_ref.environment().is_some());
77
78        // 2 messages.
79        assert_eq!(contract_ref.messages().len(), 2);
80
81        // `trait` item exists.
82        assert!(contract_ref.trait_item().is_some());
83    }
84}