Skip to content

Commit e98438d

Browse files
committed
adds optional bytes bug
1 parent f603c0d commit e98438d

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

pallets/template/src/lib.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ mod benchmarking;
1616

1717
#[frame_support::pallet]
1818
pub mod pallet {
19-
use frame_support::pallet_prelude::*;
19+
use core::str::Bytes;
20+
21+
use frame_support::pallet_prelude::*;
2022
use frame_system::pallet_prelude::*;
2123

2224
/// Configure the pallet by specifying the parameters and types on which it depends.
@@ -38,6 +40,11 @@ pub mod pallet {
3840
// https://docs.substrate.io/v3/runtime/storage#declaring-storage-items
3941
pub type Something<T> = StorageValue<_, u32>;
4042

43+
44+
#[pallet::storage]
45+
#[pallet::getter(fn my_bytes_val)]
46+
pub type MyBytesVal<T> = StorageValue<_, MyBytes, ValueQuery>;
47+
4148
// Pallets use events to inform users when important changes are made.
4249
// https://docs.substrate.io/v3/runtime/events-and-errors
4350
#[pallet::event]
@@ -48,6 +55,8 @@ pub mod pallet {
4855
SomethingStored(u32, T::AccountId),
4956
}
5057

58+
pub type MyBytes = BoundedVec<u8,ConstU32<16>>;
59+
5160
// Errors inform users that something went wrong.
5261
#[pallet::error]
5362
pub enum Error<T> {
@@ -98,5 +107,19 @@ pub mod pallet {
98107
},
99108
}
100109
}
110+
111+
#[pallet::weight(10_000 + T::DbWeight::get().writes(1))]
112+
pub fn insert_my_bytes(origin: OriginFor<T>, optional_bytes: Option<MyBytes>) -> DispatchResult {
113+
// Check that the extrinsic was signed and get the signer.
114+
// This function will return an error if the extrinsic is not signed.
115+
// https://docs.substrate.io/v3/runtime/origins
116+
let who = ensure_signed(origin)?;
117+
118+
// Update storage.
119+
let s = optional_bytes.unwrap_or_default();
120+
<MyBytesVal<T>>::put(s);
121+
// Return a successful DispatchResultWithPostInfo
122+
Ok(())
123+
}
101124
}
102125
}

0 commit comments

Comments
 (0)