All files / src/commands/contract finders.ts

100% Statements 67/67
93.75% Branches 30/32
100% Functions 19/19
100% Lines 65/65

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 13842923x   1x 1x   1x 1x 65x 47x       1x       1x   1x 13590x 54087x   13590x     1x 1380x   1380x   3858x       1380x     1x 1613x 1613x   1613x   1613x 115433x     1613x     1x 3130x   3130x   3130x 1548x       1x         7612x   7612x   7612x 325x     7612x 7612x 7612x   7612x 581412x     7612x 860x     6752x     1x 1632x   1632x     1x 5541x     1x 2667x   2667x 403x     2264x 11x     2253x     1x 7992x 18x     7974x   7974x 242399x   44751x         2x         3909x     3909x    
import * as Antelope from '@wharfkit/antelope'
import type {ABI} from '@wharfkit/antelope'
import {capitalize, extractDecorator, formatInternalType, parseType, trim} from './helpers'
import {formatClassName} from '../../utils'
 
const ANTELOPE_CLASSES: string[] = []
Object.keys(Antelope).map((key) => {
    if (Antelope[key].abiName) {
        ANTELOPE_CLASSES.push(key)
    }
})
 
export const ANTELOPE_CLASS_MAPPINGS = {
    block_timestamp_type: 'BlockTimestamp',
}
 
export const ANTELOPE_CLASS_WITHOUT_TYPES = ['BlockTimestamp', 'TimePointSec']
 
export function findTypeFromAlias(typeString: string, abi: ABI.Def): string | undefined {
    const {type: typeStringWithoutDecorator} = extractDecorator(typeString)
    const alias = abi.types.find((type) => type.new_type_name === typeStringWithoutDecorator)
 
    return alias?.type
}
 
export function findAliasFromType(typeString: string, abi: ABI.Def): string | undefined {
    const {type: typeStringWithoutDecorator, decorator} = extractDecorator(typeString)
 
    const alias = abi.types.find(
        (type) =>
            type.type === typeStringWithoutDecorator ||
            type.type === `${typeStringWithoutDecorator}[]`
    )
 
    return alias?.new_type_name && `${alias?.new_type_name}${decorator || ''}`
}
 
export function findAbiStruct(type: string, abi: ABI.Def): ABI.Struct | undefined {
    const extractDecoratorResponse = extractDecorator(type)
    const typeString = extractDecoratorResponse.type
 
    const aliasType = findTypeFromAlias(typeString, abi)
 
    const abiStruct = abi.structs.find(
        (abiType) => abiType.name === extractDecorator(aliasType || typeString).type
    )
 
    return abiStruct
}
 
export function findVariant(typeString: string, abi: ABI.Def): ABI.Variant | undefined {
    const {type: typeStringWithoutDecorator} = extractDecorator(typeString)
 
    const aliastype = findTypeFromAlias(typeStringWithoutDecorator, abi)
 
    return abi.variants.find(
        (variant) => variant.name === typeStringWithoutDecorator || variant.name === aliastype
    )
}
 
export function findAbiType(
    type: string,
    abi: ABI.Def,
    typeNamespace = ''
): {type: string; decorator?: string} {
    let typeString = parseType(trim(type))
 
    const aliasType = findTypeFromAlias(typeString, abi)
 
    if (aliasType) {
        typeString = aliasType
    }
 
    const extractDecoratorResponse = extractDecorator(typeString)
    typeString = extractDecoratorResponse.type
    const decorator = extractDecoratorResponse.decorator
 
    const abiType = [...abi.structs, ...abi.variants].find(
        (abiType) => abiType.name === typeString
    )?.name
 
    if (abiType) {
        return {type: `${typeNamespace}${formatClassName(abiType)}`, decorator}
    }
 
    return {type: typeString, decorator}
}
 
export function findExternalType(type: string, typeNamespace = '', abi: ABI.Def): string {
    const {type: typeString, decorator} = findType(type, abi, typeNamespace)
 
    return `${findCoreType(typeString) || capitalize(typeString)}${decorator === '[]' ? '[]' : ''}`
}
 
function findType(type: string, abi: ABI.Def, typeNamespace?: string) {
    return findAbiType(type, abi, typeNamespace)
}
 
export function findCoreType(type: string): string | undefined {
    const coreType = findCoreClass(type)
 
    if (!coreType) {
        return
    }
 
    if (ANTELOPE_CLASS_WITHOUT_TYPES.includes(coreType)) {
        return coreType
    }
 
    return `${coreType}Type`
}
 
export function findCoreClass(type: string): string | undefined {
    if (ANTELOPE_CLASS_MAPPINGS[type]) {
        return ANTELOPE_CLASS_MAPPINGS[type]
    }
 
    const parsedType = parseType(trim(type)).split('_').join('').toLowerCase()
 
    return (
        ANTELOPE_CLASSES.find((antelopeClass) => parsedType === antelopeClass.toLowerCase()) ||
        ANTELOPE_CLASSES.find(
            (antelopeClass) => parsedType.replace(/[0-9]/g, '') === antelopeClass.toLowerCase()
        )
    )
}
 
export function findInternalType(
    type: string,
    typeNamespace: string | undefined,
    abi: ABI.Def
): string {
    const {type: typeString, decorator} = findType(type, abi, typeNamespace)
 
    // TODO: inside findType, namespace is prefixed, but format internal is doing the same
    return formatInternalType(typeString, typeNamespace, abi, decorator)
}