|
| 1 | +import { readdirSync, readFileSync, statSync, writeFileSync } from 'fs' |
| 2 | +import { join, extname } from 'path' |
| 3 | +import { CONTRACTS_DIR, OUTPUT_FILE, REGEX_SELECTOR } from '@scripts' |
| 4 | + |
| 5 | +function getSolidityFiles(dir: string): string[] { |
| 6 | + const entries = readdirSync(dir) |
| 7 | + let files: string[] = [] |
| 8 | + |
| 9 | + for (const entry of entries) { |
| 10 | + const fullPath = join(dir, entry) |
| 11 | + const stats = statSync(fullPath) |
| 12 | + |
| 13 | + if (stats.isDirectory()) { |
| 14 | + files = files.concat(getSolidityFiles(fullPath)) |
| 15 | + } else if (extname(entry) === '.sol') { |
| 16 | + files.push(fullPath) |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + return files |
| 21 | +} |
| 22 | + |
| 23 | +function extractFunctions(content: string) { |
| 24 | + const regex = |
| 25 | + /function\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s+(public|external)((?:\s+\w+)*)\s*(returns\s*\(.*?\))?/g |
| 26 | + |
| 27 | + const normalFns = new Set<string>() |
| 28 | + const viewFns = new Set<string>() |
| 29 | + const pureFns = new Set<string>() |
| 30 | + |
| 31 | + const normalFnsSelector = new Set<string>() |
| 32 | + const viewFnsSelector = new Set<string>() |
| 33 | + const pureFnsSelector = new Set<string>() |
| 34 | + |
| 35 | + let match: RegExpExecArray | null |
| 36 | + |
| 37 | + while ((match = regex.exec(content)) !== null) { |
| 38 | + const [ |
| 39 | + fullMatch, |
| 40 | + name, |
| 41 | + args, |
| 42 | + visibility, |
| 43 | + modifiers = '', |
| 44 | + returns = '', |
| 45 | + ] = match |
| 46 | + |
| 47 | + const normalized = |
| 48 | + `function ${name}(${args.trim()}) ${visibility}${modifiers}${returns ? ' ' + returns.trim() : ''}` |
| 49 | + .replace(/\s+/g, ' ') |
| 50 | + .trim() |
| 51 | + |
| 52 | + const lower = modifiers.toLowerCase() |
| 53 | + const selector = REGEX_SELECTOR.exec(fullMatch) |
| 54 | + |
| 55 | + if (selector === null) continue |
| 56 | + |
| 57 | + const [fullMatchSelector] = selector |
| 58 | + |
| 59 | + if (lower.includes('pure')) { |
| 60 | + if (pureFnsSelector.has(fullMatchSelector)) continue |
| 61 | + pureFnsSelector.add(fullMatchSelector) |
| 62 | + pureFns.add(normalized) |
| 63 | + } else if (lower.includes('view')) { |
| 64 | + if (viewFnsSelector.has(fullMatchSelector)) continue |
| 65 | + viewFnsSelector.add(fullMatchSelector) |
| 66 | + viewFns.add(normalized) |
| 67 | + } else { |
| 68 | + if (normalFnsSelector.has(fullMatchSelector)) continue |
| 69 | + normalFnsSelector.add(fullMatchSelector) |
| 70 | + normalFns.add(normalized) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return { normalFns, viewFns, pureFns } |
| 75 | +} |
| 76 | + |
| 77 | +export function main() { |
| 78 | + const files = getSolidityFiles(CONTRACTS_DIR) |
| 79 | + |
| 80 | + const normalSet = new Set<string>() |
| 81 | + const viewSet = new Set<string>() |
| 82 | + const pureSet = new Set<string>() |
| 83 | + |
| 84 | + const normalSetSelectors = new Set<string>() |
| 85 | + const viewSetSelectors = new Set<string>() |
| 86 | + const pureSetSelectors = new Set<string>() |
| 87 | + |
| 88 | + for (const file of files) { |
| 89 | + const content = readFileSync(file, 'utf8') |
| 90 | + const { normalFns, viewFns, pureFns } = extractFunctions(content) |
| 91 | + |
| 92 | + normalFns.forEach((fn) => { |
| 93 | + const selector = REGEX_SELECTOR.exec(fn) |
| 94 | + |
| 95 | + if (selector === null) return |
| 96 | + |
| 97 | + const [fullMatchSelector] = selector |
| 98 | + |
| 99 | + if (normalSetSelectors.has(fullMatchSelector)) return |
| 100 | + normalSetSelectors.add(fullMatchSelector) |
| 101 | + normalSet.add(fn) |
| 102 | + }) |
| 103 | + |
| 104 | + viewFns.forEach((fn) => { |
| 105 | + const selector = REGEX_SELECTOR.exec(fn) |
| 106 | + |
| 107 | + if (selector === null) return |
| 108 | + |
| 109 | + const [fullMatchSelector] = selector |
| 110 | + |
| 111 | + if (viewSetSelectors.has(fullMatchSelector)) return |
| 112 | + viewSetSelectors.add(fullMatchSelector) |
| 113 | + viewSet.add(fn) |
| 114 | + }) |
| 115 | + |
| 116 | + pureFns.forEach((fn) => { |
| 117 | + const selector = REGEX_SELECTOR.exec(fn) |
| 118 | + |
| 119 | + if (selector === null) return |
| 120 | + |
| 121 | + const [fullMatchSelector] = selector |
| 122 | + |
| 123 | + if (pureSetSelectors.has(fullMatchSelector)) return |
| 124 | + pureSetSelectors.add(fullMatchSelector) |
| 125 | + pureSet.add(fn) |
| 126 | + }) |
| 127 | + } |
| 128 | + |
| 129 | + const output = [ |
| 130 | + '====== ✅ Non-view/pure external/public methods ======\n', |
| 131 | + ...Array.from(normalSet).sort(), |
| 132 | + '\n====== 👁️ View functions ======\n', |
| 133 | + ...Array.from(viewSet).sort(), |
| 134 | + '\n====== 🧪 Pure functions ======\n', |
| 135 | + ...Array.from(pureSet).sort(), |
| 136 | + ] |
| 137 | + |
| 138 | + writeFileSync(OUTPUT_FILE, output.join('\n'), 'utf8') |
| 139 | + console.log(`✅ Methods extracted to ${OUTPUT_FILE}`) |
| 140 | +} |
| 141 | + |
| 142 | +main() |
0 commit comments