简体   繁体   English

如何在前端(在 JavaScript 中)获取 Solana unix_timestamp?

[英]How to get the Solana unix_timestamp on the front-end (in JavaScript)?

Solana Rust smart contracts have access to Solana Rust 智能合约可以访问

solana_program::clock::Clock::get()?.unix_timestamp

which is seconds from epoch (midnight Jan 1st 1970 GMT) but has a significant drift from any real-world time-zone as a product of Solana's slowdowns over time.这是从纪元(格林威治标准时间 1970 年 1 月 1 日午夜)开始的几秒钟,但由于 Solana 随着时间的推移而减速,因此与任何现实世界的时区都有很大的偏差。 Many contracts factor in this unix timestamp when calculating reward amounts (notably Step Finance and therefore Gem Farm which reuses the logic).许多合约在计算奖励金额时会考虑这个 unix 时间戳(特别是 Step Finance 和因此重用逻辑的 Gem Farm)。 How can I reconstruct this Solana unix timestamp on the front-end in JavaScript without requiring any transaction / wallet signature?如何在 JavaScript 前端重建这个 Solana unix 时间戳而不需要任何交易/钱包签名? Calls to a Solana node RPC are fine.对 Solana 节点 RPC 的调用很好。

You can use the getBlockTime endpoint from JSON RPC.您可以使用 JSON RPC 中的getBlockTime端点。 First you'll need the highest slot using the getSlot .首先,您需要使用getSlot的最高插槽。 That would become:那将变成:

const connection = new Connection('https://api.testnet.solana.com', 'processed');
const slot = await connection.getSlot();
const timestamp = await connection.getBlockTime(slot);

More info at https://docs.solana.com/developing/clients/jsonrpc-api#getblocktime and https://docs.solana.com/developing/clients/jsonrpc-api#getslot更多信息,请访问https://docs.solana.com/developing/clients/jsonrpc-api#getblocktimehttps://docs.solana.com/developing/clients/jsonrpc-api#getslot

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM