I have a contract like this

pragma solidity ^0.4.24;

import './MyLibrary.sol';

contract A {
  // Do Stuff

And I have a factory contract instanciating A contracts

pragma solidity ^0.4.24;

import './A.sol';

contract B {
  mapping(uint => address) instances;
  function newA(uint id) {
    instances[id] = new A();

I want to use B as a factory, and A is calling MyLibrary regularly. Is there any way with Truffle migrations to do something like this (deploying MyLibrary on the network, and then linking each A deployed contracts with the library)? It's easy to link a library to a contract during deployment, but I don't know how to do it dynamically on the blockchain.

1 Answer 1


Since B is instantiating A, and A is using the library. B will need to be linked to the library just like it is the case for A.

So in your truffle migration script, you would do the following:

deployer.then(async function() {
    await deployer.deploy(MyLibrary);
    await deployer.link(MyLibrary, [A, B]);
    await deployer.deploy(b);

    var b = await B.deployed();

    await b.newA(100);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.