Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse-block fiel update with parsing method #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 31 additions & 23 deletions packages/backend-utils/src/parse-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,38 @@ import { L2Block } from "@aztec/aztec.js";
import { ChicmozL2Block, chicmozL2BlockSchema } from "@chicmoz-pkg/types";

const getTxEffectWithHashes = (txEffects: L2Block["body"]["txEffects"]) => {
return txEffects.map((txEffect) => {
return {
...txEffect,
hash: "0x" + txEffect.hash().toString("hex"),
};
});
return txEffects.map((txEffect) => ({
...txEffect,
hash: "0x" + txEffect.hash().toString("hex"),
}));
};

export const blockFromString = (stringifiedBlock: string): L2Block => {
return L2Block.fromString(stringifiedBlock);
}

export const parseBlock = (b: L2Block): ChicmozL2Block => {
const blockHash = b.hash();
const blockWithTxEffectsHashesAdded = {
...b,
body: {
...b.body,
txEffects: getTxEffectWithHashes(b.body.txEffects),
},
};
return chicmozL2BlockSchema.parse({
hash: blockHash.toString(),
height: b.number,
...JSON.parse(JSON.stringify(blockWithTxEffectsHashesAdded)),
});
try {
return L2Block.fromString(stringifiedBlock);
} catch (error) {
throw new Error("Failed to parse L2Block from string");
}
};

export const parseBlock = (block: L2Block): ChicmozL2Block => {
try {
const blockHash = block.hash();
const blockWithTxEffectsHashesAdded = {
body: {
...block.body,
txEffects: getTxEffectWithHashes(block.body.txEffects),
},
};

const parsedBlock = {
hash: blockHash.toString(),
height: block.number,
...blockWithTxEffectsHashesAdded,
};

return chicmozL2BlockSchema.parse(parsedBlock);
} catch (error) {
throw new Error("Failed to parse ChicmozL2Block");
}
};