5/5
## Recap: Building the Favorites Vyper Smart Contract Before we take the exciting step of deploying our `favorites.vy` smart contract to a live blockchain network, let's recap the essential Vyper concepts and features we've implemented so far. This contract demonstrates fundamental building blocks for storing and managing data on-chain. The primary goal of the `favorites.vy` contract is twofold: 1. To store the contract deployer's favorite number. 2. To store the favorite numbers of other individuals, associating their names with their chosen numbers. To achieve this, we've utilized several core Vyper features: **Structs: Defining Custom Data Types** Vyper allows us to create custom data structures using the `struct` keyword. This helps organize related data logically. In our contract, we defined a `Person` struct: ```vyper struct Person: favorite_number: uint256 name: String[100] ``` This creates a new type named `Person`, which bundles an unsigned 256-bit integer (`favorite_number`) and a string with a maximum length of 100 characters (`name`). **State Variables: Persistent On-Chain Storage** State variables hold the data that persists on the blockchain, defining the contract's current state. We've declared several state variables, using the `public()` visibility wrapper which automatically generates getter functions for easy data retrieval: ```vyper # Simple state variables for the deployer my_name: public(String[100]) my_favorite_number: public(uint256) # Fixed-size arrays to store lists list_of_numbers: public(uint256[5]) # Stores 5 numbers list_of_people: public(Person[5]) # Stores 5 'Person' structs # Mapping (like a dictionary or hash map) name_to_favorite_number: public(HashMap[String[100], uint256]) # Utility variable for array management index: public(uint256) ``` These variables store the deployer's details (`my_name`, `my_favorite_number`), lists of numbers and `Person` structs (`list_of_numbers`, `list_of_people`), a mapping from names to favorite numbers (`name_to_favorite_number`), and an `index` variable to track the current position when adding elements to our arrays. **Constructor: Initializing Contract State** The constructor is a special function marked with the `@deploy` decorator and named `__init__`. It executes only *once* when the contract is first deployed to the blockchain. Its primary role is to set the initial values of state variables: ```vyper @deploy def __init__(): self.my_favorite_number = 7 self.index = 0 # uint256 defaults to 0, but explicit is clear self.my_name = "Patrick!" ``` In our contract, the constructor sets the deployer's favorite number to `7`, initializes the `index` to `0`, and sets the deployer's name to `"Patrick!"`. **Functions: Defining Contract Interactions** Functions define how users and other contracts interact with our smart contract. We use the `@external` decorator to make functions callable from outside the contract. * **Simple State Modification (`store`)**: This function allows updating the deployer's favorite number. It modifies the contract's state. ```vyper @external def store(new_number: uint256): self.my_favorite_number = new_number ``` * **Read-Only Data Retrieval (`retrieve`)**: This function returns the deployer's favorite number. Crucially, it uses the `@view` decorator, signifying that it does *not* modify the contract's state. Calling `@view` functions typically doesn't incur gas costs for the transaction itself (though node providers might charge for state access). ```vyper @external @view def retrieve() -> uint256: return self.my_favorite_number ``` * **Complex Logic (`add_person`)**: This function demonstrates more complex interactions, involving multiple state variables. It takes a name and favorite number as input and performs several actions: ```vyper @external def add_person(name: String[100], favorite_number: uint256): # 1. Store the number in the simple list self.list_of_numbers[self.index] = favorite_number # 2. Create a Person struct instance in memory new_person: Person = Person({favorite_number: favorite_number, name: name}) # 3. Store the Person struct in the people list self.list_of_people[self.index] = new_person # 4. Store the name-to-number mapping self.name_to_favorite_number[name] = favorite_number # 5. Increment the index for the next addition self.index = self.index + 1 ``` This function updates `list_of_numbers`, creates and stores a `Person` struct in `list_of_people`, updates the `name_to_favorite_number` mapping, and increments the `index` state variable to prepare for the next entry in the fixed-size arrays. **Key Relationships** Notice how these concepts interconnect: * The `Person` struct is the data type stored within the `list_of_people` state variable array. * The `index` state variable manages insertions into both `list_of_numbers` and `list_of_people`. * The `add_person` function orchestrates updates across arrays and mappings, utilizing the `Person` struct and modifying the `index`. * The `__init__` constructor provides the initial baseline state for variables used by other functions. With this solid foundation recapped, we are now ready to deploy the `favorites.vy` contract onto a real blockchain network.
Before we take the exciting step of deploying our favorites.vy
smart contract to a live blockchain network, let's recap the essential Vyper concepts and features we've implemented so far. This contract demonstrates fundamental building blocks for storing and managing data on-chain.
The primary goal of the favorites.vy
contract is twofold:
To store the contract deployer's favorite number.
To store the favorite numbers of other individuals, associating their names with their chosen numbers.
To achieve this, we've utilized several core Vyper features:
Structs: Defining Custom Data Types
Vyper allows us to create custom data structures using the struct
keyword. This helps organize related data logically. In our contract, we defined a Person
struct:
This creates a new type named Person
, which bundles an unsigned 256-bit integer (favorite_number
) and a string with a maximum length of 100 characters (name
).
State Variables: Persistent On-Chain Storage
State variables hold the data that persists on the blockchain, defining the contract's current state. We've declared several state variables, using the public()
visibility wrapper which automatically generates getter functions for easy data retrieval:
These variables store the deployer's details (my_name
, my_favorite_number
), lists of numbers and Person
structs (list_of_numbers
, list_of_people
), a mapping from names to favorite numbers (name_to_favorite_number
), and an index
variable to track the current position when adding elements to our arrays.
Constructor: Initializing Contract State
The constructor is a special function marked with the @deploy
decorator and named __init__
. It executes only once when the contract is first deployed to the blockchain. Its primary role is to set the initial values of state variables:
In our contract, the constructor sets the deployer's favorite number to 7
, initializes the index
to 0
, and sets the deployer's name to "Patrick!"
.
Functions: Defining Contract Interactions
Functions define how users and other contracts interact with our smart contract. We use the @external
decorator to make functions callable from outside the contract.
Simple State Modification (store
): This function allows updating the deployer's favorite number. It modifies the contract's state.
Read-Only Data Retrieval (retrieve
): This function returns the deployer's favorite number. Crucially, it uses the @view
decorator, signifying that it does not modify the contract's state. Calling @view
functions typically doesn't incur gas costs for the transaction itself (though node providers might charge for state access).
Complex Logic (add_person
): This function demonstrates more complex interactions, involving multiple state variables. It takes a name and favorite number as input and performs several actions:
This function updates list_of_numbers
, creates and stores a Person
struct in list_of_people
, updates the name_to_favorite_number
mapping, and increments the index
state variable to prepare for the next entry in the fixed-size arrays.
Key Relationships
Notice how these concepts interconnect:
The Person
struct is the data type stored within the list_of_people
state variable array.
The index
state variable manages insertions into both list_of_numbers
and list_of_people
.
The add_person
function orchestrates updates across arrays and mappings, utilizing the Person
struct and modifying the index
.
The __init__
constructor provides the initial baseline state for variables used by other functions.
With this solid foundation recapped, we are now ready to deploy the favorites.vy
contract onto a real blockchain network.
A concise recap to Recap: Building the Favorites Vyper Smart Contract - Revisit the core Vyper features used in the `favorites.vy` contract, including structs, state variables (arrays, mappings, `public`), the `__init__` constructor, and function types (`@external`, `@view`). Understand how these building blocks enable on-chain data storage and interaction before deployment.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
The basics of blockchain transactions, how to send and receive money on a blockchain network.
How to write Python based smart contracts using Vyper.
How to read and understand Vyper smart contracts.
Vyper data structures, arrays, structs, hash maps.
How to build a smart contract application and deploy on ZKsync with Moccasin.
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on June 11, 2025
Duration: 2h 08min
Duration: 2h 32min
Duration: 24min
Course Overview
About the course
The basics of blockchain transactions, how to send and receive money on a blockchain network.
How to write Python based smart contracts using Vyper.
How to read and understand Vyper smart contracts.
Vyper data structures, arrays, structs, hash maps.
How to build a smart contract application and deploy on ZKsync with Moccasin.
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on June 11, 2025