Solidity 102

1. Overloading
2. Library
3. Import
4. Receive ETH
5. Sending ETH
6. Interact with Contract
7. Call
8. Delegatecall
9. Create
10. Create2
11. DeleteContract
12. ABI Encoding and Decoding
13. Hash
14. Function Selector
15. Try Catch
.
Import

solidity supports the use of the import keyword to import global symbols in other contracts (simply understood as external source code), making development more modular. Generally, if not specified, all global symbols of the imported file will be imported into the current global scope.

Usage of import

  • Import by relative location of source file. For example:
Hierarchy
├── Import.sol
└── Yeye.sol

// Import by relative location of source file
import './Yeye.sol';
  • Import the global symbols of contracts on the Internet through the source file URL. For example:
// Import by URL
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Address.sol';
  • Import via the npm directory. For example:
import '@openzeppelin/contracts/access/Ownable.sol';
  • Import contract-specific global symbols by specifying global symbols. For example::
import {Yeye} from './Yeye.sol';
  • The location of the reference (import) in the code: after declaring the version, and before the rest of the code.

Test import

We can use the following code to test whether the external source code was successfully imported:

contract Import {
    // Successfully import the Address library
    using Address for address;
    // declare variable "yeye"
    Yeye yeye = new Yeye();

    // Test whether the function of "yeye" can be called
    function test() external{
        yeye.hip();
    }
}

result

Summary

In this lecture, we introduced the method of importing external source code using the import keyword. Through the import, you can refer to contracts or functions in other files written by us, or directly import code written by others, which is very convenient.

PreviousNext