Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

How to hardcode an address in the substrate runtime?

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.

question from:https://stackoverflow.com/questions/66056225/how-to-hardcode-an-address-in-the-substrate-runtime

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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()
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...