web icon indicating copy to clipboard operation
web copied to clipboard

State corruption in swapper when multiple trades are in flight

Open woodenfurniture opened this issue 1 year ago • 3 comments

Overview

The swapper is getting state updates from other trades when there is more than 1 trade currently in flight, resulting in corrupted state and incorrect states in the UI, as well as incorrect tx links.

References and additional details

Performing RUNE -> BTC trade immediately followed by BTC -> RUNE trade results in the execution polling both txs and shoving state from both into the same UI - note state flip-flopping between inbound and outbound and incorrect tx links:

https://github.com/shapeshift/web/assets/125113430/c04e9075-69c9-4827-83db-ca3096997da0

On completion the status corrects itself and the bogus tx link is removed: image

the correct tx link: https://viewblock.io/thorchain/tx/6c99c929485f1fc19494319fabccdc89eee841f0c8c682a0bf6f04a5395352fb the bogus one: https://viewblock.io/thorchain/tx/73D82DFCBB714CF911C321D51582D7517368E26EC80D5117184D2A158346B73C

See src/components/MultiHopTrade/components/MultiHopTradeConfirm/hooks/useTradeExecution.tsx - polling is supposed to be canceled on unmont, so that is either broken or the component isn't actually unmounted when going back to tradeinput page: image

Acceptance Criteria

A given trade should only receive status updates for itself. Other trade polling should be unmounted or cancelled.

Need By Date

No response

Screenshots/Mockups

No response

Estimated effort

No response

woodenfurniture avatar Jun 18 '24 00:06 woodenfurniture

I did try to reproduce but it's super hard and cost really much, I'm not even sure that this is a cancelPolling issue as both are called and defined, it might be linked to something else

Also I couldn't reproduce using an EVM tx as well as a thorchain Native to Native swap at the same time, might be only scoped to thorchain swaps

We might go further by using something like AVAX => BCH as it would cost less

NeOMakinG avatar Jun 27 '24 14:06 NeOMakinG

Note for grooming: we may want to use this as an opportunity to discuss handling pending Txs in the app, e.g initiating a THOR Tx but leaving the swapper, or quitting the app/leaving the swapper mid multi-hop Txs - somehow related to https://github.com/shapeshift/web/issues/7255

gomesalexandre avatar Jun 27 '24 17:06 gomesalexandre

Note for assignee taking this task: TRade execution state is stored flat in redux, i.e is not stored by trade ID meaning state from previous trades will overwrite the current one.

The solution will be to:

  1. Store trade execution state in redux by trade ID to eliminate the possibility of other trades interfering with the current state a. This also opens up the possibility to "resume" a multi-hop trade to sign the second tx after closing the page
  2. Be sure to unmount any components/polling likely to persist state updates after leaving that page

woodenfurniture avatar Jul 08 '24 22:07 woodenfurniture