diff --git a/lib/rewards.js b/lib/rewards.js index baa4b874..bd07450b 100644 --- a/lib/rewards.js +++ b/lib/rewards.js @@ -1,4 +1,5 @@ import timers from 'node:timers/promises' +import assert from 'node:assert/strict' /** * @param {object} args @@ -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 @@ -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) {