Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rate limits and block timestamps #96

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions cli/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,22 @@ export class Client {
let blockNumber = latestBlock.number - targetBlocksToMove
while (true) {
if (blockNumber <= 0n) return 0n

const block = await this.rpc.getBlock({ blockNumber })

const timeDifferenceSeconds = targetTimestamp - block.timestamp

// Block is within 1 block before the target timestamp
if (timeDifferenceSeconds >= 0n && timeDifferenceSeconds <= averageBlockTimeSeconds) break

// Math.ceil is used for averageBlockTimeSeconds because if its sub second, we will never converge
// on a solution.
if (timeDifferenceSeconds >= 0n && timeDifferenceSeconds <= Math.ceil(averageBlockTimeSeconds)) break
const blocksToMove = BigInt(Math.ceil(Math.abs(Number(timeDifferenceSeconds)) / averageBlockTimeSeconds))

if (block.timestamp > targetTimestamp) {
blockNumber -= blocksToMove
} else {
blockNumber += blocksToMove
}
// sleep momentarily to avoid rate limiting
await new Promise(resolve => setTimeout(resolve, 100))
}

// In case of multiple batched blocks for a target timestamp, find the earliest or latest block based on blockMode
Expand All @@ -110,6 +111,8 @@ export class Client {
if (block.timestamp !== targetTimestamp) break

blockNumber = nextBlockNumber
// sleep momentarily to avoid rate limiting
await new Promise(resolve => setTimeout(resolve, 100))
}

return blockNumber
Expand Down Expand Up @@ -199,6 +202,8 @@ export class Client {

const rewardUnits = totalRewardUnits - totalRewardUnitsPrevEpoch

// sleep momentarily to avoid rate limiting
await new Promise(resolve => setTimeout(resolve, 1000))
if (rewardUnits <= 0) continue

closingStateByStakingAddress[address] = { rewardUnits, totalRewardUnits, runeAddress }
Expand Down Expand Up @@ -255,7 +260,6 @@ export class Client {
const addresses = [
...new Set(stakeEvents.map(event => event.args.account).filter(address => Boolean(address))),
] as Address[]

const closingStateByStakingAddress = await this.getClosingStateByStakingAddress(addresses, startBlock, endBlock)
const distributionsByStakingAddress = await this.getDistributionsByStakingAddress(
closingStateByStakingAddress,
Expand Down Expand Up @@ -299,7 +303,7 @@ export class Client {
'The total reward distribution calculated for all stakers is outside of the expected .01% margin of the total rewards to be distributed.',
)

info(`Total Distribtution Calculated: ${totalEpochDistribution.div(100000000).toFixed()} RUNE`)
info(`Total Distribution Calculated: ${totalEpochDistribution.div(100000000).toFixed()} RUNE`)
info(`Total Epoch Distribution: ${totalDistribution.div(100000000).toFixed()} RUNE`)

const confirmed = await prompts.confirm({ message: 'Do you want to continue? ' })
Expand Down
Loading