pallet_example_view_functions/
lib.rs1#![cfg_attr(not(feature = "std"), no_std)]
21
22pub mod tests;
23
24use frame_support::Parameter;
25use scale_info::TypeInfo;
26
27pub struct SomeType1;
28impl From<SomeType1> for u64 {
29 fn from(_t: SomeType1) -> Self {
30 0u64
31 }
32}
33
34pub trait SomeAssociation1 {
35 type _1: Parameter + codec::MaxEncodedLen + TypeInfo;
36}
37impl SomeAssociation1 for u64 {
38 type _1 = u64;
39}
40
41#[frame_support::pallet]
42pub mod pallet {
43 use super::*;
44 use frame_support::pallet_prelude::*;
45
46 #[pallet::error]
47 pub enum Error<T> {}
48
49 #[pallet::config]
50 pub trait Config: frame_system::Config {}
51
52 #[pallet::pallet]
53 pub struct Pallet<T>(_);
54
55 #[pallet::storage]
56 pub type SomeValue<T: Config> = StorageValue<_, u32>;
57
58 #[pallet::storage]
59 pub type SomeMap<T: Config> = StorageMap<_, Twox64Concat, u32, u32, OptionQuery>;
60
61 #[pallet::view_functions]
62 impl<T: Config> Pallet<T>
63 where
64 T::AccountId: From<SomeType1> + SomeAssociation1,
65 {
66 pub fn get_value() -> Option<u32> {
68 SomeValue::<T>::get()
69 }
70
71 pub fn get_value_with_arg(key: u32) -> Option<u32> {
73 SomeMap::<T>::get(key)
74 }
75 }
76}
77
78#[frame_support::pallet]
79pub mod pallet2 {
80 use super::*;
81 use frame_support::pallet_prelude::*;
82
83 #[pallet::error]
84 pub enum Error<T, I = ()> {}
85
86 #[pallet::config]
87 pub trait Config<I: 'static = ()>: frame_system::Config {}
88
89 #[pallet::pallet]
90 pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);
91
92 #[pallet::storage]
93 pub type SomeValue<T: Config<I>, I: 'static = ()> = StorageValue<_, u32>;
94
95 #[pallet::storage]
96 pub type SomeMap<T: Config<I>, I: 'static = ()> =
97 StorageMap<_, Twox64Concat, u32, u32, OptionQuery>;
98
99 #[pallet::view_functions]
100 impl<T: Config<I>, I: 'static> Pallet<T, I>
101 where
102 T::AccountId: From<SomeType1> + SomeAssociation1,
103 {
104 pub fn get_value() -> Option<u32> {
106 SomeValue::<T, I>::get()
107 }
108
109 pub fn get_value_with_arg(key: u32) -> Option<u32> {
111 SomeMap::<T, I>::get(key)
112 }
113 }
114}