From 2fedba0916816addf645611e89d0007c625de71a Mon Sep 17 00:00:00 2001 From: Kumar Abhinav Date: Thu, 4 Jan 2024 19:59:09 +0530 Subject: [PATCH 1/6] Fix enum parsing bug --- ts/packages/anchor/src/coder/borsh/instruction.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/ts/packages/anchor/src/coder/borsh/instruction.ts b/ts/packages/anchor/src/coder/borsh/instruction.ts index c02aa177e6..e226563e51 100644 --- a/ts/packages/anchor/src/coder/borsh/instruction.ts +++ b/ts/packages/anchor/src/coder/borsh/instruction.ts @@ -232,7 +232,8 @@ class InstructionFormatter { .map((d: IdlField) => this.formatIdlData( { name: "", type: (idlField.type).vec }, - d + d, + types ) ) .join(", ") + @@ -303,11 +304,15 @@ class InstructionFormatter { const variants = typeDef.type.variants; const variant = Object.keys(data)[0]; const enumType = data[variant]; + const camelCaseVariant = camelCase(variant); + const enumVariant = variants.find((v) => camelCase(v.name) === camelCaseVariant); + const relevantFields = enumVariant?.fields; const namedFields = Object.keys(enumType) .map((f) => { const fieldData = enumType[f]; - const idlField = variants[variant]?.find( - (v: IdlField) => v.name === f + const camelCaseField = camelCase(f); + const idlField = relevantFields?.find( + (v): v is IdlField => camelCase((v as IdlField).name) === camelCaseField ); if (!idlField) { throw new Error("Unable to find variant"); From 7060dbf46620f22a784358762d02f9d77946f31d Mon Sep 17 00:00:00 2001 From: Kumar Abhinav Date: Sat, 6 Jan 2024 17:42:28 +0530 Subject: [PATCH 2/6] Fix tests --- ts/packages/anchor/src/coder/borsh/instruction.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ts/packages/anchor/src/coder/borsh/instruction.ts b/ts/packages/anchor/src/coder/borsh/instruction.ts index e226563e51..7bfa877246 100644 --- a/ts/packages/anchor/src/coder/borsh/instruction.ts +++ b/ts/packages/anchor/src/coder/borsh/instruction.ts @@ -306,13 +306,13 @@ class InstructionFormatter { const enumType = data[variant]; const camelCaseVariant = camelCase(variant); const enumVariant = variants.find((v) => camelCase(v.name) === camelCaseVariant); - const relevantFields = enumVariant?.fields; + const relevantFields = enumVariant?.fields as IdlField[] | undefined; const namedFields = Object.keys(enumType) .map((f) => { const fieldData = enumType[f]; const camelCaseField = camelCase(f); - const idlField = relevantFields?.find( - (v): v is IdlField => camelCase((v as IdlField).name) === camelCaseField + const idlField = relevantFields?.find( + v => camelCase((v as IdlField).name) === camelCaseField ); if (!idlField) { throw new Error("Unable to find variant"); From ede4635dac3e207e1955bcafe90fdc3e8ee0795d Mon Sep 17 00:00:00 2001 From: Kumar Abhinav Date: Sat, 6 Jan 2024 17:44:04 +0530 Subject: [PATCH 3/6] Remove unnecessary type assertion --- ts/packages/anchor/src/coder/borsh/instruction.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ts/packages/anchor/src/coder/borsh/instruction.ts b/ts/packages/anchor/src/coder/borsh/instruction.ts index 7bfa877246..41eaa78de4 100644 --- a/ts/packages/anchor/src/coder/borsh/instruction.ts +++ b/ts/packages/anchor/src/coder/borsh/instruction.ts @@ -312,7 +312,7 @@ class InstructionFormatter { const fieldData = enumType[f]; const camelCaseField = camelCase(f); const idlField = relevantFields?.find( - v => camelCase((v as IdlField).name) === camelCaseField + v => camelCase(v.name) === camelCaseField ); if (!idlField) { throw new Error("Unable to find variant"); From 5ac37b29b6ea6de5c4a1522f75d21c572bb2357b Mon Sep 17 00:00:00 2001 From: acheron Date: Tue, 9 Jan 2024 12:48:30 +0100 Subject: [PATCH 4/6] Fix formatting --- ts/packages/anchor/src/coder/borsh/instruction.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ts/packages/anchor/src/coder/borsh/instruction.ts b/ts/packages/anchor/src/coder/borsh/instruction.ts index 41eaa78de4..fcca263f19 100644 --- a/ts/packages/anchor/src/coder/borsh/instruction.ts +++ b/ts/packages/anchor/src/coder/borsh/instruction.ts @@ -305,14 +305,16 @@ class InstructionFormatter { const variant = Object.keys(data)[0]; const enumType = data[variant]; const camelCaseVariant = camelCase(variant); - const enumVariant = variants.find((v) => camelCase(v.name) === camelCaseVariant); + const enumVariant = variants.find( + (v) => camelCase(v.name) === camelCaseVariant + ); const relevantFields = enumVariant?.fields as IdlField[] | undefined; const namedFields = Object.keys(enumType) .map((f) => { const fieldData = enumType[f]; const camelCaseField = camelCase(f); const idlField = relevantFields?.find( - v => camelCase(v.name) === camelCaseField + (v) => camelCase(v.name) === camelCaseField ); if (!idlField) { throw new Error("Unable to find variant"); From 88ba82650d650f500a35c77c945d46bf1a47798c Mon Sep 17 00:00:00 2001 From: acheron Date: Tue, 9 Jan 2024 13:02:02 +0100 Subject: [PATCH 5/6] Remove unnecessary case conversion --- .../anchor/src/coder/borsh/instruction.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/ts/packages/anchor/src/coder/borsh/instruction.ts b/ts/packages/anchor/src/coder/borsh/instruction.ts index fcca263f19..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"; @@ -304,21 +305,21 @@ class InstructionFormatter { const variants = typeDef.type.variants; const variant = Object.keys(data)[0]; const enumType = data[variant]; - const camelCaseVariant = camelCase(variant); const enumVariant = variants.find( - (v) => camelCase(v.name) === camelCaseVariant + (v) => camelCase(v.name) === variant ); - const relevantFields = enumVariant?.fields as IdlField[] | undefined; + 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 camelCaseField = camelCase(f); - const idlField = relevantFields?.find( - (v) => camelCase(v.name) === camelCaseField - ); + 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 + ": " + From a439eb72ac9f564c54cd351c5a144c1b56f0570f Mon Sep 17 00:00:00 2001 From: acheron Date: Tue, 9 Jan 2024 13:05:39 +0100 Subject: [PATCH 6/6] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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