Skip to content

Commit

Permalink
Make fields not nullable (#137)
Browse files Browse the repository at this point in the history
## Summary

This PR makes all non-nullable fields non-nullable.

Previously, the packages followed the Protobuf wisdom of marking all
fields as nullable to enable backward and forward compatibility of the
protocol.
In practice, this degraded the user experience since they had to perform
null checks themselves.
  • Loading branch information
fracek authored Jan 20, 2025
2 parents e0b831c + a6b4ba5 commit 854fcc7
Show file tree
Hide file tree
Showing 9 changed files with 291 additions and 285 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "beaconchain: make fields required",
"packageName": "@apibara/beaconchain",
"email": "[email protected]",
"dependentChangeType": "patch"
}
7 changes: 7 additions & 0 deletions change/@apibara-evm-792c8a2f-e1df-4606-9ba7-a2dacb1264aa.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "evm: make more fields required",
"packageName": "@apibara/evm",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "starknet: make some types required",
"packageName": "@apibara/starknet",
"email": "[email protected]",
"dependentChangeType": "patch"
}
12 changes: 2 additions & 10 deletions examples/beaconchain-client/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,9 @@ const command = defineCommand({
console.log(response);

const filter = Filter.make({
// blobs: [{ includeTransaction: true }],
// transactions: [
// {
// // create: true,
// // to: "0xFf00000000000000000000000000000000102421",
// // to: "0xAD3C787556B1E9D32D3AE4f2A0B4b1dC6692eDAc",
// // includeBlob: true,
// },
// ],
validators: [{}],
transactions: [{}],
blobs: [{}],
// validators: [{}],
});

const startBlock = 5_931_000;
Expand Down
7 changes: 4 additions & 3 deletions examples/evm-client/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const command = defineCommand({
args: {
stream: {
type: "string",
default: "https://sepolia.ethereum.a5a.ch",
default: "https://ethereum-sepolia.preview.apibara.org",
description: "EVM stream URL",
},
authToken: {
Expand All @@ -36,8 +36,9 @@ const command = defineCommand({
console.log(response);

const filter = Filter.make({
header: "on_data",
transactions: [{}],
transactions: [
{ includeReceipt: true, transactionStatus: "all", includeLogs: true },
],
withdrawals: [{}],
logs: [{}],
});
Expand Down
43 changes: 17 additions & 26 deletions examples/starknet-client/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import assert from "node:assert";
import { createClient } from "@apibara/protocol";
import {
type Abi,
Filter,
StarknetStream,
decodeEvent,
getSelector,
} from "@apibara/starknet";
import { type Abi, Filter, StarknetStream } from "@apibara/starknet";
import { defineCommand, runMain } from "citty";
import consola from "consola";
import { colors } from "consola/utils";
import { formatUnits } from "viem";

const abi = [
{
Expand Down Expand Up @@ -62,20 +54,19 @@ const command = defineCommand({
console.log(response);

const filter = Filter.make({
events: [
{
address:
"0x053c91253bc9682c04929ca02ed00b3e423f6710d2ee7e0d5ebb06f3ecf368a8",
keys: [getSelector("Transfer")],
},
],
transactions: [{ includeReceipt: true }],
messages: [{}],
events: [{}],
storageDiffs: [{}],
contractChanges: [{}],
nonceUpdates: [{}],
});

const request = StarknetStream.Request.make({
filter: [filter],
finality: "accepted",
finality: "pending",
startingCursor: {
orderKey: 1_078_335n,
orderKey: 1_083_705n,
},
});

Expand All @@ -95,14 +86,14 @@ const command = defineCommand({
);

for (const event of block.events) {
const { args, transactionHash } = decodeEvent({
abi,
eventName: "Transfer",
event,
});
consola.info(
`${prettyAddress(args.from)} -> ${prettyAddress(args.to)} ${formatUnits(args.value, 6)} ${colors.gray(transactionHash ?? "")}`,
);
// const { args, transactionHash } = decodeEvent({
// abi,
// eventName: "Transfer",
// event,
// });
// consola.info(
// `${prettyAddress(args.from)} -> ${prettyAddress(args.to)} ${formatUnits(args.value, 6)} ${colors.gray(transactionHash ?? "")}`,
// );
}
}

Expand Down
103 changes: 56 additions & 47 deletions packages/beaconchain/src/block.ts
Original file line number Diff line number Diff line change
@@ -1,91 +1,100 @@
import { AccessListItem, Signature } from "@apibara/evm";
import { AccessListItem } from "@apibara/evm";
import { BytesFromUint8Array } from "@apibara/protocol";
import { Schema } from "@effect/schema";

import { Address, B256, B384, U128, U256, ValidatorStatus } from "./common";
import * as proto from "./proto";

export const ExecutionPayload = Schema.Struct({
parentHash: Schema.optional(B256),
feeRecipient: Schema.optional(Address),
stateRoot: Schema.optional(B256),
receiptsRoot: Schema.optional(B256),
parentHash: B256,
feeRecipient: Address,
stateRoot: B256,
receiptsRoot: B256,
logsBloom: BytesFromUint8Array,
prevRandao: Schema.optional(B256),
blockNumber: Schema.optional(Schema.BigIntFromSelf),
timestamp: Schema.optional(Schema.DateFromSelf),
prevRandao: B256,
blockNumber: Schema.BigIntFromSelf,
timestamp: Schema.DateFromSelf,
});

export type ExecutionPayload = typeof ExecutionPayload.Type;

export const BlockHeader = Schema.Struct({
slot: Schema.BigIntFromSelf,
proposerIndex: Schema.Number,
parentRoot: Schema.optional(B256),
stateRoot: Schema.optional(B256),
parentRoot: B256,
stateRoot: B256,
randaoReveal: BytesFromUint8Array,
depositCount: Schema.optional(Schema.BigIntFromSelf),
depositRoot: Schema.optional(B256),
blockHash: Schema.optional(B256),
graffiti: Schema.optional(B256),
depositCount: Schema.BigIntFromSelf,
depositRoot: B256,
blockHash: B256,
graffiti: B256,
executionPayload: Schema.optional(ExecutionPayload),
blobKzgCommitments: Schema.optional(Schema.Array(B384)),
blobKzgCommitments: Schema.Array(B384),
});

export type BlockHeader = typeof BlockHeader.Type;

export const Validator = Schema.Struct({
filterIds: Schema.optional(Schema.Array(Schema.Number)),
validatorIndex: Schema.optional(Schema.Number),
balance: Schema.optional(Schema.BigIntFromSelf),
status: Schema.optional(ValidatorStatus),
pubkey: Schema.optional(B384),
withdrawalCredentials: Schema.optional(B256),
effectiveBalance: Schema.optional(Schema.BigIntFromSelf),
slashed: Schema.optional(Schema.Boolean),
activationEligibilityEpoch: Schema.optional(Schema.BigIntFromSelf),
activationEpoch: Schema.optional(Schema.BigIntFromSelf),
exitEpoch: Schema.optional(Schema.BigIntFromSelf),
withdrawableEpoch: Schema.optional(Schema.BigIntFromSelf),
filterIds: Schema.Array(Schema.Number),
validatorIndex: Schema.Number,
balance: Schema.BigIntFromSelf,
status: ValidatorStatus,
pubkey: B384,
withdrawalCredentials: B256,
effectiveBalance: Schema.BigIntFromSelf,
slashed: Schema.Boolean,
activationEligibilityEpoch: Schema.BigIntFromSelf,
activationEpoch: Schema.BigIntFromSelf,
exitEpoch: Schema.BigIntFromSelf,
withdrawableEpoch: Schema.BigIntFromSelf,
});

export const Blob = Schema.Struct({
filterIds: Schema.optional(Schema.Array(Schema.Number)),
blobIndex: Schema.optional(Schema.Number),
blob: Schema.optional(Schema.Uint8ArrayFromSelf),
kzgCommitment: Schema.optional(B384),
kzgProof: Schema.optional(B384),
kzgCommitmentInclusionProof: Schema.optional(Schema.Array(B256)),
blobHash: Schema.optional(B256),
transactionIndex: Schema.optional(Schema.Number),
transactionHash: Schema.optional(B256),
filterIds: Schema.Array(Schema.Number),
blobIndex: Schema.Number,
blob: Schema.Uint8ArrayFromSelf,
kzgCommitment: B384,
kzgProof: B384,
kzgCommitmentInclusionProof: Schema.Array(B256),
blobHash: B256,
transactionIndex: Schema.Number,
transactionHash: B256,
});

export const Signature = Schema.Struct({
r: Schema.optional(U256),
s: Schema.optional(U256),
v: Schema.optional(U256),
YParity: Schema.optional(Schema.Boolean),
});

export type Signature = typeof Signature.Type;

export const Transaction = Schema.Struct({
filterIds: Schema.optional(Schema.Array(Schema.Number)),
transactionHash: Schema.optional(B256),
nonce: Schema.optional(Schema.BigIntFromSelf),
transactionIndex: Schema.optional(Schema.Number),
from: Schema.optional(Address),
filterIds: Schema.Array(Schema.Number),
transactionHash: B256,
nonce: Schema.BigIntFromSelf,
transactionIndex: Schema.Number,
from: Address,
to: Schema.optional(Address),
value: Schema.optional(U256),
value: U256,
gasPrice: Schema.optional(U128),
gasLimit: Schema.optional(U128),
maxFeePerGas: Schema.optional(U128),
maxPriorityFeePerGas: Schema.optional(U128),
input: Schema.optional(Schema.Uint8ArrayFromSelf),
input: Schema.Uint8ArrayFromSelf,
signature: Schema.optional(Signature),
chainId: Schema.optional(Schema.BigIntFromSelf),
accessList: Schema.optional(Schema.Array(AccessListItem)),
transactionType: Schema.optional(Schema.BigIntFromSelf),
accessList: Schema.Array(AccessListItem),
transactionType: Schema.BigIntFromSelf,
maxFeePerBlobGas: Schema.optional(U128),
blobVersionedHashes: Schema.optional(Schema.Array(B256)),
blobVersionedHashes: Schema.Array(B256),
});

export type Transaction = typeof Transaction.Type;

export const Block = Schema.Struct({
header: Schema.optional(BlockHeader),
header: BlockHeader,
validators: Schema.Array(Validator),
blobs: Schema.Array(Blob),
transactions: Schema.Array(Transaction),
Expand Down
Loading

0 comments on commit 854fcc7

Please sign in to comment.