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

contrib: live position & oracle price updates v1 #577

Conversation

0xcryptovenom
Copy link
Contributor

@0xcryptovenom 0xcryptovenom commented Nov 24, 2024

Draft: not tested at all yet, but should mostly work.

Description

  • Enable user positions & pyth oracle price updates on the trading page.
  • Fixes a few bugs, ie, when a position is updated, it should not change order.
  • Aims to finance some of the performance impact of positions live updates with some related performance improvements.

Changes

  • rework usePositions hook
  • TODO

Related issues & PRs

Copy link

vercel bot commented Nov 24, 2024

@0xcryptovenom is attempting to deploy a commit to the adrena Team on Vercel.

A member of the Team first needs to authorize it.

Copy link

vercel bot commented Nov 24, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
frontend ✅ Ready (Inspect) Visit Preview Nov 24, 2024 5:25am

Copy link
Contributor

@adrena-orex adrena-orex left a comment

Choose a reason for hiding this comment

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

Some feedback! Thank you for the PR 🙏

src/actions/thunks.ts Show resolved Hide resolved
src/actions/thunks.ts Outdated Show resolved Hide resolved
src/actions/thunks.ts Outdated Show resolved Hide resolved
src/actions/thunks.ts Outdated Show resolved Hide resolved
src/actions/thunks.ts Outdated Show resolved Hide resolved
src/actions/thunks.ts Show resolved Hide resolved
src/actions/thunks.ts Outdated Show resolved Hide resolved
src/components/pages/buy_alp_adx/ALPSwap/ALPSwap.tsx Outdated Show resolved Hide resolved
positions && positions.length > 0
? Array.from(
new Set(
...positions.flatMap((position) => [
Copy link
Contributor

Choose a reason for hiding this comment

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

The token prices used to calculate the PnL are:

      const exitPriceUi = tokenPrices[custody.tradeTokenInfo.symbol];
      const collateralTokenPriceUi =
        tokenPrices[collateralCustody.tokenInfo.symbol];

Reminder:
On a SOL LONG position, JITOSOL is the CollateralToken, SOL is the the TradeToken.
On a BONK LONG position, BONK is the CollateralToken, BONK is the the TradeToken.
On a SOL SHORT position, USDC is the CollateralToken, SOL is the the TradeToken.

Can use getTokenSymbol(position.token.symbol) to get the trade token

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess this is where we need to be careful.

My assumption is that we always need to react to the price changes of both the collateral token & the trade token, is this correct?

Copy link
Contributor Author

@0xcryptovenom 0xcryptovenom Nov 24, 2024

Choose a reason for hiding this comment

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

Re-reading this now..

You're saying my assumption is indeed correct, but actually we don't care about position.token.symbol because it doesn't really reflect the trade token, only getTokenSymbol(position.token.symbol) does.

I'm making a commit in that sense, let me know how it looks then:

4be0113

fixup: usePositions should react to the Trade Token price changes obtained through getTokenSymbol(position.token.symbol)

src/selectors/streamingTokenPrices.ts Outdated Show resolved Hide resolved
- introduce a selectStreamingTokenPricesFallback selector:
  - select streaming token prices, fallback to regular token price
- introduce a stopStreamingTokenPrices action + creator:
  - reset the streaming token prices state to initial state
- dispatch the stopStreamingTokenPrices when closing streaming.
- fetchUserPositions, fetchAndSubscribeToFullUserPositions, usePositions now leverage selectStreamingTokenPricesFallback under the hood
…ained through getTokenSymbol(position.token.symbol)
…tion

although the user positions are only fetched after user's wallet is connected,
the AdrenaClient.connection (from the user-scoped program's provider attached to the client),
may not yet be available.

this is fine because we still have the read-only program available, we can fetch positions.
@0xcryptovenom 0xcryptovenom force-pushed the contrib/live-positions-oracle-price-updates branch from b56b349 to 135d8c2 Compare November 26, 2024 22:57
const candidate =
selectedAction === 'long'
? tokenB ?? pickDefaultToken(positions)
: tokenACandidate[0];

if (tokenACandidate.some((t) => t.symbol === candidate.symbol)) {
setTokenA(candidate);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For slightly improved performance, we could move this out of the effect & do it during the render, similarly to the other state updates below.

There's also a lot more room for refactoring.

Copy link
Contributor

Choose a reason for hiding this comment

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

I let you do your magic

@adrena-orex
Copy link
Contributor

Created a new PR and deployed

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.

User positions shouldn't be loaded unnecessarily on unrelated pages
2 participants