From 211982affc37883ef6b04578757d2815f63a7fef Mon Sep 17 00:00:00 2001 From: 0xabhinav <97421185+0xabhinav@users.noreply.github.com> Date: Tue, 9 Jan 2024 18:17:59 +0530 Subject: [PATCH] ts: Fix formatting enums (#2763) Co-authored-by: acheron --- CHANGELOG.md | 1 + .../anchor/src/coder/borsh/instruction.ts | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0374e5cec4..8838c67455 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/ts/packages/anchor/src/coder/borsh/instruction.ts b/ts/packages/anchor/src/coder/borsh/instruction.ts index c02aa177e6..480a1191a5 100644 --- a/ts/packages/anchor/src/coder/borsh/instruction.ts +++ b/ts/packages/anchor/src/coder/borsh/instruction.ts @@ -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: (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 + ": " +