1

I want to construct a customized call in Instruction::Transact. But how can I pass parameters palletId and methodId to it?

I configure the palletId and methodId in my Config, which can be set in my Runtime. But how can I pass parameters to it?


Here's my pallet code:

  1. This is my customize call

     #[derive(Encode, Decode, Clone, PartialEq, Eq, Default, RuntimeDebug, TypeInfo)]
     pub struct XregisterCall<AccountId> {
         // two call parameter: palletId and methodId
         call_index: [u8; 2],
         // method parameter: register_account and register_name
         account: AccountId,
         name: Vec<u8>,
     }
    
  2. This is my Config

     #[pallet::config]
     pub trait Config: frame_system::Config {
         type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
    
         /// The XCM sender module.
         type XcmSender: SendXcm;
    
         /// Xregister Pallet ID in xregister server
         type XregisterPalletID: Get<u8>;
    
         /// Xregister Method ID in xregister server
         type XregisterMethodID: Get<u8>;
    
         /// Xregister maximum weight
         type XregisterWeightAtMost: Get<u64>;
     }
    
  3. In my dispatchable call, the code is this:

     let call = XregisterCall::<T::AccountId>::new(
                 T::XregisterPalletID::get(), // palletId
                 T::XregisterMethodID::get(), // pallet中的mathod Id
                 who.clone(),                 // accountId
                 name.clone(),                // registered name
             );
    
             // build the xcm transact message
             let message = Xcm(vec![Instruction::Transact {
                 origin_type: OriginKind::Native,
                 require_weight_at_most: T::XregisterWeightAtMost::get(),
                 call: call.encode().into(),
             }]);
    
             // send the message to xregister server chain
             match T::XcmSender::send_xcm(
                 (1, Junction::Parachain(4000u32.into())),  // 4000 is the destination parachain Id.
                 message,
             ) {
                 // send_xcm结果是一个result
                 Ok(()) => {
                     // emit the event if send successfully
                     Self::deposit_event(Event::Xregister(who, name));
                     Ok(().into())
                 },
                 Err(_) => Err(Error::<T>::XcmSendError.into()),
             }
    
  4. My runtime config code is this:

    https://github.com/VegeBun-csj/Proof-Chain/blob/main/runtime/src/lib.rs#L635

In the above, I configure the XregisterPalletID and XregisterMethodID, so that I can pass the two parameters in runtime. Is this scheme OK? So the question is that I don't know how can I pass parameter in Runtime. If it's wrong, how can I correct it? Or are there any examples for me to take? I'm not familiar with xcm customize calls.

1
  • 2
    Please share the snippets of code here for people to review, so they don't have to parse your fill code. Great to reference it though in full, best by line numbers if possible as you have done
    – Nuke
    Commented Mar 24, 2022 at 22:06

1 Answer 1

1

I'm not sure what your pallet's functionality is, but any configurable parameter for the pallet should appear as a parameter to the dispatch function, i.e. this here:

pub fn xregister(origin: OriginFor<T>, name: Vec<u8>) -> DispatchResultWithPostInfo

needs to be changed to:

pub fn xregister(origin: OriginFor<T>, name: Vec<u8>, pallet_id: u8, method_id: u8) -> DispatchResultWithPostInfo

... in order for the caller of xregister to be able to specify the palletId and the methodId in the call.

2
  • HI, is there any customize call in parachain XCM example? thanks! i want to learn it. if i want to pass parameters in call, how to pass palletId and methodId
    – bun
    Commented Mar 27, 2022 at 13:35
  • As mentioned in the answer, you first need to modify the xregister dispatchable function on the pallet before you can use Transact. It is not possible to "customize" the call function while constructing the XCM Transact instruction. Commented Mar 29, 2022 at 10:29

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.