1

I started using web3j and I want to generate smart contract wrapper. According to documentation, it can be done this way:

web3j generate solidity -b /path/to/<smart-contract>.bin -a /path/to/<smart-contract>.abi -o /path/to/src/main/java -p com.your.organisation.name

And this works very good. You need .bin and .abi files for it. It's easy to generate as well with this command:

solcjs <contract>.sol --abi --bin

On contracts where I don't import Openzeppelin, this works great. But on contracts where I do import Openzeppelin, I get an error when calling the solcjs command:

ParserError: Source "@openzeppelin/contracts/token/ERC721/ERC721.sol" not found: File not found inside the base path or any of the include paths.
 --> MyNFT.sol:5:1:
  |
5 | import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


ParserError: Source "@openzeppelin/contracts/utils/Counters.sol" not found: File not found inside the base path or any of the include paths.
 --> MyNFT.sol:6:1:
  |
6 | import "@openzeppelin/contracts/utils/Counters.sol";
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


ParserError: Source "@openzeppelin/contracts/access/Ownable.sol" not found: File not found inside the base path or any of the include paths.
 --> MyNFT.sol:7:1:
  |
7 | import "@openzeppelin/contracts/access/Ownable.sol";
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Syntax how I import this library in smart contract in Solidity:

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

I use pragma solidity >=0.7.3;

Smartcontract is valid, I compiled it with hardhat with no problem.

I imported openzeppelin version - @openzeppelin/contracts@3.1.0-solc-0.7

Where those files should be to be able generate .abi and .bin files? Thank you for help.

2 Answers2

1

I had the same error, and finally found the solution nowhere else, but the GitHub page of the Solidity compiler itself! See this.

To compile a contract that imports other contracts via relative paths:
solcjs --bin --include-path node_modules/ --base-path . MainContract.sol
Use the --base-path and --include-path options to describe the layout of your project. --base-path represents the root of your own source tree while --include-path allows you to specify extra locations containing external code (e.g. libraries installed with a package manager).
Note: ensure that all the files you specify on the command line are located inside the base path or one of the include paths. The compiler refers to files from outside of these directories using absolute paths. Having absolute paths in contract metadata will result in your bytecode being reproducible only when it's placed in these exact absolute locations.

Assuming that all your packages (including OpenZeppelin) are nicely organized within a single node_modules directory (probably a local one that you created for your project), just replace node_modules/ with the path to this node-modules directory. And similarly, replace . with the path to the directory which contains the contract you are compiling.

The only issue with compiling the contract this way, is that it generates ABI files for all contracts, including the ones that you have imported to the main contract (and the ones they import, and so on). I don't know if there's any command line option which specifies which ABI files to produce (for example, to produce only the ABI file for MainContract.sol).

PS: You can also specify the output directory for your .abi and .bin output files using the -o option. This turns out to be very helpful specially when a large number of output files are being produced.

0

You need to download the openzeppelin contracts directly on your hard drive and import it with the file system. You can do this with the following command :"npm i openzeppelin-solidity"

  • Thank you, I will try it. Last time I solved it with npx hardhat compile and then in artifacts/.json you have abi and byte code, so I copy it manually from there and create new files .abi and .bin manually :D And it works :) But I'll try your idea and let you know if it helped. I dont want to do it manually all the time. – David Tilšer Jan 20 '22 at 09:18
  • It didn't help. Still same error. – David Tilšer Jan 25 '22 at 14:57