From 77966730684a86b5bd0cf65d86bcd82a4f92c4bb Mon Sep 17 00:00:00 2001 From: Codex Date: Wed, 21 Oct 2026 18:40:02 +0000 Subject: [PATCH 1/2] Fix mainnet local signer support and Earn withdrawal handling --- .../components/wallet/LocalSignerButton.tsx | 4 +- .../src/components/wallet/LocalSignerForm.tsx | 8 ++- package.json | 7 ++- .../@epoch-protocol__epoch-intents-sdk.patch | 53 +++++++++++++++++++ pnpm-lock.yaml | 19 ++++--- .../earn/EarnWithdrawDetailView.tsx | 2 +- src/earn/use-earn-engine.ts | 11 ++++ src/earn/use-earn-intent-flow.ts | 31 +++++++++-- 8 files changed, 119 insertions(+), 16 deletions(-) create mode 100644 patches/@epoch-protocol__epoch-intents-sdk.patch diff --git a/demo/src/components/wallet/LocalSignerButton.tsx b/demo/src/components/wallet/LocalSignerButton.tsx index 22bd962..7ede3dc 100644 --- a/demo/src/components/wallet/LocalSignerButton.tsx +++ b/demo/src/components/wallet/LocalSignerButton.tsx @@ -37,8 +37,6 @@ export function LocalSignerButton({ network, className }: Props) { ) : ( diff --git a/demo/src/components/wallet/LocalSignerForm.tsx b/demo/src/components/wallet/LocalSignerForm.tsx index a6549c0..691cd7d 100644 --- a/demo/src/components/wallet/LocalSignerForm.tsx +++ b/demo/src/components/wallet/LocalSignerForm.tsx @@ -1,5 +1,5 @@ import { useId, useRef, useState, type FormEvent } from 'react'; -import { useChainId, useChains, useConnect } from 'wagmi'; +import { useAccount, useChainId, useChains, useConnect, useDisconnect } from 'wagmi'; import type { LocalSignerConnector } from '../../wallet/local-signer'; interface Props { @@ -18,6 +18,8 @@ export function LocalSignerForm({ connector, network, onConnected, onCancel }: P const [selectedChainId, setSelectedChainId] = useState(currentChainId); const chainId = chains.find((chain) => chain.id === selectedChainId)?.id ?? chains[0]?.id; const { connectAsync, isPending } = useConnect(); + const { connector: activeConnector, isConnected } = useAccount(); + const { disconnectAsync } = useDisconnect(); const [error, setError] = useState(); async function submit(event: FormEvent) { @@ -35,6 +37,9 @@ export function LocalSignerForm({ connector, network, onConnected, onCancel }: P } try { // The key never enters Wagmi/React Query mutation variables or storage. + if (isConnected && activeConnector) { + await disconnectAsync({ connector: activeConnector }); + } await connectAsync({ connector, chainId }); onConnected(); } catch { @@ -48,6 +53,7 @@ export function LocalSignerForm({ connector, network, onConnected, onCancel }: P

Use a development account. The key stays in this page’s memory until disconnect or reload. Signing happens locally without wallet confirmation popups. + {isConnected && ' Connecting will disconnect the current EVM wallet.'}

{ const usd = position.underlyingUsdValue; diff --git a/src/earn/use-earn-engine.ts b/src/earn/use-earn-engine.ts index 55bc368..cfd355d 100644 --- a/src/earn/use-earn-engine.ts +++ b/src/earn/use-earn-engine.ts @@ -499,6 +499,16 @@ export function useEarnEngine(props: EarnIntentWidgetProps) { (!earnMiden?.connected || !miden.quoteSource) ) return; + // A withdraw is sourced from the selected position's chain. Do not quote + // while the connected EVM wallet is still on another chain: the SDK would + // otherwise use the wallet chain as the intent origin and can construct a + // malformed route (for example, an Optimism token looked up on Base). + if ( + fundingSource === "evm" && + effectiveSourceChainId !== null && + chainId !== effectiveSourceChainId + ) + return; // Destination not yet moved off the position's own chain/token → no route // to quote. Skip until the user picks a real destination. if (isSmartWithdrawDegenerate) return; @@ -526,6 +536,7 @@ export function useEarnEngine(props: EarnIntentWidgetProps) { activeMarket, effectiveSourceChainId, effectiveSourceToken?.address, + chainId, address, earnTab, smartWithdraw, diff --git a/src/earn/use-earn-intent-flow.ts b/src/earn/use-earn-intent-flow.ts index 8382339..3cbc3ec 100644 --- a/src/earn/use-earn-intent-flow.ts +++ b/src/earn/use-earn-intent-flow.ts @@ -553,7 +553,14 @@ export function useEarnIntentFlow({ pendingQuoteRef.current = null; try { - const originChainId = params.isMidenDeposit ? input.sourceChainId : undefined; + // Withdrawals originate on the position's chain, not necessarily on + // the wallet's currently selected chain. Keeping this explicit avoids + // asking SIO/dex solvers to resolve a position token on the wrong EVM + // chain while the UI is switching networks. + const originChainId = + params.isMidenDeposit || input.tab === 'withdraw' + ? input.sourceChainId + : undefined; const sdk = createEarnIntentSdk(apiBaseUrl, walletClient, originChainId); const { taskTypeString, intentData } = await sdk.getTaskData({ @@ -625,6 +632,22 @@ export function useEarnIntentFlow({ try { const statusList = await sdk.getIntentStatus(address, nonceStr); if (!mountedRef.current) return; + const failed = Array.isArray(statusList) && statusList.some( + (s: { status?: string }) => s.status?.toLowerCase() === 'failed', + ); + if (failed) { + if (pollingRef.current) { + clearInterval(pollingRef.current); + pollingRef.current = null; + } + const failure = new Error('Intent execution failed on-chain. No deposit was completed.'); + setStatus('error'); + setError(failure.message); + setStatusProgress(0); + setActiveStep(0); + onErrorRef.current?.({ sessionId, error: failure }); + return; + } const isComplete = Array.isArray(statusList) ? statusList.some( (s: { status?: string }) => @@ -740,7 +763,7 @@ export function useEarnIntentFlow({ setStatus('submitting'); setActiveStep(1); setStatusProgress(10); - setStepLabel('Quoting withdrawal…'); + setStepLabel('Preparing withdrawal and bridge quote…'); onStartRef.current?.({ sessionId, mode: 'earn' }); swLog('submit:begin — executeActions(Withdraw)', { amount: input.amount, @@ -873,7 +896,9 @@ export function useEarnIntentFlow({ const submitParams = buildParams(input); const originChainId = pendingQuoteRef.current?.originChainId ?? - (submitParams.isMidenDeposit ? input.sourceChainId : undefined); + (submitParams.isMidenDeposit || input.tab === 'withdraw' + ? input.sourceChainId + : undefined); const sdk = createEarnIntentSdk(apiBaseUrl, walletClient, originChainId); let taskTypeString: string; From 6d3d60f40fb2db763685aa278546e426823f9050 Mon Sep 17 00:00:00 2001 From: Codex Date: Wed, 21 Oct 2026 18:50:57 +0000 Subject: [PATCH 2/2] Allow private Miden P2ID recipients in smart withdrawals --- demo/test/miden-withdraw-recipient.test.ts | 20 ++++++++++++++++++++ src/components/EarnIntentWidget.tsx | 8 ++------ src/earn/use-earn-miden.ts | 5 ++--- test/earn-miden-mainnet.test.tsx | 20 +++++++++++++++----- 4 files changed, 39 insertions(+), 14 deletions(-) create mode 100644 demo/test/miden-withdraw-recipient.test.ts diff --git a/demo/test/miden-withdraw-recipient.test.ts b/demo/test/miden-withdraw-recipient.test.ts new file mode 100644 index 0000000..db9203c --- /dev/null +++ b/demo/test/miden-withdraw-recipient.test.ts @@ -0,0 +1,20 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { AccountId, Address, NoteType, createP2IDNote } from '@miden-sdk/miden-sdk'; + +test('public P2ID delivery accepts the connected private Miden recipient', () => { + const recipient = AccountId.fromHex('0x0e9089055fb6f54170bbf6159da028'); + assert.equal(recipient.isPublic(), false); + const faucet = Address.fromBech32( + 'mm1arnfum9xuwwlrvgvrefejwvlavn3vk24_qr7qqq9wr6w', + ).accountId(); + // Construct a note locally; no client, proof, or network submission is used. + const note = createP2IDNote({ + from: '0x5d38246547d94e5165c9e0fafbc667', + to: recipient, + assets: [{ token: faucet, amount: 10000n }], + type: 'public', + }); + assert.equal(note.metadata().noteType(), NoteType.Public); + assert.match(note.id().toString(), /^0x[0-9a-f]{64}$/); +}); diff --git a/src/components/EarnIntentWidget.tsx b/src/components/EarnIntentWidget.tsx index 00ba718..1906d42 100644 --- a/src/components/EarnIntentWidget.tsx +++ b/src/components/EarnIntentWidget.tsx @@ -101,11 +101,7 @@ export function EarnIntentWidget(props: EarnIntentWidgetProps) { earnFlow.stepLabel ?? (smartWithdraw ? "Routing…" : "Withdrawing…") ); if (earnFlow.isQuoting) return "Fetching quote…"; - if ( - ctaState.action === "switch" || - ctaState.action === "retry" || - ctaState.action === "disabled" - ) + if (ctaState.action !== "submit") return ctaState.label; // Idle + ready. "Confirm" (not "Review") — the click executes: it re-quotes // both legs and opens the wallet, it does not open a separate review step. @@ -256,4 +252,4 @@ export function EarnIntentWidget(props: EarnIntentWidgetProps) { {modalView.content} ); -} \ No newline at end of file +} diff --git a/src/earn/use-earn-miden.ts b/src/earn/use-earn-miden.ts index 9723ed8..17a2506 100644 --- a/src/earn/use-earn-miden.ts +++ b/src/earn/use-earn-miden.ts @@ -198,14 +198,14 @@ export function useEarnMiden({ // The withdrawal still executes on the EVM position chain; only the swap-leg // output is bridged. The connected adapter account is the recipient — the same - // one used to fund Miden deposits. + // one used to fund Miden deposits. Public P2ID notes can target either public + // or private accounts; recipient storage mode does not limit note delivery. const smartDest = useMemo(() => { if ( earnTab !== 'withdraw' || !smartWithdraw || smartDestChainId !== MIDEN_VIRTUAL_CHAIN_ID || !earnMiden?.accountId || - earnMiden.accountIsPublic === false || !smartDestTokenAddress ) { return undefined; @@ -224,7 +224,6 @@ export function useEarnMiden({ smartDestChainId, smartDestTokenAddress, earnMiden?.accountId, - earnMiden?.accountIsPublic, graphTokens, ]); diff --git a/test/earn-miden-mainnet.test.tsx b/test/earn-miden-mainnet.test.tsx index 745369e..a70120a 100644 --- a/test/earn-miden-mainnet.test.tsx +++ b/test/earn-miden-mainnet.test.tsx @@ -98,12 +98,14 @@ test("Miden earn submits once source metadata is ready", () => { ); }); -function PrivateMidenDestinationProbe() { +function PrivateMidenDestinationProbe({ + accountId = "0x0e9089055fb6f54170bbf6159da028", +}: { accountId?: string | null }) { const miden = useEarnMiden({ earnMiden: { enabled: true, connected: true, - accountId: "0x0e9089055fb6f54170bbf6159da028", + accountId, accountIsPublic: false, assets: [], createP2IDNote: async () => ({ success: false }), @@ -118,12 +120,20 @@ function PrivateMidenDestinationProbe() { smartDestTokenAddress: DEFAULT_MIDEN_FAUCET.faucetId, }); - return createElement("output", null, String(miden.smartDestNotReady)); + return createElement("output", null, + `${miden.smartDestNotReady}:${miden.smartDest?.recipientAccount ?? ""}`); } -test("private Miden accounts remain blocked as withdrawal destinations", () => { +test("private Miden recipients can receive public P2ID withdrawal notes", () => { assert.equal( renderToStaticMarkup(createElement(PrivateMidenDestinationProbe)), - "true", + "false:0x0e9089055fb6f54170bbf6159da028", + ); +}); + +test("a missing Miden recipient still blocks withdrawal preparation", () => { + assert.equal( + renderToStaticMarkup(createElement(PrivateMidenDestinationProbe, { accountId: null })), + "true:", ); });