File size: 1,000 Bytes
7e26449
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
017c628
7e26449
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import * as parser from "@solidity-parser/parser";

export interface ConstructorInfo {
  parameters: string;
}

export function extractConstructor(sourceCode: string, contractName: string): ConstructorInfo | null {
  try {
    const ast = parser.parse(sourceCode, { range: true });
    let constructorParams = "";
    let found = false;

    parser.visit(ast, {
      ContractDefinition: (node) => {
        if (node.name === contractName) {
          for (const part of node.subNodes) {
            if (part.type === "FunctionDefinition" && (part as any).isConstructor) {
              found = true;
              if (part.range) {
                  constructorParams = sourceCode.slice(part.range[0], part.range[1]).split("{")[0].trim();
              }
            }
          }
        }
      }
    });

    if (found) {
      return {
        parameters: constructorParams
      };
    }
  } catch (e) {
    // console.warn("Failed to parse Solidity for constructor:", e);
  }
  return null;
}