-
Notifications
You must be signed in to change notification settings - Fork 33
/
types.ts
364 lines (315 loc) · 8.45 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Currently a limited version of the types found inside https://github.com/project-serum/anchor/blob/master/ts/src/idl.ts
// Will be extended to include the full spec eventually. At this point only cases actually encountered in contracts were
// addressed
import {
BeetExports,
BeetTypeMapKey,
SupportedTypeDefinition,
} from '@metaplex-foundation/beet'
import {
BeetSolanaExports,
BeetSolanaTypeMapKey,
} from '@metaplex-foundation/beet-solana'
import { SerdePackage } from './serdes'
import { strict as assert } from 'assert'
// -----------------
// Config
// -----------------
export type TypeAliases = Record<string, PrimitiveTypeKey>
/**
* Key: account name for which to customize de/serializer
* Value: path to module from project root providing `serialize` and/or
* `deserialize` methods
*/
export type Serializers = Record<string, string>
// -----------------
// IDL
// -----------------
export type IdlField = {
name: string
type: IdlType
attrs?: string[]
}
export const IDL_FIELD_ATTR_PADDING = 'padding'
export type IdlInstructionAccount = {
name: string
isMut: boolean
isSigner: boolean
desc?: string
optional?: boolean
}
export type IdlType =
| BeetTypeMapKey
| 'publicKey'
| IdlTypeDefined
| IdlTypeOption
| IdlTypeVec
| IdlTypeArray
| IdlTypeEnum
| IdlTypeDataEnum
| IdlTypeTuple
| IdlTypeMap
// User defined type.
export type IdlTypeDefined = {
defined: string
}
export type IdlTypeOption = {
option: IdlType
}
export type IdlTypeVec = {
vec: IdlType
}
export type IdlTypeArray = {
array: [idlType: IdlType, size: number]
}
// -----------------
// Enums
// -----------------
export type IdlEnumVariant = {
name: string
}
export type IdlDataEnumVariant =
| IdlDataEnumVariantWithNamedFields
| IdlDataEnumVariantWithUnnamedFields
// Rust allows mixing data variants with scalar variants
| IdlEnumVariant
export type IdlDataEnumVariantWithNamedFields = {
name: string
fields: IdlField[]
}
export type IdlDataEnumVariantWithUnnamedFields = {
name: string
fields: IdlType[]
}
export type IdlTypeEnum = IdlTypeScalarEnum | IdlTypeDataEnum
export type IdlTypeScalarEnum = {
kind: 'enum'
name?: string
variants: IdlEnumVariant[]
}
export type IdlTypeDataEnum = {
kind: 'enum'
name?: string
variants: IdlDataEnumVariant[]
}
export type IdlTypeTuple = {
tuple: IdlType[]
}
// -----------------
// Maps
// -----------------
export type IdlTypeMap = IdlTypeHashMap | IdlTypeBTreeMap
export type IdlTypeHashMap = {
hashMap: [IdlType, IdlType]
}
export type IdlTypeBTreeMap = {
bTreeMap: [IdlType, IdlType]
}
// -----------------
// Defined
// -----------------
export type IdlDefinedType = {
kind: 'struct' | 'enum'
fields: IdlField[]
}
export type IdlDefinedTypeDefinition = {
name: string
type: IdlDefinedType | IdlTypeEnum | IdlTypeDataEnum
}
// -----------------
// Instruction
// -----------------
export type IdlInstructionArg = {
name: string
type: IdlType
}
export type IdlInstruction = {
name: string
accounts: IdlInstructionAccount[]
args: IdlInstructionArg[]
}
// -----------------
// Account
// -----------------
export type IdlAccountType = {
kind: 'struct' | 'enum'
fields: IdlField[]
}
export type IdlAccount = {
name: string
type: IdlAccountType
}
export type IdlError = {
code: number
name: string
msg?: string
}
export type Idl = {
version: string
name: string
instructions: IdlInstruction[]
accounts?: IdlAccount[]
errors?: IdlError[]
types?: IdlDefinedTypeDefinition[]
metadata: {
address: string
}
}
// -----------------
// Shank Idl Extensions
// -----------------
export type ShankIdl = Idl & {
instructions: ShankIdlInstruction[]
metadata: ShankMetadata
}
export type ShankIdlInstruction = IdlInstruction & {
accounts: IdlInstructionAccountWithDesc[]
discriminant: {
type: IdlType
value: number
}
}
export type IdlInstructionAccountWithDesc = IdlInstructionAccount & {
desc: string
}
export type ShankMetadata = Idl['metadata'] & { origin: 'shank' }
// -----------------
// De/Serializers + Extensions
// -----------------
export type PrimitiveTypeKey = BeetTypeMapKey | BeetSolanaTypeMapKey
export type PrimaryType = SupportedTypeDefinition & {
beet: BeetExports | BeetSolanaExports
}
export type PrimaryTypeMap = Record<PrimitiveTypeKey, PrimaryType>
export type ProcessedSerde = {
name: string
sourcePack: SerdePackage
type: string
inner?: ProcessedSerde
}
export type TypeMappedSerdeField = {
name: string
type: string
}
// -----------------
// Resolvers
// -----------------
export type ResolveFieldType = (
typeName: string
) => IdlAccountType | IdlTypeEnum | null
// -----------------
// Guards
// -----------------
export function isIdlTypeOption(ty: IdlType): ty is IdlTypeOption {
return (ty as IdlTypeOption).option != null
}
export function isIdlTypeVec(ty: IdlType): ty is IdlTypeVec {
return (ty as IdlTypeVec).vec != null
}
export function isIdlTypeArray(ty: IdlType): ty is IdlTypeArray {
return (ty as IdlTypeArray).array != null
}
export function asIdlTypeArray(ty: IdlType): IdlTypeArray {
assert(isIdlTypeArray(ty))
return ty
}
export function isIdlTypeDefined(ty: IdlType): ty is IdlTypeDefined {
return (ty as IdlTypeDefined).defined != null
}
export function isIdlTypeEnum(
ty: IdlType | IdlDefinedType | IdlTypeEnum
): ty is IdlTypeEnum {
return (ty as IdlTypeEnum).variants != null
}
// -----------------
// Enums
// -----------------
export function isIdlTypeDataEnum(
ty: IdlType | IdlDefinedType | IdlTypeEnum
): ty is IdlTypeDataEnum {
const dataEnum = ty as IdlTypeDataEnum
return (
dataEnum.variants != null &&
dataEnum.variants.length > 0 &&
// if only one variant has data then we have to treat the entire enum as a data enum
// since we can no longer represent it as a TypeScript enum
dataEnum.variants.some(isDataEnumVariant)
)
}
export function isIdlTypeScalarEnum(
ty: IdlType | IdlDefinedType | IdlTypeEnum
): ty is IdlTypeScalarEnum {
return isIdlTypeEnum(ty) && !isIdlTypeDataEnum(ty)
}
export function isDataEnumVariant(
ty: IdlDataEnumVariant
): ty is
| IdlDataEnumVariantWithNamedFields
| IdlDataEnumVariantWithUnnamedFields {
return (
(
ty as
| IdlDataEnumVariantWithNamedFields
| IdlDataEnumVariantWithUnnamedFields
).fields != null
)
}
export function isDataEnumVariantWithNamedFields(
ty: IdlDataEnumVariant
): ty is IdlDataEnumVariantWithNamedFields {
return (
isDataEnumVariant(ty) &&
(ty as IdlDataEnumVariantWithNamedFields).fields[0].name != null
)
}
export function isDataEnumVariantWithUnnamedFields(
ty: IdlDataEnumVariant
): ty is IdlDataEnumVariantWithUnnamedFields {
return !isDataEnumVariantWithNamedFields(ty)
}
export function isIdlTypeTuple(ty: IdlType): ty is IdlTypeTuple {
return (ty as IdlTypeTuple).tuple != null
}
export function isIdlTypeHashMap(ty: IdlType): ty is IdlTypeHashMap {
return (ty as IdlTypeHashMap).hashMap != null
}
export function isIdlTypeBTreeMap(ty: IdlType): ty is IdlTypeBTreeMap {
return (ty as IdlTypeBTreeMap).bTreeMap != null
}
export function isIdlTypeMap(ty: IdlType): ty is IdlTypeMap {
return isIdlTypeHashMap(ty) || isIdlTypeBTreeMap(ty)
}
export function isIdlDefinedType(
ty: IdlType | IdlDefinedType
): ty is IdlDefinedType {
return (ty as IdlDefinedType).fields != null
}
export function isShankIdl(ty: Idl): ty is ShankIdl {
return (ty as ShankIdl).metadata?.origin === 'shank'
}
export function isShankIdlInstruction(
ty: IdlInstruction
): ty is ShankIdlInstruction {
return typeof (ty as ShankIdlInstruction).discriminant === 'object'
}
export function isIdlInstructionAccountWithDesc(
ty: IdlInstructionAccount
): ty is IdlInstructionAccountWithDesc {
return typeof (ty as IdlInstructionAccountWithDesc).desc === 'string'
}
export function hasPaddingAttr(field: IdlField): boolean {
return field.attrs != null && field.attrs.includes(IDL_FIELD_ATTR_PADDING)
}
// -----------------
// Packages
// -----------------
export const BEET_PACKAGE = '@metaplex-foundation/beet'
export const BEET_SOLANA_PACKAGE = '@metaplex-foundation/beet-solana'
export const SOLANA_WEB3_PACKAGE = '@solana/web3.js'
export const SOLANA_SPL_TOKEN_PACKAGE = '@solana/spl-token'
export const BEET_EXPORT_NAME = 'beet'
export const BEET_SOLANA_EXPORT_NAME = 'beetSolana'
export const SOLANA_WEB3_EXPORT_NAME = 'web3'
export const SOLANA_SPL_TOKEN_EXPORT_NAME = 'splToken'
export const PROGRAM_ID_PACKAGE = '<program-id>'
export const PROGRAM_ID_EXPORT_NAME = '<program-id-export>'