block-interval.ts 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // import {ApiPromise, WsProvider} from "@polkadot/api";
  2. // import {Hash, Header} from "@polkadot/types/interfaces/runtime";
  3. // import { types } from '@joystream/types'
  4. //
  5. // async function main() {
  6. // let startDate = new Date(2020, 4, 20, 13, 0);
  7. // console.log(startDate);
  8. // let endDate = new Date(2020, 4, 29, 23, 59);
  9. //
  10. // // Initialise the provider to connect to the local node
  11. // const provider = new WsProvider('wss://rome-rpc-endpoint.joystream.org:9944');
  12. //
  13. // // Create the API and wait until ready
  14. // const api = await ApiPromise.create({provider, types});
  15. //
  16. // let blockInterval = await getBlockInterval(api, startDate.getTime(), endDate.getTime());
  17. // console.log(blockInterval);
  18. // }
  19. //
  20. // async function getBlockInterval(api: ApiPromise, startTimestamp: number, endTimestamp: number) {
  21. //
  22. // let approximateStartBlockHash = await getApproximatedBlockHash(api, startTimestamp);
  23. // let startBlock = await adjustApproximatedBlockHash(api, startTimestamp, approximateStartBlockHash);
  24. //
  25. // let approximateEndBlockHash = await getApproximatedBlockHash(api, endTimestamp);
  26. // let endBlock = await adjustApproximatedBlockHash(api, endTimestamp, approximateEndBlockHash);
  27. //
  28. // let startBlockHeader = await api.rpc.chain.getHeader(startBlock) as Header;
  29. // let endBlockHeader = await api.rpc.chain.getHeader(endBlock) as Header;
  30. //
  31. // return {
  32. // 'startBlock':
  33. // startBlockHeader.number.unwrap().toNumber(),
  34. // 'endBlock':
  35. // endBlockHeader.number.unwrap().toNumber()
  36. // };
  37. // }
  38. //
  39. // async function getApproximatedBlockHash(api: ApiPromise, timestampToFound: number): Promise<Hash> {
  40. // let lastHeader = await api.rpc.chain.getHeader();
  41. // let lastHash = lastHeader.hash.toString();
  42. // let lastTimestamp = parseInt((await api.query.timestamp.now.at(lastHash)).toString());
  43. //
  44. // let prevousBlockHash = lastHeader.parentHash;
  45. // let previousBlockTimestamp = parseInt((await api.query.timestamp.now.at(prevousBlockHash)).toString());
  46. //
  47. // let secondsPerBlock = lastTimestamp - previousBlockTimestamp;
  48. //
  49. // let blocksDiff = Math.floor((lastTimestamp - timestampToFound) / secondsPerBlock);
  50. // let lastBlockNumber = lastHeader.number.unwrap();
  51. // let approximatedBlockNr = lastBlockNumber.toNumber() - blocksDiff;
  52. // return await api.rpc.chain.getBlockHash(approximatedBlockNr);
  53. // }
  54. //
  55. // async function adjustApproximatedBlockHash(api: ApiPromise, timestamp: number, hash: Hash) {
  56. // let approximatedBlockTimestamp = parseInt((await api.query.timestamp.now.at(hash)).toString());
  57. //
  58. // if (timestamp == approximatedBlockTimestamp) {
  59. // return hash;
  60. // }
  61. //
  62. // let step = 1;
  63. // if (timestamp < approximatedBlockTimestamp) {
  64. // step = -1;
  65. // }
  66. //
  67. // let approximatedBlockHeader = await api.rpc.chain.getHeader(hash);
  68. // let blockNumber = approximatedBlockHeader.number.unwrap().toNumber();
  69. // let lastHashFound = hash;
  70. // do {
  71. // blockNumber += step;
  72. // let nextBlockHash = await api.rpc.chain.getBlockHash(blockNumber);
  73. // let nextBlockTimeStamp = parseInt((await api.query.timestamp.now.at(nextBlockHash)).toString());
  74. //
  75. // if (Math.abs(approximatedBlockTimestamp - timestamp) < Math.abs(nextBlockTimeStamp - timestamp)) {
  76. // return lastHashFound;
  77. // }
  78. //
  79. // approximatedBlockTimestamp = nextBlockTimeStamp;
  80. // lastHashFound = nextBlockHash;
  81. //
  82. // } while (true);
  83. // }
  84. //
  85. // main();