solc-typed-ast
solc-typed-ast copied to clipboard
Resolver for non-local imports in compileSol function?
Description
Hello, I am not sure if this is a feature request or support request.
Can I compile smart contracts that contain external imports using solc-typed-ast?
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Permit.sol";
contract MyToken is ERC20, ERC20Permit {
constructor() ERC20("MyToken", "MTK") ERC20Permit("MyToken") {}
}
Trying to compile the above contract with solc-typed-ast results in:
CompileInferenceError: Couldn't find @openzeppelin/contracts/token/ERC20/extensions/ERC20Permit.sol
For example, in the @remix-project/remix-solidity package, I can specify a callback function like this and pass it to the compile function, which leads to compiling the above contract like a charm :
export function handleNpmImport(url: string, cb: (error: any, content: string) => void) {
try {
const req = 'https://unpkg.com/' + url
axios.get(req).then(response => cb(null, response.data))
} catch (e) {
throw e
}
}
Is it possible to resolve external imports like this in solc-typed-ast?
In the codebase, I have only found LocalNpmResolver and FileSystemResolver.
Thank you very much in advance.
Hello there.
Currently package supports only resolvers that you mentioned. We also provide pathOptions argument for compile*() function family: https://github.com/Consensys/solc-typed-ast/blob/36ee1b0b79f24086ae43d721f5ea322d792ed053/src/bin/compile.ts#L205-L208
You can pre-download necessary referenced sources and reference them via path remappings (similarly to what Solidity compiler supports).
Package not yet support remote source downloading and we not yet received any feature requests for this functionality.
As the other option, you can compile Solidity on your own (with external logic), and the supply resulting compiler AST to an ASTReader (if you want to use our traversal logic and some other functionality, provided by the package). Basically it does not really matter how you would get Solc AST in order to supply to a downstream package components.
@blitz-1306 great, thank you very much for the quick & helpful reply!
@blitz-1306 another question - can you tell me how such a path remapping could be implemented (simplified)?
Also, with the classic solc something like this is possible:
solc.compile(JSON.stringify(input), { import: findImports })
Would defining such an import resolver function also work in solc-typed-ast?
Please excuse me, I know my request is quite specific but I think there may also be others who will find this helpful.
Thank you very much in advance!