Compiling non-inlinable libraries
Learn how to handle compiling non-inlinable libraries.
Solidity libraries can be divided into two categories:
Inlinable. The ones that contain only
private
orinternal
methods. Since they can never be called from outside, the Solidity compiler inlines them, i.e. does not use external calls to access the library methods and uses the code of these libraries as part of the code that uses them.Non-inlinable. The ones that have at least one
public
orexternal
method. While they may be inlined by the Solidity compiler, they are not inlined when compiled to Yul representation. Since Yul is an intermediate step when compiling to Validum VM bytecode, this means that these libraries can not be inlined by the Validum compiler.
Libraries with public methods must be deployed separately, and their addresses should be passed as arguments when compiling the main contract. Usage of the methods of this library will be replaced with calls to its address.
OpenZeppelin utility libraries
Please note, that the total majority of the OpenZeppelin utility libraries are inlinable. That means that there is no need to do any further actions to make them compile.
This section describes the compilation of non-inlinable libraries only.
Example
Let's say that we have a small library that calculates the square of a number:
And there is a smart contract that uses this library
Support for missing libraries in hardhat-zksync-solc ^0.4.2: Version 0.4.2 introduced a mode that detects non-inlinable libraries that are missing and that are required for the compilation of contracts.
Using hardhat-zksync-solc version >= 0.4.2
Following error:
Choose the method that best suits your preferences or requirements.
Non-inline libraries deployment
Automatic deployment
This approach is effective only with specific plugin versions:
hardhat-zksync-solc
>= 0.4.2hardhat-zksync-deploy
>= 0.6.5
Make sure that you are using the specified versions or a later versions to ensure compatibility with the described resolution method.Vyper does not support automatic deployment of missing libraries, and the process needs to be handled manually.
Manual deployment
Let's say that the address of the deployed library is 0xF9702469Dfb84A9aC171E284F71615bd3D3f1EdC
. To pass this address to the compiler parameters, open the hardhat.config.ts
file of the project where the Main
contract is located and add the libraries
section in the zksolc
plugin properties:
The address of the library is passed in the following lines:
where 'contracts/MiniMath.sol'
is the location of the library's Solidity file and MiniMath
is the name of the library.
Now, running yarn hardhat compile
should successfully compile the Main
contract.
Last updated