explorer: add enum parsing for anchor arguments (#29275)
add enum parsing for anchor arguments
This commit is contained in:
parent
a5c8c7c536
commit
50ad0390f9
|
@ -6,6 +6,7 @@ import { useAnchorProgram } from "providers/anchor";
|
||||||
import { getProgramName } from "utils/tx";
|
import { getProgramName } from "utils/tx";
|
||||||
import { snakeToTitleCase, camelToTitleCase, numberWithSeparator } from "utils";
|
import { snakeToTitleCase, camelToTitleCase, numberWithSeparator } from "utils";
|
||||||
import {
|
import {
|
||||||
|
IdlField,
|
||||||
IdlInstruction,
|
IdlInstruction,
|
||||||
IdlType,
|
IdlType,
|
||||||
IdlTypeDef,
|
IdlTypeDef,
|
||||||
|
@ -279,8 +280,43 @@ function mapField(
|
||||||
</ExpandableRow>
|
</ExpandableRow>
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
const enumValue = Object.keys(value)[0];
|
const enumVariantName = Object.keys(value)[0];
|
||||||
return (
|
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
|
<SimpleRow
|
||||||
key={keySuffix ? `${key}-${keySuffix}` : key}
|
key={keySuffix ? `${key}-${keySuffix}` : key}
|
||||||
rawKey={key}
|
rawKey={key}
|
||||||
|
@ -288,7 +324,7 @@ function mapField(
|
||||||
keySuffix={keySuffix}
|
keySuffix={keySuffix}
|
||||||
nestingLevel={nestingLevel}
|
nestingLevel={nestingLevel}
|
||||||
>
|
>
|
||||||
{camelToTitleCase(enumValue)}
|
{camelToTitleCase(enumVariantName)}
|
||||||
</SimpleRow>
|
</SimpleRow>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue