Skip to content

Commit f35a367

Browse files
authored
Merge pull request #226 from hashed-io/optional-bytes-error
adds optional bytes bug
2 parents f603c0d + f2dde3e commit f35a367

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

pallets/template/src/lib.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ mod benchmarking;
1616

1717
#[frame_support::pallet]
1818
pub mod pallet {
19-
use frame_support::pallet_prelude::*;
19+
20+
use frame_support::pallet_prelude::*;
2021
use frame_system::pallet_prelude::*;
2122

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

42+
43+
#[pallet::storage]
44+
#[pallet::getter(fn my_bytes_val)]
45+
pub type MyBytesVal<T> = StorageValue<_, MyBytes, ValueQuery>;
46+
4147
// Pallets use events to inform users when important changes are made.
4248
// https://docs.substrate.io/v3/runtime/events-and-errors
4349
#[pallet::event]
@@ -48,6 +54,8 @@ pub mod pallet {
4854
SomethingStored(u32, T::AccountId),
4955
}
5056

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

0 commit comments

Comments
 (0)