Skip to content

Commit 126a070

Browse files
committed
fix: fixing failing tests
1 parent 5fc0e65 commit 126a070

6 files changed

Lines changed: 340 additions & 70 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Shared utility functions for contract code generation.
3+
* These are extracted to avoid circular dependencies between helpers.ts and finders.ts.
4+
*/
5+
6+
const decorators = ['?', '[]']
7+
8+
export function extractDecorator(type: string): {type: string; decorator?: string} {
9+
for (const decorator of decorators) {
10+
if (type.includes(decorator)) {
11+
type = type.replace(decorator, '')
12+
13+
return {type, decorator}
14+
}
15+
}
16+
17+
return {type}
18+
}
19+
20+
export function parseType(type: string): string {
21+
type = type.replace('$', '')
22+
23+
if (type === 'String') {
24+
return 'string'
25+
}
26+
27+
if (type === 'String[]') {
28+
return 'string[]'
29+
}
30+
31+
if (type === 'Boolean') {
32+
return 'boolean'
33+
}
34+
35+
if (type === 'Boolean[]') {
36+
return 'boolean[]'
37+
}
38+
39+
return type
40+
}
41+
42+
export function trim(string: string) {
43+
return string.replace(/\s/g, '')
44+
}
45+
46+
export function capitalize(string: string) {
47+
if (typeof string !== 'string' || string.length === 0) {
48+
return ''
49+
}
50+
51+
return string.charAt(0).toUpperCase() + string.slice(1)
52+
}
53+
54+
export function cleanupType(type: string): string {
55+
return extractDecorator(parseType(trim(type))).type
56+
}
57+

src/commands/contract/deploy.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -454,14 +454,7 @@ export async function deployContract(
454454
console.log(`Transaction ID: ${result.resolved?.transaction.id}`)
455455
} catch (error) {
456456
const errorMessage = (error as Error).message
457-
throw new Error(
458-
`Failed to deploy contract: ${errorMessage}\n\n` +
459-
`Make sure:\n` +
460-
`1. The blockchain is running (wharfkit chain local start)\n` +
461-
`2. The account "${accountName}" exists\n` +
462-
`3. You have a wallet key with permissions for this account\n` +
463-
`4. The ABI file exists alongside the WASM file`
464-
)
457+
throw new Error(`Failed to deploy contract: ${errorMessage}`)
465458
}
466459
}
467460

src/commands/contract/finders.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as Antelope from '@wharfkit/antelope'
22
import type {ABI} from '@wharfkit/antelope'
3-
import {capitalize, extractDecorator, formatInternalType, parseType, trim} from './helpers'
3+
import {capitalize, extractDecorator, parseType, trim} from './contract-utils'
44
import {formatClassName} from '../../utils'
55

66
const ANTELOPE_CLASSES: string[] = []
@@ -125,13 +125,3 @@ export function findCoreClass(type: string): string | undefined {
125125
)
126126
}
127127

128-
export function findInternalType(
129-
type: string,
130-
typeNamespace: string | undefined,
131-
abi: ABI.Def
132-
): string {
133-
const {type: typeString, decorator} = findType(type, abi, typeNamespace)
134-
135-
// TODO: inside findType, namespace is prefixed, but format internal is doing the same
136-
return formatInternalType(typeString, typeNamespace, abi, decorator)
137-
}

src/commands/contract/helpers.ts

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import type {ABI} from '@wharfkit/antelope'
22
import * as ts from 'typescript'
33
import {formatClassName} from '../../utils'
4+
import {capitalize, extractDecorator, parseType, trim} from './contract-utils'
45
import {findAbiType, findAliasFromType, findCoreClass, findCoreType, findVariant} from './finders'
56
import type {TypeInterfaceDeclaration} from './interfaces'
67

8+
// Re-export utilities for backwards compatibility
9+
export {capitalize, extractDecorator, parseType, trim, cleanupType} from './contract-utils'
10+
711
export function getCoreImports(abi: ABI.Def) {
812
const coreImports: string[] = []
913
const coreTypes: string[] = []
@@ -211,55 +215,15 @@ export function formatInternalType(
211215
return `${type}${decorator}`
212216
}
213217

214-
const decorators = ['?', '[]']
215-
export function extractDecorator(type: string): {type: string; decorator?: string} {
216-
for (const decorator of decorators) {
217-
if (type.includes(decorator)) {
218-
type = type.replace(decorator, '')
219-
220-
return {type, decorator}
221-
}
222-
}
223-
224-
return {type}
225-
}
226-
227-
export function cleanupType(type: string): string {
228-
return extractDecorator(parseType(trim(type))).type
229-
}
230-
231-
export function parseType(type: string): string {
232-
type = type.replace('$', '')
233-
234-
if (type === 'String') {
235-
return 'string'
236-
}
237-
238-
if (type === 'String[]') {
239-
return 'string[]'
240-
}
241-
242-
if (type === 'Boolean') {
243-
return 'boolean'
244-
}
245-
246-
if (type === 'Boolean[]') {
247-
return 'boolean[]'
248-
}
249-
250-
return type
251-
}
252-
253-
export function trim(string: string) {
254-
return string.replace(/\s/g, '')
255-
}
256-
257-
export function capitalize(string) {
258-
if (typeof string !== 'string' || string.length === 0) {
259-
return ''
260-
}
218+
export function findInternalType(
219+
type: string,
220+
typeNamespace: string | undefined,
221+
abi: ABI.Def
222+
): string {
223+
const {type: typeString, decorator} = findAbiType(type, abi, typeNamespace)
261224

262-
return string.charAt(0).toUpperCase() + string.slice(1)
225+
// TODO: inside findAbiType, namespace is prefixed, but formatInternalType is doing the same
226+
return formatInternalType(typeString, typeNamespace, abi, decorator)
263227
}
264228

265229
export function removeDuplicateInterfaces(

src/commands/contract/structs.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import type {ABI} from '@wharfkit/antelope'
22
import ts from 'typescript'
3-
import {extractDecorator, parseType} from './helpers'
3+
import {extractDecorator, findInternalType, parseType} from './helpers'
44
import {formatClassName} from '../../utils'
5-
import {findInternalType} from './finders'
65

76
interface FieldType {
87
name: string

0 commit comments

Comments
 (0)