1/5
## Mappings to track funders In this lesson, we'll learn how to keep track of who sent funds to our contract, and how much they sent. We'll use a mapping. Mappings are a fundamental concept in smart contracts. Think of them as dictionaries in Python or JavaScript. They pair a key with a value. We can make mappings public to allow anyone to read them. We'll create a new mapping to keep track of funders and how much they sent. ```python # # Keep track of who sent us # # How much they sent us ``` We'll call this new mapping `_funders_to_amount_funded`. ```python # funder -> how much they funded _funders_to_amount_funded: public(HashMap[address, uint256]) ``` This mapping will store the address of the funder as a key and the amount they sent as a value. In our `fund` function, we'll track the amount each person sent us. ```python @payable def fund(): """ Allows users to send $ to this contract Have a minimum $ amount to send """ # ... previous code self._funders_to_amount_funded[msg.sender] += msg.value # ... ``` Here, we access the hash map with the `msg.sender` address as the key and add the amount sent by the `msg.sender` (`msg.value`) to the value associated with the `msg.sender` in the hash map. We can also use the `+=` operator as a shorthand for adding to a value in a mapping. ```python self._funders_to_amount_funded[msg.sender] = self._funders_to_amount_funded[msg.sender] + msg.value ``` We can also use the `+=` operator as a shorthand for adding to a value in a mapping. ```python self._funders_to_amount_funded[msg.sender] += msg.value ``` This is the same as writing the line above.
A practical guide to using mappings in Vyper - This lesson demonstrates how to use mappings to keep track of the amount funded by a particular address in a Vyper smart contract. The video covers the fundamentals of setting up a mapping, adding values to the mapping, and using the plus equals operator for efficient value updates.
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 April 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 April 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