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

refactor: simplify refresh balance triggers #6232

Merged
merged 5 commits into from
Nov 15, 2024

Conversation

kathaypacific
Copy link
Collaborator

@kathaypacific kathaypacific commented Nov 14, 2024

Description

This PR consolidates the way we are refreshing the token balances. I think we let this evolve organically for some time and it has become buggy and inconsistent.

Currently, before this change:

  • we have a bunch of watcher functions that seem overcomplicated (dispatching intermediate actions to trigger other sagas)
  • we need to remember to dispatch the fetchTokenBalances action after every flow that initiates a transaction. i think it is probably unavoidable to need to remember to trigger a balance fetch, but currently we dispatch a success action for the flow and a separate action for the balances (the dreaded double dispatch)
  • we forget to refresh balances in a bunch of places (e.g. swap flow) which means users can try (and fail) to execute other transactions with outdated balances.

This change:

  • remove the fetchTokenBalances action entirely and instead, trigger the balance refresh sagas on specific actions. i've tried to document them all.
  • there is a loading state that seems unnecessary in the home reducer, most flows were never allowing this loading state to be set. only the send flow was, i think that's a bug. balance refreshes should be a background task and not affect whether we show or hide the balance....
  • define balance refresh as a combination of refreshing token balances, exchange rates (the total balance is wrong if the exchange rate is outdated), positions so trigger all those sagas

Test plan

I should see a network request to refresh token balance, exchange rate, positions, shortcuts when I have:

  • completed onboarding
  • received a payment (transaction feed updated)
  • executed a transaction via any app flow

Related issues

Backwards compatibility

yes

Network scalability

If a new NetworkId and/or Network are added in the future, the changes in this PR will:

  • Continue to work without code changes, OR trigger a compilation error (guaranteeing we find it when a new network is added)

@kathaypacific kathaypacific marked this pull request as ready for review November 14, 2024 15:18
createFiatConnectTransferCompleted.type,
depositTransactionSucceeded.type,
swapSuccess.type,
],
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this is the main change

Copy link
Contributor

Choose a reason for hiding this comment

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

does it belong to home? what do you think about moving it to tokens and spawning it in the root saga instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yeah i had the same thought but i am not sure where is best for this. i also don't think tokens is the best place, because the balance refresh also involves refreshing local currency + positions. home kind of makes sense if we think of it being a middle ground between everything?

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, tokens is not the best place, but home doesn't feel right either for the same reason, because this saga updates the other screens too. i feel like it should be a separate one spawned from the root because of its "general" nature. don't have any good naming idea though 😅. so i agree that home is a reasonable place for now.

Copy link

codecov bot commented Nov 14, 2024

Codecov Report

Attention: Patch coverage is 82.85714% with 6 lines in your changes missing coverage. Please review.

Project coverage is 88.93%. Comparing base (e34ca3b) to head (901374b).
Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
src/localCurrency/saga.ts 25.00% 3 Missing ⚠️
src/fiatExchanges/saga.ts 50.00% 1 Missing ⚠️
src/home/saga.ts 92.30% 1 Missing ⚠️
src/positions/saga.ts 50.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #6232      +/-   ##
==========================================
- Coverage   88.98%   88.93%   -0.05%     
==========================================
  Files         737      737              
  Lines       31449    31444       -5     
  Branches     5828     5521     -307     
==========================================
- Hits        27984    27966      -18     
- Misses       3267     3433     +166     
+ Partials      198       45     -153     
Files with missing lines Coverage Δ
src/account/saga.ts 75.35% <ø> (-0.35%) ⬇️
src/components/TokenBalance.tsx 97.65% <100.00%> (-0.02%) ⬇️
src/earn/saga.ts 95.65% <ø> (-0.07%) ⬇️
src/import/saga.ts 83.76% <ø> (-0.28%) ⬇️
src/localCurrency/actions.ts 77.77% <ø> (-22.23%) ⬇️
src/localCurrency/reducer.ts 68.42% <ø> (+6.51%) ⬆️
src/redux/migrations.ts 97.08% <100.00%> (+<0.01%) ⬆️
src/redux/store.ts 80.00% <ø> (ø)
src/send/saga.ts 77.01% <ø> (-0.52%) ⬇️
src/tokens/saga.ts 96.82% <100.00%> (+1.44%) ⬆️
... and 8 more

... and 70 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e34ca3b...901374b. Read the comment docs.

export function* localCurrencySaga() {
yield* spawn(watchFetchCurrentRate)
yield* spawn(watchSelectPreferredCurrency)
yield* takeLatest([Actions.SELECT_PREFERRED_CURRENCY], safely(fetchLocalCurrencyRateSaga))
Copy link
Contributor

Choose a reason for hiding this comment

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

supernit: keeping the watch- prefixed saga seems easier to read to me

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

hum, but that means from the root saga we spawn this localCurrencySaga and all it does is spawn another function that executes this line? 🙈 i'm not sure i understand why the extra layer of abstraction increases readability, is it because we use the watch* pattern sometimes? we've not been so consistent with that pattern though

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, i noticed we use watch* pattern quite frequently and it seemed to me as a standard "building block". the abstraction could help to skim right away what this saga is doing: oh, it spawns a "watcher", that's it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

okay i put the watcher function back. i don't see a lot of value in the particular pattern 🙈 but i do see value in the consistency

Copy link
Contributor

@bakoushin bakoushin left a comment

Choose a reason for hiding this comment

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

Like the idea of getting rid of double dispatch and having an explicit list of what should trigger the refresh! 💅

@kathaypacific kathaypacific added this pull request to the merge queue Nov 15, 2024
Merged via the queue into main with commit b393e94 Nov 15, 2024
15 checks passed
@kathaypacific kathaypacific deleted the kathy/refresh-balance-actions branch November 15, 2024 12:40
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.

2 participants