-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(store-sync): add store sync package (#1075)
* feat(block-events-stream): add block events stream package * feat(store-sync): add store sync package * wip anvil test * Revert "wip anvil test" This reverts commit 1952a98. * accidentally left in a store refernence * Update packages/block-events-stream/src/createBlockEventsStream.ts Co-authored-by: alvarius <89248902+alvrs@users.noreply.github.com> * make streams closeable I don't love this design * clean up * add log back in * move comments * refactor with just streams * add README with example * renamed * rename again and take in a tuple as input * fix scope * add TODO * add tests for grouping logs * wip rxjs tests * move fetchLogs to async generator, add tests * add block range tests * get rid of old approach * add note about timers * use concatMap instead of exhaustMap * update readme * feat(schema-type): add isDynamicAbiType (#1096) * update with new block events stream * missed a spot * refine types, move logging to debug * add test * wip config types * refactor as a list of storage operations * getting closer * config is already expanded * fix types * clean up * remove log * pass through log to storage operation * update test snapshot * Create eighty-tigers-argue.md --------- Co-authored-by: alvarius <89248902+alvrs@users.noreply.github.com> Co-authored-by: alvrs <alvarius@lattice.xyz>
- Loading branch information
1 parent
0a230b4
commit 904fd7d
Showing
19 changed files
with
567 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"@latticexyz/block-logs-stream": patch | ||
"@latticexyz/protocol-parser": patch | ||
"@latticexyz/store-sync": minor | ||
"@latticexyz/store": patch | ||
--- | ||
|
||
Add store sync package |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export * from "./blockRangeToLogs"; | ||
export * from "./createBlockStream"; | ||
export * from "./fetchLogs"; | ||
export * from "./getLogs"; | ||
export * from "./groupLogsByBlockNumber"; | ||
export * from "./isNonPendingBlock"; | ||
export * from "./isNonPendingLog"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Hex } from "viem"; | ||
import { SchemaAbiType, SchemaAbiTypeToPrimitiveType, isDynamicAbiType } from "@latticexyz/schema-type"; | ||
import { decodeDynamicField } from "./decodeDynamicField"; | ||
import { decodeStaticField } from "./decodeStaticField"; | ||
|
||
export function decodeField< | ||
TAbiType extends SchemaAbiType, | ||
TPrimitiveType extends SchemaAbiTypeToPrimitiveType<TAbiType> | ||
>(abiType: TAbiType, data: Hex): TPrimitiveType { | ||
return ( | ||
isDynamicAbiType(abiType) ? decodeDynamicField(abiType, data) : decodeStaticField(abiType, data) | ||
) as TPrimitiveType; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { SchemaAbiType } from "@latticexyz/schema-type"; | ||
import { Schema } from "./common"; | ||
|
||
export function schemaIndexToAbiType(schema: Schema, schemaIndex: number): SchemaAbiType { | ||
if (schemaIndex < schema.staticFields.length) { | ||
return schema.staticFields[schemaIndex]; | ||
} | ||
return schema.dynamicFields[schemaIndex - schema.staticFields.length]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": ["../../.eslintrc"], | ||
"rules": { | ||
"@typescript-eslint/explicit-function-return-type": "error" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
* | ||
|
||
!dist/** | ||
!src/** | ||
!package.json | ||
!README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"name": "@latticexyz/store-sync", | ||
"version": "1.42.0", | ||
"description": "Utilities to sync MUD Store events with a client or cache", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/latticexyz/mud.git", | ||
"directory": "packages/store-sync" | ||
}, | ||
"license": "MIT", | ||
"type": "module", | ||
"exports": { | ||
".": "./dist/index.js" | ||
}, | ||
"types": "src/index.ts", | ||
"scripts": { | ||
"build": "pnpm run build:js", | ||
"build:js": "tsup", | ||
"clean": "pnpm run clean:js", | ||
"clean:js": "rimraf dist", | ||
"dev": "tsup --watch", | ||
"lint": "eslint .", | ||
"test": "vitest --run" | ||
}, | ||
"dependencies": { | ||
"@latticexyz/block-logs-stream": "workspace:*", | ||
"@latticexyz/common": "workspace:*", | ||
"@latticexyz/protocol-parser": "workspace:*", | ||
"@latticexyz/schema-type": "workspace:*", | ||
"@latticexyz/store": "workspace:*", | ||
"@latticexyz/store-cache": "workspace:*", | ||
"@latticexyz/utils": "workspace:*", | ||
"debug": "^4.3.4", | ||
"viem": "1.1.7" | ||
}, | ||
"devDependencies": { | ||
"@types/debug": "^4.1.7", | ||
"tsup": "^6.7.0", | ||
"vitest": "0.31.4" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"gitHead": "914a1e0ae4a573d685841ca2ea921435057deb8f" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
import { BlockEventsToStorageOptions, blockEventsToStorage } from "./blockEventsToStorage"; | ||
import storeConfig from "@latticexyz/store/mud.config"; | ||
|
||
const mockedCallbacks = { | ||
registerTableSchema: vi.fn< | ||
Parameters<BlockEventsToStorageOptions["registerTableSchema"]>, | ||
ReturnType<BlockEventsToStorageOptions["registerTableSchema"]> | ||
>(), | ||
registerTableMetadata: vi.fn< | ||
Parameters<BlockEventsToStorageOptions["registerTableMetadata"]>, | ||
ReturnType<BlockEventsToStorageOptions["registerTableMetadata"]> | ||
>(), | ||
getTableSchema: vi.fn< | ||
Parameters<BlockEventsToStorageOptions["getTableSchema"]>, | ||
ReturnType<BlockEventsToStorageOptions["getTableSchema"]> | ||
>(), | ||
getTableMetadata: vi.fn< | ||
Parameters<BlockEventsToStorageOptions["getTableMetadata"]>, | ||
ReturnType<BlockEventsToStorageOptions["getTableMetadata"]> | ||
>(), | ||
}; | ||
|
||
const mockedDecode = blockEventsToStorage<typeof storeConfig>(mockedCallbacks as any as BlockEventsToStorageOptions); | ||
|
||
describe("blockEventsToStorage", () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it("call setField with data properly decoded", async () => { | ||
mockedCallbacks.getTableSchema.mockImplementation(async ({ namespace, name }) => { | ||
if (namespace === "mudstore" && name === "StoreMetadata") { | ||
return { | ||
namespace: "mudstore", | ||
name: "StoreMetadata", | ||
schema: { | ||
keySchema: { | ||
staticFields: ["bytes32"], | ||
dynamicFields: [], | ||
}, | ||
valueSchema: { | ||
staticFields: [], | ||
dynamicFields: ["string", "bytes"], | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
if (namespace === "" && name === "Inventory") { | ||
return { | ||
namespace: "", | ||
name: "Inventory", | ||
schema: { | ||
keySchema: { | ||
staticFields: ["address", "uint32", "uint32"], | ||
dynamicFields: [], | ||
}, | ||
valueSchema: { | ||
staticFields: ["uint32"], | ||
dynamicFields: [], | ||
}, | ||
}, | ||
}; | ||
} | ||
}); | ||
|
||
mockedCallbacks.getTableMetadata.mockImplementation(async ({ namespace, name }) => { | ||
if (namespace === "" && name === "Inventory") { | ||
return { | ||
namespace: "", | ||
name: "Inventory", | ||
keyNames: ["owner", "item", "itemVariant"], | ||
valueNames: ["amount"], | ||
}; | ||
} | ||
}); | ||
|
||
const operations = await mockedDecode({ | ||
blockNumber: 5448n, | ||
blockHash: "0x03e962e7402b2ab295b92feac342a132111dd14b0d1fd4d4a0456fdc77981577", | ||
logs: [ | ||
{ | ||
address: "0x5fbdb2315678afecb367f032d93f642f64180aa3", | ||
topics: ["0xd01f9f1368f831528fc9fe6442366b2b7d957fbfff3bcf7c24d9ab5fe51f8c46"], | ||
data: "0x00000000000000000000000000000000496e76656e746f7279000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000796eb990a3f9c431c69149c7a168b91596d87f600000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000040000000800000000000000000000000000000000000000000000000000000000", | ||
blockHash: "0x03e962e7402b2ab295b92feac342a132111dd14b0d1fd4d4a0456fdc77981577", | ||
blockNumber: 5448n, | ||
transactionHash: "0xa6986924609542dc4c2d81c53799d8eab47109ef34ee1e422de595e19ee9bfa4", | ||
transactionIndex: 0, | ||
logIndex: 0, | ||
removed: false, | ||
args: { | ||
table: "0x00000000000000000000000000000000496e76656e746f727900000000000000", | ||
key: [ | ||
"0x000000000000000000000000796eb990a3f9c431c69149c7a168b91596d87f60", | ||
"0x0000000000000000000000000000000000000000000000000000000000000001", | ||
"0x0000000000000000000000000000000000000000000000000000000000000001", | ||
], | ||
schemaIndex: 0, | ||
data: "0x00000008", | ||
}, | ||
eventName: "StoreSetField", | ||
}, | ||
], | ||
}); | ||
|
||
expect(operations).toMatchInlineSnapshot(` | ||
{ | ||
"blockHash": "0x03e962e7402b2ab295b92feac342a132111dd14b0d1fd4d4a0456fdc77981577", | ||
"blockNumber": 5448n, | ||
"operations": [ | ||
{ | ||
"keyTuple": { | ||
"item": 1, | ||
"itemVariant": 1, | ||
"owner": "0x796eb990A3F9C431C69149c7a168b91596D87F60", | ||
}, | ||
"log": { | ||
"address": "0x5fbdb2315678afecb367f032d93f642f64180aa3", | ||
"args": { | ||
"data": "0x00000008", | ||
"key": [ | ||
"0x000000000000000000000000796eb990a3f9c431c69149c7a168b91596d87f60", | ||
"0x0000000000000000000000000000000000000000000000000000000000000001", | ||
"0x0000000000000000000000000000000000000000000000000000000000000001", | ||
], | ||
"schemaIndex": 0, | ||
"table": "0x00000000000000000000000000000000496e76656e746f727900000000000000", | ||
}, | ||
"blockHash": "0x03e962e7402b2ab295b92feac342a132111dd14b0d1fd4d4a0456fdc77981577", | ||
"blockNumber": 5448n, | ||
"data": "0x00000000000000000000000000000000496e76656e746f7279000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000796eb990a3f9c431c69149c7a168b91596d87f600000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000040000000800000000000000000000000000000000000000000000000000000000", | ||
"eventName": "StoreSetField", | ||
"logIndex": 0, | ||
"removed": false, | ||
"topics": [ | ||
"0xd01f9f1368f831528fc9fe6442366b2b7d957fbfff3bcf7c24d9ab5fe51f8c46", | ||
], | ||
"transactionHash": "0xa6986924609542dc4c2d81c53799d8eab47109ef34ee1e422de595e19ee9bfa4", | ||
"transactionIndex": 0, | ||
}, | ||
"name": "Inventory", | ||
"namespace": "", | ||
"type": "SetField", | ||
"value": 8, | ||
"valueName": "amount", | ||
}, | ||
], | ||
} | ||
`); | ||
}); | ||
}); |
Oops, something went wrong.