aboutsummaryrefslogtreecommitdiffstats
path: root/packages/react-docs/src/utils/doxity_utils.ts
blob: 6815daa0c73ee9d5d75a20581c717138b579af85 (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
import * as _ from 'lodash';

import {
    AbiTypes,
    DocAgnosticFormat,
    DocSection,
    DoxityAbiDoc,
    DoxityContractObj,
    DoxityDocObj,
    DoxityInput,
    EventArg,
    Parameter,
    Property,
    SolidityMethod,
    Type,
    TypeDocTypes,
} from '../types';

export const doxityUtils = {
    convertToDocAgnosticFormat(doxityDocObj: DoxityDocObj): DocAgnosticFormat {
        const docAgnosticFormat: DocAgnosticFormat = {};
        _.each(doxityDocObj, (doxityContractObj: DoxityContractObj, contractName: string) => {
            const doxityConstructor = _.find(doxityContractObj.abiDocs, (abiDoc: DoxityAbiDoc) => {
                return abiDoc.type === AbiTypes.Constructor;
            });
            const constructors = [];
            if (!_.isUndefined(doxityConstructor)) {
                const constructor = {
                    isConstructor: true,
                    name: doxityContractObj.name,
                    comment: doxityConstructor.details,
                    returnComment: doxityConstructor.return,
                    callPath: '',
                    parameters: doxityUtils._convertParameters(doxityConstructor.inputs),
                    returnType: doxityUtils._convertType(doxityContractObj.name),
                };
                constructors.push(constructor);
            }

            const doxityMethods: DoxityAbiDoc[] = _.filter<DoxityAbiDoc>(
                doxityContractObj.abiDocs,
                (abiDoc: DoxityAbiDoc) => {
                    return doxityUtils._isMethod(abiDoc);
                },
            );
            const methods: SolidityMethod[] = _.map<DoxityAbiDoc, SolidityMethod>(
                doxityMethods,
                (doxityMethod: DoxityAbiDoc) => {
                    const outputs = !_.isUndefined(doxityMethod.outputs) ? doxityMethod.outputs : [];
                    let returnTypeIfExists: Type;
                    if (outputs.length === 0) {
                        // no-op. It's already undefined
                    } else if (outputs.length === 1) {
                        const outputsType = outputs[0].type;
                        returnTypeIfExists = doxityUtils._convertType(outputsType);
                    } else {
                        const outputsType = `[${_.map(outputs, output => output.type).join(', ')}]`;
                        returnTypeIfExists = doxityUtils._convertType(outputsType);
                    }
                    // For ZRXToken, we want to convert it to zrxToken, rather then simply zRXToken
                    const callPath =
                        contractName !== 'ZRXToken'
                            ? `${contractName[0].toLowerCase()}${contractName.slice(1)}.`
                            : `${contractName.slice(0, 3).toLowerCase()}${contractName.slice(3)}.`;
                    const method = {
                        isConstructor: false,
                        isConstant: doxityMethod.constant,
                        isPayable: doxityMethod.payable,
                        name: doxityMethod.name,
                        comment: doxityMethod.details,
                        returnComment: doxityMethod.return,
                        callPath,
                        parameters: doxityUtils._convertParameters(doxityMethod.inputs),
                        returnType: returnTypeIfExists,
                    };
                    return method;
                },
            );

            const doxityProperties: DoxityAbiDoc[] = _.filter<DoxityAbiDoc>(
                doxityContractObj.abiDocs,
                (abiDoc: DoxityAbiDoc) => {
                    return doxityUtils._isProperty(abiDoc);
                },
            );
            const properties = _.map<DoxityAbiDoc, Property>(doxityProperties, (doxityProperty: DoxityAbiDoc) => {
                // We assume that none of our functions return more then a single return value
                let typeName = doxityProperty.outputs[0].type;
                if (!_.isEmpty(doxityProperty.inputs)) {
                    // Properties never have more then a single input
                    typeName = `(${doxityProperty.inputs[0].type} => ${typeName})`;
                }
                const property = {
                    name: doxityProperty.name,
                    type: doxityUtils._convertType(typeName),
                    comment: doxityProperty.details,
                };
                return property;
            });

            const doxityEvents = _.filter(
                doxityContractObj.abiDocs,
                (abiDoc: DoxityAbiDoc) => abiDoc.type === AbiTypes.Event,
            );
            const events = _.map(doxityEvents, doxityEvent => {
                const event = {
                    name: doxityEvent.name,
                    eventArgs: doxityUtils._convertEventArgs(doxityEvent.inputs),
                };
                return event;
            });

            const docSection: DocSection = {
                comment: doxityContractObj.title,
                constructors,
                methods,
                properties,
                types: [],
                functions: [],
                events,
            };
            docAgnosticFormat[contractName] = docSection;
        });
        return docAgnosticFormat;
    },
    _convertParameters(inputs: DoxityInput[]): Parameter[] {
        const parameters = _.map(inputs, input => {
            const parameter = {
                name: input.name,
                comment: input.description,
                isOptional: false,
                type: doxityUtils._convertType(input.type),
            };
            return parameter;
        });
        return parameters;
    },
    _convertType(typeName: string): Type {
        const type = {
            name: typeName,
            typeDocType: TypeDocTypes.Intrinsic,
        };
        return type;
    },
    _isMethod(abiDoc: DoxityAbiDoc): boolean {
        if (abiDoc.type !== AbiTypes.Function) {
            return false;
        }
        const hasInputs = !_.isEmpty(abiDoc.inputs);
        const hasNamedOutputIfExists = !hasInputs || !_.isEmpty(abiDoc.inputs[0].name);
        const isNameAllCaps = abiDoc.name === abiDoc.name.toUpperCase();
        const isMethod = hasNamedOutputIfExists && !isNameAllCaps;
        return isMethod;
    },
    _isProperty(abiDoc: DoxityAbiDoc): boolean {
        if (abiDoc.type !== AbiTypes.Function) {
            return false;
        }
        const hasInputs = !_.isEmpty(abiDoc.inputs);
        const hasNamedOutputIfExists = !hasInputs || !_.isEmpty(abiDoc.inputs[0].name);
        const isNameAllCaps = abiDoc.name === abiDoc.name.toUpperCase();
        const isProperty = !hasNamedOutputIfExists || isNameAllCaps;
        return isProperty;
    },
    _convertEventArgs(inputs: DoxityInput[]): EventArg[] {
        const eventArgs = _.map(inputs, input => {
            const eventArg = {
                isIndexed: input.indexed,
                name: input.name,
                type: doxityUtils._convertType(input.type),
            };
            return eventArg;
        });
        return eventArgs;
    },
};