Skip to content

Commit f430431

Browse files
committed
Lint
1 parent 322139a commit f430431

File tree

3 files changed

+38
-105
lines changed

3 files changed

+38
-105
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"@metaplex-foundation/mpl-bubblegum": "0.6.0",
7676
"@metaplex-foundation/mpl-token-metadata": "2.10.0",
7777
"@ngraveio/bc-ur": "1.1.13",
78+
"@noble/hashes": "^1.8.0",
7879
"@onsol/tldparser": "0.5.3",
7980
"@react-native-async-storage/async-storage": "1.18.1",
8081
"@react-native-community/checkbox": "0.5.17",

src/features/txnDelegation/convertLegacyIdl.tsx

Lines changed: 29 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
IdlType,
1818
IdlTypeDef,
1919
IdlTypeDefined,
20-
IdlTypeDefTy,
2120
} from '@coral-xyz/anchor/dist/cjs/idl'
2221
import { 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-
267185
function 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

346263
function 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 {
432348
function 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 {
456378
function 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)}`)

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5809,6 +5809,13 @@ __metadata:
58095809
languageName: node
58105810
linkType: hard
58115811

5812+
"@noble/hashes@npm:^1.8.0":
5813+
version: 1.8.0
5814+
resolution: "@noble/hashes@npm:1.8.0"
5815+
checksum: c94e98b941963676feaba62475b1ccfa8341e3f572adbb3b684ee38b658df44100187fa0ef4220da580b13f8d27e87d5492623c8a02ecc61f23fb9960c7918f5
5816+
languageName: node
5817+
linkType: hard
5818+
58125819
"@nodelib/fs.scandir@npm:2.1.5":
58135820
version: 2.1.5
58145821
resolution: "@nodelib/fs.scandir@npm:2.1.5"
@@ -15134,6 +15141,7 @@ __metadata:
1513415141
"@metaplex-foundation/mpl-bubblegum": 0.6.0
1513515142
"@metaplex-foundation/mpl-token-metadata": 2.10.0
1513615143
"@ngraveio/bc-ur": 1.1.13
15144+
"@noble/hashes": ^1.8.0
1513715145
"@onsol/tldparser": 0.5.3
1513815146
"@react-native-async-storage/async-storage": 1.18.1
1513915147
"@react-native-community/checkbox": 0.5.17

0 commit comments

Comments
 (0)