Skip to content

fix(perps-controller): handle revoked HyperLiquid agents and wallet-chain signing - #10643

Merged
abretonc7s merged 7 commits into
mainfrom
fix/perps-agent-rejection-signature-chain
Oct 2, 2026
Merged

abretonc7s merged 7 commits into
mainfrom
fix/perps-agent-rejection-signature-chain

Conversation

@abretonc7s

@abretonc7s abretonc7s commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Explanation

Running 19.0.0 with an extension wallet (EIP-1193 accountSigner plus a HyperLiquid agent) showed three problems:

  • A revoked agent wasn't detected. After a revoke, HyperLiquid can answer Must deposit before performing actions, not only User or API Wallet … does not exist. That answer now counts as a rejection only if extraAgents no longer lists the agent that signed that request. The answer is attributed by the request's own signature, inside a wallet binding per network and session. When it counts, the write returns KEYRING_LOCKED, the agent is dropped and onAgentRejected fires. Main-account signatures are never attributed to an agent.
  • User-signed actions were signed for chain 1. PerpsAccountSigner gets an optional getChainId(), passed to the SDK as signatureChainId. Builder fee approvals, withdrawals and transfers are then signed on the wallet's chain. Without it nothing changes, so Mobile and Extension are unaffected.
  • A refused builder fee approval was requested again. A permanent refusal (e.g. the builder's balance is too low) is now cached until disconnect, so there's no repeat prompt, and prepareTradingWallet returns the venue's message.

Also: getExchangeClient() initializes the provider's clients before returning one.

Validation

  • Unit: new tests for each case, including network switches, reconnects and queued writes. 31 of them fail on main with the reported symptoms. Mutation checks on the attribution guards each fail a test. The package suite passes with coverage on Node 22 and 24.
  • HyperLiquid testnet, through the core harness: 14 approve → trade → revoke → order cycles. This branch returns KEYRING_LOCKED and calls onAgentRejected. main returns the raw venue error. User-signed actions go out with signatureChainId 0x66eee (421614).

Known issues / follow-ups

  • Whether extraAgents lists an unnamed agent is undocumented. The onAgentRejected docs ask hosts to approve named agents.
  • validUntil is compared against the device clock.
  • If re-init fails inside toggleTestnet(), the client service keeps the previous network's wallet. This is harmless today; the fix is a follow-up.
  • Market data without a selected account needs a lazy exchange client. That's a separate change.

References

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
  • I've communicated my changes to consumers by updating changelogs for packages I've changed
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

Note

Medium Risk
Changes HyperLiquid signing, agent eviction, and trading-wallet setup paths; mistakes could mis-classify venue errors or drop agents incorrectly, but behavior is heavily covered by new tests.

Overview
Fixes HyperLiquid trading setup and signing when hosts use an accountSigner plus an agent (typical extension wallets).

Revoked / expired agents: The provider now ties each L1 signature to the network and session wallet that created it, and links venue answers back to that signature via onExchangeRequest. A "Must deposit before performing actions" response is treated as agent rejection only when extraAgents no longer lists (or has expired) the agent that signed that request—not when the account is simply unfunded. Unknown-wallet errors are attributed to the request’s network, so a shared agent on mainnet and testnet is dropped only where the venue rejected it. Queued writes that sign after a network switch use the original wallet’s agent resolver.

User-signed actions: Optional PerpsAccountSigner.getChainId flows through the wallet adapter as SDK signatureChainId, so builder fee, withdraw, and transfer payloads can be signed on the connected chain instead of chain 1.

Builder fee: Permanent venue refusals (e.g. insufficient builder balance) are cached until disconnect—no repeat approval prompts; prepareTradingWallet can return the venue message without logging it as a generic setup failure.

Other: getExchangeClient() ensures the provider’s SDK clients (and agent wallet) are initialized first; subscription-created wallets use the same agent binding as the provider.

Reviewed by Cursor Bugbot for commit 88f7383. Bugbot is set up for automated code reviews on this repo. Configure here.

…gn user actions for the signer's chain, stop re-prompting a refused builder fee

- An agent-signed write that the venue answers with "Must deposit before
  performing actions" is a rejection when extraAgents no longer lists the
  agent (or lists it as expired): the agent is dropped, onAgentRejected is
  called and the write fails with KEYRING_LOCKED. Otherwise the venue error
  is returned as before.
- Add optional PerpsAccountSigner.getChainId, passed to the SDK as the
  signatureChainId of user-signed actions. Without it nothing changes.
- A builder fee approval the venue refuses for a reason signing again cannot
  fix is not requested again until disconnect.
@abretonc7s
abretonc7s marked this pull request as ready for review October 1, 2026 05:42
@abretonc7s
abretonc7s requested review from a team as code owners October 1, 2026 05:42
@abretonc7s
abretonc7s deployed to default-branch October 1, 2026 05:42 — with GitHub Actions Active
… that signed the request

The rejection check used the latest agent signature for the main account
on the current network, so a main-signed builder fee approval, an answer
that arrived after a network switch, or a request signed by an agent the
host had just replaced could evict the wrong agent.

- The client service reports each exchange request with the venue's
  answer, from inside the SDK request; the wallet adapter reports each
  agent signature; the provider matches them by the signature's r value.
- Main-signed requests are never attributed to an agent; requests signed
  on another network or before a disconnect are not checked.
- A builder fee refusal settling after a disconnect is not recorded.
- Document that the "Must deposit" check relies on named agents and the
  device clock, and the prepareTradingWallet result for a refused builder
  fee.
…before signing

The network and lifecycle generation of an agent signature were read once
the signature completed, so a mainnet signature that finished after a
network switch or a reconnect was checked as a current request.

- Read the network, lifecycle generation and main account when the agent
  is asked for, and record each signature the resolved agent makes with
  them; the wallet adapter no longer reports signatures.
- Do not track a signature the SDK would refuse before sending it, and
  drop unanswered signatures on disconnect.
…them

The SDK queues a write before it asks the wallet to sign, so a mainnet
write queued behind another one could sign after toggleTestnet(): it then
resolved the testnet agent, recorded the new network and session, and its
mainnet rejection was checked against testnet agents.

- Each wallet adapter the provider creates is bound to the network and
  lifecycle generation of its exchange client; it resolves agents on that
  network and records its agent signatures in the binding.
- The client service reports the wallet with each exchange request, so the
  venue's answer is matched in that wallet's binding.
- createWalletAdapter takes the resolver for that adapter.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 0c63793. Configure here.

Comment thread packages/perps-controller/src/providers/HyperLiquidProvider.ts
…t and bind every wallet

- A "User or API Wallet ... does not exist" answer is attributed to the
  agent signature linked to the request it answers, on that wallet's
  network, so an agent used on both networks is dropped only where the
  venue rejected it. The agent's last account is used only when no
  signature is linked.
- The subscription service creates its wallets through the provider, so
  they are bound and resolve agents like the provider's.
- HyperLiquidProvider.getExchangeClient initializes the provider's clients
  before handing out the exchange client.
@abretonc7s abretonc7s changed the title fix(perps-controller): HyperLiquid agent rejection, user-signed chain and refused builder fee fix(perps-controller): detect revoked HyperLiquid agents, sign on the wallet chain, stop re-asking refused builder fees Oct 2, 2026
@abretonc7s abretonc7s changed the title fix(perps-controller): detect revoked HyperLiquid agents, sign on the wallet chain, stop re-asking refused builder fees fix(perps-controller): handle revoked HyperLiquid agents and wallet-chain signing Oct 2, 2026
geositta
geositta previously approved these changes Oct 2, 2026
…tion-signature-chain

# Conflicts:
#	packages/perps-controller/CHANGELOG.md
@abretonc7s
abretonc7s added this pull request to the merge queue Oct 2, 2026
Merged via the queue into main with commit d716c96 Oct 2, 2026
45 checks passed
@abretonc7s
abretonc7s deleted the fix/perps-agent-rejection-signature-chain branch October 2, 2026 13:20
@abretonc7s abretonc7s mentioned this pull request Oct 6, 2026
4 tasks done
Naz-Ovh pushed a commit to 0x-fork/metamask-core that referenced this pull request Oct 7, 2026
## Explanation

Releases `@metamask/perps-controller` **19.0.0 → 20.0.0**. The monorepo
version goes **1314.0.0 → 1315.0.0**.

No other package is being published. Perps-controller has no in-monorepo
dependents that need a workspace range bump.

The bump is **major**. Public unions and Lighter order validation
change:

- **BREAKING:** Lighter `placeOrder` / `validateOrder` accept supported
native attached TP/SL instead of refusing all attachments. Gate
forwarding on `attachedTpsl`.
([MetaMask#10638](MetaMask#10638))
- **BREAKING:** Grouped Lighter signer calls require grouping/count
`1/2` or `2/2` with two orders, or `3/3` with three orders.
([MetaMask#10638](MetaMask#10638))
- **BREAKING:** `ScaleOrderChild.state` adds `canceled`.
([MetaMask#10638](MetaMask#10638))
- **BREAKING:** `DirectProviderOrderCapabilitiesUnavailableReason` and
`OrderCapabilitiesUnavailableReason` add `order_market_unsupported`.
([MetaMask#10638](MetaMask#10638))
- **BREAKING:** Stray `triggerPrice` on Lighter basic market/limit
orders is refused with `ORDER_TRIGGER_PRICE_NOT_SUPPORTED`.
([MetaMask#10638](MetaMask#10638))
- **BREAKING:** Lighter position TP/SL replacement and removal preserve
independent partial triggers.
([MetaMask#10638](MetaMask#10638))
- **BREAKING:** `OrderFill.pnl` is optional when the venue omits
realized PnL. Treat missing as unknown, not zero.
([MetaMask#10605](MetaMask#10605))

Also ships Lighter Scale/Chase/TWAP probe work, fee quote attribution
(`feeSource`, `metamaskFeeDiscountBips`), HyperLiquid agent/signing
fixes, quieter optional-messenger failures, and watchlist
hydration/write races.

## Changelog

Moved Unreleased entries in `packages/perps-controller/CHANGELOG.md`
under `[20.0.0]`. Removed Uncategorized monorepo release markers
(1311–1314) that do not affect package consumers. Dropped a duplicate
Chase probe summary already covered by more specific entries.

## References

- Source PRs: [MetaMask#10638](MetaMask#10638),
[MetaMask#10618](MetaMask#10618),
[MetaMask#10605](MetaMask#10605),
[MetaMask#10643](MetaMask#10643),
[MetaMask#10650](MetaMask#10650),
[MetaMask#10651](MetaMask#10651),
[MetaMask#10665](MetaMask#10665),
[MetaMask#10670](MetaMask#10670),
[MetaMask#10683](MetaMask#10683)
- No in-monorepo consumer packages to bump. Mobile and Extension
exhaustive union matches need the new members on upgrade. Lighter
attached TP/SL stays gated on `attachedTpsl` and client rollout.

## Checklist

- [x] I've updated the test suite for new or updated code as appropriate
- [x] 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)
- [x] 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

Made with [Cursor](https://cursor.com)

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
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.

3 participants