如何解决如何在基板模块中将块号转换为整数类型? 我的代码错误日志
我正在测试substrate off-chain worker,我想要做的是接收当前的区块号,然后做一些计算,就像下面的代码if (get_block / 10 == 0)
,我得到了一些错误。如何将块号转换为整数类型?
我的代码
use frame_support::{decl_storage,decl_module,dispatch::dispatchResult,debug};
use frame_system::{ensure_signed,offchain};
use sp_runtime::{
offchain::http,transaction_validity::{
TransactionValidity,TransactionLongevity,ValidTransaction,InvalidTransaction
}
};
pub trait Trait: frame_system::Trait {}
decl_storage! {
trait Store for Module<T: Trait> as Runtime_example {
SubjectCount: u32;
}
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn offchain_worker(block: T::BlockNumber){
let get_block = block;
if (get_block / 10 == 0) {
debug::info!("print !!!!!!!!!!!!!!!!");
}
}
}
}
错误日志
error[E0308]: mismatched types
--> /home/substrate-node-template/runtime/src/runtime_example.rs:32:29
|
32 | if (get_block / 10 == 0) {
| ^ expected associated type,found integer
|
= note: expected associated type `<T as frame_system::Trait>::BlockNumber`
found type `{integer}`
= help: consider constraining the associated type `<T as frame_system::Trait>::BlockNumber` to `{integer}` or calling a method that returns `<T as frame_system::Trait>::BlockNumber`
error[E0308]: mismatched types
--> /home/substrate-node-template/runtime/src/runtime_example.rs:32:34
|
32 | if (get_block / 10 == 0) {
| ^ expected associated type,found integer
|
= note: expected associated type `<T as frame_system::Trait>::BlockNumber`
found type `{integer}`
= help: consider constraining the associated type `<T as frame_system::Trait>::BlockNumber` to `{integer}` or calling a method that returns `<T as frame_system::Trait>::BlockNumber`
解决方法
@kmdreko 是对的。您不想将块号转换为整数,而是将整数转换为块号然后进行数学运算。
所以替换:
get_block / 10 == 0
与:
(get_block / 10.into()).is_zero()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。