Skip to content

Commit

Permalink
fix: rate limits and block timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
0xean committed Dec 4, 2024
1 parent 647bb76 commit 5971ed8
Showing 1 changed file with 10 additions and 6 deletions.
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

0 comments on commit 5971ed8

Please sign in to comment.