Skip to content

Commit 9a6082c

Browse files
critesjoshclaude
andcommitted
docs: add type-checked examples for skip_initialization and poll_for_events
Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 1cdd80b commit 9a6082c

File tree

3 files changed

+56
-41
lines changed

3 files changed

+56
-41
lines changed

docs/docs-developers/docs/aztec-js/how_to_deploy_contract.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,7 @@ Universal deployment excludes the sender from address computation, allowing the
106106

107107
Deploy without running the constructor:
108108

109-
```typescript
110-
// wallet and alice are from the connection guide
111-
const contract = await MyContract.deploy(wallet).send({
112-
from: aliceAddress,
113-
skipInitialization: true,
114-
});
115-
116-
// Initialize later
117-
await contract.methods.initialize(arg1, arg2).send({ from: aliceAddress });
118-
```
109+
#include_code skip_initialization /docs/examples/ts/aztecjs_advanced/index.ts typescript
119110

120111
### Deploy with a specific initializer
121112

docs/docs-developers/docs/aztec-js/how_to_read_data.md

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -165,36 +165,7 @@ collectedEvents.forEach((ev) => {
165165

166166
To continuously monitor for new events, poll at regular intervals while tracking the last processed block:
167167

168-
```typescript
169-
import { BlockNumber } from "@aztec/foundation/branded-types";
170-
171-
// aztecNode is from createAztecNodeClient() in the connection guide
172-
// TokenContract is your deployed token contract class
173-
let lastProcessedBlock = startBlock; // BlockNumber type
174-
175-
async function pollForEvents() {
176-
const currentBlock = await aztecNode.getBlockNumber();
177-
178-
if (currentBlock > lastProcessedBlock) {
179-
const events = await getDecodedPublicEvents<Transfer>(
180-
aztecNode,
181-
TokenContract.events.Transfer,
182-
lastProcessedBlock + 1,
183-
currentBlock - lastProcessedBlock,
184-
);
185-
186-
for (const event of events) {
187-
// Process each event
188-
console.log(`Transfer: ${event.amount} from ${event.from}`);
189-
}
190-
191-
lastProcessedBlock = currentBlock;
192-
}
193-
}
194-
195-
// Poll every 10 seconds
196-
setInterval(pollForEvents, 10000);
197-
```
168+
#include_code poll_for_events /docs/examples/ts/aztecjs_advanced/index.ts typescript
198169

199170
For private events, use the same pattern with `wallet.getPrivateEvents()` and update the `fromBlock` in your filter accordingly.
200171

docs/examples/ts/aztecjs_advanced/index.ts

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
TestWallet,
88
registerInitialLocalNetworkAccountsInWallet,
99
} from "@aztec/test-wallet/server";
10-
import { TokenContract } from "@aztec/noir-contracts.js/Token";
10+
import { TokenContract, type Transfer } from "@aztec/noir-contracts.js/Token";
1111
import { SponsoredFPCContract } from "@aztec/noir-contracts.js/SponsoredFPC";
1212
import { Fr } from "@aztec/aztec.js/fields";
1313
import { NO_WAIT, BatchCall } from "@aztec/aztec.js/contracts";
@@ -189,4 +189,57 @@ console.log(`Contract 2 at: ${contracts[1].address}`);
189189
console.log(`Contract 3 at: ${contracts[2].address}`);
190190
// docs:end:parallel_deploy
191191

192+
// docs:start:skip_initialization
193+
// Deploy without running the constructor using skipInitialization
194+
const uninitializedToken = await TokenContract.deploy(
195+
wallet,
196+
aliceAddress,
197+
"UninitToken",
198+
"UNIT",
199+
18,
200+
).send({
201+
from: aliceAddress,
202+
skipInitialization: true,
203+
});
204+
205+
console.log(`Uninitialized contract at: ${uninitializedToken.address}`);
206+
207+
// Initialize later by calling the constructor manually
208+
await uninitializedToken.methods
209+
.constructor(aliceAddress, "UninitToken", "UNIT", 18)
210+
.send({ from: aliceAddress });
211+
212+
console.log("Contract initialized");
213+
// docs:end:skip_initialization
214+
215+
// docs:start:poll_for_events
216+
import { getDecodedPublicEvents } from "@aztec/aztec.js/events";
217+
218+
// Poll for new events at regular intervals
219+
let lastProcessedBlock = await node.getBlockNumber();
220+
221+
async function pollForTransferEvents() {
222+
const currentBlock = await node.getBlockNumber();
223+
224+
if (currentBlock > lastProcessedBlock) {
225+
const events = await getDecodedPublicEvents<Transfer>(
226+
node,
227+
TokenContract.events.Transfer,
228+
lastProcessedBlock + 1,
229+
currentBlock - lastProcessedBlock,
230+
);
231+
232+
for (const event of events) {
233+
// Process each transfer event
234+
console.log(`Transfer: ${event.amount} from ${event.from} to ${event.to}`);
235+
}
236+
237+
lastProcessedBlock = currentBlock;
238+
}
239+
}
240+
241+
// Example: poll once (in production, use setInterval)
242+
await pollForTransferEvents();
243+
// docs:end:poll_for_events
244+
192245
console.log("All advanced examples completed successfully");

0 commit comments

Comments
 (0)