1

You can pre-set integer, string, and a lot of things manually within a pallet but how do I set an AccountId manually in pallet other than ensure_root functionality?

I would assume that since blockchain is already running for a long time, I am unable to change the chain spec genesis.

Afeez Aziz
  • 1,337
  • 2
  • 12
  • 26
  • what do you exactly mean by pre-setting? – kianenigma Feb 05 '21 at 10:03
  • @kianenigma for example every dispatch call would transfer 1 Unit of DOT to the AccountId. I cannot find on how to set the said account in the code. – Afeez Aziz Feb 05 '21 at 10:06
  • Would be helpful if you share some code that shows a dispatchable and clearly express what you want to achieve. I think I know what you want and the answer is probably simple, but the question is till pretty vague :) – kianenigma Feb 05 '21 at 13:52
  • hi @kianenigma, The idea is like this `let donation: AccountId = '5axsca'; fn give_money(origin, amount: Balance) { Balance.transfer(origin, donation, amount) }` Based on the charity example, it actually created another account upon pallet instantiate but I do not want this way. Is this doable? – Afeez Aziz Feb 06 '21 at 09:53
  • posted an answer, I hope it is what you wanted. – kianenigma Feb 06 '21 at 10:39

1 Answers1

2

Based on your comment, I can only assume that your question's correct title is: "how to hardcode an address in the substrate runtime".

A similar question has been answered here. To briefly recap here, the AccountId type that is known inside the pallets is only bounded to these traits: https://github.com/paritytech/substrate/blob/master/frame/system/src/lib.rs#L195

So it is a bit harder to hardcode one inside a pallet, albeit still possible. A more sensible way would be to inject the hardcoded account into your pallet from outside as a configuration.

First, your config trait need to accept a type that contains this account:

trait Config: frame_system::Config {
   // A type that can deliver a single account id value to the pallet.
   type CharityDest: Get<<Self as frame_system::Config>::AccountId>
}

Then, to use it, you'd simple do:

// inside the `impl<T> Module<T>` or `impl<C> Pallet<T>` or `decl_module` where `T` is in scope.
some_transfer_function(T::CharityDest::get(), amount);

Finally, in your top level runtime file you can hardcode the AccountId and pass it in through, as such:

parameter_types! {
     pub CharityDest: AccountId = hex_literal::hex!["hex-bytes-of-account"].into()
}

impl my_pallet::Config for Runtime {
   type CharityDest = CharityDest;
}

Alternatively, you can also use the fact that the AccountId type of most runtimes is AccountId32, which implements Ss58Codec, meaning that it has a bunch of useful functions to convert from the Ss58 string to the account, so you can do something like

parameter_types! {
     pub CharityDest: AccountId = AccountId::from_ss58check("AccountSs58Format").unwrap()
}
kianenigma
  • 1,365
  • 12
  • 20
  • Thanks for the answer. I really appreciate this. For the `AccountId::from_ss58check("AccountSs58Format")` , it can be in Polkadot, Kusama, or Substrate format right? I also change the title of the question to reflect the question better. – Afeez Aziz Feb 06 '21 at 11:50
  • Yes, and you can use this function to set which one you intend to use: https://docs.rs/sp-core/2.0.1/sp_core/crypto/fn.set_default_ss58_version.html. – kianenigma Feb 06 '21 at 16:29
  • 2
    When I want to use `from_ss58check` in my node-template, I import the trait: `use sp_core::crypto::{KeyTypeId, Ss58Codec}`. It reports error: `no Ss58Codec in crypto`. why? – gfan Jan 14 '22 at 11:17
  • This answer is no longer working. – shredding Nov 09 '22 at 17:02