Browse Source

ts: Scope fields when formatting nested instruction accounts (#376)

Armani Ferrante 4 years ago
parent
commit
50b000542c
2 changed files with 20 additions and 7 deletions
  1. 19 3
      ts/src/coder/instruction.ts
  2. 1 4
      ts/src/error.ts

+ 19 - 3
ts/src/coder/instruction.ts

@@ -338,18 +338,34 @@ class InstructionFormatter {
     }
   }
 
-  private static flattenIdlAccounts(accounts: IdlAccountItem[]): IdlAccount[] {
+  private static flattenIdlAccounts(
+    accounts: IdlAccountItem[],
+    prefix?: string
+  ): IdlAccount[] {
     // @ts-ignore
     return accounts
       .map((account) => {
+        const accName = sentenceCase(account.name);
         // @ts-ignore
         if (account.accounts) {
+          const newPrefix = prefix ? `${prefix} > ${accName}` : accName;
           // @ts-ignore
-          return InstructionFormatter.flattenIdlAccounts(account.accounts);
+          return InstructionFormatter.flattenIdlAccounts(
+            account.accounts,
+            newPrefix
+          );
         } else {
-          return account;
+          return {
+            ...account,
+            name: prefix ? `${prefix} > ${accName}` : accName,
+          };
         }
       })
       .flat();
   }
 }
+
+function sentenceCase(field: string): string {
+  const result = field.replace(/([A-Z])/g, " $1");
+  return result.charAt(0).toUpperCase() + result.slice(1);
+}

+ 1 - 4
ts/src/error.ts

@@ -131,10 +131,7 @@ const LangErrorMessage = new Map([
     LangErrorCode.ConstraintAssociatedInit,
     "An associated init constraint was violated",
   ],
-  [
-    LangErrorCode.ConstraintClose,
-    "A close constraint was violated"
-  ],
+  [LangErrorCode.ConstraintClose, "A close constraint was violated"],
 
   // Accounts.
   [