All files / src/commands/contract class.ts

100% Statements 43/43
90.9% Branches 10/11
100% Functions 6/6
100% Lines 43/43

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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 3246418x 1x   1x   1x   16x     16x                                       16x         16x           16x   16x 16x   16x     16x 3x 3x     16x 16x   16x       16x         16x     1x 16x                                                                         1x 16x             16x                 16x                 16x                   16x                                       16x                       1x   3x             3x                 3x                         3x                                                         3x                             2x   16x               16x                 16x                   16x             16x                                               16x                     16x    
import type {ABI} from '@wharfkit/antelope'
import * as ts from 'typescript'
 
import {generateClassDeclaration} from './helpers'
 
export async function generateContractClass(contractName: string, abi: ABI.Def) {
    // Prepare the member fields of the class
    const classMembers: ts.ClassElement[] = []
 
    // Generate the `constructor` member
    const constructorParams: ts.ParameterDeclaration[] = [
        ts.factory.createParameterDeclaration(
            undefined,
            undefined,
            'args',
            undefined,
            ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('PartialBy'), [
                ts.factory.createTypeReferenceNode(
                    ts.factory.createIdentifier('ContractArgs'),
                    undefined
                ),
                ts.factory.createUnionTypeNode([
                    ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral('abi')),
                    ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral('account')),
                ]),
            ]),
            undefined
        ),
    ]
 
    const constructorBody = ts.factory.createBlock(
        [generateConstructorFunction(contractName)],
        true
    )
 
    const constructorMember = ts.factory.createConstructorDeclaration(
        undefined,
        constructorParams,
        constructorBody
    )
 
    classMembers.push(constructorMember)
 
    if (abi.actions.length) {
        const actionMethod = generateActionMethod()
 
        classMembers.push(actionMethod)
    }
 
    if (abi.action_results.length) {
        const readonlyMethod = generateReadonlyMethod()
        classMembers.push(readonlyMethod)
    }
 
    if (abi.tables.length) {
        const tableMethod = generateTableMethod()
 
        classMembers.push(tableMethod)
    }
 
    // Construct class declaration
    const classDeclaration = generateClassDeclaration('Contract', classMembers, {
        parent: 'BaseContract',
        export: true,
    })
 
    return {classDeclaration}
}
 
function generateConstructorFunction(contractName) {
    return ts.factory.createExpressionStatement(
        ts.factory.createCallExpression(ts.factory.createSuper(), undefined, [
            ts.factory.createObjectLiteralExpression(
                [
                    ts.factory.createPropertyAssignment(
                        'client',
                        ts.factory.createPropertyAccessExpression(
                            ts.factory.createIdentifier('args'),
                            'client'
                        )
                    ),
                    ts.factory.createPropertyAssignment('abi', ts.factory.createIdentifier('abi')),
                    ts.factory.createPropertyAssignment(
                        'account',
                        ts.factory.createBinaryExpression(
                            ts.factory.createPropertyAccessExpression(
                                ts.factory.createIdentifier('args'),
                                'account'
                            ),
                            ts.factory.createToken(ts.SyntaxKind.BarBarToken),
                            ts.factory.createCallExpression(
                                ts.factory.createPropertyAccessExpression(
                                    ts.factory.createIdentifier('Name'),
                                    'from'
                                ),
                                undefined,
                                [ts.factory.createStringLiteral(contractName)]
                            )
                        )
                    ),
                ],
                true
            ),
        ])
    )
}
 
function generateActionMethod(): ts.MethodDeclaration {
    const typeParameter = ts.factory.createTypeParameterDeclaration(
        undefined,
        'T',
        ts.factory.createTypeReferenceNode('ActionNames')
    )
 
    // 3. Create the function parameters.
    const nameParameter = ts.factory.createParameterDeclaration(
        [],
        undefined,
        'name',
        undefined,
        ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('T')),
        undefined
    )
 
    const dataParameter = ts.factory.createParameterDeclaration(
        [],
        undefined,
        'data',
        undefined,
        ts.factory.createTypeReferenceNode('ActionNameParams[T]'),
        undefined
    )
 
    const optionsParameter = ts.factory.createParameterDeclaration(
        [],
        undefined,
        'options',
        ts.factory.createToken(ts.SyntaxKind.QuestionToken),
        ts.factory.createTypeReferenceNode('ActionOptions'),
        undefined
    )
 
    // 4. Generate the function body.
    const methodBody = ts.factory.createBlock(
        [
            ts.factory.createReturnStatement(
                ts.factory.createCallExpression(
                    ts.factory.createPropertyAccessExpression(
                        ts.factory.createSuper(),
                        ts.factory.createIdentifier('action')
                    ),
                    undefined,
                    [
                        ts.factory.createIdentifier('name'),
                        ts.factory.createIdentifier('data'),
                        ts.factory.createIdentifier('options'),
                    ]
                )
            ),
        ],
        true
    )
 
    return ts.factory.createMethodDeclaration(
        [],
        undefined,
        'action',
        undefined,
        [typeParameter],
        [nameParameter, dataParameter, optionsParameter],
        ts.factory.createTypeReferenceNode('Action'),
        methodBody
    )
}
 
function generateReadonlyMethod(): ts.MethodDeclaration {
    // Create the generic type parameter 'T' with a constraint to 'ActionReturnNames'
    const typeParameter = ts.factory.createTypeParameterDeclaration(
        undefined,
        'T',
        ts.factory.createTypeReferenceNode('ActionReturnNames')
    )
 
    // Create the function parameters.
    const nameParameter = ts.factory.createParameterDeclaration(
        [],
        undefined,
        'name',
        undefined,
        ts.factory.createTypeReferenceNode('T'),
        undefined
    )
 
    const dataParameter = ts.factory.createParameterDeclaration(
        [],
        undefined,
        'data',
        ts.factory.createToken(ts.SyntaxKind.QuestionToken),
        ts.factory.createIndexedAccessTypeNode(
            ts.factory.createTypeReferenceNode('ActionNameParams'),
            ts.factory.createTypeReferenceNode('T')
        ),
        undefined
    )
 
    // Generate the function body.
    const methodBody = ts.factory.createBlock(
        [
            ts.factory.createReturnStatement(
                ts.factory.createAsExpression(
                    ts.factory.createAsExpression(
                        ts.factory.createCallExpression(
                            ts.factory.createPropertyAccessExpression(
                                ts.factory.createSuper(),
                                'readonly'
                            ),
                            undefined,
                            [
                                ts.factory.createIdentifier('name'),
                                ts.factory.createIdentifier('data'),
                            ]
                        ),
                        ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword)
                    ),
                    ts.factory.createIndexedAccessTypeNode(
                        ts.factory.createTypeReferenceNode('ActionReturnValues'),
                        ts.factory.createTypeReferenceNode('T')
                    )
                )
            ),
        ],
        true
    )
 
    // Create the method declaration
    return ts.factory.createMethodDeclaration(
        [],
        undefined,
        'readonly',
        undefined,
        [typeParameter],
        [nameParameter, dataParameter],
        ts.factory.createIndexedAccessTypeNode(
            ts.factory.createTypeReferenceNode('ActionReturnValues'),
            ts.factory.createTypeReferenceNode('T')
        ),
        methodBody
    )
}
 
export function generateTableMethod(): ts.MethodDeclaration {
    // Create the generic type parameter 'T' with a constraint to 'tables'
    const typeParameter = ts.factory.createTypeParameterDeclaration(
        undefined,
        ts.factory.createIdentifier('T'),
        ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('TableNames')),
        undefined
    )
 
    // Create the parameters for the method
    const nameParameter = ts.factory.createParameterDeclaration(
        [],
        undefined,
        ts.factory.createIdentifier('name'),
        undefined,
        ts.factory.createTypeReferenceNode('T'),
        undefined
    )
 
    const scopeParameter = ts.factory.createParameterDeclaration(
        [],
        undefined,
        'scope',
        ts.factory.createToken(ts.SyntaxKind.QuestionToken),
        ts.factory.createTypeReferenceNode('NameType'),
        undefined
    )
 
    // Create the method return type 'Table<RowType<T>>'
    const returnType = ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('Table'), [
        ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('RowType'), [
            ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('T')),
        ]),
    ])
 
    // Generate the function body
    const methodBody = ts.factory.createBlock(
        [
            ts.factory.createReturnStatement(
                ts.factory.createCallExpression(
                    ts.factory.createPropertyAccessExpression(
                        ts.factory.createSuper(),
                        ts.factory.createIdentifier('table')
                    ),
                    undefined,
                    [
                        ts.factory.createIdentifier('name'),
                        ts.factory.createIdentifier('scope'),
                        ts.factory.createElementAccessExpression(
                            ts.factory.createIdentifier('TableMap'),
                            ts.factory.createIdentifier('name')
                        ),
                    ]
                )
            ),
        ],
        true
    )
 
    // Create the method declaration
    const methodDeclaration = ts.factory.createMethodDeclaration(
        [],
        undefined,
        'table',
        undefined,
        [typeParameter],
        [nameParameter, scopeParameter],
        returnType,
        methodBody
    )
 
    return methodDeclaration
}