Skip to content

Commit

Permalink
Read scheduled rewards from spark-rewards (#606)
Browse files Browse the repository at this point in the history
* add read scheduled rewards from `spark-rewards`

* rename

* service now returns `0`
  • Loading branch information
juliangruber authored Oct 11, 2024
1 parent 0faaff9 commit 29579ea
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
@@ -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

0 comments on commit 29579ea

Please sign in to comment.