All files / src/commands/contract interfaces.ts

95.06% Statements 77/81
75% Branches 24/32
100% Functions 17/17
94.93% Lines 75/79

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 2185076x 1x 1x 1x 1x   1x   16x 352x 352x   352x               16x                     1x         482x   482x   482x 1235x     1235x   1235x 19x   19x 397x     19x         19x           19x   1216x     1235x 1613x   1613x 130x 130x       1235x       1235x 1235x 46787x 1235x 3274x 1235x 1235x       1235x               482x               482x     1x 16x   16x   16x   352x 16001x     352x       352x   352x 352x     352x   352x   16x             16x             16x     1x 1235x   1235x 1x     1234x       1234x 17x     1217x       1217x       1217x 24x     1193x     1x 3x 3x 29x                     3x                 2x 3x                    
import type {ABI} from '@wharfkit/antelope'
import ts from 'typescript'
import {parseType, removeCommas, removeDuplicateInterfaces} from './helpers'
import {findFieldTypeString, getActionFieldFromAbi, type StructData} from './structs'
import {findAbiStruct, findExternalType, findTypeFromAlias, findVariant} from './finders'
 
export function generateActionNamesInterface(abi: ABI.Def): ts.InterfaceDeclaration {
    // Generate property signatures for each action
    const members = abi.actions.map((action) => {
        const actionName = String(action.name)
        const actionNameKey = actionName.includes('.') ? `'${actionName}'` : actionName
 
        return ts.factory.createPropertySignature(
            undefined,
            actionNameKey,
            undefined,
            ts.factory.createTypeReferenceNode(`ActionParams.${removeCommas(actionName)}`)
        )
    })
 
    return ts.factory.createInterfaceDeclaration(
        [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
        'ActionNameParams',
        undefined,
        undefined,
        members
    )
}
 
export type TypeInterfaceDeclaration = ts.InterfaceDeclaration | ts.TypeAliasDeclaration
 
export function generateActionInterface(
    struct: any,
    abi: ABI.Def,
    allStructData: StructData[]
): {actionInterface: ts.InterfaceDeclaration; typeInterfaces: TypeInterfaceDeclaration[]} {
    const typeInterfaces: TypeInterfaceDeclaration[] = []
 
    const structName = typeof struct.name === 'string' ? struct.name : ''
 
    const members = struct.fields.map((field) => {
        const abiVariant = findVariant(field.type, abi)
 
        let types
        const aliasType = findTypeFromAlias(field.type, abi)
 
        if (abiVariant) {
            types = abiVariant.types
 
            const variantTypeNodes = types.map((type) =>
                ts.factory.createTypeReferenceNode(parseType(findExternalType(type, 'Type.', abi)))
            )
 
            const allTypeNodes = [
                ...variantTypeNodes,
                ts.factory.createTypeReferenceNode(`Types.${abiVariant.name}`),
            ]
 
            const variantTypeAlias = ts.factory.createTypeAliasDeclaration(
                [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
                abiVariant.name,
                undefined,
                ts.factory.createUnionTypeNode(allTypeNodes)
            )
            typeInterfaces.push(variantTypeAlias)
        } else {
            types = [field.type]
        }
 
        types.forEach((type) => {
            const typeStruct = findAbiStruct(type, abi)
 
            if (typeStruct) {
                const interfaces = generateActionInterface(typeStruct, abi, allStructData)
                typeInterfaces.push(interfaces.actionInterface, ...interfaces.typeInterfaces)
            }
        })
 
        const typeReferenceNode = ts.factory.createTypeReferenceNode(
            findParamTypeString(aliasType || field.type, 'Type.', abi)
        )
 
        let isOptional = false
        const fieldName = field.name
        const processedStruct = allStructData.find((s) => s.name === structName)
        if (processedStruct) {
            const processedField = processedStruct.fields.find((f) => f.name === fieldName)
            if (processedField) {
                isOptional = processedField.optional
            }
        }
 
        return ts.factory.createPropertySignature(
            undefined,
            fieldName,
            isOptional ? ts.factory.createToken(ts.SyntaxKind.QuestionToken) : undefined,
            typeReferenceNode
        )
    })
 
    const actionInterface = ts.factory.createInterfaceDeclaration(
        [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
        structName ? removeCommas(structName) : ts.createIdentifier('UnknownStruct'),
        undefined,
        undefined,
        members
    )
 
    return {actionInterface, typeInterfaces: removeDuplicateInterfaces(typeInterfaces)}
}
 
export function generateActionsNamespace(abi: ABI.Def): ts.Statement {
    const actionStructsWithFields = getActionFieldFromAbi(abi)
 
    const typeInterfaces: TypeInterfaceDeclaration[] = []
 
    const actionParamInterfaces = abi.actions
        .map((action) => {
            const actionStruct = actionStructsWithFields.find(
                (actionStructWithField) => actionStructWithField.name === action.type
            )
 
            Iif (!actionStruct) {
                return undefined
            }
 
            const interfaces = generateActionInterface(actionStruct, abi, actionStructsWithFields)
 
            if (interfaces.actionInterface) {
                typeInterfaces.push(...interfaces.typeInterfaces)
            }
 
            return interfaces.actionInterface
        })
        .filter((iface): iface is ts.InterfaceDeclaration => !!iface)
 
    const actionParamsTypes = ts.factory.createModuleDeclaration(
        [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
        ts.factory.createIdentifier('Type'),
        ts.factory.createModuleBlock(removeDuplicateInterfaces(typeInterfaces)),
        ts.NodeFlags.Namespace
    )
 
    const actionParamsNamespace = ts.factory.createModuleDeclaration(
        [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
        ts.factory.createIdentifier('ActionParams'),
        ts.factory.createModuleBlock([actionParamsTypes, ...actionParamInterfaces]),
        ts.NodeFlags.Namespace
    )
 
    return actionParamsNamespace
}
 
function findParamTypeString(typeString: string, namespace = '', abi: ABI.Def): string {
    const fieldType = findExternalType(typeString, namespace, abi)
 
    if (fieldType === 'Symbol_code') {
        return 'Asset.SymbolCodeType'
    }
 
    Iif (fieldType === 'Symbol_code[]') {
        return 'Asset.SymbolCodeType[]'
    }
 
    if (fieldType === 'Symbol') {
        return 'Asset.SymbolType'
    }
 
    Iif (fieldType === 'Symbol[]') {
        return 'Asset.SymbolType[]'
    }
 
    Iif (fieldType === 'Bool[]') {
        return 'boolean[]'
    }
 
    if (fieldType === 'Bool') {
        return 'boolean'
    }
 
    return parseType(fieldType)
}
 
export function generateActionReturnValuesInterface(abi: ABI.Def): ts.InterfaceDeclaration {
    const actionResults: ABI.ActionResult[] = abi.action_results
    const members = actionResults.map((result) => {
        return ts.factory.createPropertySignature(
            undefined,
            String(result.name),
            undefined,
            ts.factory.createTypeReferenceNode(
                findFieldTypeString(result.result_type, 'Types.', abi),
                undefined
            )
        )
    })
 
    return ts.factory.createInterfaceDeclaration(
        [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
        'ActionReturnValues',
        undefined,
        undefined,
        members
    )
}
 
export function generateActionReturnNamesType(): ts.TypeAliasDeclaration {
    return ts.factory.createTypeAliasDeclaration(
        [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
        'ActionReturnNames',
        undefined,
        ts.factory.createTypeOperatorNode(
            ts.SyntaxKind.KeyOfKeyword,
            ts.factory.createTypeReferenceNode('ActionReturnValues', undefined)
        )
    )
}