Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ts: Fix IDL type issues #1726

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ts/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
7 changes: 3 additions & 4 deletions ts/src/coder/borsh/idl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ export class IdlCoder {
field: { name?: string } & Pick<IdlField, "type">,
types?: IdlTypeDef[]
): Layout {
const fieldName =
field.name !== undefined ? camelCase(field.name) : undefined;
const fieldName = field.name;
switch (field.type) {
case "bool": {
return borsh.bool(fieldName);
Expand Down Expand Up @@ -124,8 +123,8 @@ export class IdlCoder {
});
return borsh.struct(fieldLayouts, name);
} else if (typeDef.type.kind === "enum") {
let variants = typeDef.type.variants.map((variant: IdlEnumVariant) => {
const name = camelCase(variant.name);
const variants = typeDef.type.variants.map((variant: IdlEnumVariant) => {
const name = variant.name;
if (variant.fields === undefined) {
return borsh.struct([], name);
}
Expand Down
106 changes: 94 additions & 12 deletions ts/src/program/namespace/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { PublicKey } from "@solana/web3.js";
import BN from "bn.js";
import { Idl } from "../../";
import {
IdlEnumVariant,
IdlField,
IdlInstruction,
IdlType,
Expand Down Expand Up @@ -105,6 +106,7 @@ type TypeMap = {
publicKey: PublicKey;
bool: boolean;
string: string;
bytes: Buffer;
} & {
[K in "u8" | "i8" | "u16" | "i16" | "u32" | "i32" | "f32" | "f64"]: number;
} & {
Expand Down Expand Up @@ -138,19 +140,93 @@ type ArgsTuple<A extends IdlField[], Defined> = {
: unknown;
} & unknown[];

type FieldsOfType<I extends IdlTypeDef> = NonNullable<
type StructDef<I extends IdlField[], Defined> = {
[F in I[number]["name"]]: DecodeType<
(I[number] & { name: F })["type"],
Defined
>;
};

type VariantDef<
T extends IdlEnumVariant,
Defined
> = T["fields"] extends IdlField[]
? StructDef<T["fields"], Defined>
: T["fields"] extends IdlType[]
? unknown // TODO: Tuple enum variants are not supported yet.
: Record<string, never>;

type XorVariantDefs<T extends Record<string, unknown>> = ValueOf<{
[K1 in keyof T]: {
[K2 in Exclude<keyof T, K1>]?: never;
} & { [K2 in K1]: T[K2] };
}>;
type VariantDefs<I extends IdlEnumVariant[], Defined> = {
[K in I[number]["name"]]: VariantDef<I[number] & { name: K }, Defined>;
};
type EnumDef<I extends IdlEnumVariant[], Defined> = XorVariantDefs<
VariantDefs<I, Defined>
>;

export type TypeDef<
I extends IdlTypeDef,
Defined
> = I["type"] extends IdlTypeDefTyStruct
? StructDef<I["type"]["fields"], Defined>
: I["type"] extends IdlTypeDefTyEnum
? EnumDef<I["type"]["variants"], Defined>
: never;

type ValueOf<T> = T[keyof T];
type FindTypeDeps<T extends IdlType> = T extends {
defined: string;
}
? T["defined"]
: T extends { option: { defined: string } }
? T["option"]["defined"]
: T extends { coption: { defined: string } }
? T["coption"]["defined"]
: never;
type FindStructDeps<I extends IdlTypeDefTyStruct> = FindTypeDeps<
I["fields"][number]["type"]
>;
type FindEnumDeps<I extends IdlTypeDefTyEnum> = NonNullable<
I["variants"][number]["fields"]
> extends infer T
? T extends IdlField[]
? FindTypeDeps<T[number]["type"]>
: T extends IdlType[]
? FindTypeDeps<T[number]>
: never
: never;
type FindUserDefinedDeps<I extends IdlTypeDef> =
I["type"] extends IdlTypeDefTyStruct
? I["type"]["fields"]
? FindStructDeps<I["type"]>
: I["type"] extends IdlTypeDefTyEnum
? I["type"]["variants"][number]["fields"]
: any[]
>[number];
? FindEnumDeps<I["type"]>
: never;
type FindUserDefined<T extends Record<string, IdlTypeDef>> = ValueOf<{
[K in keyof T]: FindUserDefinedDeps<T[K]>;
}>;
type UserDefinedDeps<T extends Record<string, IdlTypeDef>> = {
[K in FindUserDefined<T>]: FindUserDefinedDeps<T[K]>;
};

export type TypeDef<I extends IdlTypeDef, Defined> = {
[F in FieldsOfType<I>["name"]]: DecodeType<
(FieldsOfType<I> & { name: F })["type"],
Defined
>;
type UserDefinedDicionary<T extends Record<string, IdlTypeDef>, Defined> = {
[K in keyof Defined]: K extends keyof T
? Defined[K] extends keyof T
? TypeDef<
T[K],
UserDefinedDicionary<
T,
Pick<T, Defined[K]> & UserDefinedDeps<Pick<T, Defined[K]>>
>
>
: TypeDef<T[K], Record<string, never>>
: never;
};
type MapIdlTypeDefs<T extends IdlTypeDef[]> = {
[K in T[number]["name"]]: T[number] & { name: K };
};

type TypeDefDictionary<T extends IdlTypeDef[], Defined> = {
Expand All @@ -159,12 +235,18 @@ type TypeDefDictionary<T extends IdlTypeDef[], Defined> = {

export type IdlTypes<T extends Idl> = TypeDefDictionary<
NonNullable<T["types"]>,
Record<string, never>
UserDefinedDicionary<
MapIdlTypeDefs<NonNullable<T["types"]>>,
UserDefinedDeps<MapIdlTypeDefs<NonNullable<T["types"]>>>
>
>;

export type IdlAccounts<T extends Idl> = TypeDefDictionary<
NonNullable<T["accounts"]>,
Record<string, never>
UserDefinedDicionary<
MapIdlTypeDefs<NonNullable<T["types"]>>,
UserDefinedDeps<MapIdlTypeDefs<NonNullable<T["types"]>>>
>
>;

export type IdlErrorInfo<IDL extends Idl> = NonNullable<IDL["errors"]>[number];
12 changes: 0 additions & 12 deletions ts/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2227,13 +2227,6 @@ find-versions@^4.0.0:
dependencies:
semver-regex "^3.1.2"

find@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/find/-/find-0.3.0.tgz#4082e8fc8d8320f1a382b5e4f521b9bc50775cb8"
integrity sha512-iSd+O4OEYV/I36Zl8MdYJO0xD82wH528SaCieTVHhclgiYNe9y+yPKSwK+A7/WsmHL1EZ+pYUJBXWTL5qofksw==
dependencies:
traverse-chain "~0.1.0"

flat-cache@^3.0.4:
version "3.0.4"
resolved "https://registry.yarnpkg.com/flat-cache/-/flat-cache-3.0.4.tgz#61b0338302b2fe9f957dcc32fc2a87f1c3048b11"
Expand Down Expand Up @@ -4506,11 +4499,6 @@ tr46@~0.0.3:
resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a"
integrity sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=

traverse-chain@~0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/traverse-chain/-/traverse-chain-0.1.0.tgz#61dbc2d53b69ff6091a12a168fd7d433107e40f1"
integrity sha1-YdvC1Ttp/2CRoSoWj9fUMxB+QPE=

trim-newlines@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/trim-newlines/-/trim-newlines-3.0.1.tgz#260a5d962d8b752425b32f3a7db0dcacd176c144"
Expand Down