forked from coral-xyz/anchor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.ts
156 lines (136 loc) · 3.82 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { PublicKey } from "@solana/web3.js";
import BN from "bn.js";
import { Idl } from "../../";
import {
IdlField,
IdlInstruction,
IdlType,
IdlTypeDef,
IdlTypeDefTyEnum,
IdlTypeDefTyStruct,
} from "../../idl";
import { Accounts, Context } from "../context";
/**
* All instructions for an IDL.
*/
export type AllInstructions<IDL extends Idl> = IDL["instructions"][number];
/**
* Returns a type of instruction name to the IdlInstruction.
*/
export type InstructionMap<I extends IdlInstruction> = {
[K in I["name"]]: I & { name: K };
};
/**
* Returns a type of instruction name to the IdlInstruction.
*/
export type AllInstructionsMap<IDL extends Idl> = InstructionMap<
AllInstructions<IDL>
>;
/**
* All accounts for an IDL.
*/
export type AllAccounts<IDL extends Idl> = IDL["accounts"] extends undefined
? IdlTypeDef
: NonNullable<IDL["accounts"]>[number];
/**
* Returns a type of instruction name to the IdlInstruction.
*/
export type AccountMap<I extends IdlTypeDef> = {
[K in I["name"]]: I & { name: K };
};
/**
* Returns a type of instruction name to the IdlInstruction.
*/
export type AllAccountsMap<IDL extends Idl> = AccountMap<AllAccounts<IDL>>;
export type MakeInstructionsNamespace<
IDL extends Idl,
I extends IdlInstruction,
Ret,
Mk extends { [M in keyof InstructionMap<I>]: unknown } = {
[M in keyof InstructionMap<I>]: unknown;
}
> = {
[M in keyof InstructionMap<I>]: InstructionContextFn<
IDL,
InstructionMap<I>[M],
Ret
> &
Mk[M];
};
export type MakeMethodsNamespace<IDL extends Idl, I extends IdlInstruction> = {
[M in keyof InstructionMap<I>]: MethodsFn<IDL, InstructionMap<I>[M], any>;
};
export type InstructionContextFn<
IDL extends Idl,
I extends AllInstructions<IDL>,
Ret
> = (...args: InstructionContextFnArgs<IDL, I>) => Ret;
export type InstructionContextFnArgs<
IDL extends Idl,
I extends IDL["instructions"][number]
> = [
...ArgsTuple<I["args"], IdlTypes<IDL>>,
Context<Accounts<I["accounts"][number]>>
];
export type MethodsFn<
IDL extends Idl,
I extends IDL["instructions"][number],
Ret
> = (...args: ArgsTuple<I["args"], IdlTypes<IDL>>) => Ret;
type TypeMap = {
publicKey: PublicKey;
bool: boolean;
string: string;
} & {
[K in "u8" | "i8" | "u16" | "i16" | "u32" | "i32"]: number;
} &
{
[K in "u64" | "i64" | "u128" | "i128"]: BN;
};
export type DecodeType<T extends IdlType, Defined> = T extends keyof TypeMap
? TypeMap[T]
: T extends { defined: keyof Defined }
? Defined[T["defined"]]
: T extends { option: { defined: keyof Defined } }
? Defined[T["option"]["defined"]] | null
: T extends { option: keyof TypeMap }
? TypeMap[T["option"]] | null
: T extends { vec: keyof TypeMap }
? TypeMap[T["vec"]][]
: T extends { array: [defined: keyof TypeMap, size: number] }
? TypeMap[T["array"][0]][]
: unknown;
/**
* Tuple of arguments.
*/
type ArgsTuple<A extends IdlField[], Defined> = {
[K in keyof A]: A[K] extends IdlField
? DecodeType<A[K]["type"], Defined>
: unknown;
} &
unknown[];
type FieldsOfType<I extends IdlTypeDef> = NonNullable<
I["type"] extends IdlTypeDefTyStruct
? I["type"]["fields"]
: I["type"] extends IdlTypeDefTyEnum
? I["type"]["variants"][number]["fields"]
: any[]
>[number];
export type TypeDef<I extends IdlTypeDef, Defined> = {
[F in FieldsOfType<I>["name"]]: DecodeType<
(FieldsOfType<I> & { name: F })["type"],
Defined
>;
};
type TypeDefDictionary<T extends IdlTypeDef[], Defined> = {
[K in T[number]["name"]]: TypeDef<T[number] & { name: K }, Defined>;
};
export type IdlTypes<T extends Idl> = TypeDefDictionary<
NonNullable<T["types"]>,
Record<string, never>
>;
export type IdlAccounts<T extends Idl> = TypeDefDictionary<
NonNullable<T["accounts"]>,
Record<string, never>
>;
export type IdlErrorInfo<IDL extends Idl> = NonNullable<IDL["errors"]>[number];