How connect library to smart contract from external resources?
Asked Answered
A

2

3
pragma solidity ^0.4.15;

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

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

Adenoidectomy answered 19/9, 2017 at 11:29 Comment(0)
C
2

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

Cushing answered 19/9, 2017 at 19:4 Comment(0)
B
3

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;
  ...
}
Betseybetsy answered 16/2, 2018 at 22:5 Comment(0)
C
2

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

Cushing answered 19/9, 2017 at 19:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.