遵循基板教程并声明托盘配置,如托盘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())
}
}如何访问基板托盘V2中的时间?
发布于 2021-07-05 22:10:47
与其像你所展示的那样直接使用托盘,不如使用托盘和托盘松耦合所提供的两个特性之一。
我提到的两个特征可以在pallet_timestamp 源代码中找到
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>())
}
}要使用这一点,您应该在新的托盘中执行以下操作:
pallet_timestampuse 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;
}T::TimeProvider::now(),以毫秒为单位将unix时间作为u64返回。let time: u64 = T::TimeProvider::now().as_secs();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!`
}https://stackoverflow.com/questions/68262293
复制相似问题