[Ethereum] what does `deploy.link` exactly do in `truffle`

truffletruffle-contracttruffle-migration

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

Best Answer

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.deploy(Converter);
    deployer.link(Converter, MainContract);
    deployer.deploy(MainContract);
};

or you can use:

module.exports = function(deployer) {
    deployer.deploy(Converter);
    deployer.autolink();
    deployer.deploy(MainContract);
};

You can also find lots of useful information in this video

Related Topic