0/5
We will be going over reference types in Vyper. These are used to store data, but are not copied when assigned as a value. ## Reference Data Types We will be looking at several reference types in Vyper: fixed-sized list, mappings, and structs. ### Fixed-Sized List We can use a fixed-sized list to store data of a given type. We first declare the type, then put the fixed size in brackets, as follows: ```vyper nums: public(uint256[10]) ``` We can read and write to this list. To get the value in index 0, we use: ```vyper self.nums[0] ``` And to set the value in index 0 to 123: ```vyper self.nums[0] = 123 ``` We can also set another value in index 1 to 456: ```vyper self.nums[1] = 456 ``` ### Mappings Mappings store data based on a key value. The first value type will be the key, and the second value type will be the data stored. Let's create a mapping called "myMap": ```vyper myMap: public(HashMap[address, uint256]) ``` We can access and store data in a mapping. To get the value stored at the address of "msg.sender": ```vyper self.myMap[msg.sender] ``` Let's store the value 1 in this mapping: ```vyper self.myMap[msg.sender] = 1 ``` We can also change the value stored at "msg.sender" to 11: ```vyper self.myMap[msg.sender] = 11 ``` ### Structs Structs are custom data types. Let's declare a struct called "Person". This struct has a name and an age: ```vyper struct Person: name: String[10] age: uint256 ``` We can declare a state variable of type struct: ```vyper person: public(Person) ``` Let's store data in this state variable: ```vyper self.person.name = "vyper" self.person.age = 33 ``` Then, we can copy this state variable into memory by declaring a variable of type struct and assigning the state variable: ```vyper p: Person = self.person ``` We can then change the name to "solidity" and the age to 22, but this will only update the value stored in memory, not the state variable. ```vyper p.name = "solidity" p.age = 22 ``` We can compile this code using the Vyper compiler and then deploy the contract. To compile the code, we can click on the Vyper icon. Then click "Compile Ref.vy". The contract will be compiled. Now, we can deploy the contract. We can click on the "Deploy & run transactions" tab. Then we can click on the name of the contract file, which is Ref.vy. Next, we can click on the "Deploy" button to deploy the contract. We can see the state variables under the "Deployed Contracts" tab. We have "myMap", "nums", and "person". We can call the state variables to check what values are stored. We can call the state variable "nums" with index 0: ```bash nums(0) ``` We see it returns 0. This is because we did not set any values for index 0. Let's now call the state variable "nums" with index 1: ```bash nums(1) ``` We get 456, because we set the value for index 1 to 456. Next, we can call the state variable "person": ```bash person ``` We see it returns "name": "vyper" and "age": 33, because we set those values in the state variable. ## Summary This is how we can store data in different reference types in Vyper. Remember that when using structs or mappings, we need to understand how these are copied. We can copy the value in a state variable into memory using a variable, but this only copies the value at that moment in time, not a reference to the state variable. So, if the state variable is updated, the value stored in the memory variable won't be updated.
A comprehensive guide to reference types in Vyper. The lesson explores the concepts of fixed-size lists, dynamic arrays, mappings, and structs, providing practical examples and demonstrating how to read and write to these data types.
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)
Web3 engineer, educator, and Cyfrin co-founder. Patrick's smart contract development and security courses have helped hundreds of thousands of engineers kickstarting their careers into web3.
Last updated on March 21, 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)
Web3 engineer, educator, and Cyfrin co-founder. Patrick's smart contract development and security courses have helped hundreds of thousands of engineers kickstarting their careers into web3.
Last updated on March 21, 2025
Testimonials
Read what our students have to say about this course.
Chainlink
Chainlink
Gustavo Gonzalez
Solutions Engineer at OpenZeppelin
Francesco Andreoli
Lead Devrel at Metamask
Albert Hu
DeForm Founding Engineer
Radek
Senior Developer Advocate at Ceramic
Boidushya
WalletConnect
Idris
Developer Relations Engineer at Axelar