1

I have created library and imported in my contract. I am not able to link the library in deployment file.

solution needed. thanks in adv

pragma solidity ^0.4.2;

library StringUtils {
    /// @dev Does a byte-by-byte lexicographical comparison of two strings.
    /// @return a negative number if `_a` is smaller, zero if they are equal
    /// and a positive numbe if `_b` is smaller.
    function compare(string _a, string _b) returns (int) {
        bytes memory a = bytes(_a);
        bytes memory b = bytes(_b);
        uint minLength = a.length;
        if (b.length < minLength) 
        minLength = b.length;
        //@todo unroll the loop into increments of 32 and do full 32 byte comparisons
        for (uint i = 0; i < minLength; i ++) {
            if (a[i] < b[i])
                return -1;
            else if (a[i] > b[i])
                return 1;
        if (a.length < b.length)
            return -1;
        else if (a.length > b.length)
            return 1;
        else
            return 0;
        }
    }
    /// @dev Compares two strings and returns true iff they are equal.
    function equal(string _a, string _b) returns (bool) {
        return compare(_a, _b) == 0;
    }
    /// @dev Finds the index of the first occurrence of _needle in _haystack
    function indexOf(string _haystack, string _needle) returns (int) {
        bytes memory h = bytes(_haystack);
        bytes memory n = bytes(_needle);
        if (h.length < 1 || n.length < 1 || (n.length > h.length)) 
            return -1;
        else if (h.length > (2**128 - 1)) 
            return -1;                                  
            else {
                uint subindex = 0;
                for (uint i = 0; i < h.length; i ++) {
                    if (h[i] == n[0]) {
                        subindex = 1;
                        while (subindex < n.length && (i + subindex) < h.length && h[i + subindex] == n[subindex]) {
                            subindex++;
                        }   
                        if (subindex == n.length) {
                        return int(i);
                    }
                }
            }
            return -1;
        }   
    }
}

my contract

import "./StringUtils.sol";
contract SaveData is Whitelist {
function verifyHash(address publicAddress, string hashstring) external returns (string) {
        require(StringUtils.equal(data[publicAddress],hashstring));

    }
}

in deployment:-

var WhiteList = artifacts.require("./Whitelist.sol");
var SaveData = artifacts.require("./SaveData.sol");
var StringUtils = artifacts.require("./StringUtils.sol");

module.exports = function(deployer) {
  deployer.deploy(WhiteList);
  deployer.deploy(SaveData);
  deployer.deploy(StringUtils);
};
2
  • Can you provide some code please? How did you import your library to contract? Dec 19, 2017 at 10:02
  • @RomanFrolov details added Dec 19, 2017 at 10:13

2 Answers 2

1

First thing first, you need to deploy your library to the blockchain. After the deployment you will have smart contract address, which you should use later to access your library (how to do this).

You cannot do it when library is stored locally on your computer.

6
  • i am not getting what you are saying. I am new to solidity. Dec 19, 2017 at 10:19
  • @SandeepGupta 1) select in which blockchain and environment you want to test your code 2) create new account 3) deploy your library smart contract from your account 4) wait while block is mined 5) use your deployed library contract in your other smart contracts Dec 19, 2017 at 10:22
  • i am doing on testrpc. Dec 19, 2017 at 10:23
  • @SandeepGupta are you using geth? Dec 19, 2017 at 10:25
  • i have deployed it successfully, can u tell me how can i deploy it with C# Dec 19, 2017 at 10:35
1

It's been long, but in case someone looking for the same.

Library function can be called internally or externally. If all the library functions are internal or private then that library contract is statically linked to your contract. So you don't have to deploy the library contract separately and link to it. If the function is external or public you need to deploy it separately and use the library contract address on chain. An example of how you do this during the truffle migration is as shown below.

deployer.deploy(StringUtils);
deployer.link(StringUtils, [SaveData]);
deployer.deploy(SaveData);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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