ts: Better public key error msgs (#1098)
This commit is contained in:
parent
8467491a56
commit
517838e494
|
@ -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: 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))
|
* 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
|
## [0.18.2] - 2021-11-14
|
||||||
|
|
||||||
|
|
|
@ -58,8 +58,10 @@ export function translateAddress(address: Address): PublicKey {
|
||||||
if (typeof address === "string") {
|
if (typeof address === "string") {
|
||||||
const pk = new PublicKey(address);
|
const pk = new PublicKey(address);
|
||||||
return pk;
|
return pk;
|
||||||
} else {
|
} else if (address.constructor.prototype.constructor.name === "PublicKey") {
|
||||||
return address;
|
return address;
|
||||||
|
} else {
|
||||||
|
throw new Error("Given address is not a PublicKey nor a string.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -63,7 +63,11 @@ export default class InstructionNamespaceFactory {
|
||||||
|
|
||||||
// Utility fn for ordering the accounts for this instruction.
|
// Utility fn for ordering the accounts for this instruction.
|
||||||
ix["accounts"] = (accs: Accounts<I["accounts"][number]> | undefined) => {
|
ix["accounts"] = (accs: Accounts<I["accounts"][number]> | undefined) => {
|
||||||
return InstructionNamespaceFactory.accountsArray(accs, idlIx.accounts);
|
return InstructionNamespaceFactory.accountsArray(
|
||||||
|
accs,
|
||||||
|
idlIx.accounts,
|
||||||
|
idlIx.name
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
return ix;
|
return ix;
|
||||||
|
@ -71,7 +75,8 @@ export default class InstructionNamespaceFactory {
|
||||||
|
|
||||||
public static accountsArray(
|
public static accountsArray(
|
||||||
ctx: Accounts | undefined,
|
ctx: Accounts | undefined,
|
||||||
accounts: readonly IdlAccountItem[]
|
accounts: readonly IdlAccountItem[],
|
||||||
|
ixName?: string
|
||||||
): AccountMeta[] {
|
): AccountMeta[] {
|
||||||
if (!ctx) {
|
if (!ctx) {
|
||||||
return [];
|
return [];
|
||||||
|
@ -86,12 +91,25 @@ export default class InstructionNamespaceFactory {
|
||||||
const rpcAccs = ctx[acc.name] as Accounts;
|
const rpcAccs = ctx[acc.name] as Accounts;
|
||||||
return InstructionNamespaceFactory.accountsArray(
|
return InstructionNamespaceFactory.accountsArray(
|
||||||
rpcAccs,
|
rpcAccs,
|
||||||
(acc as IdlAccounts).accounts
|
(acc as IdlAccounts).accounts,
|
||||||
|
ixName
|
||||||
).flat();
|
).flat();
|
||||||
} else {
|
} else {
|
||||||
const account: IdlAccount = acc as IdlAccount;
|
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 {
|
return {
|
||||||
pubkey: translateAddress(ctx[acc.name] as Address),
|
pubkey,
|
||||||
isWritable: account.isMut,
|
isWritable: account.isMut,
|
||||||
isSigner: account.isSigner,
|
isSigner: account.isSigner,
|
||||||
};
|
};
|
||||||
|
|
|
@ -116,7 +116,11 @@ export class StateClient<IDL extends Idl> {
|
||||||
ixItem["accounts"] = (accounts) => {
|
ixItem["accounts"] = (accounts) => {
|
||||||
const keys = stateInstructionKeys(programId, provider, m, accounts);
|
const keys = stateInstructionKeys(programId, provider, m, accounts);
|
||||||
return keys.concat(
|
return keys.concat(
|
||||||
InstructionNamespaceFactory.accountsArray(accounts, m.accounts)
|
InstructionNamespaceFactory.accountsArray(
|
||||||
|
accounts,
|
||||||
|
m.accounts,
|
||||||
|
m.name
|
||||||
|
)
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
// Build transaction method.
|
// Build transaction method.
|
||||||
|
|
Loading…
Reference in New Issue