0

Suppose that I have a token contract as follow. How can i write the new migration.js file in truffle to deploy the contract?

TestCoin.sol

import "openzeppelin-solidity/contracts/math/SafeMath.sol";
import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";

contract ERC20 is IERC20 {

}

contract TestCoin is ERC20 {

}

1_initial_migration.js

var Migrations = artifacts.require("./Migrations.sol");

module.exports = function(deployer) {
  deployer.deploy(Migrations);
};

I am thinking of something like this but not sure if it is correct

var SafeMath = artifacts.require("./SafeMath.sol");
var TestCoin = artifacts.require("./TestCoin.sol");

module.exports = function(deployer) {
  deployer.deploy(SafeMath);
  deployer.link(SafeMath, TestCoin);
  deployer.deploy(TestCoin);
};

2 Answers 2

1

As per my knowledge, There are two types of libraries:

  1. All with internal method
    • These type of libraries are not deployed explicitly they are included in contract itself. ie. SafeMath from openzeppelin.
    • Internal libraries don't need to be linked with main contract.
    • They are included in contract's bytecode itself.
    • Consider following library:
     pragma solidity ^0.8.0;
     // library for uint array 
     library UintArrayLib {
         using UintArrayLib for uints;
    
         struct uints {
             uint[] array;
         }
    
         function add(uints storage self, uint _uint)
             internal
         {
             if(! exists(self, _uint)){
                 self.array.push(_uint);
             }
         }
    
         function remove(
             uints storage self,
             uint _uint
         ) internal {
         for (uint256 i = 0; i < self.array.length; i++) {
                 if (
                     self.array[i] == _uint 
                 ) {
                     delete self.array[i];
                 }
             }
         }
    
    
         function exists(
             uints storage self,
             uint _uint
         ) internal view returns (bool) {
             for (uint256 i = 0; i < self.array.length; i++) {
                 if (
                     self.array[i] == _uint 
                 ) {
                     return true;
                 }
             }
             return false;
         }
     }
    
     contract BuyList is Ownable, ReentrancyGuard {
         using UintArrayLib for UintArrayLib.uints;
         // Rest of the code goes here
     }
    
    • For deploying BuyList contract, You'll write following migration:
    var BuyList = artifacts.require("BuyList");
    
    module.exports = function(deployer) {
        deployer.deploy(BuyList);
    };
    
  2. At least one public method
    • These contract's public method are not included contract directly and the whole library is deployed explicitly as new address.
    • These libraries needs to be linked to the main contract.
    • These libraries are not included in contract's bytecode and mostly used when the contract reaches the max bytecode size limit.
    • consider the following library example.
    
    pragma solidity ^0.8.0;
    // library for uint array 
    library UintArrayLib {
        using UintArrayLib for uints;
    
        struct uints {
            uint[] array;
        }
    
        function add(uints storage self, uint _uint)
            public
        {
            if(! exists(self, _uint)){
                self.array.push(_uint);
            }
        }
    
        function remove(
            uints storage self,
            uint _uint
        ) public {
        for (uint256 i = 0; i < self.array.length; i++) {
                if (
                    self.array[i] == _uint 
                ) {
                    delete self.array[i];
                }
            }
        }
    
    
        function exists(
            uints storage self,
            uint _uint
        ) internal view returns (bool) {
            for (uint256 i = 0; i < self.array.length; i++) {
                if (
                    self.array[i] == _uint 
                ) {
                    return true;
                }
            }
            return false;
        }
    }
    
    contract BuyList is Ownable, ReentrancyGuard {
        using UintArrayLib for UintArrayLib.uints;
        // Rest of the code goes here
    }
    
    • For deploying BuyList contract, You'll write following migration:
    var UintArrayLib = artifacts.require("UintArrayLib");
    var BuyList = artifacts.require("BuyList");
    
    module.exports = function(deployer) {
        deployer.deploy(UintArrayLib); // Explicity deployment of lib
        deployer.link(UintArrayLib, BuyList); // Linking the lib
        deployer.deploy(BuyList); // Deploying main contract
    };
    
0

Yes, you are right, but you need linkind for truffle when you explicitly use imported libraries, meanwhile you can use:

Contract:

pragma solidity ^0.4.23;

import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";

contract TestCoin is ERC20 {
}

Deployment script:

var TestCoin = artifacts.require("./TestCoin.sol");

module.exports = function(deployer) {
    deployer.deploy(TestCoin);
};
5
  • What do you mean? You mention need linking for truffle when explicitly use imported libraries but in the deployment script example u show did not use link to library Nov 10, 2018 at 12:29
  • 1. Truffle will not ask to link external library. 2. If you use migration for testing purposes you don't need to link SafeMath with TestCoin, because you don't use SafeMath in constructor
    – Aquila
    Nov 10, 2018 at 13:32
  • I omit the using SafeMath in the code for sample code. .. in the actual code there is using safemath. so do i need to link SafeMath? Nov 10, 2018 at 16:56
  • and what does it mean by truffle will not ask to link external library Nov 10, 2018 at 18:32
  • external library is library from npm package (openzeppelin-solidity), when you use the library you don't need to link.
    – Aquila
    Nov 10, 2018 at 22:08

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.