随着时间的推移,我如何从Kulupu(polkadotjs)获得困难


// Import
import { ApiPromise, WsProvider } from "@polkadot/api";
// Construct
/*
https://rpc.kulupu.network
https://rpc.kulupu.network/ws
https://rpc.kulupu.corepaper.org
https://rpc.kulupu.corepaper.org/ws
*/
(async () => {
//const wsProvider = new WsProvider('wss://rpc.polkadot.io');
const wsProvider = new WsProvider("wss://rpc.kulupu.network/ws");
const api = await ApiPromise.create({ provider: wsProvider });
// Do something
const chain = await api.rpc.system.chain();
console.log(`You are connected to ${chain} !`);
console.log(await api.query.difficulty.pastDifficultiesAndTimestamps.toJSON());
console.log(api.genesisHash.toHex());
})();

存储项pastDifficultiesAndTimestamps只保存最后60个块的数据。为了获得这些信息,你只需要修复以下内容:

console.log(await api.query.difficulty.pastDifficultiesAndTimestamps());

如果你想查询块的难度,一般来说,这样的循环会起作用:

let best_block = await api.derive.chain.bestNumber()
// Could be 0, but that is a lot of queries...
let first_block = best_block - 100;
for (let block = first_block; block < best_block; block++) {
let block_hash = await api.rpc.chain.getBlockHash(block);
let difficulty = await api.query.difficulty.currentDifficulty.at(block_hash);
console.log(block, difficulty)
}

请注意,这需要一个归档节点,该节点具有有关所有块的信息。否则,默认情况下,在状态修剪清理之前,节点只存储256个以前的块。

如果你想知道如何进行这样的查询,但效率更高,可以看看我的博客文章:

https://www.shawntabrizi.com/substrate/porting-web3-js-to-polkadot-js/

相关内容

  • 没有找到相关文章

最新更新