【问题标题】:Substrate pallet loosely coupling example between 2 custom pallets2 个自定义托盘之间的基板托盘松散耦合示例
【发布时间】:2022-01-02 20:35:31
【问题描述】:

这个官方文档是关于使用基材托盘的自定义托盘。

https://docs.substrate.io/how-to-guides/v3/pallet-design/loose-coupling/#2-import-the-trait

我不知道如何使用 2 个自定义托盘做到这一点?

【问题讨论】:

  • 几乎以相同的方式,但不是依赖于托盘支持的Currency trait,您将使用自定义托盘之一中定义的特征。我不明白你的困惑来自哪里。
  • 是的,绝对是你的方法!因为我对 Rust 的了解很薄弱,所以我很困惑。顺便说一句,我找到了答案,并且已经成功实施了,谢谢。

标签: substrate


【解决方案1】:

我是这样写的:

例如:pallet BoTrading 需要在pallet BoLiquidity 上调用get_suitable_lp

托盘/BoLiquidity/src/lib.rs

    /// Internal helpers fn
    impl<T: Config> Pallet<T> {
        fn pick_a_suitable_lp() -> Option<u32> {
            Some(99999)
        }
    }

    pub trait BoLiquidityInterface{
        fn get_suitable_lp()->Option<u32>;
    }

    impl<T: Config> BoLiquidityInterface for Pallet<T> {
        fn get_suitable_lp()->Option<u32>{
            Self::pick_a_suitable_lp()
        }
    }

pallets/BoTrading/src/lib.rs

pub mod pallet {
    ...
    use pallet_bo_liquidity::BoLiquidityInterface;

    #[pallet::config]
    pub trait Config: frame_system::Config {
        ...
        type BoLiquidity: BoLiquidityInterface;
    }

    ...
    // call other pallet some where inside this pallet:
    let lp = T::BoLiquidity::get_suitable_lp();
    ...
}

pallets/BoTrading/Cargo.toml

[dependencies.pallet-bo-liquidity]
default-features = false
path = '../BoLiquidity'
version = '0.0.1-dev'

记得在节点运行时的 cargo toml 中包含托盘,下面的代码只关注重要的事情:

运行时/src/lib.rs

impl pallet_bo_trading::Config for Runtime {
    ...
    type BoLiquidity = BoLiquidityModule;
}

construct_runtime!(
    pub enum Runtime where
        Block = Block,
        NodeBlock = opaque::Block,
        UncheckedExtrinsic = UncheckedExtrinsic
    {
        ...
        BoLiquidityModule: pallet_bo_liquidity,
    }
);

【讨论】:

    猜你喜欢
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-05-31
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多