Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add live scheduled rewards on current day. Closes #1677 #1697

Merged
merged 9 commits into from
Jul 26, 2024

Conversation

juliangruber
Copy link
Member

Closes #1677

@juliangruber juliangruber requested a review from bajtos July 11, 2024 23:33
@@ -8,9 +8,10 @@ type ScheduledRewardsResponse = {
}[]

async function getHistoricalScheduledRewards (address: string) {
const yesterday = new Date(new Date().setDate(new Date().getDate() - 1))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So many new Date instances! Have you considered using something like this?

const yesterday = new Date(Date.now() - 24 * 3600_000)

Copy link
Member Author

@juliangruber juliangruber Jul 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a strong opinion on this. To me the current version is better because

  • performance doesn't matter here at all
  • it uses Date APIs that work on a day level, not on a millisecond level
  • it doesn't use magics or constants that you need to remember (3600s = 1h)

What is your concern with the current version? Or are you saying it's difficult to read because of the many date instances?

Copy link
Member

@bajtos bajtos Jul 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find Date APIs confusing - some values are zero-based (months), and some are one-based (day of the month). getDate returns the day of the month, and getTime returns milliseconds since the Unix epoch. I always have to check the docs to recall what each method does.

Anyhow, this is not a big deal. The variable name clearly communicates what we are calculating, the rest is an implementation detail.

We can keep the current version.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand! I also feel that way about 0 vs 1 based Date functions, but I feel safe here since we're passing a value + 1, after reading it from Date, so whether the function is 0 based or 1 based doesn't make a difference

todayRewards = {
timestamp: new Date().toISOString(),
totalRewardsReceived: {
spark: rewards[rewards.length - 1]?.totalRewardsReceived.spark || 0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am confused about this part, could you please explain at high level how is this fixing the problem?

Several lines above, I see that you call https://stats.filspark.com/participant/${address}/scheduled-rewards with to=${yesterday}.

That means we will never fetch rewards for today. The code here seems to apply yesterday's rewards for today. Shouldn't we fetch today's reward from somewhere?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the goal is to show live scheduled rewards of today, not total rewards received. Therefore, if there's no entry for today, I add a new entry and set its total rewards to the ones from the day before.

Then, below in line 170, we set the scheduled rewards of the most recent entry to the live value from the contract.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Would you mind capturing this information in a code comment?

if (
      !todayRewards ||
      new Date(todayRewards.timestamp).getDate() !== new Date().getDate() ||
      new Date(todayRewards.timestamp).getMonth() !== new Date().getMonth() ||
      new Date(todayRewards.timestamp).getFullYear() !== new Date().getFullYear()
    ) {
      // If there's no entry for today, add a new entry copying data from yesterday.
      // We can use rewards from yesterday because we will change that value
      // later to the live value obtained from the smart contract
      todayRewards = {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not what happening though: We're setting totalRewardsReceived from yesterday and will not update it afterwards. What we're doing is we're setting totalScheduledRewards to 0 and then afterwards update that with the live value from the contract.

We don't have total rewards as a live value in the contract.

@juliangruber juliangruber requested a review from bajtos July 22, 2024 11:09
Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am struggling to understand the fix fully, but I also don't have the appetite to study the code closely to improve my understanding.

Let's get this shipped and see how it works in practice.

@juliangruber
Copy link
Member Author

I think the main disconnect is that this is setting a live value for scheduled rewards, not total rewards. Happy to walk through it if you would like

@juliangruber juliangruber merged commit 74f4b61 into main Jul 26, 2024
11 checks passed
@juliangruber juliangruber deleted the add/live-scheduled-rewards-on-current-day branch July 26, 2024 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Next payout different from the value in the chart
2 participants