From 50ad0390f96e499ab90ce3657efde81047548c3a Mon Sep 17 00:00:00 2001 From: Noah Gundotra Date: Thu, 15 Dec 2022 11:11:25 -0500 Subject: [PATCH] explorer: add enum parsing for anchor arguments (#29275) add enum parsing for anchor arguments --- explorer/src/utils/anchor.tsx | 42 ++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/explorer/src/utils/anchor.tsx b/explorer/src/utils/anchor.tsx index 84ed844463..8ae0e6c1ee 100644 --- a/explorer/src/utils/anchor.tsx +++ b/explorer/src/utils/anchor.tsx @@ -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, @@ -279,8 +280,43 @@ function mapField( ); } 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 ? ( + + + {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 + ); + } + )} + + + ) : ( - {camelToTitleCase(enumValue)} + {camelToTitleCase(enumVariantName)} ); }