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

Merged
merged 12 commits into from
Feb 11, 2025
Merged

feat(#328): my trades implementation #337

merged 12 commits into from
Feb 11, 2025

Conversation

VanishMax
Copy link
Contributor

@VanishMax VanishMax commented Jan 30, 2025

Closes #328 #327

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.

@TalDerei
Copy link
Contributor

TalDerei commented Feb 5, 2025

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.

reviewed the diff, and I think these changes generally make sense to me. I'll spend some time investigating our missing data issue.

update: I personally haven't found the culprit, and pindexer is now returning zero swap information for me.

@TalDerei
Copy link
Contributor

TalDerei commented Feb 7, 2025

@VanishMax what happens if the user has more swaps than we can display in the page? will it overflow the rendering?

@VanishMax
Copy link
Contributor Author

@TalDerei good point, I added the limit of 10 swaps, can be adjusted any time. Also, sorted by time desc

@VanishMax
Copy link
Contributor Author

update: I personally haven't found the culprit, and pindexer is now returning zero swap information for me.

found an issue with amount filtering – should've filtered by batch_input instead of input. Do you have trades now?

@erwanor erwanor self-requested a review February 7, 2025 13:39
@erwanor
Copy link
Member

erwanor commented Feb 7, 2025

Thanks, will do a round of review as well

@TalDerei
Copy link
Contributor

TalDerei commented Feb 10, 2025

found an issue with amount filtering – should've filtered by batch_input instead of input. Do you have trades now?

only seeing a single trade on my end coming out of pindexer – will review the recent changes.


Screenshot 2025-02-10 at 8 03 27 AM

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.

this seems to be rendering recent trades, and we can circle back and iterate on why historical swaps aren't displayed. great work 👍

@erwanor
Copy link
Member

erwanor commented Feb 11, 2025

We will want to harden the virtual table query, and carve out some time to iron out the kinks as they come up. Nice work

@erwanor erwanor merged commit a1b1fa9 into main Feb 11, 2025
3 checks passed
@erwanor erwanor deleted the feat/#328-my-trades branch February 11, 2025 15:13
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
3 participants