Przeglądaj źródła

ts: Fix formatting enums (#2763)

Co-authored-by: acheron <acheroncrypto@gmail.com>
0xabhinav 1 rok temu
rodzic
commit
211982affc
2 zmienionych plików z 14 dodań i 5 usunięć
  1. 1 0
      CHANGELOG.md
  2. 13 5
      ts/packages/anchor/src/coder/borsh/instruction.ts

+ 1 - 0
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
 

+ 13 - 5
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: (<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 +
                 ": " +