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

feat: As a user, I want to get decoded tuples from Attestation data via the subgraph #613

Merged
merged 1 commit into from
May 29, 2024
Merged
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
33 changes: 22 additions & 11 deletions subgraph/src/attestation-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,17 @@ export function handleAttestationRegistered(event: AttestationRegisteredEvent):
const tempSubject = ethereum.decode("address", attestationData.subject);
attestation.subject = tempSubject ? tempSubject.toAddress() : attestationData.subject;

// Get matching Schemax
const schema = Schema.load(attestation.schema);
// Get matching Schema
const schema = Schema.load(attestationData.schemaId.toHex());

if (schema) {
// Remove the first and last characters of the schema string (the parentheses) if they exist
let tempSchema = schema.schema;
tempSchema = tempSchema.startsWith("(") ? tempSchema.substring(1) : tempSchema;
tempSchema = tempSchema.endsWith(")") ? tempSchema.substring(0, tempSchema.length - 1) : tempSchema;

// Split Schema into a "type fieldName" array
const splitSchema = schema.schema.split(",");
const splitSchema = tempSchema.split(",");

// Keep only the Schema's types
const schemaTypes = splitSchema.map<string>((item) => item.trim().split(" ")[0]);
Expand Down Expand Up @@ -86,15 +91,11 @@ export function handleAttestationRegistered(event: AttestationRegisteredEvent):

// If the decode function went through, save it as an Array of Strings
if (decoded) {
const tempStringArray: string[] = [];

// Make the decoded data into a Tuple
const tupleValue = decoded.toTuple();

// Convert every field of the Tuple into a String
for (let i = 0; i < tupleValue.length; i++) {
tempStringArray.push(valueToString(tupleValue[i]));
}
const tempStringArray: string[] = tupleToStringArray(tupleValue);

// Add this decoded Array to the Attestation Entity
attestation.decodedData =
Expand Down Expand Up @@ -211,14 +212,24 @@ export function handleVersionUpdated(event: VersionUpdated): void {
registryVersion.save();
}

function tupleToStringArray(tuple: ethereum.Tuple): string[] {
const tempStringArray: string[] = [];

for (let i = 0; i < tuple.length; i++) {
tempStringArray.push(valueToString(tuple[i]));
}

return tempStringArray;
}

function valueToString(value: ethereum.Value): string {
switch (value.kind) {
case ethereum.ValueKind.ADDRESS:
return value.toAddress().toHexString();
case ethereum.ValueKind.FIXED_BYTES:
return value.toBytes().toHexString().toLowerCase();
return value.toBytes().toHex();
case ethereum.ValueKind.BYTES:
return value.toBytes().toString();
return value.toBytes().toHex();
case ethereum.ValueKind.INT:
return value.toBigInt().toHexString();
case ethereum.ValueKind.UINT:
Expand All @@ -238,7 +249,7 @@ function valueToString(value: ethereum.Value): string {
.map<string>((item) => valueToString(item))
.toString();
case ethereum.ValueKind.TUPLE:
return "TUPLE NOT SUPPORTED";
return tupleToStringArray(value.toTuple()).toString();
default:
return "UNKNOWN TYPE";
}
Expand Down
Loading