Please explain use cases for deploy.link() function in migrations in truffle framework. How would you use it and for what.

4 Answers 4


EDIT: As stated in the comment by @feihcsim autolink has been deprecated

Let's say ecosystem of your dapp has a library and a contract that calls functions from that library. So you have:

library Converter {
    function weiToEther() returns (uint256) { //return ether value }

Then you have contract:

import "Converter.sol";

contract MainContract {
    function getBalanceInEth() returns (uint256) {
        return Converter.weiToEther(this.balance);

So, in order for the MainContract to work, you will first need library deployed on the chain and also MainContract needs to know address of that library. To explicitly do this, you use:

module.exports = function(deployer) {
    deployer.link(Converter, MainContract);

or you can use:

module.exports = function(deployer) {

You can also find lots of useful information in this video

  • 7
    autolink has been deprecated
    – feihcsim
    Dec 30, 2017 at 14:18
  • Wouldn't it be better to use promises by waiting for Converter to be deployed? Sep 18, 2018 at 18:55
  • The way it works is that actual address of the deployed library have to be inserted into contracts bytecode before deployment of that contract. So you can wait for async response and than insert it on the right spot. Look at both answers on next question, you can see how it works and how it can be linked in web3 ethereum.stackexchange.com/questions/6927/… . My opinion is that you don't have to do it manually since you are using truffle, except you have a valid reason. Sep 22, 2018 at 10:05

This function adds the given library to the links property of the destination contract. This ensures that the location of the deployed library can be found when the contract's bytecode is generated.

It also merges all of the library's events in with the destination contract.

API docs here: https://github.com/trufflesuite/truffle-contract#mycontractlinkinstance

See source here: https://github.com/trufflesuite/truffle-contract/blob/develop/contract.js#L539

As mentioned above by @feihcsim, this has to be done manually now: https://github.com/trufflesuite/truffle-deployer/commit/52e63189453e078f64b84aa3a50a2c7991b022b4


From documentation:

Link an already-deployed library to a contract or multiple contracts. destinations can be a single contract or an array of multiple contracts. If any contract within the destination doesn't rely on the library being linked, the deployer will ignore that contract.


// Deploy library LibA, then link LibA to contract B, then deploy B.
deployer.link(LibA, B);

// Link LibA to many contracts
deployer.link(LibA, [B, C, D]);

Take the default metacoin project that comes with truffle for example

// deploy ConvertLib into the network
// Take the deployed ConvertLib address and link it to MetaCoin contract in bytecode
deployer.link(ConvertLib, MetaCoin);
// Now deploy the linked metacoin contract to the network

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.