Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
apexearth committed Feb 28, 2025
1 parent 3c81bae commit ca0f67f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
16 changes: 9 additions & 7 deletions lib/calculateBlockRate.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/calculateBlockRate.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions src/calculateBlockRate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,28 @@ export const calculateBlockRate = async (ctx: Context) => {
const latestBlock = ctx.blocks[ctx.blocks.length - 1]

// Update state for next context
if (latestBlock) {
lastContextBlock = latestBlock.header
}
// Move this to end of function
let blockRateResult = 1 // Default fallback

// If we have multiple blocks in current context, use those
if (ctx.blocks.length >= 2) {
const firstBlock = ctx.blocks[0]
const timeDiffSeconds = (latestBlock.header.timestamp - firstBlock.header.timestamp) / 1000
const blockDiff = latestBlock.header.height - firstBlock.header.height
return timeDiffSeconds / blockDiff
blockRateResult = timeDiffSeconds / blockDiff
}

// If we have one block and previous context data, use that
if (ctx.blocks.length === 1 && lastContextBlock) {
const timeDiffSeconds = (latestBlock.header.timestamp - lastContextBlock.timestamp) / 1000
const blockDiff = latestBlock.header.height - lastContextBlock.height
return timeDiffSeconds / blockDiff
blockRateResult = timeDiffSeconds / blockDiff
}

// Update state for next context at the end
if (latestBlock) {
lastContextBlock = latestBlock.header
}

// Default fallback
return 1
return blockRateResult
}

0 comments on commit ca0f67f

Please sign in to comment.