1// This file is part of Substrate.
23// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
56// 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.
1718//! This pallet demonstrates the use of the `pallet::view_functions` api for service
19//! work.
20#![cfg_attr(not(feature = "std"), no_std)]
2122pub mod tests;
2324use frame_support::Parameter;
25use scale_info::TypeInfo;
2627pub struct SomeType1;
28impl From<SomeType1> for u64 {
29fn from(_t: SomeType1) -> Self {
300u64
31}
32}
3334pub trait SomeAssociation1 {
35type _1: Parameter + codec::MaxEncodedLen + TypeInfo;
36}
37impl SomeAssociation1 for u64 {
38type _1 = u64;
39}
4041#[frame_support::pallet]
42pub mod pallet {
43use super::*;
44use frame_support::pallet_prelude::*;
4546#[pallet::error]
47pub enum Error<T> {}
4849#[pallet::config]
50pub trait Config: frame_system::Config {}
5152#[pallet::pallet]
53pub struct Pallet<T>(_);
5455#[pallet::storage]
56pub type SomeValue<T: Config> = StorageValue<_, u32>;
5758#[pallet::storage]
59pub type SomeMap<T: Config> = StorageMap<_, Twox64Concat, u32, u32, OptionQuery>;
6061#[pallet::view_functions]
62impl<T: Config> Pallet<T>
63where
64T::AccountId: From<SomeType1> + SomeAssociation1,
65 {
66/// Query value with no input args.
67pub fn get_value() -> Option<u32> {
68 SomeValue::<T>::get()
69 }
7071/// Query value with input args.
72pub fn get_value_with_arg(key: u32) -> Option<u32> {
73 SomeMap::<T>::get(key)
74 }
75 }
76}
7778#[frame_support::pallet]
79pub mod pallet2 {
80use super::*;
81use frame_support::pallet_prelude::*;
8283#[pallet::error]
84pub enum Error<T, I = ()> {}
8586#[pallet::config]
87pub trait Config<I: 'static = ()>: frame_system::Config {}
8889#[pallet::pallet]
90pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);
9192#[pallet::storage]
93pub type SomeValue<T: Config<I>, I: 'static = ()> = StorageValue<_, u32>;
9495#[pallet::storage]
96pub type SomeMap<T: Config<I>, I: 'static = ()> =
97 StorageMap<_, Twox64Concat, u32, u32, OptionQuery>;
9899#[pallet::view_functions]
100impl<T: Config<I>, I: 'static> Pallet<T, I>
101where
102T::AccountId: From<SomeType1> + SomeAssociation1,
103 {
104/// Query value with no input args.
105pub fn get_value() -> Option<u32> {
106 SomeValue::<T, I>::get()
107 }
108109/// Query value with input args.
110pub fn get_value_with_arg(key: u32) -> Option<u32> {
111 SomeMap::<T, I>::get(key)
112 }
113 }
114}