importing more than one library with foundry

Posted on Fri 26 May 2023 in tech

One feature that is missing in Foundry and Forge is the ability to dynamically bind libraries. In fact, it can be pretty difficult to understand how to bind libraries in general and the documenation is fairly lacking. It took me several tries to get it right.

How do I link libraries with forge? Can't really find any docs

The docs are the here

Also here is an example of specifying a library which has been deployed previously in the call to forge, note the call to `--libraries:

$ forge script ./script/DeployTreasuryBuilder.sol --sig 'deploy()' --resume --libraries ./lib/openzeppelin-contracts/contracts/utils/cryptography/ECDSA.sol:ECDSA:${ECDSA_LIB_ADDRESS} --slow --broadcast --rpc-url ${RPC_URL} --private-key ${PRIVATE_KEY} --etherscan-api-key ${ETHERSCAN_API_KEY} --verify

More importantly here is an example of linking two libraries that have previously been deployed. I first tried comma and colon separators and find the idea that a space is used here very confusing because the grammar is not consistent acrosss the whole command line. Nevertheless a space works here.

$forge script ./script/DeployTreasuryBuilder.sol --sig 'deploy()' --resume --libraries contracts/Constant.sol:Constant:${CONSTANT_LIB_ADDRESS} ./lib/openzeppelin-contracts/contracts/utils/cryptography/ECDSA.sol:ECDSA:${ECDSA_LIB_ADDRESS} --slow --broadcast --rpc-url ${RPC_URL} --private-key ${PRIVATE_KEY} --etherscan-api-key ${ETHERSCAN_API_KEY} --verify