Skip to content

Commit

Permalink
explorer: add enum parsing for anchor arguments (solana-labs#29275)
Browse files Browse the repository at this point in the history
add enum parsing for anchor arguments
  • Loading branch information
ngundotra authored Dec 15, 2022
1 parent a5c8c7c commit 50ad039
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions explorer/src/utils/anchor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useAnchorProgram } from "providers/anchor";
import { getProgramName } from "utils/tx";
import { snakeToTitleCase, camelToTitleCase, numberWithSeparator } from "utils";
import {
IdlField,
IdlInstruction,
IdlType,
IdlTypeDef,
Expand Down Expand Up @@ -279,16 +280,51 @@ function mapField(
</ExpandableRow>
);
} else {
const enumValue = Object.keys(value)[0];
return (
const enumVariantName = Object.keys(value)[0];
const variant = fieldType.type.variants.find(
(val) =>
val.name.toLocaleLowerCase() === enumVariantName.toLocaleLowerCase()
);

return variant && variant.fields ? (
<ExpandableRow
fieldName={itemKey}
fieldType={typeDisplayName({ enum: enumVariantName })}
nestingLevel={nestingLevel}
key={keySuffix ? `${key}-${keySuffix}` : key}
>
<Fragment key={keySuffix ? `${key}-${keySuffix}` : key}>
{Object.entries(value[enumVariantName]).map(
([innerKey, innerValue]: [string, any], index) => {
const innerFieldType = variant.fields![index];
if (!innerFieldType) {
throw Error(
`Could not type definition for ${innerKey} field in user-defined struct ${fieldType.name}`
);
}
return mapField(
innerKey,
innerValue,
(innerFieldType as any).name
? (innerFieldType as IdlField).type
: (innerFieldType as IdlType),
idl,
key,
nestingLevel + 1
);
}
)}
</Fragment>
</ExpandableRow>
) : (
<SimpleRow
key={keySuffix ? `${key}-${keySuffix}` : key}
rawKey={key}
type={{ enum: type.defined }}
keySuffix={keySuffix}
nestingLevel={nestingLevel}
>
{camelToTitleCase(enumValue)}
{camelToTitleCase(enumVariantName)}
</SimpleRow>
);
}
Expand Down

0 comments on commit 50ad039

Please sign in to comment.