@@ -17,7 +17,6 @@ import {
1717 IdlType ,
1818 IdlTypeDef ,
1919 IdlTypeDefined ,
20- IdlTypeDefTy ,
2120} from '@coral-xyz/anchor/dist/cjs/idl'
2221import { sha256 } from '@noble/hashes/sha256'
2322
@@ -166,7 +165,7 @@ export function convertLegacyIdl(
166165 }
167166 return {
168167 accounts : ( legacyIdl . accounts || [ ] ) . map ( convertAccount ) ,
169- address : address ,
168+ address,
170169 constants : ( legacyIdl . constants || [ ] ) . map ( convertConst ) ,
171170 errors : legacyIdl . errors ?. map ( convertErrorCode ) || [ ] ,
172171 events : legacyIdl . events ?. map ( convertEvent ) || [ ] ,
@@ -183,87 +182,6 @@ export function convertLegacyIdl(
183182 }
184183}
185184
186- function traverseType ( type : IdlType | string , refs : Set < string > ) {
187- if ( typeof type === 'string' ) {
188- // skip
189- } else if ( 'vec' in type ) {
190- traverseType ( type . vec , refs )
191- } else if ( 'option' in type ) {
192- traverseType ( type . option , refs )
193- } else if ( 'defined' in type ) {
194- refs . add ( type . defined . name )
195- } else if ( 'array' in type ) {
196- traverseType ( type . array [ 0 ] , refs )
197- } else if ( 'generic' in type ) {
198- refs . add ( type . generic )
199- } else if ( 'coption' in type ) {
200- traverseType ( type . coption , refs )
201- }
202- }
203-
204- function traverseIdlFields ( fields : IdlDefinedFields , refs : Set < string > ) {
205- fields . forEach ( ( field : IdlField | IdlType | string ) =>
206- typeof field === 'string'
207- ? traverseType ( field , refs )
208- : typeof field === 'object' && 'type' in field
209- ? traverseType ( field . type , refs )
210- : traverseType ( field , refs ) ,
211- )
212- }
213-
214- function traverseTypeDef ( type : IdlTypeDefTy , refs : Set < string > ) {
215- switch ( type . kind ) {
216- case 'struct' :
217- traverseIdlFields ( type . fields ?? [ ] , refs )
218- return
219- case 'enum' :
220- type . variants . forEach ( ( variant : any ) =>
221- traverseIdlFields ( variant . fields ?? [ ] , refs ) ,
222- )
223- return
224- case 'type' :
225- traverseType ( type . alias , refs )
226- return
227- }
228- }
229-
230- function getTypeReferences ( idl : Idl ) : Set < string > {
231- const refs = new Set < string > ( )
232- idl . constants ?. forEach ( ( constant : any ) => traverseType ( constant . type , refs ) )
233- idl . accounts ?. forEach ( ( account : any ) => refs . add ( account . name ) )
234- idl . instructions ?. forEach ( ( instruction : any ) =>
235- instruction . args . forEach ( ( arg : any ) => traverseType ( arg . type , refs ) ) ,
236- )
237- idl . events ?. forEach ( ( event : any ) => refs . add ( event . name ) )
238-
239- // Build up recursive type references in breadth-first manner.
240- // Very inefficient since we traverse same types multiple times.
241- // But it works. Open to contributions that do proper graph traversal
242- let prevSize = refs . size
243- let sizeDiff = 1
244- while ( sizeDiff > 0 ) {
245- for ( const idlType of idl . types ?? [ ] ) {
246- if ( refs . has ( idlType . name ) ) {
247- traverseTypeDef ( idlType . type , refs )
248- }
249- }
250- sizeDiff = refs . size - prevSize
251- prevSize = refs . size
252- }
253- return refs
254- }
255-
256- // Remove types that are not used in definition of instructions, accounts, events, or constants
257- function removeUnusedTypes ( idl : Idl ) : Idl {
258- const usedElsewhere = getTypeReferences ( idl )
259- return {
260- ...idl ,
261- types : ( idl . types ?? [ ] ) . filter ( ( type : any ) =>
262- usedElsewhere . has ( type . name ) ,
263- ) ,
264- }
265- }
266-
267185function getDisc ( prefix : string , name : string ) : number [ ] {
268186 const hash = sha256 ( `${ prefix } :${ name } ` )
269187 return Array . from ( hash . slice ( 0 , 8 ) )
@@ -336,11 +254,10 @@ function convertEnumFields(fields: LegacyEnumFields): IdlDefinedFields {
336254 'type' in fields [ 0 ]
337255 ) {
338256 return ( fields as LegacyIdlField [ ] ) . map ( convertField ) as IdlField [ ]
339- } else {
340- return ( fields as LegacyIdlType [ ] ) . map ( ( type ) =>
341- convertType ( type ) ,
342- ) as IdlType [ ]
343257 }
258+ return ( fields as LegacyIdlType [ ] ) . map ( ( type ) =>
259+ convertType ( type ) ,
260+ ) as IdlType [ ]
344261}
345262
346263function convertEvent ( event : LegacyIdlEvent ) : IdlEvent {
@@ -371,16 +288,15 @@ function convertInstructionAccount(
371288) : IdlInstructionAccountItem {
372289 if ( 'accounts' in account ) {
373290 return convertInstructionAccounts ( account )
374- } else {
375- return {
376- docs : account . docs || [ ] ,
377- name : getSnakeCase ( account . name ) ,
378- optional : account . isOptional || false ,
379- pda : account . pda ? convertPda ( account . pda ) : undefined ,
380- relations : account . relations || [ ] ,
381- signer : account . isSigner || false ,
382- writable : account . isMut || false ,
383- }
291+ }
292+ return {
293+ docs : account . docs || [ ] ,
294+ name : getSnakeCase ( account . name ) ,
295+ optional : account . isOptional || false ,
296+ pda : account . pda ? convertPda ( account . pda ) : undefined ,
297+ relations : account . relations || [ ] ,
298+ signer : account . isSigner || false ,
299+ writable : account . isMut || false ,
384300 }
385301}
386302
@@ -432,17 +348,23 @@ function convertEventToTypeDef(event: LegacyIdlEvent): IdlTypeDef {
432348function convertType ( type : LegacyIdlType ) : IdlType {
433349 if ( typeof type === 'string' ) {
434350 return type === 'publicKey' ? 'pubkey' : type
435- } else if ( 'vec' in type ) {
351+ }
352+ if ( 'vec' in type ) {
436353 return { vec : convertType ( type . vec ) }
437- } else if ( 'option' in type ) {
354+ }
355+ if ( 'option' in type ) {
438356 return { option : convertType ( type . option ) }
439- } else if ( 'defined' in type ) {
357+ }
358+ if ( 'defined' in type ) {
440359 return { defined : { generics : [ ] , name : type . defined } } as IdlTypeDefined
441- } else if ( 'array' in type ) {
360+ }
361+ if ( 'array' in type ) {
442362 return { array : [ convertType ( type . array [ 0 ] ) , type . array [ 1 ] ] }
443- } else if ( 'generic' in type ) {
363+ }
364+ if ( 'generic' in type ) {
444365 return type
445- } else if ( 'definedWithTypeArgs' in type ) {
366+ }
367+ if ( 'definedWithTypeArgs' in type ) {
446368 return {
447369 defined : {
448370 generics : type . definedWithTypeArgs . args . map ( convertDefinedTypeArg ) ,
@@ -456,9 +378,11 @@ function convertType(type: LegacyIdlType): IdlType {
456378function convertDefinedTypeArg ( arg : LegacyIdlDefinedTypeArg ) : any {
457379 if ( 'generic' in arg ) {
458380 return { generic : arg . generic }
459- } else if ( 'value' in arg ) {
381+ }
382+ if ( 'value' in arg ) {
460383 return { value : arg . value }
461- } else if ( 'type' in arg ) {
384+ }
385+ if ( 'type' in arg ) {
462386 return { type : convertType ( arg . type ) }
463387 }
464388 throw new Error ( `Unsupported defined type arg: ${ JSON . stringify ( arg ) } ` )
0 commit comments