2

I am trying to use a solidity library inside another. The minimal code would be something like this:

The deepest library: SLib.sol

pragma solidity ^0.4.18;

library SLib {
  struct SCtx {
    uint a;
  }

  function init(SCtx storage self, uint _a) public
  {
    self.a = _a;
  }

  function getA(SCtx storage self) public view returns (uint)
  {
    return self.a;
  }
}

Then TLib.sol uses the previous one:

pragma solidity ^0.4.18;

import "./SLib.sol";

library TLib {
  using SLib for SLib.SCtx;

  struct TCtx {
    SLib.SCtx s;
  }

  function init(TCtx storage self, uint _a) public
  {
    self.s.init(_a);
  }

  function getA(TCtx storage self) public view returns (uint) {
    return self.s.getA();
  }
}

Then some contract to use them TInterface.sol:

pragma solidity ^0.4.18;

import "./TLib.sol";

contract TInterface {
  using TLib for TLib.TCtx;
  TLib.TCtx t;

  function TInterface(uint _a) public {
    t.init(_a);
  }

  function getA() public view returns (uint) {
    return t.getA();
  }
}

In Truffle I can't even link these guys. In 2_deploy_libraries.js:

var TLib = artifacts.require("./TLib.sol");
var SLib = artifacts.require("./SLib.sol");
var TInterface = artifacts.require("./TInterface.sol");

module.exports = function(deployer) {
  deployer.deploy(SLib);
  deployer.deploy(TLib);
  deployer.link(SLib, TLib);
  deployer.link(TLib, TInterface);
};

Returns:

  Deploying SLib...
  ... 0xe0317d15a3eac...
  SLib: 0x8e4c131b37383e431b9cd0635d3cf9f3f628edae
  Deploying TLib...
  Linking SLib to TLib
Error encountered, bailing. Network state unknown. Review successful transactions manually.
Error: TLib contains unresolved libraries. You must deploy and link the following libraries before you can deploy a new version of TLib: SLib
    at /usr/l...:/~/truffle-contract/contract.js:371:1
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:228:7)

Is doing this even allowed? And if yes, what is the right way to link and use it?

0

I understood what I was doing wrong. It has to do with the order that libraries should be linked/deployed. This is the right way to do it:

var TLib = artifacts.require("./TLib.sol");
var SLib = artifacts.require("./SLib.sol");
var TInterface = artifacts.require("./TInterface.sol");

module.exports = function(deployer) {
  deployer.deploy(SLib);
  deployer.link(SLib, TLib);
  deployer.deploy(TLib);
  deployer.link(TLib, TInterface);
};

It makes sense in fact.

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.