Upgrading from v1
This page contains a list of changes between DNA v1 and DNA v2.
@apibara/starknet package
This package now works in combination with @apibara/protocol to provide a DNA stream
that automatically encodes and decodes the Protobuf data. This means tha field elements
are automatically converted to 0x${string} values.
Notice that the data stream is now unary.
import { createClient } from "@apibara/protocol";
import { Filter, StarknetStream } from "@apibara/starknet";
const client = createClient(StarknetStream, process.env.STREAM_URL);
const filter = {
events: [{
address:
"0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
}],
} satisfies Filter;
const request = StarknetStream.Request.make({
filter: [filter],
finality: "accepted",
startingCursor: {
orderKey: 800_000n,
},
});
for await (const message of client.streamData(request)) {
switch (message._tag) {
case "data": {
break;
}
case "invalidate": {
break;
}
default: {
break;
}
}
}Reconnecting on error
NOTE: this section only applies if you're using the gRPC client directly.
The client now doesn't automatically reconnect on error. This is because the reconnection step
is very delicate and depends on your indexer's implementation.
The recommended approach is to wrap your indexer's main loop in a try/catch block.
import { createClient, type ClientError, type Status } from "@apibara/protocol";
import { Filter, StarknetStream } from "@apibara/starknet";
const client = createClient(StarknetStream, process.env.STREAM_URL);
const filter = {
events: [
{
address:
"0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
},
],
} satisfies Filter;
while (true) {
try {
const startingCursor = await loadCursorFromDatabase();
const request = StarknetStream.Request.make({
filter: [filter],
finality: "accepted",
startingCursor,
});
for await (const message of client.streamData(request)) {
}
} catch (err) {
if (err instanceof ClientError) {
// It's a gRPC error.
if (err.status !== Status.INTERNAL) {
// NON-INTERNAL errors are not recoverable.
throw err;
}
// INTERNAL errors are caused by a disconnection.
// Sleep and reconnect.
await new Promise((r) => setTimeout(r, 2_000));
}
}
}Filter
Header
- The
headerfield is now an enum. See the dedicated section in the filter documentation for more information.
Events
fromAddressis nowaddress.- The
keysfield acceptsnullvalues to match any key at that position. - The
datafield was removed. - Use
transactionStatus: "all"instead ofincludeRevertedto include reverted transactions. includeReceiptandincludeTransactionare nowfalseby default.
Transactions
- Now you can only filter by transaction type.
- We will add transaction-specific filters in the future.
- Use
transactionStatus: "all"instead ofincludeRevertedto include reverted transactions. includeReceiptis nowfalseby default.
Messages
- Can now filter by
fromAddressandtoAddress. - Use
transactionStatus: "all"instead ofincludeRevertedto include reverted transactions. includeReceiptandincludeTransactionare nowfalseby default.
State Update
- State update has been split into separate filters for storage diffs, contract changes, and nonce updates.
- Declared and deployed contracts, declared classes, and replaced classes are now
a single
contractChangesfilter.
Block data
- Block data has been "flattened". Use the
*Indexfield to access related data. For example, the following code iterates over all events and looks up their transactions.
for (const event of block.events) {
const transaction = block.transactions.find(
(tx) => tx.transactionIndex === event.transactionIndex
);
}Events
fromAddressis nowaddress.indexis noweventIndex.- Events now include
transactionIndex,transactionHash, andtransactionStatus.
Transactions
TransactionMetanow includestransactionIndex,transactionHash, andtransactionStatus.- The transaction type is now an enum using the
_tagfield as discriminator. - For other minor changes, see the transaction documentation.
Receipts
- Transaction receipts are now transaction-specific.
- For other minor changes, see the receipts documentation.
Messages
indexis nowmessageIndex.- Messages now include
transactionIndex,transactionHash, andtransactionStatus.