HomePhabricator

[Cashtab] Implement websocket in OrderBook

Description

[Cashtab] Implement websocket in OrderBook

Summary:
On busier OrderBooks, it's just like an exchange. Users are buying and selling.

Right now Cashtab only loads the state of the OrderBook once. If another user creates a new listing or buys an existing listing, the OrderBook does not refresh for users who already have it open. Then if they try to accept an offer, its utxo may no longer exist and they get an error.

Solution is to use websockets so that the OrderBook updates itself according to agora activity as it happens. It is possible to optimize this going forward for speed. For a first pass, we only refresh the OrderBook every time we see an agora action related to its token added to the mempool.

Test Plan:
npm test to confirm we did not break anything

test site: https://cashtab-local-dev.netlify.app

Added debug log on the test site so you can see the msgs that come in. List a token. Observe that the OrderBook updates itself and the wsMsg is in the dev console.

This is quite a complicated piece of logic to test in react testing library. We could accomplish it with sinon and spies. However the tested behavior (for now) is very simple. If it gets more complex, perhaps OrderBook should be exported and tested by ecash-agora; may need regtest integration to get a "good" websocket test.

Here is what I did

  1. Navigate to test site
  2. Hard refresh to make sure you are on this version (ctrl + shift + r in brave/chrome)
  3. Go to a token page
  4. Buy or list that token, notice in the dev console that the offers are refreshed. Confirm the offers are refreshed by checking the offer your action changed.
  5. Manually enter another tokenId in the nav bar. Check the dev console to confirm we are closing the ws for this tokenId and loading one for a new tokenId. Also getting offers for the new tokenId.
  6. Navigate to the Agora screen. In the dev console, note that the whitelisted OrderBooks get a websocket.
  7. Buy some XECX or Star Crystal. In the dev console see the orderbooks refresh. Confirm the offer you bought has been updated.
  8. Toggle to Manage Offers. Confirm websockets are presesnt.
  9. Load all offers. In the dev console, see that all offers are loaded with websocket disabled.

It's quite a test plan and a bit manual. But testing websocket actions without actually firing transactions quickly becomes an exercise in testing the testing libraries. imo the solution is to not make the websocket more complicated in Cashtab. If we need more optimized orderbook websockets (updating indivdual offers as they are partially accepted, removing individual offers as they are canceled) -- then this component should get its own tested library. That way we could use regtest on it with ABC monorepo CI, like we do with ecash-lib and ecash-agora.

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Subscribers: Fabien

Differential Revision: https://reviews.bitcoinabc.org/D17519

Details

Provenance
bytesofmanAuthored on Jan 12 2025, 18:28
bytesofmanPushed on Jan 13 2025, 21:04
Reviewer
Restricted Project
Differential Revision
D17519: [Cashtab] Implement websocket in OrderBook
Parents
rABCd1d73830cfbf: [chronik] implement blockchain.transaction.get_merkle Electrum method
Branches
Unknown
Tags
Unknown