-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
181 additions
and
48 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
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,50 @@ | ||
import { Client, parseAbiItem, Hex, Address, getAddress } from "viem"; | ||
import { WorldDeploy, worldTables } from "./common"; | ||
import { debug } from "./debug"; | ||
import { storeSpliceStaticDataEvent } from "@latticexyz/store"; | ||
import { getLogs } from "viem/actions"; | ||
import { decodeKey } from "@latticexyz/protocol-parser"; | ||
import { getTableValue } from "./getTableValue"; | ||
|
||
export async function getResourceAccess({ | ||
client, | ||
worldDeploy, | ||
}: { | ||
client: Client; | ||
worldDeploy: WorldDeploy; | ||
}): Promise<{ resourceId: Hex; address: Address }[]> { | ||
// This assumes we only use `ResourceAccess._set(...)`, which is true as of this writing. | ||
// TODO: PR to viem's getLogs to accept topics array so we can filter on all store events and quickly recreate this table's current state | ||
|
||
debug("looking up resource access for", worldDeploy.address); | ||
|
||
const logs = await getLogs(client, { | ||
strict: true, | ||
fromBlock: worldDeploy.fromBlock, | ||
address: worldDeploy.address, | ||
// our usage of `ResourceAccess._set(...)` emits a splice instead of set record | ||
// TODO: https://github.com/latticexyz/mud/issues/479 | ||
event: parseAbiItem(storeSpliceStaticDataEvent), | ||
args: { tableId: worldTables.world_ResourceAccess.tableId }, | ||
}); | ||
|
||
const keys = logs.map((log) => decodeKey(worldTables.world_ResourceAccess.keySchema, log.args.keyTuple)); | ||
|
||
const access = ( | ||
await Promise.all( | ||
keys.map( | ||
async (key) => | ||
[key, await getTableValue({ client, worldDeploy, table: worldTables.world_ResourceAccess, key })] as const | ||
) | ||
) | ||
) | ||
.filter(([, value]) => value.access) | ||
.map(([key]) => ({ | ||
resourceId: key.resourceId, | ||
address: getAddress(key.caller), | ||
})); | ||
|
||
debug("found", access.length, "resource<>address access pairs"); | ||
|
||
return access; | ||
} |
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