-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add getRecords util and use instead of fetching logs in deployer
- Loading branch information
Showing
8 changed files
with
117 additions
and
122 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
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,36 +1,24 @@ | ||
import { Client, Hex } from "viem"; | ||
import { flattenStoreLogs, getStoreLogs } from "@latticexyz/store/internal"; | ||
import { WorldDeploy } from "./common"; | ||
import { debug } from "./debug"; | ||
import storeConfig from "@latticexyz/store/mud.config"; | ||
import { fetchBlockLogs } from "@latticexyz/block-logs-stream"; | ||
import { getRecords } from "@latticexyz/store-sync"; | ||
|
||
export async function getResourceIds({ | ||
client, | ||
worldDeploy, | ||
}: { | ||
readonly client: Client; | ||
readonly worldDeploy: WorldDeploy; | ||
}): Promise<readonly Hex[]> { | ||
debug("looking up resource IDs for", worldDeploy.address); | ||
|
||
const blockLogs = await fetchBlockLogs({ | ||
fromBlock: worldDeploy.deployBlock, | ||
toBlock: worldDeploy.stateBlock, | ||
maxBlockRange: 100_000n, | ||
async getLogs({ fromBlock, toBlock }) { | ||
return getStoreLogs(client, { | ||
address: worldDeploy.address, | ||
fromBlock, | ||
toBlock, | ||
tableId: storeConfig.namespaces.store.tables.ResourceIds.tableId, | ||
}); | ||
}, | ||
const { records } = await getRecords({ | ||
table: storeConfig.namespaces.store.tables.ResourceIds, | ||
worldAddress: worldDeploy.address, | ||
indexerUrl: "https://indexer.mud.garnetchain.com", | ||
chainId: 17069, | ||
}); | ||
const logs = flattenStoreLogs(blockLogs.flatMap((block) => block.logs)); | ||
const resourceIds = records.map((record) => record.resourceId); | ||
|
||
const resourceIds = logs.map((log) => log.args.keyTuple[0]); | ||
debug("found", resourceIds.length, "resource IDs for", worldDeploy.address); | ||
|
||
return resourceIds; | ||
} |
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,70 @@ | ||
import { fetchBlockLogs } from "@latticexyz/block-logs-stream"; | ||
import { Table } from "@latticexyz/config"; | ||
import { getSchemaPrimitives } from "@latticexyz/protocol-parser/internal"; | ||
import { LogToRecordArgs, flattenStoreLogs, getStoreLogs, logToRecord } from "@latticexyz/store/internal"; | ||
import { Address, createPublicClient, http } from "viem"; | ||
import { debug } from "./debug"; | ||
import { getSnapshot } from "./getSnapshot"; | ||
import { StorageAdapterLog } from "./common"; | ||
|
||
type GetRecordsOptions<table extends Table = Table> = { | ||
table: table; | ||
worldAddress: Address; | ||
} & ( | ||
| { | ||
indexerUrl: string; | ||
chainId: number; | ||
} | ||
| { | ||
rpcUrl: string; | ||
fromBlock?: bigint; | ||
toBlock?: bigint; | ||
} | ||
); | ||
|
||
type GetRecordsResult<table extends Table = Table> = { | ||
records: getSchemaPrimitives<table["schema"]>[]; | ||
blockNumber: bigint; | ||
}; | ||
|
||
export async function getRecords<table extends Table>( | ||
options: GetRecordsOptions<table>, | ||
): Promise<GetRecordsResult<table>> { | ||
async function getLogs(): Promise<readonly StorageAdapterLog[]> { | ||
if ("indexerUrl" in options) { | ||
debug("fetching records for", options.table.label, "via indexer from", options.indexerUrl); | ||
const logs = await getSnapshot({ | ||
chainId: options.chainId, | ||
address: options.worldAddress, | ||
indexerUrl: options.indexerUrl, | ||
filters: [{ tableId: options.table.tableId }], | ||
}); | ||
return logs?.logs ?? []; | ||
} else { | ||
debug("fetching records for", options.table.label, "via RPC from", options.rpcUrl); | ||
const client = createPublicClient({ | ||
transport: http(options.rpcUrl), | ||
}); | ||
const blockLogs = await fetchBlockLogs({ | ||
fromBlock: options.fromBlock ?? 0n, | ||
toBlock: options.toBlock ?? (await client.getBlockNumber()), | ||
maxBlockRange: 100_000n, | ||
async getLogs({ fromBlock, toBlock }) { | ||
return getStoreLogs(client, { | ||
address: options.worldAddress, | ||
fromBlock, | ||
toBlock, | ||
tableId: options.table.tableId, | ||
}); | ||
}, | ||
}); | ||
return flattenStoreLogs(blockLogs.flatMap((block) => block.logs)); | ||
} | ||
} | ||
|
||
const logs = await getLogs(); | ||
const records = logs.map((log) => logToRecord({ log: log as LogToRecordArgs<table>["log"], table: options.table })); | ||
const blockNumber = logs.length > 0 ? logs[logs.length - 1].blockNumber ?? 0n : 0n; | ||
debug("found", records.length, "records for table", options.table.label, "at block", blockNumber); | ||
return { records, blockNumber }; | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.