-
Notifications
You must be signed in to change notification settings - Fork 24
fix: prevent swap autoclaim race between service worker and UI #558
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -51,16 +51,25 @@ export default function AppBoltzSwap() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const [processing, setProcessing] = useState<boolean>(false) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const [opDone, setOpDone] = useState(false) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const [success, setSuccess] = useState<boolean>(false) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const [swapManagerProcessing, setSwapManagerProcessing] = useState(false) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Subscribe to real-time updates for this swap | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!swapManager || !swapInfo) return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let unsub: (() => void) | null = null | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let cancelled = false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| swapManager.isProcessing(swapInfo.id).then((p) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!cancelled) setSwapManagerProcessing(p) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| swapManager | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .subscribeToSwapUpdates(swapInfo.id, (updatedSwap) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setSwapInfo(updatedSwap) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| swapManager.isProcessing(updatedSwap.id).then((p) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!cancelled) setSwapManagerProcessing(p) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+63
to
+72
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: # First, let's check the actual file and its imports
head -50 src/screens/Apps/Boltz/Swap.tsxRepository: arkade-os/wallet Length of output: 2284 🏁 Script executed: # Read the specific lines mentioned in the review (63-72) and surrounding context
sed -n '50,85p' src/screens/Apps/Boltz/Swap.tsxRepository: arkade-os/wallet Length of output: 1106 🏁 Script executed: # Check all imports in the file to verify consoleError is available
grep -n "^import" src/screens/Apps/Boltz/Swap.tsx | head -20Repository: arkade-os/wallet Length of output: 1187 🏁 Script executed: # Search for other similar unhandled promise patterns in this file
rg -n "\.then\(" src/screens/Apps/Boltz/Swap.tsx | head -20Repository: arkade-os/wallet Length of output: 218 Handle failed processing-state checks with proper error containment. Both 🛡️ Proposed fix- swapManager.isProcessing(swapInfo.id).then((p) => {
- if (!cancelled) setSwapManagerProcessing(p)
- })
+ const refreshSwapManagerProcessing = (id: string) => {
+ swapManager
+ .isProcessing(id)
+ .then((p) => {
+ if (!cancelled) setSwapManagerProcessing(p)
+ })
+ .catch((error) => {
+ if (!cancelled) {
+ consoleError(error, `Error checking swap processing state ${id}`)
+ setSwapManagerProcessing(false)
+ }
+ })
+ }
+
+ refreshSwapManagerProcessing(swapInfo.id)
swapManager
.subscribeToSwapUpdates(swapInfo.id, (updatedSwap) => {
setSwapInfo(updatedSwap)
- swapManager.isProcessing(updatedSwap.id).then((p) => {
- if (!cancelled) setSwapManagerProcessing(p)
- })
+ refreshSwapManagerProcessing(updatedSwap.id)
})📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .then((unsubscribe) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (cancelled) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -147,11 +156,15 @@ export default function AppBoltzSwap() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const isRefundable = isSubmarineSwapRefundable(swapInfo) || isChainSwapRefundable(swapInfo) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const isClaimable = isReverseSwapClaimable(swapInfo) || isChainSwapClaimable(swapInfo) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const buttonLabel = isClaimable ? 'Complete swap' : 'Refund swap' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const buttonLabel = swapManagerProcessing ? 'Claiming...' : isClaimable ? 'Complete swap' : 'Refund swap' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const refunded = swapInfo.status === 'transaction.refunded' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const buttonHandler = async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (swapManager) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const alreadyProcessing = await swapManager.isProcessing(swapInfo.id) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (alreadyProcessing) return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+159
to
+167
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Scope the guard to claims and sync stale UI on early return. The current 🐛 Proposed fix- const buttonLabel = swapManagerProcessing ? 'Claiming...' : isClaimable ? 'Complete swap' : 'Refund swap'
+ const claimInProgress = swapManagerProcessing && !isRefundable
+ const buttonLabel = claimInProgress ? 'Claiming...' : isClaimable ? 'Complete swap' : 'Refund swap'
const refunded = swapInfo.status === 'transaction.refunded'
const buttonHandler = async () => {
try {
- if (swapManager) {
+ if (isClaimable && swapManager) {
const alreadyProcessing = await swapManager.isProcessing(swapInfo.id)
- if (alreadyProcessing) return
+ if (alreadyProcessing) {
+ setSwapManagerProcessing(true)
+ return
+ }
}- {!success && (isRefundable || isClaimable || swapManagerProcessing) ? (
+ {!success && (isRefundable || isClaimable || claimInProgress) ? (
<ButtonsOnBottom>
- <Button onClick={buttonHandler} label={buttonLabel} disabled={processing || swapManagerProcessing} />
+ <Button onClick={buttonHandler} label={buttonLabel} disabled={processing || claimInProgress} />
</ButtonsOnBottom>
) : null}Also applies to: 233-235 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setProcessing(true) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (isReverseSwapClaimable(swapInfo)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await claimVHTLC(swapInfo) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -217,9 +230,9 @@ export default function AppBoltzSwap() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| )} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Padded> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Content> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {!success && (isRefundable || isClaimable) ? ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {!success && (isRefundable || isClaimable || swapManagerProcessing) ? ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <ButtonsOnBottom> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Button onClick={buttonHandler} label={buttonLabel} disabled={processing} /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Button onClick={buttonHandler} label={buttonLabel} disabled={processing || swapManagerProcessing} /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </ButtonsOnBottom> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) : null} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.