calculate 24h time window on block time

This commit is contained in:
BitcoinVM 2024-02-02 12:57:17 +08:00
parent 7b4379c902
commit bd080281a7

View File

@ -12,6 +12,7 @@ import { onMounted, ref } from 'vue';
import { Icon } from '@iconify/vue'; import { Icon } from '@iconify/vue';
import type { Key, SlashingParam, Validator } from '@/types'; import type { Key, SlashingParam, Validator } from '@/types';
import { formatSeconds} from '@/libs/utils' import { formatSeconds} from '@/libs/utils'
import { diff } from 'semver';
const staking = useStakingStore(); const staking = useStakingStore();
const base = useBaseStore(); const base = useBaseStore();
@ -40,12 +41,12 @@ onMounted(() => {
}) })
}); });
async function fetchChange() { async function fetchChange(blockWindow: number = 14400) {
let page = 0; let page = 0;
let height = Number(base.latest?.block?.header?.height || 0); let height = Number(base.latest?.block?.header?.height || 0);
if (height > 14400) { if (height > blockWindow) {
height -= 14400; height -= blockWindow;
} else { } else {
height = 1; height = 1;
} }
@ -71,7 +72,6 @@ async function fetchChange() {
} }
} }
const changes = computed(() => { const changes = computed(() => {
const changes = {} as Record<string, number>; const changes = {} as Record<string, number>;
Object.keys(latest.value).forEach((k) => { Object.keys(latest.value).forEach((k) => {
@ -198,11 +198,13 @@ const logo = (identity?: string) => {
: `https://s3.amazonaws.com/keybase_processed_uploads/${url}`; : `https://s3.amazonaws.com/keybase_processed_uploads/${url}`;
}; };
let height_in_24h = ref(0)
base.$subscribe((_, s) => { base.$subscribe((_, s) => {
if (Number(s.earlest.block.header.height) !== height_in_24h.value) { console.log(s.recents.length)
height_in_24h.value = Number(s.earlest.block.header.height); if (s.recents.length === 2) {
fetchChange(); const diff_time = Date.parse(s.recents[1].block.header.time) - Date.parse(s.recents[0].block.header.time)
const diff_height = Number(s.recents[1].block.header.height) - Number(s.recents[0].block.header.height)
const block_window = Number(Number(86400 * 1000 * diff_height / diff_time).toFixed(0))
fetchChange(block_window);
} }
}); });