Stepan Poperechnyi
Stepan Poperechnyi

Reputation: 344

How connect library to smart contract from external resources?

pragma solidity ^0.4.15;

import './ERC20.sol';
import './SafeMath.sol';

How connect SafeMath.sol from external(non-local) resourses?

Upvotes: 3

Views: 2046

Answers (2)

martriay
martriay

Reputation: 5742

While James' answer is valid, I would not recommend linking your contract's dependencies from an online repository, this is highly insecure since your code depends on some online source that can be dynamically updated and because you might get unstable versions.

I would strongly recommend you follow Zeppelin's recommended way to use OpenZeppelin contracts, allowing you to use only stable releases and easily update the dependencies to get the latest features and bug-fixes:

npm init -y
npm install -E zeppelin-solidity

Then in your contract:

import 'zeppelin-solidity/contracts/math/SafeMath.sol';

contract MyContract {
  using SafeMath for uint;
  ...
}

Upvotes: 3

James Lockhart
James Lockhart

Reputation: 1040

This is probably what you mean:

pragma solidity ^0.4.0;

import "github.com/OpenZeppelin/zeppelin-solidity/contracts/math/SafeMath.sol";

contract MathExtended {
    using SafeMath for uint;
    function exec(uint a, uint b) returns (uint){
        return a.add(b);
    }
}

Solidity supports importing from Github directly, just remember not to include commits or branches when reference it must be the user/project/file-path/file.sol directly.

See http://solidity.readthedocs.io/en/develop/layout-of-source-files.html

Upvotes: 2

Related Questions