首页 > 解决方案 > 将存储宏从帧 v1 转换为帧 v2

问题描述

标题非常具有描述性。

我想转换这个:

decl_storage! {
    trait Store for Module<T: Config> as SimpleTreasury {
        // No storage items of our own, but we still need decl_storage to initialize the pot
    }
    add_extra_genesis {
        build(|_config| {
            // Create the charity's pot of funds, and ensure it has the minimum required deposit
            let _ = T::Currency::make_free_balance_be(
                &<Module<T>>::account_id(),
                T::Currency::minimum_balance(),
            );
        });
    }
}

进入帧 V2。下面我有我的转换版本,但它似乎不起作用,最低余额似乎没有正确设置。

#[pallet::genesis_config]
    #[derive(Default)]
    pub struct GenesisConfig {

    }

    #[pallet::genesis_build]
    impl<T: Config> GenesisBuild<T> for GenesisConfig {
        fn build(&self) {
            let _ = T::Currency::make_free_balance_be(
                &<Module<T>>::account_id(),
                T::Currency::minimum_balance(),
            );
        }
    }

有人可以看看并告诉我出了什么问题。我真的很感激。Pallet Github 链接:https ://github.com/herou/recipes/tree/recipes_pallet_v2/pallets/charity 注意:一些测试失败

标签: substrateparity

解决方案


该宏有一个关于从宏pallet升级的部分: https ://substrate.dev/rustdocs/latest/frame_support/attr.pallet.html#upgrade-guidelinesdecl_*

我建议仔细阅读该部分。

要说这个文档的几点,我可以说:

  1. 里面有一个助手decl_storage可以为新的托盘宏生成一些代码。通过使用环境变量运行宏扩展PRINT_PALLET_UPGRADE:例如PRINT_PALLET_UPGRADE=1 cargo check -p $my_pallet$
  2. 存储使用的托盘前缀配置不同:在 decl_storage 中给出了托盘前缀(例如SimpleTreasury在您的示例中)。现在,pallet 前缀是构建运行时时配置的名称。(例如在construct_runtime 调用中,当它被写入时:MyPalletName: path_to_pallet::{Call, ...},,名称MyPalletName)。如果名称不同,您可以使用一些实用程序函数来创建迁移:https ://substrate.dev/rustdocs/latest/frame_support/storage/migration/index.html
  3. 现在应该将属性config()build()将特定值放入存储中的属性放入GenesisConfig::build实现中。
  4. 通常在声明存储时使用的哈希器和类型应该非常简单。但人们必须小心QueryKind泛型。如果存储是用值写入的,Option<$Something>则 querykind 是OptionQuery,否则它是ValueQuery。(但助手PRINT_PALLET_UPGRADE必须给你正确的翻译。)

推荐阅读