【问题标题】:Substrate frame V2 how to use pallet_timestampSubstrate frame V2如何使用pallet_timestamp
【发布时间】:2021-07-06 06:10:47
【问题描述】:

按照基板教程并声明托盘配置如下 在托盘中lib.rs

use pallet_timestamp as timestamp;
    #[pallet::config]
    pub trait Config: frame_system::Config + pallet_timestamp::Config{
        type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
    }

Caego.toml中的配置

pallet-timestamp = { version = '3.0', default-features = false}

std = [
    'codec/std',
    'frame-support/std',
    'frame-system/std',
    'sp-runtime/std',
    'pallet-timestamp/std',
    'log/std',
]

我需要使用pallet_timestamp获取时间戳

#[pallet::call]
impl<T: Config> Pallet<T> {

        #[pallet::weight(0)]
        pub fn update_recoed(origin: OriginFor<T>, record: Vec<u8>) -> DispatchResultWithPostInfo {
            let pallet_time = <timestamp::Module<T>>::get(); // Error
            Ok(().into())
        }
}

如何获取 Substrate Pallet V2 中的时间?

【问题讨论】:

  • 我已经为您提供了如何设计托盘以获得时间的更好答案,但是,总的来说,您上面写的应该工作......所以可以您提供了您收到的错误消息,以便我们也可以为您解决该问题?

标签: rust substrate


【解决方案1】:

与其直接使用您所展示的托盘,不如使用托盘提供的两个特征之一和托盘的松散耦合。

我提到的两个特征可以在pallet_timestampsource code

impl<T: Config> Time for Pallet<T> {
    type Moment = T::Moment;

    /// Before the first set of now with inherent the value returned is zero.
    fn now() -> Self::Moment {
        Self::now()
    }
}

/// Before the timestamp inherent is applied, it returns the time of previous block.
///
/// On genesis the time returned is not valid.
impl<T: Config> UnixTime for Pallet<T> {
    fn now() -> core::time::Duration {
        // now is duration since unix epoch in millisecond as documented in
        // `sp_timestamp::InherentDataProvider`.
        let now = Self::now();
        sp_std::if_std! {
            if now == T::Moment::zero() {
                log::error!(
                    target: "runtime::timestamp",
                    "`pallet_timestamp::UnixTime::now` is called at genesis, invalid value returned: 0",
                );
            }
        }
        core::time::Duration::from_millis(now.saturated_into::<u64>())
    }
}

要使用它,您应该在新托盘中执行以下操作:

  1. 更新您的配置以获得使用这些特征之一的新配置。您无需将托盘与pallet_timestamp 紧密耦合:
use frame_support::traits::UnixTime;

#[pallet::config]
pub trait Config: frame_system::Config {
    type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
    type TimeProvider: UnixTime;
}
  1. 然后,您可以在托盘中的任何位置调用 T::TimeProvider::now() 以将 unix 时间(以毫秒为单位)作为 u64 返回。
let time: u64 = T::TimeProvider::now();
  1. 然后,要使其工作,您需要将pallet_timstamp 托盘插入为您的“TimeProvider”。当你 impl 你的 my_pallet::Config 时,你可以通过配置这个来做到这一点:
impl my_pallet::Config for Runtime {
    type Event = Event;
    type TimeProvider = pallet_timestamp::Pallet<Runtime>;
    // Or more easily just `Timestamp` assuming you used that name in `construct_runtime!`
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-15
    • 2021-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多