Skip to content

Commit

Permalink
feat(store-sync): add util to fetch snapshot from indexer with SQL API (
Browse files Browse the repository at this point in the history
#2996)

Co-authored-by: Andy Cernera <[email protected]>
Co-authored-by: Ori Pomerantz <[email protected]>
  • Loading branch information
3 people authored Sep 18, 2024
1 parent 83fc93c commit 8b4110e
Show file tree
Hide file tree
Showing 29 changed files with 1,662 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changeset/soft-boats-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@latticexyz/protocol-parser": patch
"@latticexyz/store-sync": patch
---

Added `store-sync` helper libraries to interact with the indexer's experimental SQL API endpoint. Documentation is available at [https://mud.dev/indexer/sql](https://mud.dev/indexer/sql).
13 changes: 13 additions & 0 deletions docs/components/common-text/FilterTypes.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Callout } from "nextra/components";

<Callout type="info">
MUD initial data hydration, and therefore filtering, comes in two flavors: [SQL](/indexer/sql) and [generic](/guides/hello-world/filter-sync).
Note that this is for the initial hydration, currently limits on on-going synchronization are limited to [the generic method](/guides/hello-world/filter-sync).

| | SQL | Generic |
| -------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Filtering | Can filter on most SQL functions | Can only filter on tables and the first two key fields (limited by [`eth_getLogs`](https://ethereum.github.io/execution-apis/api-documentation/) filters) |
| Availability | [Redstone](https://redstone.xyz/docs/network-info), [Garnet](https://garnetchain.com/docs/network-info), or elsewhere if you run your own instance | Any EVM chain |
| Security assumptions | The indexer instance returns accurate information | The endpoint returns accurate information (same assumption as any other blockchain app) |

</Callout>
15 changes: 15 additions & 0 deletions docs/pages/guides/hello-world/filter-sync.mdx
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import { CollapseCode } from "../../../components/CollapseCode";
import FilterTypes from "../../../components/common-text/FilterTypes.mdx";

# Filter data synchronization

In this tutorial you modify `networkSetup.ts` to filter the information you synchronize.
Filtering information this way allows you to reduce the use of network resources and makes loading times faster.

<FilterTypes />

<details>

<summary>Why are only the first two key fields available for filtering?</summary>

Ethereum log entries can have [up to four indexed fields](https://www.evm.codes/?fork=cancun#a4).
However, Solidity only supports [three indexed fields](https://www.alchemy.com/overviews/solidity-events) because the first indexed field is used for the event name and type.
In MUD, [this field](https://github.com/latticexyz/mud/blob/main/packages/store/src/IStoreEvents.sol) specifies whether [a new record is created](https://github.com/latticexyz/mud/blob/main/packages/store/src/IStoreEvents.sol#L26-L32), a record is changed (either [static fields](https://github.com/latticexyz/mud/blob/main/packages/store/src/IStoreEvents.sol#L43) or [dynamic fields](https://github.com/latticexyz/mud/blob/main/packages/store/src/IStoreEvents.sol#L56-L64)), or [a record is deleted](https://github.com/latticexyz/mud/blob/main/packages/store/src/IStoreEvents.sol#L71).
The second indexed fields is always the table's [resource ID](/world/resource-ids).
This leaves two fields for key fields.

</details>

## Setup

To see the effects of filtering we need a table with entries to filter. To get such a table:
Expand Down
3 changes: 2 additions & 1 deletion docs/pages/indexer/_meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export default {
"using": "Using the Indexer",
"sqlite": "SQLite Indexer",
"postgres-event-only": "PostgreSQL for events",
"postgres-decoded": "PostgreSQL for data (and events)",
"postgres-decoded": "PostgreSQL for data (and events)",
sql: "SQL API (Experimental)",
};
Loading

0 comments on commit 8b4110e

Please sign in to comment.