2 个自定义托盘之间的基板托盘松散耦合示例

Substrate pallet loosely coupling example between 2 custom pallets

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

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

我不知道如何使用 2 个自定义托盘执行此操作?

我是这样编辑的:

例如:pallet BoTrading 需要在 pallet BoLiquidity

上调用 get_suitable_lp

pallets/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 中包含 pallets,下面的代码只关注重要的事情:

runtime/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,
    }
);