I want to know What is the differences between :

1) import './OtherContract.sol'

2) contract cont is OtherContract {}

3)in truffle's migration.js : deployer.link(cont,OtherContract)

i know they serve to make some relationships between contracts, but i can't figure out the differences


1) The import keyword will import all global symbols from OtherContract.sol into the current scope, allowing you to use whatever contract definitions are defined in there (or in their nested imports).

2) This is called inheritance. This is an OOP (Object Oriented Programming) concept way broader than importing contracts. You can read some of it in here(*1) and from Solidity's official documentation. Long story short, it's a way to create a contract based on another contract, copying its variables and functions.

3) When a contract uses a library, most of the times (*2) it needs for it to be deployed so that the contract can know its library address at deploy time, to call its methods when necessary. The deployer.link function is to link a deployed library to a contract that uses it. See what does deploy.link exactly do in truffle.

(*1) this blogpost uses python to explain it, but it's the same concept in all OOP languages.

(*2) internal library functions can be inlined at compile time, and there is no need to deploy those libraries. Take OpenZeppelin's SafeMath for example.

| improve this answer | |

The first two are related: you need to import the contract first before you can inherit it.

The 3rd one is used to link a library:

As the compiler cannot know where the library will be deployed at, these addresses have to be filled into the final bytecode by a linker

| improve this answer | |
  • 1
    What is the difference between a library and a contract, is it just the keyword? so cna i use link a contract with an other ? – Kaki Master Of Time Feb 17 '18 at 14:30
  • 2
    When you use a library it is deployed to the blockchain only once and then other contracts can call to this library with DELEGATECALL opcode. When you use inheritance with contracts, the parent contract code will bundled and deployed together with the child contract. When you use libraries the resulting code of your contract is smaller thus it allows you to save gas. Another cool feature with libraries is using for solidity.readthedocs.io/en/develop/contracts.html#using-for – medvedev1088 Feb 17 '18 at 14:38
  • 2
    You can link only a library into another contract but can't link a contract into another contract because as I noted above the parent contract bytecode is bundled with the child. – medvedev1088 Feb 17 '18 at 14:39

In very simple analogy...

1st one is importing a contract into another so that you can use its instances in the contract you are importing it in. Just like you import some library in Python or some other language of your love.

2nd is the inheritance of features and qualities of one contract into another. As you want, in some case, for instance, you feel that a particular contract is the subset of the one you have already defined. You can simply use it to import the qualities and features of the previous contract into another contract.

3rd is the statement used to link some library into your contract. In case your contract depends on some library.

Before a contract using the library, the used library should be deployed to the network.

DO comment, if you need some better explanation...Thanks

| improve this answer | |

Your Answer

