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

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

Solana Rust 智能合约可以访问

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

这是距纪元(格林威治标准时间 1970 年 1 月 1 日午夜)的秒数,但由于 Solana 随着时间的推移而放缓,因此与任何现实世界的时区都有显着差异。许多合约在计算奖励金额时都会考虑这个 unix 时间戳(特别是 Step Finance,因此 Gem Farm 重用了逻辑)。如何在 JavaScript 的前端重建这个 Solana unix 时间戳而不需要任何交易/钱包签名?调用 Solana 节点 RPC 没问题。

您可以使用 JSON RPC 中的 getBlockTime 端点。首先,您需要使用 getSlot 的最高插槽。那会变成:

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

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