fix: handle wallet rejection and unsubscribe in signAndSendTx#515
Open
jim-counter wants to merge 4 commits intomainfrom
Open
fix: handle wallet rejection and unsubscribe in signAndSendTx#515jim-counter wants to merge 4 commits intomainfrom
jim-counter wants to merge 4 commits intomainfrom
Conversation
When using signAndSendTx with browser wallet extensions (SubWallet, Talisman, Polkadot.js), rejecting the signing request caused the returned promise to hang forever. This happened because: 1. tx.signAndSend() returns a Promise that rejects on wallet denial, but this rejection was never caught 2. The status callback only fires after submission — never on signing denial 3. No unsubscribe cleanup happened when the promise settled Fix: - Catch the outer promise rejection via .then(onFulfilled, onRejected) - Wrap signAndSend in try/catch for synchronous throws from some extensions - Store and call the unsubscribe function on settlement to clean up websocket listeners - Use settled guard to prevent double-resolution Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Remove duck-type check and double cast on the outerPromise. The return type of signAndSend with a callback is well-defined as Promise<() => void> in @polkadot/api@^15, so the defensive type gymnastics are unnecessary. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Bugbot correctly identified that if the status callback settles the promise before outerPromise.then() assigns unsub, cleanup never runs. Fix: .then() now checks if already settled and unsubs immediately (fast path). If not yet settled, it stores unsub for safeResolve/safeReject to call later (slow path). Both paths are now covered. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Tests cover: - Wallet rejection propagates to caller - Slow path: unsub assigned before settlement, cleaned up by tryUnsub - Fast path: callback settles first, unsub called when outer resolves - Synchronous throw from signAndSend - Settled guard prevents issues on isFinalized after isInBlock - Transaction failure (isDropped) rejects and unsubs Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Member
Author
|
bugbot run |
Member
Author
|
bugbot run |
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.
Summary
Fix
signAndSendTxto properly handle wallet signing rejection and clean up subscriptions.Problem
When using
signAndSendTxwith browser wallet extensions (SubWallet, Talisman, Polkadot.js), if the user rejects the signing request, the returned promise hangs forever. This is because:tx.signAndSend()returns aPromise<() => void>that rejects when the wallet denies signing, but this rejection was never caughtFix
.then(fn, safeReject)to the outer promise so wallet denials propagate to the callerisInBlockthenisFinalized).then()assignsunsub, unsubscribe immediately when the outer promise resolves; if.then()fires first, storeunsubfor the settlement handlersignAndSendin try/catch for wallet extensions that throw synchronouslyTests
Added 6 unit tests covering:
isFinalizedafterisInBlockis a safe no-opisDropped) rejects and unsubsNote
detectTxSuccesshas a pre-existing bug wherereturn trueinsideforEachreturns from the callback, not the function — sosuccessis alwaysfalse. This is unrelated to this PR and does not affect transaction outcomes (the tx still succeeds/fails correctly).Test plan
npx jest __test__/signAndSendTx.test.ts— all 6 tests passnpx lerna run build— all 17 packages build cleanly