Back
Lesson 19:
Imports
Introduction to import Vyper files to your codebase
Progress: 0%
Visit desktop version for better experiences.
Imports
The import statements allow us to import Vyper code using the import
or from ... import
syntax.
Example of file structure:
├── YourContract.vy
└── HelperContract.vy
HelperContract.vy
# pragma version 0.4.0
@external
@pure
def addition_function(x: uint256, y: uint256) -> uint256:
return x + y
YourContract.vy
import HelperContract # accessed as `HelperContract`
# Alternatives to importing `HelperContract.vy`
# import HelperContract as hc --> accessed as `hc`
# from . import HelperContract --> accessed as `HelperContract`
# from . import HelperContract as hc --> accessed as `ex`
@external
def calculate_sum(x: uint256, y: uint256) -> uint256:
return HelperContract.addition_function(x, y)
Vyper also includes built-in interfaces such as IERC20 and IERC721 in its language. These can be imported from ethereum.ercs
.
from ethereum.ercs import IERC20
implements: IERC20
balance_of_address: public(HashMap[address, uint256])
# Example of `transfer` function accessing `IERC20` interface
@external
def transfer(_to: address, _value: uint256) -> bool:
self.balanceOf[msg.sender] -= _value
self.balanceOf[_to] += _value
log IERC20.Transfer(msg.sender, _to, _value)
return True
Vyper Differentiators
- You can assign alias to the imported contract using
as
keyword. - Vyper has built-in interfaces
Further Reading:
Back to:
Reentrancy LocksEnd of lessons
Reentrancy LocksEnd of lessons