shazow / whatsabi

Extract the ABI (and resolve proxies, and get other metadata) from Ethereum bytecode, even without source code.
https://shazow.github.io/whatsabi/
MIT License
1.06k stars 74 forks source link

Feature Request: Fallback for Contract Name in Sourcify ABI Loader #117

Closed yohamta closed 1 month ago

yohamta commented 1 month ago

Currently, in the Sourcify ABI Loader, the contract name is retrieved from the Natspec comments (i.e., devdoc.title). However, if the contract name is missing in the Natspec comments, no fallback is provided, which results in the contract name being null. I propose adding a fallback mechanism to retrieve the contract name from the compiler settings' compilationTarget if it's not available in the Natspec comments.

https://github.com/shazow/whatsabi/blob/7e9d811679cb4b296fd29491c0929a7b05f1a301/src/loaders.ts#L252-L268

Proposed Change:

             // Note: Sometimes metadata.json contains sources, but not always. So we can't rely on just the metadata.json
             const m = JSON.parse(metadata.content);

+            // Sourcify includes a title from the Natspec comments
+            let name = m.output.devdoc?.title;
+
+            if (!name && m.settings.compilationTarget) {
+                // Try to use the compilation target name as a fallback
+                const targetNames = Object.values(m.settings.compilationTarget);
+                if (targetNames.length > 0) {
+                    name = targetNames[0];
+                }
+            }
+
             return {
                 abi: m.output.abi,
-                name: m.output.devdoc?.title ?? null, // Sourcify includes a title from the Natspec comments
+                name: name ?? null,
                 evmVersion: m.settings.evmVersion,
                 compilerVersion: m.compiler.version,
                 runs: m.settings.optimizer.runs,

If this feature request is accepted, I will be happy to open a pull request implementing the above change.

shazow commented 1 month ago

Looks good to me, let's do that! Thank you. :)