如何在基板运行时硬编码地址?



您可以在托盘内手动预设整数,字符串和很多东西,但是我如何在托盘中手动设置AccountId而不是ensure_root功能?

我认为,由于区块链已经运行了很长时间,我无法改变链规范的起源。

根据你的评论,我只能假设你的问题的正确标题是:">如何在基板运行时硬编码地址,

。一个类似的问题已经在这里得到了回答。这里简要回顾一下,在托盘中已知的AccountId类型仅局限于这些特征:https://github.com/paritytech/substrate/blob/master/frame/system/src/lib.rs#L195

所以在里面硬编码一个有点困难一个托盘,尽管仍然是可能的。更明智的方法是注入将硬编码帐户作为配置从外部放入您的托盘。

首先,你的配置trait需要接受一个包含这个帐户的类型:

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

然后,要使用它,你只需这样做:

// 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);

最后,在顶层运行时文件中,您可以硬编码AccountId并将其传入,如下所示:

parameter_types! {
pub CharityDest: AccountId = hex_literal::hex!["hex-bytes-of-account"].into()
}
impl my_pallet::Config for Runtime {
type CharityDest = CharityDest;
}

或者,您也可以使用大多数运行时的AccountId类型是AccountId32的事实,它实现了Ss58Codec,这意味着它有一堆有用的函数可以从Ss58字符串转换为帐户,因此您可以执行类似

的操作
parameter_types! {
pub CharityDest: AccountId = AccountId::from_ss58check("AccountSs58Format").unwrap()
}

最新更新