Trouble linking library contracts with openzeppelin/test-environment

I’m trying to deploy a contract that uses several libraries for testing with openzeppelin/test-environment. I’m giving this a shot as Truffle testing has gotten very slow.
The problem is that when I run my deploy utility, library function won’t link. Running the same utility on truffle works just fine.

:computer: Environment

I’m using truffle and openzeppelin/test-environment.
:memo:Details

Compile, getArtifacts as follows:

const MyContract = contract.fromArtifact(‘MyContract’)

Create an array of my artifacts and pass them to a deploying/linking utility, The deploy utility
runs newContract.link(previousContract.address)
This link fails with error:
Error: Contract has no network id set, cannot lookup artifact data. Either set the network manually using Contract.setNetwork(), run Contract.detectNetwork(), or use new(), at() or deployed() as a thenable which will detect the network automatically.

when the contract with the dependency reaches is to be deployed, errors with:
You must deploy and link the following libraries before you can deploy a new version of Contract: (list of libraries)

Any guidance would be much appreceated
:1234: Code to reproduce

3 Likes

Hi @nicholas,

Welcome to the community :wave:

What is the deploy/linking utility that you are using? Is this a Truffle migrations script or something else?

Do you have a repository that you can share?

1 Like

Welcome to the community :wave:

Hi!! thanks for the welcome :slight_smile:

What is the deploy/linking utility that you are using? Is this a Truffle migrations script or something else?

Not a migrations script (although we have one) It was just a .js module that linked contracts. I've moved away from this to try to figure out what's going on. I am now trying to link contracts individually and it's still not working:

Contract contains unresolved libraries. You must deploy and link the following libraries before you can deploy a new version of Contract: LibraryToDeploy

I would love some advice on how to link libraries to contracts in tests (not through migrations, just a local link for testing)

Do you have a repository that you can share?

:woozy_face: not public yet

2 Likes

Hello @nicholas, thanks for asking about this!

According to @truffle/contract's documentation, the link method should suffice. However, these things often assume they are being used in a truffle environment, which have quite a bit of setup done beforehand, and it is not always easy to use them in standalone scripts.

I got the library to link by calling detectNetwork manually and using an alternate form of link: I'm not entirely sure why the one-argument variant doesn't work properly:

const MyLibrary = contract.fromArtifact('MyLibrary');
const MyContract = contract.fromArtifact('MyContract');

...

const myLibrary = await MyLibrary.new();
await MyContract.detectNetwork();
await MyContract.link('MyLibrary', myLibrary.address);
await MyContract.new();

With that, I was able to properly link the contract and call functions that are forwarded to the library. Let me know if that snippet works for you!

7 Likes

this worked! thank you :slight_smile:

1 Like