@sentio/sdk 2.51.0 → 2.52.0-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,14 @@ import path from 'path'
2
2
  import fs from 'fs'
3
3
  import chalk from 'chalk'
4
4
  import { Idl } from '@coral-xyz/anchor'
5
- import { IdlField, IdlInstruction, IdlType, IdlInstructionAccountItem } from '@coral-xyz/anchor/dist/esm/idl.js'
5
+ import {
6
+ IdlField,
7
+ IdlInstruction,
8
+ IdlType,
9
+ IdlInstructionAccountItem,
10
+ IdlDefinedFields,
11
+ IdlTypeDef
12
+ } from '@coral-xyz/anchor/dist/esm/idl.js'
6
13
 
7
14
  export function codegen(abisDir: string, targetPath = path.join('src', 'types', 'solana'), genExample = false) {
8
15
  if (!fs.existsSync(abisDir)) {
@@ -33,12 +40,14 @@ export function codegen(abisDir: string, targetPath = path.join('src', 'types',
33
40
  function codeGenSolanaIdlProcessor(idlObj: Idl): string {
34
41
  const idlName = idlObj.metadata.name
35
42
  const idlNamePascalCase = toPascalCase(idlName)
36
- const instructions: any[] = idlObj.instructions
43
+ const instructions = idlObj.instructions
37
44
  return `import { BorshInstructionCoder, Instruction, Idl } from '@sentio/sdk/solana'
38
45
  import { SolanaBaseProcessor, SolanaContext, SolanaBindOptions } from "@sentio/sdk/solana"
39
46
  import { ${idlName}_idl } from "./${idlName}.js"
40
47
  import { PublicKey } from '@solana/web3.js'
41
48
 
49
+ ${idlObj.types?.map((def) => codeGenType(def)).join('')}
50
+
42
51
  export class ${idlNamePascalCase}Processor extends SolanaBaseProcessor {
43
52
  static DEFAULT_OPTIONS = {
44
53
  name: '${idlNamePascalCase}',
@@ -54,6 +63,49 @@ export class ${idlNamePascalCase}Processor extends SolanaBaseProcessor {
54
63
  `
55
64
  }
56
65
 
66
+ function codeGenDefinedFields(fields: IdlDefinedFields): string[] {
67
+ if (typeof fields[0] == 'object' && 'name' in fields[0]) {
68
+ return (fields as IdlField[]).map(({ name, type }) => `${name}: ${mapType(type)},`)
69
+ }
70
+
71
+ return (fields as IdlType[]).map((t) => mapType(t))
72
+ }
73
+
74
+ function codeGenType({ name, type }: IdlTypeDef): string {
75
+ switch (type.kind) {
76
+ case 'struct':
77
+ const fields = type.fields ? codeGenDefinedFields(type.fields).join('\n ') : ''
78
+ return `
79
+ interface ${name} {
80
+ ${fields}
81
+ }
82
+ `
83
+ case 'enum':
84
+ const variants = type.variants
85
+ .map(({ name, fields }) => {
86
+ if (fields) {
87
+ const body = codeGenDefinedFields(fields).join('\n ')
88
+ return `{
89
+ ${name}: {
90
+ ${body}
91
+ }
92
+ }`
93
+ }
94
+ return `{ ${name}: {} }`
95
+ })
96
+ .join('\n | ')
97
+ return `
98
+ type ${name} = ${variants}
99
+ `
100
+ case 'type':
101
+ return `
102
+ type ${name} = ${mapType(type.alias)}
103
+ `
104
+ default:
105
+ return ''
106
+ }
107
+ }
108
+
57
109
  function codeGenSolanaInstruction(idlName: string, ins: IdlInstruction): string {
58
110
  const instructionName = ins.name
59
111
 
@@ -103,7 +155,24 @@ function codeGenAccountTypeArgs(args: IdlInstructionAccountItem[]): string {
103
155
 
104
156
  // Reference: https://github.com/coral-xyz/anchor/blob/93332766f13e86efbe77c9986722731742317ede/ts/src/program/namespace/types.ts#L104
105
157
  function mapType(tpe: IdlType): string {
106
- // TODO handle complex type
158
+ if (typeof tpe == 'object') {
159
+ if ('array' in tpe) {
160
+ //IdlTypeArray
161
+ return `${mapType(tpe.array[0])}[]`
162
+ }
163
+ if ('defined' in tpe) {
164
+ // IdlTypeDefined
165
+ return tpe.defined.name
166
+ }
167
+ if ('option' in tpe) {
168
+ // IdlTypeOption
169
+ return mapType(tpe.option) + ' | null | undefined'
170
+ }
171
+ if ('vec' in tpe) {
172
+ //IdlTypeVec
173
+ return `${mapType(tpe.vec)}[]`
174
+ }
175
+ }
107
176
  switch (tpe) {
108
177
  case 'pubkey':
109
178
  return 'PublicKey'