Repository navigation
feat(perps): every close position analytics event carries order_type (market or limit) - #11027
Merged
Merged
Conversation
abretonc7s
marked this pull request as ready for review
October 23, 2026 07:12
abretonc7s
temporarily deployed
to
default-branch
October 23, 2026 07:12 — with
GitHub Actions
Inactive
The Perp Position Close Transaction summary event emitted by closePositions had no order_type, so close-all events reached Mixpanel as "not set". A batch close always submits market orders, on both the provider batch path and the per-position fallback, so tag the summary as market. Refs: TAT-4200
abretonc7s
force-pushed
the
TAT-4200-feat-every-close-position-analytics
branch
from
October 23, 2026 07:40
6225cc5 to
4ab4f09
Compare
abretonc7s
enabled auto-merge
October 23, 2026 08:15
michalconsensys
approved these changes
Oct 23, 2026
SnowingFox
pushed a commit
to SnowingFox/core-1
that referenced
this pull request
Oct 24, 2026
## Explanation Major release of `@metamask/perps-controller` (`21.0.0` → `22.0.0`). - **Breaking:** testnet deposit and withdrawal routes use Arbitrum Sepolia USDC2 (`0x1baAbB04529D43a73232B713C0FE471f7c7334d5`). `USDC_ARBITRUM_TESTNET_ADDRESS` and `HYPERLIQUID_ASSET_CONFIGS.usdc.testnet` change to that address, and `withdraw` rejects the previous testnet asset ID. `depositWithConfirmation` prepares an Arbitrum Sepolia transfer to the Hyperliquid testnet bridge. ([MetaMask#10991](MetaMask#10991)) - `TrackingData`, `TPSLTrackingData`, and `WithdrawParams` accept an optional `environmentType`. Events that already copy attribution include `environment_type` when it is set. ([MetaMask#10899](MetaMask#10899)) - New export `@metamask/perps-controller/providers/lighter`, plus optional `LighterCredentials.loadProvider`, so a web client can bundle Lighter itself. `switchProvider('lighter')` reports why the provider failed to load. A missing Lighter module is debug-logged instead of sent through `logger.error`. ([MetaMask#10996](MetaMask#10996)) - The `closePositions` summary event includes `order_type: 'market'`. ([MetaMask#11027](MetaMask#11027)) - Engines allow Node.js 26. ([MetaMask#10981](MetaMask#10981)) ## References - [MetaMask#10991](MetaMask#10991) - [MetaMask#10899](MetaMask#10899) - [MetaMask#10996](MetaMask#10996) - [MetaMask#11027](MetaMask#11027) - [MetaMask#10981](MetaMask#10981) ## Checklist - [ ] I've updated the test suite for new or updated code as appropriate - [ ] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've communicated my changes to consumers by [updating changelogs for packages I've changed](https://github.com/MetaMask/core/tree/main/docs/processes/updating-changelogs.md) - [ ] I've introduced [breaking changes](https://github.com/MetaMask/core/tree/main/docs/processes/breaking-changes.md) in this PR and have prepared draft pull requests for clients and consumer packages to resolve them <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Medium Risk** > Major perps-controller release with breaking testnet token/bridge behavior; this PR only bumps versions but consumers picking up 22.0.0 inherit that risk. > > **Overview** > **Monorepo release 1359.0.0** that publishes **`@metamask/perps-controller` 22.0.0** and bumps **`@metamask/transaction-pay-middleware`** to depend on `^22.0.0` (plus `yarn.lock` and root package version). > > The diff is mostly versioning and changelog housekeeping; the shipped **22.0.0** behavior (already merged) includes a **breaking testnet USDC2 / Arbitrum Sepolia deposit path** change, optional **`environmentType`** on perps tracking/withdraw params, **Lighter** `loadProvider` + public provider export, **`closePositions`** analytics `order_type: 'market'`, and **Node.js 26** engine support. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 8356d7d. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.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
Mixpanel shows
Perp Position Close Transactionevents withorder_type= not set.Root cause: the close-all summary event that
TradingService.closePositionsemits from itsfinallyblock carriesstatus,completion_duration,bulk_action_id,number_positions_closedanderror_message, but noorder_type. On Hyperliquid the provider's batch close runs, so this summary is the only close event a close-all produces.Fix: tag that summary event
order_type: market. A batch close only ever submits market orders:ClosePositionsParamshas no order type,HyperLiquidProvider.closePositionsbuilds reduce-only IOC orders taggedmarket, and the fallback closes each position withclosePosition({ symbol }), which defaults to market. The property is set infinally, so failed and thrown batches carry it too.Every close event path in the controller, after this change:
order_typeclosePositionsubmitted / executed / failed / partially filledPerp Position Close Transactionparams.orderType ?? market(unchanged)closePositionsper-position (fallback)closePositionssummaryflipPositionPerp Trade TransactionPerp Risk ManagementContract impact: one added property on an emitted analytics payload. No state, method, export, constant or type change. Mobile (
app/components/UI/Perps/adapters/mobileInfrastructure.ts) and Extension (app/scripts/controllers/perps/infrastructure.ts) forward controller event properties unchanged, so both pick this up on the next controller release.Client paths that emit their own close event without
order_type(separate client fix, not in this PR):ui/components/app/perps/perps-view.tsx:252,:265,:274(close-all handler;mainat2454d1f133): tracksMetaMetricsEventName.PerpsPositionCloseTransactionwith onlystatusandnumber_positions_closed.mainat6441e74cbd): none.PERPS_POSITION_CLOSE_TRANSACTIONappears only in the event definitions and the A/B mapping (app/components/UI/Perps/abTestConfig.ts:97).Validation:
PerpsControllerthrough its public API with the realMessengeron Hyperliquid testnet (fixturedev1, SOL, ~11 USD positions), records everymetrics.trackPerpsEventcall, and asserts each close path. Every position it opened was closed, and teardown asserts no SOL positions or orders.submitted/executed, bothmarket. Limit close:submitted/executed, bothlimit. Flip: trade eventssubmitted/executed, bothmarket. TP/SL update: no close event. Close-all summary:executed,market.order_typemissing).TradingServicetests (success, thrown and fallback batch close) assertorder_type: marketon every close event; all fail with the fix reverted.References
Validation Recipe
recipe.json (23 nodes — Close position analytics carry order_type - Core)
{ "$schema": "https://farmslot.io/schemas/recipe-v1.schema.json", "title": "Close position analytics carry order_type - Core", "description": "Drives PerpsController closePosition (market and limit), flipPosition, updatePositionTPSL and closePositions through the real messenger on Hyperliquid testnet, as a client wallet does, and records every analytics event the controller hands to metrics.trackPerpsEvent. Only the wallet side (account selection, keyring signing with the fixture key) is answered by the driver. Positions are opened with the library place_order action on one task-owned market (default SOL, about 11 USD notional) and every one is closed by its phase or by teardown. Close-all is scoped to that market. Driver records are written to fixed scratch paths, deleted by the driver before it runs, copied into the run package, and asserted against this run's run_tag.", "paramsSchema": { "type": "object", "additionalProperties": false, "required": [ "run_tag", "exclusive_testnet_market" ], "properties": { "account": { "type": "string", "default": "dev1", "description": "Wallet-fixture signing account for Hyperliquid testnet." }, "market": { "type": "string", "default": "SOL", "description": "Task-owned testnet market. Not BTC: other runs hold BTC state on dev1." }, "notional": { "type": "string", "default": "11", "description": "USD notional of each opened position (Hyperliquid minimum is 10)." }, "leverage": { "type": "number", "default": 2, "description": "Leverage of each opened position." }, "run_tag": { "type": "string", "description": "Fresh id for this run; the driver records and assertions are bound to it." }, "exclusive_testnet_market": { "type": "boolean", "enum": [ true ], "description": "Confirm this run owns the account/market during setup and teardown cleanup." } } }, "proofTargets": [ { "id": "AC-1", "kind": "acceptance", "proofMode": "state", "claim": "[TAT-4200] Every close position event emitted by perps-controller (single close, close all, flip, TP/SL close) carries order_type market or limit." }, { "id": "AC-2", "kind": "acceptance", "proofMode": "review", "claim": "[TAT-4200] Any client path that emits its own close event without order_type is listed with file and line." } ], "workflow": { "entry": "setup-driver-tsconfig", "teardown": "teardown-clean-market", "nodes": { "setup-driver-tsconfig": { "action": "command", "timeout_ms": 30000, "intent": "Point the client driver at this checkout's package sources", "next": "setup-clean-market" }, "setup-clean-market": { "action": "call", "ref": "perps.clean-market-testnet", "params": { "account": "{{params.account}}", "market": "{{params.market}}", "exclusive_testnet_market": "{{params.exclusive_testnet_market}}" }, "intent": "Start from a flat task-owned testnet market with no open orders", "next": "setup-open-for-tpsl" }, "setup-open-for-tpsl": { "action": "metamask.perps.place_order", "network": "testnet", "account": "{{params.account}}", "market": "{{params.market}}", "side": "long", "notional": "{{params.notional}}", "leverage": "{{params.leverage}}", "intent": "Open a small long to attach TP/SL to and then close by market", "next": "ac1-tpsl-drive" }, "ac1-tpsl-drive": { "action": "command", "timeout_ms": 180000, "intent": "TP/SL update through the controller public API, recording its analytics events", "next": "ac1-tpsl-evidence" }, "ac1-tpsl-evidence": { "action": "index_artifacts", "artifacts": [ { "type": "json", "category": "proof", "label": "TP/SL update: controller result, positions and captured analytics events", "proofTarget": "AC-1" }, { "type": "log", "category": "proof", "label": "TP/SL update: controller debug log", "proofTarget": "AC-1" } ], "intent": "Keep the tp/sl update record and controller log in the run package", "next": "ac1-tpsl-no-close-event" }, "ac1-tpsl-no-close-event": { "action": "assert_json", "assert": { "all": [ { "path": "$.runTag", "operator": "eq", "value": "{{params.run_tag}}" }, { "path": "$.phase", "operator": "eq", "value": "tpsl" }, { "path": "$.market", "operator": "eq", "value": "{{params.market}}" }, { "path": "$.error", "operator": "falsy" }, { "path": "$.positionBefore.side", "operator": "eq", "value": "long" }, { "path": "$.result.success", "operator": "eq", "value": true }, { "path": "$.closeEvents", "operator": "length_eq", "value": 0 }, { "path": "$.riskEventCount", "operator": "gte", "value": 1 }, { "path": "$.positionAfter.side", "operator": "eq", "value": "long" } ] }, "intent": "Setting TP/SL on an open position emits no close position event", "proves": [ "AC-1" ], "next": "ac1-close-market-drive" }, "ac1-close-market-drive": { "action": "command", "timeout_ms": 180000, "intent": "Single market close through the controller public API, recording its analytics events", "next": "ac1-close-market-evidence" }, "ac1-close-market-evidence": { "action": "index_artifacts", "artifacts": [ { "type": "json", "category": "proof", "label": "Single market close: controller result, positions and captured analytics events", "proofTarget": "AC-1" }, { "type": "log", "category": "proof", "label": "Single market close: controller debug log", "proofTarget": "AC-1" } ], "intent": "Keep the single market close record and controller log in the run package", "next": "ac1-close-market-order-type" }, "ac1-close-market-order-type": { "action": "assert_json", "assert": { "all": [ { "path": "$.runTag", "operator": "eq", "value": "{{params.run_tag}}" }, { "path": "$.phase", "operator": "eq", "value": "close-market" }, { "path": "$.market", "operator": "eq", "value": "{{params.market}}" }, { "path": "$.error", "operator": "falsy" }, { "path": "$.positionBefore.side", "operator": "eq", "value": "long" }, { "path": "$.result.success", "operator": "eq", "value": true }, { "path": "$.closeStatuses", "operator": "deep_eq", "value": [ "submitted", "executed" ] }, { "path": "$.closeOrderTypes", "operator": "deep_eq", "value": [ "market", "market" ] }, { "path": "$.positionAfter", "operator": "falsy" } ] }, "intent": "Every close event from a market close carries order_type market", "proves": [ "AC-1" ], "next": "setup-open-for-limit" }, "setup-open-for-limit": { "action": "metamask.perps.place_order", "network": "testnet", "account": "{{params.account}}", "market": "{{params.market}}", "side": "long", "notional": "{{params.notional}}", "leverage": "{{params.leverage}}", "intent": "Open a small long to close with a limit order", "next": "ac1-close-limit-drive" }, "ac1-close-limit-drive": { "action": "command", "timeout_ms": 180000, "intent": "Single limit close through the controller public API, recording its analytics events", "next": "ac1-close-limit-evidence" }, "ac1-close-limit-evidence": { "action": "index_artifacts", "artifacts": [ { "type": "json", "category": "proof", "label": "Single limit close: controller result, positions and captured analytics events", "proofTarget": "AC-1" }, { "type": "log", "category": "proof", "label": "Single limit close: controller debug log", "proofTarget": "AC-1" } ], "intent": "Keep the single limit close record and controller log in the run package", "next": "ac1-close-limit-order-type" }, "ac1-close-limit-order-type": { "action": "assert_json", "assert": { "all": [ { "path": "$.runTag", "operator": "eq", "value": "{{params.run_tag}}" }, { "path": "$.phase", "operator": "eq", "value": "close-limit" }, { "path": "$.market", "operator": "eq", "value": "{{params.market}}" }, { "path": "$.error", "operator": "falsy" }, { "path": "$.positionBefore.side", "operator": "eq", "value": "long" }, { "path": "$.request.orderType", "operator": "eq", "value": "limit" }, { "path": "$.result.success", "operator": "eq", "value": true }, { "path": "$.closeStatuses", "operator": "deep_eq", "value": [ "submitted", "executed" ] }, { "path": "$.closeOrderTypes", "operator": "deep_eq", "value": [ "limit", "limit" ] }, { "path": "$.positionAfter", "operator": "falsy" } ] }, "intent": "Every close event from a limit close carries order_type limit", "proves": [ "AC-1" ], "next": "setup-open-for-flip" }, "setup-open-for-flip": { "action": "metamask.perps.place_order", "network": "testnet", "account": "{{params.account}}", "market": "{{params.market}}", "side": "long", "notional": "{{params.notional}}", "leverage": "{{params.leverage}}", "intent": "Open a small long to flip short and then close with close-all", "next": "ac1-flip-drive" }, "ac1-flip-drive": { "action": "command", "timeout_ms": 180000, "intent": "Flip through the controller public API, recording its analytics events", "next": "ac1-flip-evidence" }, "ac1-flip-evidence": { "action": "index_artifacts", "artifacts": [ { "type": "json", "category": "proof", "label": "Flip: controller result, positions and captured analytics events", "proofTarget": "AC-1" }, { "type": "log", "category": "proof", "label": "Flip: controller debug log", "proofTarget": "AC-1" } ], "intent": "Keep the flip record and controller log in the run package", "next": "ac1-flip-order-type" }, "ac1-flip-order-type": { "action": "assert_json", "assert": { "all": [ { "path": "$.runTag", "operator": "eq", "value": "{{params.run_tag}}" }, { "path": "$.phase", "operator": "eq", "value": "flip" }, { "path": "$.market", "operator": "eq", "value": "{{params.market}}" }, { "path": "$.error", "operator": "falsy" }, { "path": "$.positionBefore.side", "operator": "eq", "value": "long" }, { "path": "$.result.success", "operator": "eq", "value": true }, { "path": "$.closeEvents", "operator": "length_eq", "value": 0 }, { "path": "$.tradeStatuses", "operator": "deep_eq", "value": [ "submitted", "executed" ] }, { "path": "$.tradeOrderTypes", "operator": "deep_eq", "value": [ "market", "market" ] }, { "path": "$.positionAfter.side", "operator": "eq", "value": "short" } ] }, "intent": "A flip reports its trade events with order_type market and emits no close event without it", "proves": [ "AC-1" ], "next": "ac1-close-all-drive" }, "ac1-close-all-drive": { "action": "command", "timeout_ms": 180000, "intent": "Close all through the controller public API, recording its analytics events", "next": "ac1-close-all-evidence" }, "ac1-close-all-evidence": { "action": "index_artifacts", "artifacts": [ { "type": "json", "category": "proof", "label": "Close all: controller result, positions and captured analytics events", "proofTarget": "AC-1" }, { "type": "log", "category": "proof", "label": "Close all: controller debug log", "proofTarget": "AC-1" } ], "intent": "Keep the close all record and controller log in the run package", "next": "ac1-close-all-order-type" }, "ac1-close-all-order-type": { "action": "assert_json", "assert": { "all": [ { "path": "$.runTag", "operator": "eq", "value": "{{params.run_tag}}" }, { "path": "$.phase", "operator": "eq", "value": "close-all" }, { "path": "$.market", "operator": "eq", "value": "{{params.market}}" }, { "path": "$.error", "operator": "falsy" }, { "path": "$.positionBefore.side", "operator": "eq", "value": "short" }, { "path": "$.request.symbols", "operator": "deep_eq", "value": [ "{{params.market}}" ] }, { "path": "$.result.success", "operator": "eq", "value": true }, { "path": "$.result.successCount", "operator": "eq", "value": 1 }, { "path": "$.closeBulkActionIds.0", "operator": "not_null" }, { "path": "$.closeStatuses", "operator": "deep_eq", "value": [ "executed" ] }, { "path": "$.closeOrderTypes", "operator": "deep_eq", "value": [ "market" ] }, { "path": "$.positionAfter", "operator": "falsy" } ] }, "intent": "The close-all summary event carries order_type market", "proves": [ "AC-1" ], "next": "done" }, "done": { "action": "end", "status": "pass" }, "teardown-clean-market": { "action": "call", "ref": "perps.clean-market-testnet", "params": { "account": "{{params.account}}", "market": "{{params.market}}", "exclusive_testnet_market": "{{params.exclusive_testnet_market}}" }, "intent": "Leave the task-owned testnet market flat with no open orders", "next": "teardown-done" }, "teardown-done": { "action": "end", "status": "pass" } } } }Validation Logs
Run: f6a420f0-7153-4fb4-99b2-6eeaf1e04b56
Runner: @metamask/recipe-runner
Runner revision: unknown
Recipe digest: sha256:fce1f8d2462f33723f63e80e2b71f39d6215b61dcbbd155c4424abb839b2b12f
Full output (31/31 passed, pass)
Checklist
Full evidence gallery and publication receipt
Publication approval
farm-automation · service · farm-automation · 2026-10-23T07:11:32.670Z
Package:
72087ec4d6325418ad8bbbd8f21c7b5969db49bac009499fa16a4e6877f36a57. Visual receipts: none.Note
Low Risk
Analytics-only payload change in TradingService.closePositions with no API or trading behavior changes.
Overview
Fixes Perp Position Close Transaction analytics for close-all / batch closes by setting
order_type: 'market'on the summary eventTradingService.closePositionsemits from itsfinallyblock.On Hyperliquid’s batch path that summary was often the only close event, so Mixpanel showed an unset
order_typeeven though batch closes always submit market orders (provider batch or per-position fallback). The property is included on success and failure. Unit tests cover provider success, thrown errors, and the fallback path.Reviewed by Cursor Bugbot for commit 4ab4f09. Bugbot is set up for automated code reviews on this repo. Configure here.