[Ethereum] Finding and Using Solidity Libraries (ie. Math functions)

contract-developmentlibrarysolidity

How do I find and use any existing Solidity libraries like you might in Javascript?

For example, Math functions: How do I implement the equivalent of Math.sqrt(x) in a solidity function?

Related: What is –libraries doing in that second question (if relevant here)?

I looked at the docs and answers below, but they didn't seem to answer this. Thanks.

How to import solidity libraries in mix?

Out of gas error deploying library

Best Answer

Currently the standard library isn't fully set up yet, but in this case what you would do would be to link your contracts to your library's address (and hopefully map it to a named registration in that case). That will be implemented in the future and it will be much easier when that time comes.

Until then however, you will have to do it manually, ie the hard way.

One thing you would do would be to pull the square root function from this dapp bin (though frankly it's not worth using at this point since the fixed point variable has yet to be set up, but we'll use it for this example)

https://github.com/ethereum/dapp-bin/blob/master/library/math.sol

you would do something along the lines of

import "math.sol"

contract myContract {
    function f() {
        int a = Math.sqrt(9);
    }
}

from there the --libraries flag enabled you to link to certain addresses where the argument is the name of the library followed by the address so if you have the math library at address 0x123456, you would name it:

solc yourContract.sol --libraries Math: 0x123456
Related Topic