/
index.ts
242 lines (216 loc) · 6.29 KB
/
index.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import type {
Address,
BN,
Context as AnchorContext,
Program as AProgram,
} from "@project-serum/anchor";
import type {
Idl,
IdlAccountItem,
IdlAccounts,
IdlEvent,
IdlEventField,
IdlField,
IdlInstruction,
IdlType,
IdlTypeDef,
} from "@project-serum/anchor/dist/idl";
import type { ProgramAccount } from "@project-serum/anchor/dist/program/namespace";
import type {
AccountMeta,
Commitment,
PublicKey,
Signer,
Transaction,
TransactionInstruction,
TransactionSignature,
} from "@solana/web3.js";
import type EventEmitter from "eventemitter3";
type InstructionsParsed = Record<
string,
{
accounts: IdlAccountItem[];
args: Array<unknown>;
}
>;
export type ContextAccounts<A extends IdlAccountItem[]> = {
[K in A[number]["name"]]: A[number] & { name: K } extends IdlAccounts
? ContextAccounts<
NonNullable<(A[number] & { name: K } & IdlAccounts)["accounts"]>
>
: Address;
};
type Context<A extends IdlAccountItem[]> = Omit<AnchorContext, "accounts"> & {
accounts: ContextAccounts<A>;
};
type MakeInstructionsNamespace<
R extends InstructionsParsed,
Ret,
Mk extends { [M in keyof R]: unknown } = { [M in keyof R]: unknown }
> = {
[M in keyof R]: ((
...args: [...R[M]["args"], Context<R[M]["accounts"]>]
) => Ret) &
Mk[M];
};
type RpcNamespace<R extends InstructionsParsed> = MakeInstructionsNamespace<
R,
Promise<TransactionSignature>
>;
type InstructionNamespace<R extends InstructionsParsed> =
MakeInstructionsNamespace<
R,
TransactionInstruction,
{
[M in keyof R]: {
accounts: (ctx: ContextAccounts<R[M]["accounts"]>) => AccountMeta[];
};
}
>;
type TransactionNamespace<R extends InstructionsParsed> =
MakeInstructionsNamespace<R, Transaction>;
type StateNamespace<R extends InstructionsParsed, S> = {
address: () => Promise<PublicKey>;
rpc: RpcNamespace<R>;
fetch: () => Promise<S>;
instruction: InstructionNamespace<R>;
subscribe: (commitment?: Commitment) => EventEmitter;
unsubscribe: () => void;
};
type AccountsNamespace<A> = {
[K in keyof A]: {
fetch: (address: PublicKey) => Promise<A[K]>;
size: number;
all: (filter?: Buffer) => Promise<ProgramAccount<A[K]>[]>;
subscribe: (address: Address, commitment?: Commitment) => EventEmitter;
unsubscribe: (address: Address) => void;
createInstruction: (
signer: Signer,
sizeOverride?: number
) => Promise<TransactionInstruction>;
associated: (...args: PublicKey[]) => Promise<A[K]>;
associatedAddress: (...args: PublicKey[]) => Promise<PublicKey>;
};
};
type TypeMap = {
publicKey: PublicKey;
u64: BN;
i64: BN;
u128: BN;
i128: BN;
} & {
[K in "u8" | "i8" | "u16" | "i16" | "u32" | "i32"]: number;
};
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: { defined: keyof Defined } }
? Defined[T["vec"]["defined"]][]
: T extends { vec: keyof TypeMap }
? TypeMap[T["vec"]][]
: T extends { array: [idlType: keyof TypeMap, size: number] }
? TypeMap[T["array"][0]][]
: unknown;
type MakeArgs<A extends IdlField[], Defined> = {
[K in keyof A]: A[K] extends IdlField
? DecodeType<A[K]["type"], Defined>
: unknown;
};
type MakeInstructions<I extends IdlInstruction[], Defined> = {
[K in I[number]["name"]]: {
accounts: (I[number] & { name: K })["accounts"];
args: MakeArgs<(I[number] & { name: K })["args"], Defined> & unknown[];
};
};
export type AnchorProgram<
IDL extends Idl,
A,
Defined = AnchorDefined<IDL>,
RPCInstructions extends MakeInstructions<
IDL["instructions"],
Defined
> = MakeInstructions<IDL["instructions"], Defined>,
Methods extends MakeInstructions<
NonNullable<IDL["state"]>["methods"],
Defined
> = MakeInstructions<NonNullable<IDL["state"]>["methods"], Defined>
> = Omit<
AProgram,
"rpc" | "state" | "account" | "transaction" | "instruction"
> & {
rpc: RpcNamespace<RPCInstructions>;
state: StateNamespace<Methods, AnchorState<IDL, Defined>>;
account: AccountsNamespace<A>;
transaction: TransactionNamespace<RPCInstructions & Methods>;
instruction: InstructionNamespace<RPCInstructions & Methods>;
};
export type AnchorError<T extends Idl> = NonNullable<T["errors"]>[number];
type FieldsOfType<I extends IdlTypeDef> = NonNullable<
I["type"]["fields"]
>[number];
type AnchorTypeDef<I extends IdlTypeDef, Defined> = {
[F in FieldsOfType<I>["name"]]: DecodeType<
(FieldsOfType<I> & { name: F })["type"],
Defined
>;
};
type AnchorTypeDefs<T extends IdlTypeDef[], Defined> = {
[K in T[number]["name"]]: AnchorTypeDef<T[number] & { name: K }, Defined>;
};
export type AnchorDefined<
T extends Idl,
D = Record<string, never>
> = AnchorTypeDefs<NonNullable<T["types"]>, D>;
export type AnchorAccounts<T extends Idl, Defined> = AnchorTypeDefs<
NonNullable<T["accounts"]>,
Defined
>;
export type AnchorState<T extends Idl, Defined> = AnchorTypeDef<
NonNullable<T["state"]>["struct"],
Defined
>;
export type AnchorTypes<
T extends Idl,
AccountMap = Record<string, never>,
D = Record<string, never>,
DEF = AnchorDefined<T, D>
> = {
Defined: DEF;
Accounts: AnchorAccounts<T, DEF>;
State: AnchorState<T, DEF>;
Error: AnchorError<T>;
Program: AnchorProgram<T, AccountMap, DEF>;
Instructions: MakeInstructions<T["instructions"], DEF>;
Methods: MakeInstructions<NonNullable<T["state"]>["methods"], DEF>;
Events: AnchorEvents<NonNullable<T["events"]>[number], DEF>;
};
type ErrorMap<T extends Idl> = {
[K in AnchorError<T>["name"]]: AnchorError<T> & { name: K };
};
/**
* Generates the error mapping
* @param idl
* @returns
*/
export const generateErrorMap = <T extends Idl>(idl: T): ErrorMap<T> => {
return (idl.errors?.reduce((acc, err) => {
return {
...acc,
[err.name]: err,
};
}, {}) ?? {}) as ErrorMap<T>;
};
type AnchorEvent<T extends IdlEventField, Defined> = {
[N in T["name"]]: DecodeType<(T & { name: N })["type"], Defined>;
};
type AnchorEvents<T extends IdlEvent, Defined> = {
[K in T["name"]]: {
name: K;
data: AnchorEvent<(T & { name: K })["fields"][number], Defined>;
};
};