|
| 1 | +import { CodeBlock } from "@/components/ui/CodeBlock"; |
| 2 | +import { Badge } from "@/components/ui/badge"; |
| 3 | +import { useContractSources } from "contract-ui/hooks/useContractSources"; |
| 4 | +import { useMemo } from "react"; |
| 5 | +import type { ThirdwebContract } from "thirdweb"; |
| 6 | + |
| 7 | +/** |
| 8 | + * Take in a contract & function, try to fetch the comment of that function |
| 9 | + */ |
| 10 | +export default function ContractFunctionComment({ |
| 11 | + contract, |
| 12 | + functionName, |
| 13 | +}: { contract: ThirdwebContract; functionName: string }) { |
| 14 | + const sourceQuery = useContractSources(contract); |
| 15 | + const comment = useMemo(() => { |
| 16 | + if (!sourceQuery.data?.length) { |
| 17 | + return null; |
| 18 | + } |
| 19 | + const file = sourceQuery.data.find((item) => |
| 20 | + item.source.includes(functionName), |
| 21 | + ); |
| 22 | + if (!file) { |
| 23 | + return null; |
| 24 | + } |
| 25 | + return extractFunctionComment(file.source, functionName); |
| 26 | + }, [sourceQuery.data, functionName]); |
| 27 | + |
| 28 | + if (sourceQuery.isLoading) { |
| 29 | + return null; |
| 30 | + } |
| 31 | + if (!comment) { |
| 32 | + return null; |
| 33 | + } |
| 34 | + return ( |
| 35 | + <> |
| 36 | + <p className="mt-6"> |
| 37 | + About this function <Badge>Beta</Badge> |
| 38 | + </p> |
| 39 | + <CodeBlock code={comment} canCopy={false} language="solidity" /> |
| 40 | + </> |
| 41 | + ); |
| 42 | +} |
| 43 | + |
| 44 | +function extractFunctionComment( |
| 45 | + // Tthe whole code from the solidity file containing (possibly) the function |
| 46 | + solidityCode: string, |
| 47 | + functionName: string, |
| 48 | +): string | null { |
| 49 | + // Regular expression to match function declarations and their preceding comments |
| 50 | + // This regex now captures both single-line (//) and multi-line (/** */) comments |
| 51 | + const functionRegex = |
| 52 | + /(?:\/\/[^\n]*|\/\*\*[\s\S]*?\*\/)\s*function\s+(\w+)\s*\(/g; |
| 53 | + |
| 54 | + while (true) { |
| 55 | + const match = functionRegex.exec(solidityCode); |
| 56 | + if (match === null) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + const [fullMatch, name] = match; |
| 60 | + if (!fullMatch || !fullMatch.length) { |
| 61 | + return null; |
| 62 | + } |
| 63 | + if (name === functionName) { |
| 64 | + // Extract the comment part |
| 65 | + const comment = (fullMatch.split("function")[0] || "").trim(); |
| 66 | + if (!comment) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + // comment = comment |
| 70 | + // .replace(/^\/\*\*|\*\/$/g, "") // Remove start and end of block comments |
| 71 | + // .replace(/^\s*\*\s?/gm, "") // Remove leading asterisks and spaces from each line |
| 72 | + // .replace(/^\/\//gm, "") // Remove single-line comment symbols |
| 73 | + // .split("\n") // Split into lines |
| 74 | + // .map((line) => line.trim()) // Trim each line |
| 75 | + // .filter((line) => line !== "") // Remove empty lines |
| 76 | + // .join("\n"); // Join back into a single string |
| 77 | + |
| 78 | + if (/^[^a-zA-Z0-9]+$/.test(comment)) { |
| 79 | + return null; |
| 80 | + } |
| 81 | + return comment; |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments