ts: Fix formatting enums (#2763)

Co-authored-by: acheron <acheroncrypto@gmail.com>
This commit is contained in:
0xabhinav 2024-01-09 18:17:59 +05:30 committed by GitHub
parent 7cbdff657d
commit 211982affc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 5 deletions

View File

@ -35,6 +35,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- lang: Allow custom lifetime in Accounts structure ([#2741](https://github.com/coral-xyz/anchor/pull/2741)).
- lang: Remove `try_to_vec` usage while setting the return data in order to reduce heap memory usage ([#2744](https://github.com/coral-xyz/anchor/pull/2744))
- cli: Show installation progress if Solana tools are not installed when using toolchain overrides ([#2757](https://github.com/coral-xyz/anchor/pull/2757)).
- ts: Fix formatting enums ([#2763](https://github.com/coral-xyz/anchor/pull/2763)).
### Breaking

View File

@ -17,6 +17,7 @@ import {
IdlTypeOption,
IdlTypeDefined,
IdlAccounts,
IdlEnumFieldsNamed,
} from "../../idl.js";
import { IdlCoder } from "./idl.js";
import { InstructionCoder } from "../index.js";
@ -232,7 +233,8 @@ class InstructionFormatter {
.map((d: IdlField) =>
this.formatIdlData(
{ name: "", type: (<IdlTypeVec>idlField.type).vec },
d
d,
types
)
)
.join(", ") +
@ -303,15 +305,21 @@ class InstructionFormatter {
const variants = typeDef.type.variants;
const variant = Object.keys(data)[0];
const enumType = data[variant];
const enumVariant = variants.find(
(v) => camelCase(v.name) === variant
);
if (!enumVariant) {
throw new Error(`Unable to find variant \`${variant}\``);
}
const fields = enumVariant.fields as IdlEnumFieldsNamed;
const namedFields = Object.keys(enumType)
.map((f) => {
const fieldData = enumType[f];
const idlField = variants[variant]?.find(
(v: IdlField) => v.name === f
);
const idlField = fields.find((v) => v.name === f);
if (!idlField) {
throw new Error("Unable to find variant");
throw new Error(`Unable to find field \`${f}\``);
}
return (
f +
": " +