Skip to content

Commit

Permalink
ts: Fix formatting enums (#2763)
Browse files Browse the repository at this point in the history
Co-authored-by: acheron <acheroncrypto@gmail.com>
  • Loading branch information
0xabhinav and acheroncrypto committed Jan 9, 2024
1 parent 7cbdff6 commit 211982a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 13 additions & 5 deletions ts/packages/anchor/src/coder/borsh/instruction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
IdlTypeOption,
IdlTypeDefined,
IdlAccounts,
IdlEnumFieldsNamed,
} from "../../idl.js";
import { IdlCoder } from "./idl.js";
import { InstructionCoder } from "../index.js";
Expand Down Expand Up @@ -232,7 +233,8 @@ class InstructionFormatter {
.map((d: IdlField) =>
this.formatIdlData(
{ name: "", type: (<IdlTypeVec>idlField.type).vec },
d
d,
types
)
)
.join(", ") +
Expand Down Expand Up @@ -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 +
": " +
Expand Down

1 comment on commit 211982a

@vercel
Copy link

@vercel vercel bot commented on 211982a Jan 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

anchor-docs – ./

anchor-docs-git-master-200ms.vercel.app
anchor-docs-200ms.vercel.app
anchor-lang.com
www.anchor-lang.com

Please sign in to comment.