referrerpolicy=no-referrer-when-downgrade

pallet_example_view_functions/
lib.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! This pallet demonstrates the use of the `pallet::view_functions` api for service
19//! work.
20#![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		/// Query value with no input args.
67		pub fn get_value() -> Option<u32> {
68			SomeValue::<T>::get()
69		}
70
71		/// Query value with input args.
72		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		/// Query value with no input args.
105		pub fn get_value() -> Option<u32> {
106			SomeValue::<T, I>::get()
107		}
108
109		/// Query value with input args.
110		pub fn get_value_with_arg(key: u32) -> Option<u32> {
111			SomeMap::<T, I>::get(key)
112		}
113	}
114}