From 921c486c615c025ecf0e5b95cc994bddfc9351e3 Mon Sep 17 00:00:00 2001 From: Julian Gruber Date: Mon, 7 Oct 2024 15:05:04 +0200 Subject: [PATCH 1/3] add read scheduled rewards from `spark-rewards` --- lib/rewards.js | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/rewards.js b/lib/rewards.js index baa4b874..b9c8174d 100644 --- a/lib/rewards.js +++ b/lib/rewards.js @@ -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 @@ -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) { From 078b247f2b1094c9762ec15146582ddaf361f44c Mon Sep 17 00:00:00 2001 From: Julian Gruber Date: Fri, 11 Oct 2024 10:57:42 +0200 Subject: [PATCH 2/3] rename --- lib/rewards.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/rewards.js b/lib/rewards.js index b9c8174d..b7c5163f 100644 --- a/lib/rewards.js +++ b/lib/rewards.js @@ -15,7 +15,7 @@ export const runUpdateRewardsLoop = async ({ contracts, ethAddress, onMetrics }) ...contracts.map(async contract => { return getContractScheduledRewardsWithFallback(contract, ethAddress) }), - getSparkRewardsScheduledRewardsWithFallback(ethAddress) + getOffchainScheduledRewardsWithFallback(ethAddress) ]) const totalRewards = rewards.reduce((a, b) => a + b, 0n) onMetrics({ rewardsScheduledForAddress: totalRewards }) @@ -26,7 +26,7 @@ export const runUpdateRewardsLoop = async ({ contracts, ethAddress, onMetrics }) } } -async function getSparkRewardsScheduledRewardsWithFallback (ethAddress) { +async function getOffchainScheduledRewardsWithFallback (ethAddress) { try { const res = await fetch( `https://spark-rewards.fly.dev/scheduled-rewards/${ethAddress}` From a3e276e9851627f838e998289139db435b3005ae Mon Sep 17 00:00:00 2001 From: Julian Gruber Date: Fri, 11 Oct 2024 11:09:31 +0200 Subject: [PATCH 3/3] service now returns `0` --- lib/rewards.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/rewards.js b/lib/rewards.js index b7c5163f..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 @@ -32,9 +33,8 @@ async function getOffchainScheduledRewardsWithFallback (ethAddress) { `https://spark-rewards.fly.dev/scheduled-rewards/${ethAddress}` ) const json = await res.json() - return typeof json === 'string' - ? BigInt(json) - : 0n // json can be `null` + assert(typeof json === 'string') + return BigInt(json) } catch (err) { console.error('Failed to get scheduled rewards:', err.stack) return 0n