0/5
We're learning about the default function in Vyper. Vyper is a smart contract language similar to Solidity, but it has some key differences. We're building a smart contract that allows people to send money to this contract, which we'll call "Buy Me A Coffee". We'll use Vyper to make sure that when somebody sends money to this contract, we can keep track of them as a funder. We're going to do this through a function called "fund". The fund function is a key part of our "Buy Me A Coffee" smart contract. It helps us keep track of who's donated to our cause. The fund function allows people to send us money in the form of Ether. This contract uses a price feed to convert the Ether amount into US dollars. We've already made this contract work, and we've even added functionality so we can withdraw money from this contract. However, right now, this contract is dependent on the fund function to keep track of the funders. We can make our smart contract more robust by allowing somebody to send us money without even calling the fund function. We can do this using the default function. Here is an example of the code we've written so far. ```python @external @payable def fund(): """Allows users to send $ to this contract Have a minimum $ amount to send """ usd_value_of_eth: uint256 = self.get_eth_to_usd_rate(msg.value) assert usd_value_of_eth >= MINIMUM_USD, "You must spend more ETH!!" self.funders.append(msg.sender) self.funder_to_amount_funded[msg.sender] += msg.value ``` We're going to add a new function above this one called "def _default_". The default function is executed when somebody sends money to our contract without calling a specific function. Here is the code: ```python @external @payable def _default_(): pass ``` The default function should accept no parameters. We use the "pass" keyword to tell Vyper to do nothing. We then recompile the code and deploy this contract again. In our Remix VM, we'll now try to send 1 Ether to this contract, without calling the fund function, but the transaction will revert. Our "funders" array still shows zero funders. Now, we'll try to call the fund function from our default function. Here is the updated code: ```python @external @payable def _default_(): self.fund() ``` We'll recompile the code and deploy this contract. Now when we send 1 Ether to our "Buy Me A Coffee" contract, it should still update the balance of the contract, but this time, it should also add the funder to the "funders" array. This default function is a very powerful tool that can be used to add a layer of functionality to our Vyper smart contracts.
A comprehensive guide to the `_default_` function in Vyper. The lesson covers the purpose and functionality of the `_default_` function, how it interacts with other functions in a contract, and its role in handling transactions.
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