For now I use this code to link library during truffle migration:

deployer.link(SafeMathLibExt, [CrowdsaleTokenExt]);

This code deploys library each time and I don't like it. I want to deploy library only once and then always use it. So I want something like that (pseudo code):

const library = deployer.getLibrary(libraryAddress);
deployer.link(library, [CrowdsaleTokenExt]);

is it possible to achieve in truffle?


Well you would have to go outside truffle and deploy the script manually. By doing that the library contract would be outside the purview of truffle, so whenever you do truffle migrate, only the token contract is deployed again.

So it would work something like this:

var web3 = require("web3");
var fs = require("fs");
var solc = require("solc");

web3 = new web3(new web3.providers.HttpProvider("http://localhost:8545"));

var compiledCode = 

solc.compile(fs.readFileSync('./contracts/SafeMathLibExt.sol', 'utf8'),1);

var source = compiledCode.contracts[":SafeMathLibExt"];

var tokencontract = new  web3.eth.Contract(JSON.parse(source.interface));

tokencontract.deploy({data: source.bytecode}).send({from: Owneraddress, 
gas:1500000}).on('confirmation', function(confirmationNumber, receipt){ 
console.log(confirmationNumber); tokencontract.options.address = 
receipt.contractAddress; }).on('receipt', function(receipt)
  • Thanks for the answer, I got your idea. How can I pass deployed library address into truffle deployer? Could you provide code example or link to truffle documentation? – Egor Bukraba Dec 6 '17 at 19:51
  • this doesn't answer the question – okwme Mar 22 '18 at 16:58
  • 2
    This is ridiculous that truffle doesn't allow this natively. – OneChillDude Feb 19 '20 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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