如何解决通过调用名称和参数匹配,用于来自其他托盘的调用
我有一个模块方法,可以将固定费用与来自各种托盘的呼叫相关联。 所以我试图通过托盘、调用名称和调用参数匹配作为参数传递给我的方法的调用。
类似的东西:
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 trait:
pub trait Config: system::Config + palletA::Config + palletB::Config {
type Call: Parameter + dispatchable<Origin = Self::Origin> + GetdispatchInfo;
}
如何通过原始托盘、函数名称和函数参数在 call
上进行匹配?
我试过了:
-
使用类似上面的语法,没有成功
-
调用实现
IsSubType
,并遵循以下答案:How to decode and match a call when passed as a parameter in Substrate。如果我做对了,这与来自其他托盘的呼叫无关。为了使用
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) } }
解决方法
在您粘贴完整代码(特别是 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
并查找 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.
,
最后,使用 IsSubType
确实是解决方案。
我需要将它添加到与 type Call
特性关联的 Config
中,如@kiaenigma 所示:
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;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。