feat(social-controllers): add the position-lifecycle stage of a trade - #9871
Draft
xavier-brochard wants to merge 1 commit into
Draft
feat(social-controllers): add the position-lifecycle stage of a trade#9871xavier-brochard wants to merge 1 commit into
xavier-brochard wants to merge 1 commit into
Conversation
A position moves through four events: it is opened, added to, reduced, and closed. `Trade['intent']` only distinguishes two — `exit` covers both a partial trim and a full close — so a client could not tell a trader who trimmed 10% from one who sold out, and social feeds announced "closed" for both. The social-api now classifies each fill and sends the result, so add the two fields that carry it: - `Trade['action']` — `opened` / `added` / `reduced` / `closed`, the fill's stage. Asset-agnostic, so clients that render only a direction for spot still get the value that decides whether a row realizes P&L. - `Position['isOpen']` — the API's own open/closed verdict, which beats a `positionAmount === 0` check: it survives precision dust and distinguishes "no position" from "a position of size ~0". Both are optional. The social-api ships independently of this package, so a response from a deployment that predates them must still validate; clients keep a local fallback that derives the stage from trade history. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Explanation
A trading position moves through four events: it is opened, added to, reduced, and closed.
Trade['intent']only distinguishes two of them —exitcovers both a partial trim and a full close — so a consumer cannot tell a trader who sold 10% of a position from one who sold out. In MetaMask Mobile's social feed that surfaced as a real defect: both announced "closed", and the row switched to realized P&L on the strength of the same wrong boolean.The social-api already had the information to settle this (it classifies each fill server-side, using a per-position
isOpenflag), but nothing in this package's types carried it. This PR adds the two fields that do:Trade['action']—'opened' | 'added' | 'reduced' | 'closed', the fill's stage in its position's lifecycle. Deliberately asset-agnostic: a client may render only a direction for spot tokens while still needing the full value, because it is what decides whether a row shows realized or current P&L.Position['isOpen']— the API's own open/closed verdict. Stronger than apositionAmount === 0check: it survives precision dust, and it distinguishes "no position" from "a position of size ~0". Perpetuals in particular retain a non-zeropositionAmountafter closing, so size alone cannot answer the question.TradeActionis exported for consumers that branch on the value.Both fields are optional, so this is not a breaking change. The social-api deploys independently of this package, so a response from a deployment that predates the fields must still validate —
SocialServiceuses superstruct's permissivetype(), and there is now a test pinning that a payload without them is accepted. Clients keep a local fallback that derives the stage from trade history for exactly this window.References
va-mmcx-social-apiPR addingTradeDto.actionandPositionDto.isOpenChecklist
🤖 Generated with Claude Code