Skip to content

Commit

Permalink
add read scheduled rewards from spark-rewards
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangruber committed Oct 7, 2024
1 parent 90c7522 commit 921c486
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions lib/rewards.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,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)
}),
getSparkRewardsScheduledRewardsWithFallback(ethAddress)
])
const totalRewards = rewards.reduce((a, b) => a + b, 0n)
onMetrics({ rewardsScheduledForAddress: totalRewards })

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

async function getScheduledRewardsWithFallback (contract, ethAddress) {
async function getSparkRewardsScheduledRewardsWithFallback (ethAddress) {
try {
const res = await fetch(
`https://spark-rewards.fly.dev/scheduled-rewards/${ethAddress}`
)
const json = await res.json()
return typeof json === 'string'
? BigInt(json)
: 0n // json can be `null`
} 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

0 comments on commit 921c486

Please sign in to comment.