0

To link library in migrations I use:

await deployer.deploy(MyTokenL)
await deployer.link(MyTokenL, MyToken)

We want to use migrations as infrastructure deployment and then have a deployment factory nodejs for tokens that use the infrastructure. I read that migrations are not ideal for this factory approach because migrations are not intended to deploy the same token multiple times.

So I have the deployed MyTokenL and want to link it to a new contract deployed without migrations via:

//how link existing library, there is no deployer available???
const newlyDeployedToken = await MyToken.new()

1 Answer 1

1

This is the gold:

const linkContractWithLibrary = async (
  ContractArtifact,
  LibraryArtifact,
  libraryAddress
) => {
  const libraryName = LibraryArtifact.contractName;
  const libraryInstance = await LibraryArtifact.at(libraryAddress);

  //link function https://github.com/trufflesuite/truffle-contract/blob/develop/contract.js#L564
  ContractArtifact.link(libraryName, libraryInstance.address);

  console.log("=> Used lib " + libraryName + ":" + libraryInstance.address);
};

Use it like this:

const MyToken = artifacts.require("MyToken.sol");
const MyTokenL = artifacts.require("MyTokenL.sol");

const myTokenLAddress = "0x4921e3822Ff02E0FbfEF1CB906b785484ccaf074";

//linking existing library
await linkContractWithLibrary(
  MyToken,
  MyTokenL,
  myTokenLAddress
);

artifacts.require() is also only available in tests but I guess you can load the abi as first parameter and do the same trick...

1
  • This solved my problem :D
    – Sr.PEDRO
    Sep 4, 2020 at 2:57

Your Answer

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

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