Skip to content

Commit 1e09240

Browse files
JackieXualvrs
andauthored
feat(cli): fetch world deploy block number if available (#3417)
Co-authored-by: alvrs <alvarius@lattice.xyz>
1 parent 706f2e0 commit 1e09240

4 files changed

Lines changed: 46 additions & 6 deletions

File tree

.changeset/curly-apples-bake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@latticexyz/cli": patch
3+
---
4+
5+
When upgrading an existing world, the deployer now attempts to read the deploy block number from the `worlds.json` file. If it is found, the `HelloWorld` and `HelloStore` event are fetched from this block instead of searching for the events starting from the genesis block.

packages/cli/src/deploy/deploy.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ type DeployOptions = {
3838
artifacts: readonly ContractArtifact[];
3939
salt?: Hex;
4040
worldAddress?: Address;
41+
/**
42+
* Block number of an existing world deployment.
43+
* Only used if `worldAddress` is provided.
44+
*/
45+
worldDeployBlock?: bigint;
4146
/**
4247
* Address of determinstic deployment proxy: https://github.com/Arachnid/deterministic-deployment-proxy
4348
* By default, we look for a deployment at 0x4e59b44847b379578588920ca78fbf26c0b4956c and, if not, deploy one.
@@ -64,14 +69,15 @@ export async function deploy({
6469
artifacts,
6570
salt,
6671
worldAddress: existingWorldAddress,
72+
worldDeployBlock,
6773
deployerAddress: initialDeployerAddress,
6874
indexerUrl,
6975
chainId,
7076
}: DeployOptions): Promise<WorldDeploy> {
7177
const deployerAddress = initialDeployerAddress ?? (await ensureDeployer(client));
7278

7379
const worldDeploy = existingWorldAddress
74-
? await getWorldDeploy(client, existingWorldAddress)
80+
? await getWorldDeploy(client, existingWorldAddress, worldDeployBlock)
7581
: config.deploy.customWorld
7682
? await deployCustomWorld({
7783
client,

packages/cli/src/deploy/getWorldDeploy.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import { logsToWorldDeploy } from "./logsToWorldDeploy";
77

88
const deploys = new Map<Address, WorldDeploy>();
99

10-
export async function getWorldDeploy(client: Client, worldAddress: Address): Promise<WorldDeploy> {
10+
export async function getWorldDeploy(
11+
client: Client,
12+
worldAddress: Address,
13+
deployBlock?: bigint,
14+
): Promise<WorldDeploy> {
1115
const address = getAddress(worldAddress);
1216

1317
let deploy = deploys.get(address);
@@ -20,10 +24,9 @@ export async function getWorldDeploy(client: Client, worldAddress: Address): Pro
2024

2125
debug("looking up world deploy for", address);
2226

23-
const [fromBlock, toBlock] = await Promise.all([
24-
getBlock(client, { blockTag: "earliest" }),
25-
getBlock(client, { blockTag: "latest" }),
26-
]);
27+
const [fromBlock, toBlock] = deployBlock
28+
? [{ number: deployBlock }, { number: deployBlock }]
29+
: await Promise.all([getBlock(client, { blockTag: "earliest" }), getBlock(client, { blockTag: "latest" })]);
2730

2831
const blockLogs = await fetchBlockLogs({
2932
publicClient: client,
@@ -34,6 +37,10 @@ export async function getWorldDeploy(client: Client, worldAddress: Address): Pro
3437
maxBlockRange: 100_000n,
3538
});
3639

40+
if (blockLogs.length === 0) {
41+
throw new Error("could not find `HelloWorld` or `HelloStore` event");
42+
}
43+
3744
deploy = {
3845
...logsToWorldDeploy(blockLogs.flatMap((block) => block.logs)),
3946
stateBlock: toBlock.number,

packages/cli/src/runDeploy.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,13 @@ export async function runDeploy(opts: DeployOptions): Promise<WorldDeploy> {
144144

145145
const chainId = await getChainId(client);
146146
const indexerUrl = opts.indexerUrl ?? defaultChains.find((chain) => chain.id === chainId)?.indexerUrl;
147+
const worldDeployBlock = opts.worldAddress
148+
? getWorldDeployBlock({
149+
worldAddress: opts.worldAddress,
150+
worldsFile: config.deploy.worldsFile,
151+
chainId,
152+
})
153+
: undefined;
147154

148155
console.log("Deploying from", client.account.address);
149156

@@ -156,6 +163,7 @@ export async function runDeploy(opts: DeployOptions): Promise<WorldDeploy> {
156163
deployerAddress: opts.deployerAddress as Hex | undefined,
157164
salt,
158165
worldAddress: opts.worldAddress as Hex | undefined,
166+
worldDeployBlock,
159167
client,
160168
tables,
161169
systems,
@@ -215,3 +223,17 @@ export async function runDeploy(opts: DeployOptions): Promise<WorldDeploy> {
215223

216224
return worldDeploy;
217225
}
226+
227+
function getWorldDeployBlock({
228+
chainId,
229+
worldAddress,
230+
worldsFile,
231+
}: {
232+
chainId: number;
233+
worldAddress: string;
234+
worldsFile: string;
235+
}): bigint | undefined {
236+
const deploys = existsSync(worldsFile) ? JSON.parse(readFileSync(worldsFile, "utf-8")) : {};
237+
const worldDeployBlock = deploys[chainId]?.address === worldAddress ? deploys[chainId].blockNumber : undefined;
238+
return worldDeployBlock ? BigInt(worldDeployBlock) : undefined;
239+
}

0 commit comments

Comments
 (0)