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(#328): my trades implementation #337

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

Conversation

VanishMax
Copy link
Contributor

@VanishMax VanishMax commented Jan 30, 2025

Closes #328

Waits for penumbra-zone/web#2019 and then Prax release

Considerations of this PR:

  1. /api/my-executions is a POST method that accepts a possibly large body + AssetIds instead of symbols to not search for it in the registry and save time
  2. useLatestSwaps hook requests data in a pipeline, from Prax first, then from Pindexer
  3. If Prax returned no data or the amount of swaps in Prax is the same as previously requested data from Pindexer, the backend request to /api/my-executions will not be made to save time
  4. When new swap is made, refetches Pindexer data each 5 seconds until the swap is propagated from there
  5. The table has loading, empty, not connected states
my-trades.mp4

Before running: install local version of the protobuf package

@VanishMax VanishMax requested a review from TalDerei January 30, 2025 11:20
@VanishMax VanishMax self-assigned this Jan 30, 2025
@VanishMax VanishMax marked this pull request as draft January 30, 2025 16:35
Copy link
Contributor

@TalDerei TalDerei left a comment

Choose a reason for hiding this comment

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

encountering the following error after linking to the protobuf package locally

Screenshot 2025-01-30 at 8 04 01 PM

@VanishMax VanishMax requested a review from a team February 4, 2025 09:14
@VanishMax VanishMax marked this pull request as ready for review February 4, 2025 09:19
Comment on lines 9 to 10
// TODO: filter out irrelevant swaps from pindexer
const { data, isLoading, error } = useLatestSwaps(subaccount);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

todo: pindexer might return 2+ swaps per asset pair and a block height. Only one swap must be filtered out to match the user's swap. Might need some help do to 2+ swaps in the same block

@VanishMax VanishMax requested a review from TalDerei February 4, 2025 17:02
Copy link
Contributor

@TalDerei TalDerei left a comment

Choose a reason for hiding this comment

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

I think it’s almost there! I left some comments with observations specific to my wallet

src/pages/trade/api/latest-swaps.ts Outdated Show resolved Hide resolved
return [];
}

// Two requests to get swaps in both directions: buy and sell
Copy link
Contributor

Choose a reason for hiding this comment

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

ACK – rendering both swap directions of the pair.

src/shared/database/index.ts Outdated Show resolved Hide resolved
* Combines the list of pair:height combinations into a list of unique pairs with all heights in a merged array.
* This transformation is needed to reduce the amount of database queries.
*/
const adaptBody = (body: MyExecutionsRequestBody[]): ExecutionCollection[] => {
Copy link
Contributor

Choose a reason for hiding this comment

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

ACK – taking the mapped swap objects and transforming them into a form more amenable for efficient pindexer querying.

Screenshot 2025-02-04 at 8 47 35 PM

Comment on lines 73 to 77
const [registry, sellStream, buyStream] = await Promise.all([
registryClient.remote.get(chainId),
Promise.all(data.map(data => pindexer.myExecutions(data.base, data.quote, data.heights))),
Promise.all(data.map(data => pindexer.myExecutions(data.quote, data.base, data.heights))),
]);
Copy link
Contributor

Choose a reason for hiding this comment

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

comment:

  1. myTradesQuery is returning 52 results for UM <> USDC pair (out of 64 swaps stored in IndexedDB).
Screenshot 2025-02-04 at 7 52 22 PM Screenshot 2025-02-04 at 8 13 22 PM
  1. myExecutionsQuery then sends the mapped swap objects to the server endpoint (/api/my-executions).
Screenshot 2025-02-04 at 8 54 13 PM
  1. The server then queries pindexer which filters these results down to only 5 swaps, which isn't correct. I think this is pointing to an incorrect SQL query.
Screenshot 2025-02-04 at 8 46 18 PM Screenshot 2025-02-04 at 7 52 14 PM

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@TalDerei this might be related to the issue with pindexer not storing (or losing) some swaps, discussed in the protocol chat. Same happens to me with old swaps, the new ones should work fine

Copy link
Contributor

Choose a reason for hiding this comment

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

@TalDerei this might be related to the issue with pindexer not storing (or losing) some swaps, discussed in the protocol chat. Same happens to me with old swaps, the new ones should work fine

I see, linked this discussion in that thread

src/shared/api/server/my-executions.ts Outdated Show resolved Hide resolved
@VanishMax VanishMax requested a review from TalDerei February 5, 2025 11:26
@VanishMax
Copy link
Contributor Author

@TalDerei I did a major refactor to the SQL query. Now it sends only 1 request to Pindexer instead of multiple ones and uses only Postgres features to perform the checks:

  1. Creates a virtual table with latestSwaps data, duplicating data for sell-buy directions in advance
  2. Merges this data with swap executions from Pindexer

An important recent change: now the merge happens not only by pair and height, but also by the amount field that is different for buy-sell directions. This makes sure we only receive personal swaps, not many random swaps on a height as before.

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.

explorer: render recent trades
2 participants