4

I am working on chain extension to call rmrk pallet methods. One of the methods needs a BoundedVec that I need to pass via ink smart contract. What data type can I use to do this on smart contract side? Is there a BoundedVec<> type on ink?

I tried using Vec and then trying to convert it to BoundedVec in the chain extension in runtime but I get the following error

env.read_as()?;
      |                                                                                                
      ^^^^^^^ the trait `MaxEncodedLen` is not implemented for `sp_runtime::sp_application_crypto::Vec<u8>

The method signature on ink contract

fn create_collection(contract_address: AccountId, metadata: Vec<u8>, symbol: Vec<u8>) -> Result<CollectionId, RCError>;

Reading the arguments in the runtime

let (contract_address, metadata, symbol) : (AccountId, Vec<u8>, Vec<u8>) = env.read_as()?;

1 Answer 1

7

You can only use read_as() for types whose size is known at compile time (thus the requirement that types implement MaxEncodedLen).

If you don't know the size ahead of time, like in the case of a Vec, you can use read_as_unbounded(). In order to get the number of bytes to read you can use the in_len() method.

So your code would look something like:

let (contract_address, metadata, symbol): (AccountId, Vec<u8>, Vec<u8>)
    = env.read_as_unbounded(env.in_len())?;

I have some demo code here if you need more resources on how to implement chain-extensions.

3
  • This is awesome. Its exactly what I needed. Do you have a video recording of the demo as well somewhere?
    – Ankan
    Jul 7, 2022 at 19:04
  • Yeah I have a recording. It'll be uploaded to YouTube in the next few days :)
    – HCastano
    Jul 7, 2022 at 21:58
  • 1
    @TheDuck here is the video: youtu.be/yykPQF0tkqk
    – HCastano
    Jul 25, 2022 at 16:31

Your Answer

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

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