terminal-fi / swappa

Framework for executing cross-DEX token swaps
MIT License
20 stars 13 forks source link

Proposal: Some way to snapshot & restore pair states #10

Closed diwu1989 closed 1 year ago

diwu1989 commented 2 years ago

Right now, pairs are constructed by looking up the on-chain registry.

If a web UI is to be built on top of swappa, we need server-side acceleration. The way it could work:

  1. a backend server looks up all of the pairs and subscribes to new block to constantly refresh all pairs
  2. state information for ALL available pairs are snapshotted per refresh and stored in Redis
  3. web UI asks the server for the pair snapshots, and instantiates all of the pairs locally without querying for on-chain
  4. websocket subscription can have the server publish per-block refreshed pairs to the client as well

This will reduce the load on Forno significantly and bypass the all pair discovery lag on the web clients.

My proposed API change to Pair interface to support streaming in a snapshot state:

type Snapshot any
public snapshot(): Snapshot
public restore(snapshot: Snapshot): void

Probably also need something in the registry to snapshot the list of all discovered pair & addresses (the arguments we need to initialize the pair). After the pairs are instantiated locally, the state can be restored.

The snapshot interface is something I need as well for arbbing because i snapshot & execute & restore states to pairs continuously.

diwu1989 commented 2 years ago

Pair state snapshot is implemented https://github.com/terminal-fi/swappa/pull/11

Next step is figure out how to cleanly snapshot / restore registries and pairs.

diwu1989 commented 2 years ago

Now that pairs can be snapshotted, then we can make it possible to take a registry, and snapshot the information needed to re-initialize each pair, & then restoring the snapshot state into these re-initialized pairs.

For each pair, there's the static state that is discovered from the registry and this information does not change per-block. There's also the dynamic per-block pair state, that changes with each refresh()

Registry snapshot should only contain the static initialization data for all discovered pairs of that registry.

diwu1989 commented 2 years ago

I need to complete this snapshotting feature now that there's helper smart contracts to accelerate the registry initialization and pair refresh process.

The idea is that the registry helper contracts need to provide enough information to generate a pair Snapshot and then per block, we just restore the state in bulk from the helper contracts on-chain.