0

我有一个模块方法,它将固定费用与来自各种托盘的呼叫相关联。所以我试图通过托盘、调用名称和调用参数来匹配作为参数传递给我的方法的调用。

与此类似的东西:

impl<T: Config> Module<T>
{
    fn compute_call_fee_(call: &<T as Config>::Call) -> Result<u64, &'static str> {
        let fee = match *call {
            Call::PalletA(palletA::Call::name_of_palletA_call(x,y)) => 50u64, // here this is not real syntax, this is what I need to figure out
            _ => 100u64
        };

        Ok(fee)
    }
}

对于上面的情况, Config trait 将实现来自匹配的不同托盘的 Config 特征:

pub trait Config: system::Config + palletA::Config + palletB::Config {
    type Call: Parameter + Dispatchable<Origin = Self::Origin> + GetDispatchInfo;
}

如何按原点call托盘、函数名称和函数参数进行匹配?

我试过了:

  • 使用与上述类似的语法,但没有成功

  • 制作Config::Call工具GetCallMetadata:这只会让我得到原始托盘和函数名称,而不是参数

  • 使调用实现IsSubType,并遵循以下答案:How to decode and match a call when passing as a parameter in Substrate。如果我做对了,这与来自其他托盘的呼叫无关。

    对于 using IsSubType,我将其添加为绑定到 impl 块的特征,而不是Config特征:

    impl<T: Config> Module<T>
    where
         <T as Config>::Call: IsSubType<Call<T>>, 
    {
        fn compute_call_fee_(call: &<T as Config>::Call) -> Result<u64, &'static str> {
            if let Some(local_call) = call.is_sub_type() {
                return match local_call {
                    palletA::Call::name_of_palletA_call(x,y) => Ok(42u64),
                    _ => Ok(0u64),
                }
            }
            Ok(21u64)
        }
    }
    
4

2 回答 2

1

在您粘贴完整代码(特别是该部分)之前很难说type Call = xxx,但我可以猜到您错过的是:type Call: IsSubType<pallet_balances::Call<Self>>;. 您提到了另一个建议相同的问题,但我猜您没有正确使用它。

请注意,总体而言,外部Call(您在此处通过 传递到托盘的那个type Call)实现了两件事:

  • From<pallet_call>对于每个单独的托盘调用。这将允许您始终将内部Call转换为外部Call
  • IsSubType<pallet_call>对于每个单独的托盘调用。这将允许您有条件地将外部调用转换为内部调用(如果存在)。

也许这里的命名可能会更好一些。我会考虑至少将外部重命名为Call...OuterCall

最后,绝对推荐cargo expand在节点模板上运行 a 并查找enum Call.

TLDR;如果您将其应用于节点模板,此差异将起作用,也应该回答您的问题。

diff --git a/bin/node-template/pallets/template/Cargo.toml b/bin/node-template/pallets/template/Cargo.toml
index 12b810de1..6f91e8c9a 100644
--- a/bin/node-template/pallets/template/Cargo.toml
+++ b/bin/node-template/pallets/template/Cargo.toml
@@ -25,6 +25,11 @@ default-features = false
 version = "2.0.0"
 path = "../../../../frame/system"
 
+[dependencies.pallet-balances]
+default-features = false
+version = "2.0.0"
+path = "../../../../frame/balances"
+
 [dev-dependencies.sp-core]
 default-features = false
 version = "2.0.0"
@@ -46,5 +51,6 @@ default = ['std']
 std = [
    'codec/std',
    'frame-support/std',
-   'frame-system/std'
+   'frame-system/std',
+   'pallet-balances/std'
 ]
diff --git a/bin/node-template/pallets/template/src/lib.rs b/bin/node-template/pallets/template/src/lib.rs
index 24de4f2f5..562675a0b 100644
--- a/bin/node-template/pallets/template/src/lib.rs
+++ b/bin/node-template/pallets/template/src/lib.rs
@@ -14,9 +14,11 @@ mod mock;
 mod tests;
 
 /// Configure the pallet by specifying the parameters and types on which it depends.
-pub trait Config: frame_system::Config {
+use frame_support::traits::IsSubType;
+pub trait Config: frame_system::Config + pallet_balances::Config {
    /// Because this pallet emits events, it depends on the runtime's definition of an event.
    type Event: From<Event<Self>> + Into<<Self as frame_system::Config>::Event>;
+   type Call: IsSubType<pallet_balances::Call<Self>>;
 }
 
 // The pallet's runtime storage items.
diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs
index 51df3dd5a..4ca2ab613 100644
--- a/bin/node-template/runtime/src/lib.rs
+++ b/bin/node-template/runtime/src/lib.rs
@@ -258,6 +258,7 @@ impl pallet_sudo::Config for Runtime {
 /// Configure the pallet template in pallets/template.
 impl template::Config for Runtime {
    type Event = Event;
+   type Call = Call;
 }
 
 // Create the runtime by composing the FRAME pallets that were previously configured.

于 2021-02-08T13:40:14.197 回答
0

最后,使用IsSubType确实是解决方案。

正如@kiaenigma 所示,我需要将其添加到type Call与特征相关的内容中:Config

pub trait Config pub trait Config: system::Config + palletA::Config + palletB::Config {
    type Call: Parameter + Dispatchable<Origin = Self::Origin> + GetDispatchInfo + IsSubType<palletA::Call<Self>> + IsSubType<palletB::Call<Self>>;
}

然后在模块的方法中,将我的代码稍微更改为:

impl<T: Config> Module<T>
{
    fn compute_call_fee_(call: &<T as Config>::Call) -> Result<u64, DispatchError> {
        match call.is_sub_type() {
            Some(palletA::Call::palletA_method1(_arg1,_arg2)) => return Ok(60_u64),
            Some(palletA::Call::palletA_method2(_arg1,_arg2,_arg3)) => return Ok(60_u64),
            _ => {}
        };
        match call.is_sub_type() {
            Some(palletB::Call::palletB_method1(_arg1)) => return Ok(40_u64),
            _ => {}
        };

        return 80_u64;
    }
}
于 2021-03-03T15:14:12.087 回答