Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Oct 3, 2024
1 parent d162d19 commit a2a3ce2
Showing 1 changed file with 20 additions and 27 deletions.
47 changes: 20 additions & 27 deletions yarn-project/types/src/abi/contract_artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,46 +254,39 @@ function getStorageLayout(input: NoirCompiledContract) {
* @return A record of the note types and their ids
*/
function getNoteTypes(input: NoirCompiledContract) {
type t = {
kind: string;
fields: [
{ kind: string; sign: boolean; value: string },
{ kind: string; value: string },
{
fields: {
name: string;
value: {
kind: string;
value: string | boolean;
fields: [
{ name: string; value: { kind: string; sign: boolean; value: string } },
{ name: string; value: { kind: string; value: boolean } },
];
};
}[];
},
];
};

const notes = input.outputs.globals.notes as t[];
// The type is useless here as it does not give us any guarantee (e.g. `AbiValue` can be one of many different
// types) so we nuke it and later we manually check the values are as we expect.
const notes = input.outputs.globals.notes as any[];

if (!notes) {
return {};
}

return notes.reduce((acc: Record<string, ContractNote>, note) => {
const name = note.fields[1].value as string;
// Note id is encoded as a hex string
const id = NoteSelector.fromField(Fr.fromString(note.fields[0].value));
const fields = note.fields[2].fields.map(field => {
const noteFields = note.fields;

// We find note type id by looking for respective kinds as each of them is unique
const rawNoteTypeId = noteFields.find((field: any) => field.kind === 'integer');
const rawName = noteFields.find((field: any) => field.kind === 'string');
const rawNoteFields = noteFields.find((field: any) => field.kind === 'struct');

if (!rawNoteTypeId || !rawName || !rawNoteFields) {
throw new Error(`Could not find note type id, name or fields for note ${note}`);
}

const noteTypeId = NoteSelector.fromField(Fr.fromString(rawNoteTypeId.value));
const name = rawName.value as string;

// Note type id is encoded as a hex string
const fields = rawNoteFields.fields.map((field: any) => {
return {
name: field.name,
index: parseInt(field.value.fields[0].value.value, 16),
nullable: field.value.fields[1].value.value,
};
});
acc[name] = {
id,
id: noteTypeId,
typ: name,
fields,
};
Expand Down

0 comments on commit a2a3ce2

Please sign in to comment.