Skip to content
This repository was archived by the owner on Jun 30, 2025. It is now read-only.
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
28 changes: 23 additions & 5 deletions lib/rewards.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import timers from 'node:timers/promises'
import assert from 'node:assert/strict'

/**
* @param {object} args
Expand All @@ -11,10 +12,13 @@ export const runUpdateRewardsLoop = async ({ contracts, ethAddress, onMetrics })
while (!contracts.length) {
await timers.setTimeout(1000)
}
const contractRewards = await Promise.all(contracts.map(async contract => {
return getScheduledRewardsWithFallback(contract, ethAddress)
}))
const totalRewards = contractRewards.reduce((a, b) => a + b, 0n)
const rewards = await Promise.all([
...contracts.map(async contract => {
return getContractScheduledRewardsWithFallback(contract, ethAddress)
}),
getOffchainScheduledRewardsWithFallback(ethAddress)
])
const totalRewards = rewards.reduce((a, b) => a + b, 0n)
onMetrics({ rewardsScheduledForAddress: totalRewards })

const delay = 10 * 60 * 1000 // 10 minutes
Expand All @@ -23,7 +27,21 @@ export const runUpdateRewardsLoop = async ({ contracts, ethAddress, onMetrics })
}
}

async function getScheduledRewardsWithFallback (contract, ethAddress) {
async function getOffchainScheduledRewardsWithFallback (ethAddress) {
try {
const res = await fetch(
`https://spark-rewards.fly.dev/scheduled-rewards/${ethAddress}`
)
const json = await res.json()
assert(typeof json === 'string')
return BigInt(json)
} catch (err) {
console.error('Failed to get scheduled rewards:', err.stack)
return 0n
}
}

async function getContractScheduledRewardsWithFallback (contract, ethAddress) {
try {
return await contract.rewardsScheduledFor(ethAddress)
} catch (err) {
Expand Down