5

I need to check if an extrinsic matches "default" inside an extrinsic. The following works:

let bv: BoundedVec<u8, T::StringLimit> = "default".as_bytes().to_vec().try_into().unwrap();

However, this seems kind of ugly and hacky. Is there not a better way to construct a BoundedVec?

Blockquote

2 Answers 2

6

A more elegant way:

  1. Make your max value part of your pallet's configuration trait so you can keep it generic in your pallet:
#[pallet::constant]
type StringLimit: Get<u32>;
  1. Define some alias for your bounded vec (outside of pub mod pallet{}):
type MyBoundedVec<T> = BoundedVec<u8, <T as Config>::StringLimit>;
  1. Then, use it inside your extrinsic:
let my_bounded_vec: MyBoundedVec<T> =
    "default".as_bytes().to_vec().try_into().unwrap();
1
  • 1
    should never unwrap in the runtime.
    – Shawn Tabrizi
    May 15, 2022 at 18:22
0

Otherwise you also have the bounded_vec! macro which is similar to the vec! macro.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.