ts: fix type for accounts field in Idl (#1542)

This commit is contained in:
Krešimir Klas 2022-03-23 02:43:51 +01:00 committed by GitHub
parent 811a4cb318
commit 946fa23538
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 4 deletions

View File

@ -7,7 +7,7 @@ export type Idl = {
name: string; name: string;
instructions: IdlInstruction[]; instructions: IdlInstruction[];
state?: IdlState; state?: IdlState;
accounts?: IdlTypeDef[]; accounts?: IdlAccountDef[];
types?: IdlTypeDef[]; types?: IdlTypeDef[];
events?: IdlEvent[]; events?: IdlEvent[];
errors?: IdlErrorCode[]; errors?: IdlErrorCode[];
@ -79,6 +79,11 @@ export type IdlTypeDef = {
type: IdlTypeDefTy; type: IdlTypeDefTy;
}; };
export type IdlAccountDef = {
name: string;
type: IdlTypeDefTyStruct;
};
export type IdlTypeDefTyStruct = { export type IdlTypeDefTyStruct = {
kind: "struct"; kind: "struct";
fields: IdlTypeDefStruct; fields: IdlTypeDefStruct;

View File

@ -10,7 +10,7 @@ import {
AccountInfo, AccountInfo,
} from "@solana/web3.js"; } from "@solana/web3.js";
import Provider, { getProvider } from "../../provider.js"; import Provider, { getProvider } from "../../provider.js";
import { Idl, IdlTypeDef } from "../../idl.js"; import { Idl, IdlAccountDef } from "../../idl.js";
import { Coder, BorshCoder } from "../../coder/index.js"; import { Coder, BorshCoder } from "../../coder/index.js";
import { Subscription, Address, translateAddress } from "../common.js"; import { Subscription, Address, translateAddress } from "../common.js";
import { AllAccountsMap, IdlTypes, TypeDef } from "./types.js"; import { AllAccountsMap, IdlTypes, TypeDef } from "./types.js";
@ -42,7 +42,7 @@ export default class AccountFactory {
} }
type NullableIdlAccount<IDL extends Idl> = IDL["accounts"] extends undefined type NullableIdlAccount<IDL extends Idl> = IDL["accounts"] extends undefined
? IdlTypeDef ? IdlAccountDef
: NonNullable<IDL["accounts"]>[number]; : NonNullable<IDL["accounts"]>[number];
/** /**
@ -72,7 +72,7 @@ export type AccountNamespace<IDL extends Idl = Idl> = {
export class AccountClient< export class AccountClient<
IDL extends Idl = Idl, IDL extends Idl = Idl,
A extends NullableIdlAccount<IDL> = IDL["accounts"] extends undefined A extends NullableIdlAccount<IDL> = IDL["accounts"] extends undefined
? IdlTypeDef ? IdlAccountDef
: NonNullable<IDL["accounts"]>[number], : NonNullable<IDL["accounts"]>[number],
T = TypeDef<A, IdlTypes<IDL>> T = TypeDef<A, IdlTypes<IDL>>
> { > {