簡體   English   中英

在 Polygon Mumbai Testnet 中按塊數計算塊時間

[英]Block time calculation by block number in Polygon Mumbai Testnet

我正在尋找一種可靠的方法來確定該塊何時由 Polygon Mumbai Testnet 中給定的任何塊號生成。 我不能將 Api 用於此任務。 我只是在尋找一種方便的計算方式。 你對此有何建議?

我絕對建議調用 API,但是,鑒於您對NoAPIs的大量要求,我們必須進行估算。

在這種情況下 -

  • 首先,我們需要找到每個塊的平均時間。 多邊形大約需要 - 2.5s來生成一個新塊 -數據
  • 然后你需要確定已經生成的塊數(在這種情況下手動輸入)
    • 在撰寫此答案時 - 已生成36996889個塊。
  • 最后,您需要要獲取時間估計的塊號。
    • 在這種情況下,我們以36996800為例。

因此,要確定估計值(同樣,一點也不准確),您可以使用以下算法。

 const currentBlockNumber = 36996889; const targetBlockNumber = 36996800; const averageTimePerBlock = 2.5; // Get the numenr of blocks that have been produced sinve the target block you're trying to estimate for const numberOfBlocks = currentBlockNumber - targetBlockNumber; // Calculate the approximate time that the target block was produced at const approximateTimeInSeconds = numberOfBlocks * averageTimePerBlock; // Calculate the approximate date and time that the target block was produced const dateAndTimeCurrentBlockWasProducedAt = new Date(1671405095000); // This was the epoch currentBlock was produced at const approximateTimeInMilliseconds = dateAndTimeCurrentBlockWasProducedAt.getTime() - (approximateTimeInSeconds * 1000); // get approx epoch for targetBlock const approximateDate = new Date(approximateTimeInMilliseconds); // convert approxTime to human readable date console.log(`The target block was produced approximately on ${approximateDate}.`);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM