3

I work in Truffle development environment and use Windows PowerShell. My Solidity code is a test code and is simple. There is a library called Alireza that just compares two uint values. There is a contract called B that just uses the library function to compare two uint values. Now, i need to link Alireza to B. For that

  1. I embedded using-for command in B.
  2. I deployed Alireza and got its address.
  3. I tried to deploy B to create its bytecode. As one expects, the deplyoment failed.
  4. I embedded Alireza's address to B's bytecode. Because, as i searched, it is a way to link.
  5. I tried to deploy B again and it failed again.

Is this work flow correct? i don't know. So, i intent to try another way that here mentioned:

  1. I tried to embed the hash of Alireza's title or directory to B's bytecode. Just like:

     --$12b85d948918b1e5773c9bc099694fc73b$--
    
  2. I tried to deploy B again and again the deployment failed.

Even, before all ways i mentioned, i deployed B without deployment of Alireza before that. Because i thought maybe, by this way, the compiler would create the link. But, the below error emerges:

Error: B contains unresolved libraries. You must deploy and link the following libraries before you can deploy a new version of B: Alireza

Am i in a correct way? Where is my problem? How can i link a library to a contract just by Solidity and contract's bytecode and without node-js? How should i alter my work flow to link?

My whole code:

pragma solidity ^0.5.12;

library Alireza{

    function comparison(uint a, uint b) view public returns(bool){  
        if(a==b){
            return true;
        }else{
            return false;
        }
    }
}

contract B{

    using Alireza for uint;

    function assessment(uint m, uint n) view public returns(bool){
        return m.comparison(n);
    }
}

Thank you.

2 Answers 2

1

I face-planted on these sorts of errors recently too! Turns out there be additional setup required known as migrations...

Truffle -- Running Migrations -- deployer.link(library, destinations) section covers how to link libraries and contracts.

TLDR

./migrations/99_link_example.sol

const Alireza = artifacts.require("Alireza");
const B = artifacts.require("B");

module.exports = (deployer, _network, accounts) => {
  deployer.deploy(Alireza);
  deployer.link(Alireza, B);
  deployer.deploy(B);
};

The deployer.link bit is the magic line that allows B to make use of Alireza

Bonus Tips

  • if ya have a contract requiring parameters within it's constructor, then it is possible to define 'em during the .deploy phase

  • the accounts parameter holds an array of test account addresses that may be useful too

./contracts/Name.sol

contract Name {
    address public owner;

    function constructor(address _owner) {
        owner = _owner;
    }
}

./migrations/2_deploy_name.js

const Name = artifacts.require("Name");

module.exports = (deployer, _network, accounts) => {
  deployer.deploy(Name, accounts[0]);
};
0

Just compile the code and deploy contract B, no need to do the link thing. The library code will copy to contract B automatically by the compiler.

1
  • I tried that but the followed error emerges: Error: B contains unresolved libraries. You must deploy and link the following libraries before you can deploy a new version of B: Alireza. It says first a library must be deployed before a contract deployment.
    – Alireza
    May 11, 2021 at 12:17

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.