Skip to content

Commit 475e558

Browse files
committed
update
1 parent 0660416 commit 475e558

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
70+
if (/^[^a-zA-Z0-9]+$/.test(comment)) {
71+
return null;
72+
}
73+
return comment;
74+
}
75+
}
76+
}

apps/dashboard/src/components/contract-functions/contract-function.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ import type { AbiEvent, AbiFunction } from "abitype";
2929
import { camelToTitle } from "contract-ui/components/solidity-inputs/helpers";
3030
import { SearchIcon } from "lucide-react";
3131
import { usePathname, useSearchParams } from "next/navigation";
32-
import { type Dispatch, type SetStateAction, useMemo, useState } from "react";
32+
import {
33+
type Dispatch,
34+
type SetStateAction,
35+
lazy,
36+
useMemo,
37+
useState,
38+
} from "react";
3339
import type { ThirdwebContract } from "thirdweb";
3440
import * as ERC20Ext from "thirdweb/extensions/erc20";
3541
import * as ERC721Ext from "thirdweb/extensions/erc721";
@@ -47,6 +53,9 @@ import { CodeSegment } from "../contract-tabs/code/CodeSegment";
4753
import type { CodeEnvironment } from "../contract-tabs/code/types";
4854
import { InteractiveAbiFunction } from "./interactive-abi-function";
4955

56+
const ContractFunctionComment = lazy(
57+
() => import("./contract-function-comment"),
58+
);
5059
interface ContractFunctionProps {
5160
fn: AbiFunction | AbiEvent;
5261
contract: ThirdwebContract;
@@ -136,7 +145,6 @@ function ContractFunctionInner({ contract, fn }: ContractFunctionProps) {
136145
/>
137146
)}
138147
</Flex>
139-
140148
{isFunction && (
141149
<InteractiveAbiFunction
142150
key={JSON.stringify(fn)}
@@ -158,6 +166,7 @@ function ContractFunctionInner({ contract, fn }: ContractFunctionProps) {
158166
/>
159167
</>
160168
)}
169+
<ContractFunctionComment contract={contract} functionName={fn.name} />
161170
</Flex>
162171
);
163172
}

0 commit comments

Comments
 (0)