Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolver for non-local imports in compileSol function? #246

Open
kaifirschau opened this issue Jan 11, 2024 · 4 comments
Open

Resolver for non-local imports in compileSol function? #246

kaifirschau opened this issue Jan 11, 2024 · 4 comments
Labels
question Further information is requested

Comments

@kaifirschau
Copy link

kaifirschau commented Jan 11, 2024

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.

@kaifirschau kaifirschau changed the title Resolver for external imports in compileSol function? Resolver for non-local imports in compileSol function? Jan 11, 2024
@blitz-1306
Copy link
Contributor

blitz-1306 commented Jan 11, 2024

Hello there.

Currently package supports only resolvers that you mentioned. We also provide pathOptions argument for compile*() function family:

const pathOptions: PathOptions = {
remapping: options.pathRemapping ? options.pathRemapping.split(";") : [],
basePath: options.basePath,
includePath: options.includePath

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.

@blitz-1306 blitz-1306 added the question Further information is requested label Jan 11, 2024
@blitz-1306
Copy link
Contributor

blitz-1306 commented Jan 11, 2024

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.

@kaifirschau
Copy link
Author

@blitz-1306 great, thank you very much for the quick & helpful reply!

@kaifirschau
Copy link
Author

kaifirschau commented Jan 15, 2024

@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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants