| 1 | interface DevDocMethod { |
| 2 | details?: string; |
| 3 | params?: Record<string, string>; |
| 4 | } |
| 5 | |
| 6 | interface DevDoc { |
| 7 | kind?: string; |
| 8 | title?: string; |
| 9 | details?: string; |
| 10 | methods: Record<string, DevDocMethod>; |
| 11 | stateVariables?: Record<string, { details: string }>; |
| 12 | version?: number; |
| 13 | } |
| 14 | |
| 15 | interface ContractResponse { |
| 16 | devdoc: DevDoc; |
| 17 | abi: any[]; |
| 18 | matchId?: string; |
| 19 | creationMatch?: string; |
| 20 | runtimeMatch?: string; |
| 21 | verifiedAt?: string; |
| 22 | match?: string; |
| 23 | chainId?: string; |
| 24 | address?: string; |
| 25 | } |
| 26 | |
| 27 | export function parseContractToMarkdown(data: ContractResponse): string { |
| 28 | let markdownContent = ""; |
| 29 | |
| 30 | // Add contract header information |
| 31 | if (data.devdoc?.title) { |
| 32 | markdownContent += `# ${data.devdoc.title}\n\n`; |
| 33 | } |
| 34 | |
| 35 | if (data.devdoc?.details) { |
| 36 | markdownContent += `${data.devdoc.details}\n\n`; |
| 37 | } |
| 38 | |
| 39 | // Add contract verification info |
| 40 | if (data.address && data.chainId) { |
| 41 | markdownContent += `**Contract:** \`${data.address}\` on Chain ID \`${data.chainId}\`\n\n`; |
| 42 | } |
| 43 | |
| 44 | if (data.verifiedAt) { |
| 45 | markdownContent += `**Verified:** ${new Date(data.verifiedAt).toLocaleDateString()}\n\n`; |
| 46 | } |
| 47 | |
| 48 | // Process methods |
| 49 | if (data.devdoc?.methods) { |
| 50 | Object.entries(data.devdoc.methods).forEach(([methodName, method]) => { |
| 51 | markdownContent += `## ${methodName}\n\n`; |
| 52 | |
| 53 | if (method.details) { |
| 54 | // Replace \n with actual newlines |
| 55 | const processedDetails = method.details.replace(/\\n/g, '\n'); |
| 56 | markdownContent += `${processedDetails}\n\n`; |
| 57 | } |
| 58 | |
| 59 | if (method.params) { |
| 60 | Object.entries(method.params).forEach(([paramName, paramDesc]) => { |
| 61 | markdownContent += `**${paramName}:** `; |
| 62 | // Replace \n with actual newlines |
| 63 | const processedParamDesc = paramDesc.replace(/\\n/g, '\n'); |
| 64 | markdownContent += `${processedParamDesc}\n\n`; |
| 65 | }); |
| 66 | } |
| 67 | |
| 68 | markdownContent += "\n"; |
| 69 | }); |
| 70 | } |
| 71 | |
| 72 | return markdownContent; |
| 73 | } |
| 74 | |
| 75 | export type { ContractResponse, DevDoc, DevDocMethod }; |