aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sol-doc/src/sol_doc.ts
blob: eda76705423004db54a99a00b5911174cb9740f1 (plain) (blame)
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
import * as path from 'path';

import {
    AbiDefinition,
    ConstructorAbi,
    DataItem,
    DevdocOutput,
    EventAbi,
    EventParameter,
    FallbackAbi,
    MethodAbi,
    StandardContractOutput,
} from 'ethereum-types';
import ethUtil = require('ethereumjs-util');
import * as _ from 'lodash';

import { Compiler, CompilerOptions } from '@0xproject/sol-compiler';
import {
    CustomType,
    CustomTypeChild,
    DocAgnosticFormat,
    DocSection,
    Event,
    EventArg,
    ObjectMap,
    Parameter,
    SolidityMethod,
    Type,
    TypeDocTypes,
} from '@0xproject/types';

export class SolDoc {
    private _customTypeHashToName: ObjectMap<string> | undefined;
    private static _genEventDoc(abiDefinition: EventAbi): Event {
        const eventDoc: Event = {
            name: abiDefinition.name,
            eventArgs: SolDoc._genEventArgsDoc(abiDefinition.inputs),
        };
        return eventDoc;
    }
    private static _devdocMethodDetailsIfExist(
        methodSignature: string,
        devdocIfExists: DevdocOutput | undefined,
    ): string | undefined {
        let details;
        if (!_.isUndefined(devdocIfExists)) {
            const devdocMethodsIfExist = devdocIfExists.methods;
            if (!_.isUndefined(devdocMethodsIfExist)) {
                const devdocMethodIfExists = devdocMethodsIfExist[methodSignature];
                if (!_.isUndefined(devdocMethodIfExists)) {
                    const devdocMethodDetailsIfExist = devdocMethodIfExists.details;
                    if (!_.isUndefined(devdocMethodDetailsIfExist)) {
                        details = devdocMethodDetailsIfExist;
                    }
                }
            }
        }
        return details;
    }
    private static _genFallbackDoc(
        abiDefinition: FallbackAbi,
        devdocIfExists: DevdocOutput | undefined,
    ): SolidityMethod {
        const methodSignature = `()`;
        const comment = SolDoc._devdocMethodDetailsIfExist(methodSignature, devdocIfExists);

        const returnComment =
            _.isUndefined(devdocIfExists) || _.isUndefined(devdocIfExists.methods[methodSignature])
                ? undefined
                : devdocIfExists.methods[methodSignature].return;

        const methodDoc: SolidityMethod = {
            isConstructor: false,
            name: 'fallback',
            callPath: '',
            parameters: [],
            returnType: { name: 'void', typeDocType: TypeDocTypes.Intrinsic },
            returnComment,
            isConstant: true,
            isPayable: abiDefinition.payable,
            isFallback: true,
            comment: _.isEmpty(comment)
                ? 'The fallback function. It is executed on a call to the contract if none of the other functions match the given public identifier (or if no data was supplied at all).'
                : comment,
        };
        return methodDoc;
    }
    private static _genEventArgsDoc(args: EventParameter[]): EventArg[] {
        const eventArgsDoc: EventArg[] = [];

        for (const arg of args) {
            const name = arg.name;

            const type: Type = {
                name: arg.type,
                typeDocType: TypeDocTypes.Intrinsic,
            };

            const eventArgDoc: EventArg = {
                isIndexed: arg.indexed,
                name,
                type,
            };

            eventArgsDoc.push(eventArgDoc);
        }
        return eventArgsDoc;
    }
    private static _dedupStructs(customTypes: CustomType[]): CustomType[] {
        const uniqueCustomTypes: CustomType[] = [];
        const seenTypes: { [hash: string]: boolean } = {};
        _.each(customTypes, customType => {
            const hash = SolDoc._generateCustomTypeHash(customType);
            if (!seenTypes[hash]) {
                uniqueCustomTypes.push(customType);
                seenTypes[hash] = true;
            }
        });
        return uniqueCustomTypes;
    }
    private static _capitalize(text: string): string {
        return `${text.charAt(0).toUpperCase()}${text.slice(1)}`;
    }
    private static _generateCustomTypeHash(customType: CustomType): string {
        const customTypeWithoutName = _.cloneDeep(customType);
        delete customTypeWithoutName.name;
        const customTypeWithoutNameStr = JSON.stringify(customTypeWithoutName);
        const hash = ethUtil.sha256(customTypeWithoutNameStr).toString('hex');
        return hash;
    }
    private static _makeCompilerOptions(contractsDir: string, contractsToCompile?: string[]): CompilerOptions {
        const compilerOptions: CompilerOptions = {
            contractsDir,
            contracts: '*',
            compilerSettings: {
                outputSelection: {
                    ['*']: {
                        ['*']: ['abi', 'devdoc'],
                    },
                },
            },
        };

        const shouldOverrideCatchAllContractsConfig =
            !_.isUndefined(contractsToCompile) && contractsToCompile.length > 0;
        if (shouldOverrideCatchAllContractsConfig) {
            compilerOptions.contracts = contractsToCompile;
        }

        return compilerOptions;
    }
    /**
     * Invoke the Solidity compiler and transform its ABI and devdoc outputs into a
     * JSON format easily consumed by documentation rendering tools.
     * @param contractsToDocument list of contracts for which to generate doc objects
     * @param contractsDir the directory in which to find the `contractsToCompile` as well as their dependencies.
     * @return doc object for use with documentation generation tools.
     */
    public async generateSolDocAsync(
        contractsDir: string,
        contractsToDocument?: string[],
        customTypeHashToName?: ObjectMap<string>,
    ): Promise<DocAgnosticFormat> {
        this._customTypeHashToName = customTypeHashToName;
        const docWithDependencies: DocAgnosticFormat = {};
        const compilerOptions = SolDoc._makeCompilerOptions(contractsDir, contractsToDocument);
        const compiler = new Compiler(compilerOptions);
        const compilerOutputs = await compiler.getCompilerOutputsAsync();
        let structs: CustomType[] = [];
        for (const compilerOutput of compilerOutputs) {
            const contractFileNames = _.keys(compilerOutput.contracts);
            for (const contractFileName of contractFileNames) {
                const contractNameToOutput = compilerOutput.contracts[contractFileName];

                const contractNames = _.keys(contractNameToOutput);
                for (const contractName of contractNames) {
                    const compiledContract = contractNameToOutput[contractName];
                    if (_.isUndefined(compiledContract.abi)) {
                        throw new Error('compiled contract did not contain ABI output');
                    }
                    docWithDependencies[contractName] = this._genDocSection(compiledContract, contractName);
                    structs = [...structs, ...this._extractStructs(compiledContract)];
                }
            }
        }
        structs = SolDoc._dedupStructs(structs);
        structs = this._overwriteStructNames(structs);

        let doc: DocAgnosticFormat = {};
        if (_.isUndefined(contractsToDocument) || contractsToDocument.length === 0) {
            doc = docWithDependencies;
        } else {
            for (const contractToDocument of contractsToDocument) {
                const contractBasename = path.basename(contractToDocument);
                const contractName =
                    contractBasename.lastIndexOf('.sol') === -1
                        ? contractBasename
                        : contractBasename.substring(0, contractBasename.lastIndexOf('.sol'));
                doc[contractName] = docWithDependencies[contractName];
            }
        }

        if (structs.length > 0) {
            doc.structs = {
                comment: '',
                constructors: [],
                methods: [],
                properties: [],
                types: structs,
                functions: [],
                events: [],
            };
        }

        delete this._customTypeHashToName; // Clean up instance state
        return doc;
    }
    private _getCustomTypeFromDataItem(inputOrOutput: DataItem): CustomType {
        const customType: CustomType = {
            name: _.capitalize(inputOrOutput.name),
            kindString: 'Interface',
            children: [],
        };
        _.each(inputOrOutput.components, (component: DataItem) => {
            const childType = this._getTypeFromDataItem(component);
            const customTypeChild = {
                name: component.name,
                type: childType,
            };
            // (fabio): Not sure why this type casting is necessary. Seems TS doesn't
            // deduce that `customType.children` cannot be undefined anymore after being
            // set to `[]` above.
            (customType.children as CustomTypeChild[]).push(customTypeChild);
        });
        return customType;
    }
    private _getNameFromDataItemIfExists(dataItem: DataItem): string | undefined {
        if (_.isUndefined(dataItem.components)) {
            return undefined;
        }
        const customType = this._getCustomTypeFromDataItem(dataItem);
        const hash = SolDoc._generateCustomTypeHash(customType);
        if (_.isUndefined(this._customTypeHashToName) || _.isUndefined(this._customTypeHashToName[hash])) {
            return undefined;
        }
        return this._customTypeHashToName[hash];
    }
    private _getTypeFromDataItem(dataItem: DataItem): Type {
        const typeDocType = !_.isUndefined(dataItem.components) ? TypeDocTypes.Reference : TypeDocTypes.Intrinsic;
        let typeName: string;
        if (typeDocType === TypeDocTypes.Reference) {
            const nameIfExists = this._getNameFromDataItemIfExists(dataItem);
            typeName = _.isUndefined(nameIfExists) ? SolDoc._capitalize(dataItem.name) : nameIfExists;
        } else {
            typeName = dataItem.type;
        }

        const isArrayType = _.endsWith(dataItem.type, '[]');
        let type: Type;
        if (isArrayType) {
            // tslint:disable-next-line:custom-no-magic-numbers
            typeName = typeDocType === TypeDocTypes.Intrinsic ? typeName.slice(0, -2) : typeName;
            type = {
                elementType: { name: typeName, typeDocType },
                typeDocType: TypeDocTypes.Array,
                name: '',
            };
        } else {
            type = { name: typeName, typeDocType };
        }
        return type;
    }
    private _overwriteStructNames(customTypes: CustomType[]): CustomType[] {
        if (_.isUndefined(this._customTypeHashToName)) {
            return customTypes;
        }
        const localCustomTypes = _.cloneDeep(customTypes);
        _.each(localCustomTypes, (customType, i) => {
            const hash = SolDoc._generateCustomTypeHash(customType);
            if (!_.isUndefined(this._customTypeHashToName) && !_.isUndefined(this._customTypeHashToName[hash])) {
                localCustomTypes[i].name = this._customTypeHashToName[hash];
            }
        });
        return localCustomTypes;
    }
    private _extractStructs(compiledContract: StandardContractOutput): CustomType[] {
        let customTypes: CustomType[] = [];
        for (const abiDefinition of compiledContract.abi) {
            let types: CustomType[] = [];
            switch (abiDefinition.type) {
                case 'constructor': {
                    types = this._getStructsAsCustomTypes(abiDefinition);
                    break;
                }
                case 'function': {
                    types = this._getStructsAsCustomTypes(abiDefinition);
                    break;
                }
                case 'event':
                case 'fallback':
                    // No types exist
                    break;
                default:
                    throw new Error(
                        `unknown and unsupported AbiDefinition type '${(abiDefinition as AbiDefinition).type}'`,
                    );
            }
            customTypes = [...customTypes, ...types];
        }
        return customTypes;
    }
    private _genDocSection(compiledContract: StandardContractOutput, contractName: string): DocSection {
        const docSection: DocSection = {
            comment: _.isUndefined(compiledContract.devdoc) ? '' : compiledContract.devdoc.title,
            constructors: [],
            methods: [],
            properties: [],
            types: [],
            functions: [],
            events: [],
        };

        for (const abiDefinition of compiledContract.abi) {
            switch (abiDefinition.type) {
                case 'constructor':
                    docSection.constructors.push(
                        this._genConstructorDoc(contractName, abiDefinition, compiledContract.devdoc),
                    );
                    break;
                case 'event':
                    (docSection.events as Event[]).push(SolDoc._genEventDoc(abiDefinition));
                    // note that we're not sending devdoc to this._genEventDoc().
                    // that's because the type of the events array doesn't have any fields for documentation!
                    break;
                case 'function':
                    docSection.methods.push(this._genMethodDoc(abiDefinition, compiledContract.devdoc));
                    break;
                case 'fallback':
                    docSection.methods.push(SolDoc._genFallbackDoc(abiDefinition, compiledContract.devdoc));
                    break;
                default:
                    throw new Error(
                        `unknown and unsupported AbiDefinition type '${(abiDefinition as AbiDefinition).type}'`,
                    );
            }
        }

        return docSection;
    }
    private _genConstructorDoc(
        contractName: string,
        abiDefinition: ConstructorAbi,
        devdocIfExists: DevdocOutput | undefined,
    ): SolidityMethod {
        const { parameters, methodSignature } = this._genMethodParamsDoc('', abiDefinition.inputs, devdocIfExists);

        const comment = SolDoc._devdocMethodDetailsIfExist(methodSignature, devdocIfExists);

        const constructorDoc: SolidityMethod = {
            isConstructor: true,
            name: contractName,
            callPath: '',
            parameters,
            returnType: { name: contractName, typeDocType: TypeDocTypes.Reference }, // sad we have to specify this
            isConstant: false,
            isPayable: abiDefinition.payable,
            comment,
        };

        return constructorDoc;
    }
    private _genMethodDoc(abiDefinition: MethodAbi, devdocIfExists: DevdocOutput | undefined): SolidityMethod {
        const name = abiDefinition.name;
        const { parameters, methodSignature } = this._genMethodParamsDoc(name, abiDefinition.inputs, devdocIfExists);
        const devDocComment = SolDoc._devdocMethodDetailsIfExist(methodSignature, devdocIfExists);
        const returnType = this._genMethodReturnTypeDoc(abiDefinition.outputs);
        const returnComment =
            _.isUndefined(devdocIfExists) || _.isUndefined(devdocIfExists.methods[methodSignature])
                ? undefined
                : devdocIfExists.methods[methodSignature].return;

        const hasNoNamedParameters = _.isUndefined(_.find(parameters, p => !_.isEmpty(p.name)));
        const isGeneratedGetter = hasNoNamedParameters;
        const comment =
            _.isEmpty(devDocComment) && isGeneratedGetter
                ? `This is an auto-generated accessor method of the '${name}' contract instance variable.`
                : devDocComment;
        const methodDoc: SolidityMethod = {
            isConstructor: false,
            name,
            callPath: '',
            parameters,
            returnType,
            returnComment,
            isConstant: abiDefinition.constant,
            isPayable: abiDefinition.payable,
            comment,
        };
        return methodDoc;
    }
    /**
     * Extract documentation for each method parameter from @param params.
     */
    private _genMethodParamsDoc(
        name: string,
        abiParams: DataItem[],
        devdocIfExists: DevdocOutput | undefined,
    ): { parameters: Parameter[]; methodSignature: string } {
        const parameters: Parameter[] = [];
        for (const abiParam of abiParams) {
            const type = this._getTypeFromDataItem(abiParam);

            const parameter: Parameter = {
                name: abiParam.name,
                comment: '<No comment>',
                isOptional: false, // Unsupported in Solidity, until resolution of https://github.com/ethereum/solidity/issues/232
                type,
            };
            parameters.push(parameter);
        }

        const methodSignature = `${name}(${abiParams
            .map(abiParam => {
                if (!_.startsWith(abiParam.type, 'tuple')) {
                    return abiParam.type;
                } else {
                    // Need to expand tuples:
                    // E.g: fillOrder(tuple,uint256,bytes) -> fillOrder((address,address,address,address,uint256,uint256,uint256,uint256,uint256,uint256,bytes,bytes),uint256,bytes)
                    const isArray = _.endsWith(abiParam.type, '[]');
                    const expandedTypes = _.map(abiParam.components, c => c.type);
                    const type = `(${expandedTypes.join(',')})${isArray ? '[]' : ''}`;
                    return type;
                }
            })
            .join(',')})`;

        if (!_.isUndefined(devdocIfExists)) {
            const devdocMethodIfExists = devdocIfExists.methods[methodSignature];
            if (!_.isUndefined(devdocMethodIfExists)) {
                const devdocParamsIfExist = devdocMethodIfExists.params;
                if (!_.isUndefined(devdocParamsIfExist)) {
                    for (const parameter of parameters) {
                        parameter.comment = devdocParamsIfExist[parameter.name];
                    }
                }
            }
        }

        return { parameters, methodSignature };
    }
    private _genMethodReturnTypeDoc(outputs: DataItem[]): Type {
        let type: Type;
        if (outputs.length > 1) {
            type = {
                name: '',
                typeDocType: TypeDocTypes.Tuple,
                tupleElements: [],
            };
            for (const output of outputs) {
                const tupleType = this._getTypeFromDataItem(output);
                (type.tupleElements as Type[]).push(tupleType);
            }
            return type;
        } else if (outputs.length === 1) {
            const output = outputs[0];
            type = this._getTypeFromDataItem(output);
        } else {
            type = {
                name: 'void',
                typeDocType: TypeDocTypes.Intrinsic,
            };
        }
        return type;
    }
    private _getStructsAsCustomTypes(abiDefinition: AbiDefinition): CustomType[] {
        const customTypes: CustomType[] = [];
        // We cast to `any` here because we do not know yet if this type of abiDefinition contains
        // an `input` key
        if (!_.isUndefined((abiDefinition as any).inputs)) {
            const methodOrConstructorAbi = abiDefinition as MethodAbi | ConstructorAbi;
            _.each(methodOrConstructorAbi.inputs, input => {
                if (!_.isUndefined(input.components)) {
                    const customType = this._getCustomTypeFromDataItem(input);
                    customTypes.push(customType);
                }
            });
        }
        if (!_.isUndefined((abiDefinition as any).outputs)) {
            const methodAbi = abiDefinition as MethodAbi;
            _.each(methodAbi.outputs, output => {
                if (!_.isUndefined(output.components)) {
                    const customType = this._getCustomTypeFromDataItem(output);
                    customTypes.push(customType);
                }
            });
        }
        return customTypes;
    }
}
// tslint:disable:max-file-line-count