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

feat(synapse-interface): refresh stale quotes #2607

Merged
merged 13 commits into from
Jun 17, 2024
Merged

Conversation

bigboydiamonds
Copy link
Collaborator

@bigboydiamonds bigboydiamonds commented May 10, 2024

Description
Add hook to manage refreshing stale quotes based on staleTimeout in ms.

Summary by CodeRabbit

  • New Features

    • Added a timestamp attribute to certain data constants to enhance data structure.
    • Introduced a custom React hook for refreshing quotes when they become stale.
    • Implemented a new function to calculate time differences between two timestamps.
  • Bug Fixes

    • Adjusted error logging behavior in the formatAmount function by disabling a console error output.
  • Refactor

    • Updated quote management processes to include timestamp handling in various functions and components.

1a0ebf35886568fe41e34413017d80e888d6837c: synapse-interface preview link
8ac4392f17c009ca2ee298436ceccb2d54db6ddc: synapse-interface preview link
a932e3c94d4a399aa326bb210b8e75fb08d9fe64: synapse-interface preview link
56f21dfab051bc6c18fa6274e15f53a22cb40c88: synapse-interface preview link
924eae382dc8718a2491a84f8d92b370d6b08a0c: synapse-interface preview link
14527c1896ad0412601c008d9e7e49c81e121ff4: synapse-interface preview link
252912fc2f7da8b7dc8f38ee361d205bc2bd1f16: synapse-interface preview link
cf34b600339a5799d2195c7b10209a69def32840: synapse-interface preview link
16def59d163793f04be9814df7f6a54593ac700d: synapse-interface preview link
4002ed7a3a05204ef20d02aff805b7365c7caa66: synapse-interface preview link

Copy link
Contributor

coderabbitai bot commented May 10, 2024

Walkthrough

The recent updates to the synapse-interface enhance data handling by integrating a timestamp field across various components, ensuring more precise tracking and management of bridge quotes. A new hook, useStaleQuoteRefresher, automates the refreshing of stale quotes, improving the interface's responsiveness and accuracy.

Changes

File Path Change Summary
.../constants/bridge.ts Added timestamp: null to EMPTY_BRIDGE_QUOTE and EMPTY_BRIDGE_QUOTE_ZERO.
.../pages/state-managed-bridge/index.tsx Enhanced quote management with timestamp integration and a new hook for refreshing stale quotes.
.../utils/hooks/useStaleQuoteRefresher.ts Introduced useStaleQuoteRefresher hook to refresh quotes based on timeout.
.../utils/time.ts Added calculateTimeBetween function to compute time differences.
.../utils/types/index.tsx Added timestamp field to BridgeQuote interface.
.../utils/actions/fetchBridgeQuotes.tsx Integrated getTimeMinutesFromNow to set timestamp in bridge quotes.
.../utils/formatAmount.ts Commented out error logging in formatAmount function.

🐰✨
In the land of code, where the bits align,
A rabbit hopped, with changes divine.
Timestamps here, a refresher there,
Quotes so fresh, beyond compare.
Hop, hop, hop, on silicon trails,
Through lines of code, where logic prevails. 🌟🐾


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

cloudflare-workers-and-pages bot commented May 10, 2024

Deploying sanguine-fe with  Cloudflare Pages  Cloudflare Pages

Latest commit: d29218d
Status: ✅  Deploy successful!
Preview URL: https://d42e3d71.sanguine-fe.pages.dev
Branch Preview URL: https://fe-interval-quotes.sanguine-fe.pages.dev

View logs

Copy link

cloudflare-workers-and-pages bot commented May 10, 2024

Deploying sanguine with  Cloudflare Pages  Cloudflare Pages

Latest commit: d29218d
Status: ✅  Deploy successful!
Preview URL: https://51433fec.sanguine.pages.dev
Branch Preview URL: https://fe-interval-quotes.sanguine.pages.dev

View logs

Copy link

codecov bot commented May 10, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 37.60614%. Comparing base (9795d55) to head (d29218d).
Report is 2 commits behind head on master.

Additional details and impacted files
@@                 Coverage Diff                  @@
##              master       #2607          +/-   ##
====================================================
+ Coverage   25.93072%   37.60614%   +11.67542%     
====================================================
  Files            750         168         -582     
  Lines          53265       13426       -39839     
  Branches          80          80                  
====================================================
- Hits           13812        5049        -8763     
+ Misses         38041        7738       -30303     
+ Partials        1412         639         -773     
Flag Coverage Δ
agents ?
cctp-relayer ?
core ?
ethergo ?
git-changes-action ?
packages 90.47619% <ø> (ø)
promexporter ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@github-actions github-actions bot added size/s and removed size/xs labels May 10, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 5

Out of diff range and nitpick comments (1)
packages/synapse-interface/utils/hooks/useStaleQuoteRefresher.ts (1)

22-23: Remove or replace debug logging before merging.

Debugging statements like console.log should be removed or replaced with a more robust logging framework that can be controlled via log levels.

Review Details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits Files that changed from the base of the PR and between 84da80d and 491ddc4.
Files selected for processing (5)
  • packages/synapse-interface/constants/bridge.ts (2 hunks)
  • packages/synapse-interface/pages/state-managed-bridge/index.tsx (5 hunks)
  • packages/synapse-interface/utils/hooks/useStaleQuoteRefresher.ts (1 hunks)
  • packages/synapse-interface/utils/time.ts (1 hunks)
  • packages/synapse-interface/utils/types/index.tsx (1 hunks)
Files skipped from review due to trivial changes (1)
  • packages/synapse-interface/utils/types/index.tsx
Additional comments not posted (2)
packages/synapse-interface/pages/state-managed-bridge/index.tsx (2)

77-77: Verify the relative import path for useStaleQuoteRefresher.

Ensure that the import path is correct and consistent with the project's structure. Relative paths can be error-prone and hard to maintain.


321-321: Ensure the hook useStaleQuoteRefresher is used effectively.

Review the parameters passed to useStaleQuoteRefresher to ensure they are correctly utilized within the hook. This is crucial for the hook's effectiveness in refreshing stale quotes.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Out of diff range and nitpick comments (1)
packages/synapse-interface/utils/actions/fetchBridgeQuotes.tsx (1)

98-98: The function locateBestExchangeRateToken correctly identifies the token with the best exchange rate. Consider optimizing this function if the number of quotes is expected to be large.

Review Details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits Files that changed from the base of the PR and between 491ddc4 and bfe83d5.
Files selected for processing (2)
  • packages/synapse-interface/pages/state-managed-bridge/index.tsx (5 hunks)
  • packages/synapse-interface/utils/actions/fetchBridgeQuotes.tsx (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/synapse-interface/pages/state-managed-bridge/index.tsx
Additional comments not posted (4)
packages/synapse-interface/utils/actions/fetchBridgeQuotes.tsx (4)

6-6: Import of getTimeMinutesFromNow is correctly added to support the new timestamp functionality.


27-27: The addition of currentTimestamp using getTimeMinutesFromNow(0) is appropriate for capturing the current time as a timestamp.


98-98: The inclusion of timestamp: currentTimestamp in the return object of fetchBridgeQuote effectively adds the new timestamp functionality to the bridge quote response. Ensure that all consumers of this function are aware of the new field in the response object.


98-98: Ensure that the error handling in fetchBridgeQuotes is robust, especially considering the asynchronous nature of the requests and the potential for partial failures.

Verification successful

The error handling within fetchBridgeQuotes appears to be robust, especially considering the asynchronous nature of the requests and the potential for partial failures. The function uses try-catch blocks to manage errors at both the individual request level and the batch level. Errors are logged, and the function continues processing other requests, effectively managing partial failures by filtering out unsuccessful responses. This implementation aligns with good practices for handling multiple asynchronous operations.

  • Location: packages/synapse-interface/utils/actions/fetchBridgeQuotes.tsx
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify robust error handling in `fetchBridgeQuotes`.

# Test: Simulate errors in some requests and ensure the function handles them gracefully.
rg --type tsx $'fetchBridgeQuotes'

Length of output: 61



Script:

#!/bin/bash
# Correcting the previous script to search for 'fetchBridgeQuotes' in TypeScript files
rg 'fetchBridgeQuotes' --glob '*.tsx' --glob '*.ts'

Length of output: 704



Script:

#!/bin/bash
# Extract the implementation of 'fetchBridgeQuotes' from its file
rg --context 10 'async function fetchBridgeQuotes' packages/synapse-interface/utils/actions/fetchBridgeQuotes.tsx

Length of output: 817



Script:

#!/bin/bash
# Extract more details about the handling of batch requests and error management in 'fetchBridgeQuotes'
rg --context 20 'batchRequests' packages/synapse-interface/utils/actions/fetchBridgeQuotes.tsx

Length of output: 1646

@bigboydiamonds bigboydiamonds marked this pull request as draft May 10, 2024 07:08
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review Details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits Files that changed from the base of the PR and between bfe83d5 and d8b0ece.
Files selected for processing (2)
  • packages/synapse-interface/utils/formatAmount.ts (1 hunks)
  • packages/synapse-interface/utils/hooks/useStaleQuoteRefresher.ts (1 hunks)
Files skipped from review due to trivial changes (1)
  • packages/synapse-interface/utils/formatAmount.ts
Files skipped from review as they are similar to previous changes (1)
  • packages/synapse-interface/utils/hooks/useStaleQuoteRefresher.ts

@bigboydiamonds bigboydiamonds marked this pull request as ready for review May 10, 2024 19:31
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review Details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits Files that changed from the base of the PR and between d8b0ece and 223f0d7.
Files selected for processing (1)
  • packages/synapse-interface/utils/hooks/useStaleQuoteRefresher.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/synapse-interface/utils/hooks/useStaleQuoteRefresher.ts

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review Details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits Files that changed from the base of the PR and between 223f0d7 and fe2ee9f.
Files selected for processing (2)
  • packages/synapse-interface/pages/state-managed-bridge/index.tsx (7 hunks)
  • packages/synapse-interface/utils/hooks/useStaleQuoteRefresher.ts (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • packages/synapse-interface/pages/state-managed-bridge/index.tsx
  • packages/synapse-interface/utils/hooks/useStaleQuoteRefresher.ts

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review Details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits Files that changed from the base of the PR and between fe2ee9f and 938b3c2.
Files selected for processing (2)
  • packages/synapse-interface/pages/state-managed-bridge/index.tsx (9 hunks)
  • packages/synapse-interface/utils/hooks/useStaleQuoteRefresher.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/synapse-interface/utils/hooks/useStaleQuoteRefresher.ts
Additional comments not posted (5)
packages/synapse-interface/pages/state-managed-bridge/index.tsx (5)

75-75: Ensure that the import path for useStaleQuoteRefresher is correct and that the hook is properly integrated into other parts of the system.


95-95: Confirm that the isLoading state is correctly managed across different components and hooks to ensure consistent UI behavior.


Line range hint 157-278: Review the handling of currentTimestamp to ensure it is correctly applied and consistent with the intended functionality of timestamp tracking in bridge quotes.

Verification successful

The usage of currentTimestamp in both fetchBridgeQuotes.tsx and state-managed-bridge/index.tsx appears to be consistent and correctly applied. In both cases, currentTimestamp is derived from the function getTimeMinutesFromNow(0), which suggests it is intended to capture the current time at the point of execution. This timestamp is then used in various contexts within the bridge quote process, such as logging, setting up transaction details, and error handling.

In fetchBridgeQuotes.tsx, currentTimestamp is used to timestamp the bridge quote response, ensuring that each quote has a record of when it was generated. This is crucial for any time-sensitive operations or for debugging purposes.

In state-managed-bridge/index.tsx, currentTimestamp is used similarly to mark the initiation of a bridge quote request and to manage transaction states. It is also used as an identifier for pending transactions, which is important for tracking and managing these transactions effectively.

Overall, the application of currentTimestamp across these files supports the intended functionality of timestamp tracking in bridge quotes, ensuring that each operation can be accurately timed and traced.

Conclusion:

  • The handling of currentTimestamp is consistent and aligns with the intended functionality of timestamp tracking in bridge quotes.
  • The usage is found across multiple files, indicating a standardized approach within the codebase.
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the correct application of `currentTimestamp` in bridge quotes.

# Test: Search for the usage of `currentTimestamp` in the context of bridge quotes. Expect: Correct and consistent application.
rg --type tsx --files-with-matches $'currentTimestamp'

Length of output: 81



Script:

#!/bin/bash
# Description: Re-verify the correct application of `currentTimestamp` in bridge quotes by searching TypeScript files.

# Test: Search for the usage of `currentTimestamp` in TypeScript files. Expect: Correct and consistent application.
rg --files-with-matches 'currentTimestamp' --glob '*.tsx'

Length of output: 184



Script:

#!/bin/bash
# Description: Extract sections of code where `currentTimestamp` is used in identified files for detailed review.

# Extract relevant code from fetchBridgeQuotes.tsx
echo "fetchBridgeQuotes.tsx:"
rg 'currentTimestamp' -C 10 packages/synapse-interface/utils/actions/fetchBridgeQuotes.tsx

# Extract relevant code from state-managed-bridge/index.tsx
echo "state-managed-bridge/index.tsx:"
rg 'currentTimestamp' -C 10 packages/synapse-interface/pages/state-managed-bridge/index.tsx

Length of output: 5366


320-325: Validate the integration of useStaleQuoteRefresher to ensure it is correctly configured with appropriate parameters and effectively refreshes stale quotes as intended.


Line range hint 379-522: Examine the error handling and finalization logic in executeBridge to ensure robustness and proper cleanup, particularly in the context of wallet transactions and UI updates.

Copy link

This PR is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the Stale label May 25, 2024
})
}
}
}, [currentTime])
Copy link
Collaborator

@abtestingalpha abtestingalpha Jun 11, 2024

Choose a reason for hiding this comment

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

Do we need the other deps in here?:

[currentTime, isValidQuote, isQuoteLoading, isWalletPending, quoteTime, staleTimeout]

Copy link
Collaborator Author

@bigboydiamonds bigboydiamonds Jun 12, 2024

Choose a reason for hiding this comment

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

currentTime is the only dep we would want to react to in order to maintain quote freshness for the staleTimeout defined time intervals. Including the other deps may prematurely refresh the quote in this useEffect

Copy link
Collaborator

Choose a reason for hiding this comment

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

isValidQuote, isQuoteLoading, isWalletPending: If I'm understanding this correctly, these flags determine if an event listener should be added? And we don't want to add them bc we don't want to re-attach if they change?

quoteTime: Is this the timestamp which staleness is calculated? Don't we need to include that bc if quoteTime changes, then the staleness logic needs to re-run?

staleTimeout: Is this the value that determines what's considered stale? So if it changes, the staleness logic would re-run here as well?

Copy link
Collaborator Author

@bigboydiamonds bigboydiamonds Jun 13, 2024

Choose a reason for hiding this comment

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

Correct, isValidQuote, isQuoteLoading, isWalletPending are used to determine if an event listener should be attached, but we don't want to react to these props because:

  • If wallet action is pending, it would refresh quote in background, but does not actually update the quote used for the transaction (prevents User confusion with output amounts)
  • Listening to isQuoteLoading could cause for infinite re-renders - if new quote is being fetched for updated inputs, this would effectively re-trigger another quote fetch
  • isValidQuote may change frequently if the User is testing out various inputs (token / amount combos) and could effectively re-trigger a quote fetch before interval time has passed

Including quoteTime would also cause infinite re-renders since the change in quoteTime would cause for the quote to refresh in the background, which then updates the quoteTime value again.

Good catch on the staleTimeout! I originally expected for staleTimeout to be a static value when using the hook, but that's an improper assumption to make. Updated to include staleTimeout in the useEffect dependencies array in f8fadd6

@bigboydiamonds bigboydiamonds merged commit c0de3ca into master Jun 17, 2024
38 checks passed
@bigboydiamonds bigboydiamonds deleted the fe/interval-quotes branch June 17, 2024 23:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants