Solidity Contracts – How to Import Contracts into Each Other

contract-developmentsolidity

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

Best Answer

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.

Related Topic