From 0808a3ff6c05e00527e0451f5fe5b5e9d38fedf2 Mon Sep 17 00:00:00 2001 From: Paul Schaaf Date: Sat, 4 Dec 2021 03:24:48 +0100 Subject: [PATCH 1/4] ts: better public key error msgs --- ts/src/program/common.ts | 8 ++------ ts/src/program/namespace/instruction.ts | 26 +++++++++++++++++++++---- ts/src/program/namespace/state.ts | 6 +++++- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/ts/src/program/common.ts b/ts/src/program/common.ts index 5088072aba..0dec5df0f9 100644 --- a/ts/src/program/common.ts +++ b/ts/src/program/common.ts @@ -55,12 +55,8 @@ export function validateAccounts( // Translates an address to a Pubkey. export function translateAddress(address: Address): PublicKey { - if (typeof address === "string") { - const pk = new PublicKey(address); - return pk; - } else { - return address; - } + // this will error if address is not a string or public key + return new PublicKey(address.toString()); } /** diff --git a/ts/src/program/namespace/instruction.ts b/ts/src/program/namespace/instruction.ts index 0e5b4595c2..34929f84fa 100644 --- a/ts/src/program/namespace/instruction.ts +++ b/ts/src/program/namespace/instruction.ts @@ -63,7 +63,11 @@ export default class InstructionNamespaceFactory { // Utility fn for ordering the accounts for this instruction. ix["accounts"] = (accs: Accounts | undefined) => { - return InstructionNamespaceFactory.accountsArray(accs, idlIx.accounts); + return InstructionNamespaceFactory.accountsArray( + accs, + idlIx.accounts, + idlIx.name + ); }; return ix; @@ -71,7 +75,8 @@ export default class InstructionNamespaceFactory { public static accountsArray( ctx: Accounts | undefined, - accounts: readonly IdlAccountItem[] + accounts: readonly IdlAccountItem[], + ixName?: string ): AccountMeta[] { if (!ctx) { return []; @@ -86,12 +91,25 @@ export default class InstructionNamespaceFactory { const rpcAccs = ctx[acc.name] as Accounts; return InstructionNamespaceFactory.accountsArray( rpcAccs, - (acc as IdlAccounts).accounts + (acc as IdlAccounts).accounts, + ixName ).flat(); } else { const account: IdlAccount = acc as IdlAccount; + let pubkey; + try { + pubkey = translateAddress(ctx[acc.name] as Address); + } catch (err) { + throw new Error( + `Wrong input type for account "${ + acc.name + }" in the instruction accounts object${ + ixName !== undefined ? ' for instruction "' + ixName + '"' : "" + }. Expected PublicKey or string.` + ); + } return { - pubkey: translateAddress(ctx[acc.name] as Address), + pubkey, isWritable: account.isMut, isSigner: account.isSigner, }; diff --git a/ts/src/program/namespace/state.ts b/ts/src/program/namespace/state.ts index 944c42411a..bcdac6b7fa 100644 --- a/ts/src/program/namespace/state.ts +++ b/ts/src/program/namespace/state.ts @@ -116,7 +116,11 @@ export class StateClient { ixItem["accounts"] = (accounts) => { const keys = stateInstructionKeys(programId, provider, m, accounts); return keys.concat( - InstructionNamespaceFactory.accountsArray(accounts, m.accounts) + InstructionNamespaceFactory.accountsArray( + accounts, + m.accounts, + m.name + ) ); }; // Build transaction method. From 8c6d32ecba5057ae81bdd9c70686a52587d0df65 Mon Sep 17 00:00:00 2001 From: Paul Schaaf Date: Sat, 4 Dec 2021 03:32:41 +0100 Subject: [PATCH 2/4] docs: changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index edd2809af2..53c664b2da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ incremented for features. * lang: Add `ErrorCode::AccountNotInitialized` error to separate the situation when the account has the wrong owner from when it does not exist (#[1024](https://github.com/project-serum/anchor/pull/1024)) * lang: Called instructions now log their name by default. This can be turned off with the `no-log-ix-name` flag ([#1057](https://github.com/project-serum/anchor/pull/1057)) +* ts: Add better error msgs in the ts client if something wrong (i.e. not a pubkey or a string) is passed in as an account in an instruction accounts object ([#1098](https://github.com/project-serum/anchor/pull/1098)) ## [0.18.2] - 2021-11-14 From fc960a5b10210337fb8b5ea141822499f3dcbad1 Mon Sep 17 00:00:00 2001 From: Paul Schaaf Date: Sat, 4 Dec 2021 16:58:13 +0100 Subject: [PATCH 3/4] ts: compare prototype name instead of using instanceof --- ts/src/program/common.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/ts/src/program/common.ts b/ts/src/program/common.ts index 0dec5df0f9..7fd366efbe 100644 --- a/ts/src/program/common.ts +++ b/ts/src/program/common.ts @@ -1,5 +1,6 @@ import EventEmitter from "eventemitter3"; import { PublicKey } from "@solana/web3.js"; +import { web3 } from "../index"; import { Idl, IdlInstruction, IdlAccountItem, IdlStateMethod } from "../idl.js"; import { Accounts } from "./context.js"; @@ -55,8 +56,14 @@ export function validateAccounts( // Translates an address to a Pubkey. export function translateAddress(address: Address): PublicKey { - // this will error if address is not a string or public key - return new PublicKey(address.toString()); + if (typeof address === "string") { + const pk = new PublicKey(address); + return pk; + } else if (address.constructor.prototype.constructor.name === "PublicKey") { + return address; + } else { + throw new Error("Given address is not a PublicKey nor a string."); + } } /** From 8a608aa156313c303c75f6d947ef7a51fcee2456 Mon Sep 17 00:00:00 2001 From: Paul Schaaf Date: Sat, 4 Dec 2021 19:45:43 +0100 Subject: [PATCH 4/4] ts: remove unused import --- ts/src/program/common.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/ts/src/program/common.ts b/ts/src/program/common.ts index 7fd366efbe..5fd24e1968 100644 --- a/ts/src/program/common.ts +++ b/ts/src/program/common.ts @@ -1,6 +1,5 @@ import EventEmitter from "eventemitter3"; import { PublicKey } from "@solana/web3.js"; -import { web3 } from "../index"; import { Idl, IdlInstruction, IdlAccountItem, IdlStateMethod } from "../idl.js"; import { Accounts } from "./context.js";