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

This commit is contained in:
Armani Ferrante 2021-06-10 21:39:50 -07:00 committed by GitHub
parent 2bcd44ded3
commit 50b000542c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 7 deletions

View File

@ -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);
}

View File

@ -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.
[