Web3 Needs More Vyper Developers - How You Can Become One
Technical insights, code walkthroughs, and resources
Photo by Jefferson Santos on Unsplash
Table of contents
- What Vyper is all about?
- How Vyper is Different From Solidity?
- Will Vyper replace Solidity?
- Why should you learn Vyper?
- What You Need Before Writing Vyper – Prerequisites
- How to Write “Hello World” in Vyper
- Understanding Data Types in Vyper
- Functions in Vyper
- Sending Ether in Vyper
- 3 Awesome Resources for Learning Vyper
- Twitter Chads to Follow for Vyper Content
- Get Bitten By Vyper
I began my Ethereum smart contract engineering journey some years back. My main motivation was to increase my technical proficiency in the Web3 space.
The first question I asked was, “Which language do I need to learn?” Everyone I reached out to recommended Solidity.
After over a year of writing Solidity, I got to know that there is another popular language in the Ethereum ecosystem called Vyper.
When I heard the word “Vyper,” my mind mapped it with Python, a popular Web2 language.
I became interested in this new language, and took about a month to enter its rabbit hole. I really enjoyed writing Vyper because it was a simple and easily readable language.
I must also admit that my knowledge of Solidity and Python helped me grasp it faster.
Months after I took interest in Vyper, the Curve Finance hack happened. In this hack, the attackers exploited a compiler bug in Vyper and siphoned funds from various projects.
This was the time Vyper got the attention of a greater number of developers in the space.
Enough with the backstory, the main reason I am writing this is to explain that the Web3 ecosystem needs more Vyper developers.
Then, I will teach you the basics of writing a smart contract with Vyper to make you comfortable with the language.
I will split this article into two parts: the first one for explaining conceptual underpinnings and the second part for code.
In a way, this is a short handbook for developers who want to understand and become good at writing Vyper contracts.
Let’s have some fun.
What Vyper is all about?
Vyper is a high-level programming language for building smart contracts on the Ethereum blockchain, and any other blockchain that supports the Ethereum Virtual Machine.
I have noticed that the language was created to enhance accessibility into the Ethereum smart contract development scene.
This is why I said that:
A greater percentage of Web2 developers can build projects with Python, or at least they have a good idea of the language’s syntax and how it works.
Vyper has a similar syntax to Python. Therefore, Python developers can easily build Ethereum smart contracts with it. The main engineering philosophies behind the language are:
Security: The language and its compiler are security-sensitive.
Readability: The contracts should be simple to read, even for an unlearned mind
Auditability: It does not support unnecessarily verbose or misleading codebase, which makes security reviews easier
How Vyper is Different From Solidity?
The difference between both languages goes beyond the individual names. To be clear, I will point out some operations and possibilities that Solidity supports but Vyper does not:
Yul or Assembly
We can optimize the storage of a Solidity contract in the EVM slot stack with Yul, which some other developers call Julia.
Yul is simply the possible integration of the Assembly language into smart contracts. It is often written inline.
I have written in-line assembly to optimize my Solidity contracts in the past. However, Vyper does not support the concept of using inline assembly for smart contracts.
The reasoning behind it, according to the documentation, is that in-line assembly can be quite hard to search and trace for variable names.
Function Overloading
If you are a JavaScript developer, you should be familiar with the concept of function overloading, which is also applicable in Solidity.
This is what function overloading means: Using the same name for two or more functions.
Vyper does not support it for two basic reasons.
First, it makes code easier to read; anyone might mix up the particular function being called at a given instance.
Secondly, it can even be used to write manipulative or misleading code.
Operator Overloading
The concept behind operator overloading is giving an operator, such as a + or -, more than one responsibility in an operation. The Vyper creators believe operator overloading will lead to more complexities, which is against the ethos of simplicity.
Binary-fixed Points
Vyper is a language that loves things to be exact. However, binary-fixed points do not always give exact results. Instead, you will get some approximations.
In contrast, Vyper supports decimal-fixed points as they give greater precision.
Modifiers
The concept of function modifiers is popularly understood among Solidity developers. We often use modifiers to introduce security checks or business logic to functions in a contract.
Vyper does not support modifiers due to auditability and execution reasons. Personally, I do not really admire this engineering decision, and I disagree with the Vyper creators on this.
Will Vyper replace Solidity?
No, the intention behind Vyper is not to replace Solidity. Instead, it was designed to be an alternative.
This is not really a new thing in engineering. For instance, Android development developers can use either Kotlin or Java to build their applications.
Back to Vyper and Solidity, I must emphasize that each language has its trade-offs. As a result, engineers can pick the particular language that suits their needs per time.
The presence of language choice is fun as it gives engineers the possibility of preference. After all, variety is the spice of life.
I strongly believe that Vyper will not replace Solidity, and both languages will thrive in the ecosystem.
Why should you learn Vyper?
Solidity is the most popular language in the Ethereum ecosystem, and only a tiny fraction uses Vyper in production. So you might wonder why you should learn Vyper.
This is why you should consider learning it:
The Best Entry Point for Python Developers
As I mentioned earlier, Vyper has good syntactical similarities with Python. So, if you are a Web2 dev coming to Web3, Vyper will be the easier language for you to pick.
Become more EVM-native
I am quite proficient with Solidity as well. But I wanted to be comfortable with more EVM languages, and Vyper was the best option.
Knowing Vyper will help you to become a more proficient developer in the ecosystem as you have more languages under your belt.
Become a Better Auditor
As an auditor, you have to be familiar with many languages in the space to review contracts written in such languages.
If you only know Solidity, you might find it quite hard to review the security of a Vyper contract and detect the vulnerabilities.
As a security researcher, it is not bad to have these languages in your stack ahead. Who knows when you will need them?
Let me give you an instance:
There is an ongoing zkSync audit contest on the Code Arena Discord server with a $1.1 million bounty, which is the biggest audit contest bounty in Web3 so far.
My point is that security researchers already familiar with writing zk circuits have higher chances of performing better at the contest.
So, if you know Vyper well, you will have an edge in future audit contests.
Make Better Engineering Decisions for Your Company or Clients
Have you ever wondered why Curve Finance wrote a larger chunk of their code in Vyper? Or have you asked yourself why some Uniswap core contracts were written in Vyper?
Arguably, it offers greater security and simplicity than Solidity. Some might want to ask, “If Vyper is secure, why was Curve Finance hacked recently?”
Some particular versions of the Vyper compiler were hacked, which led to the subsequent vulnerabilities of all the contracts using those versions.
So, it was not the case that the Curve contract written in Vyper itself was vulnerable.
Back to the main point:
As a developer, if you have a client who places more emphasis on code security, then Vyper might be a better language to adopt.
Knowing how to write Vyper will help you decide the language to use for your Ethereum contract and which trade-offs you are ready to make.
What You Need Before Writing Vyper – Prerequisites
A Development Environment
You can use either Remix or VS Code. First, let me teach you how to use Remix. Go to the Plugin Manager in Remix and search for Vyper. It will bring the Vyper compiler to your Remix; install it and use it for compiling your contract.
Now, the Remix Vyper compiler has two sub-compilers: local and remote. The remote compiler only supports up to version 0.2.16. But the local compiler has no limit, as far as I know.
Moving to VS Code, go to “extensions” and search for “Vyper.” Download the one that has “Ethereum Vyper language support for Visual Studio Code” as its description.
Install Python
Install Vyper
Run this command in your terminal to install Vyper:
pip install vyper==0.3.7
How to Write “Hello World” in Vyper
# @version ^0.3.7
# @dev if you put a space or indentation before you write the "announce" variable
# remix will throw an error
# so no space or indentation
announce: public(String[100])
@external
def init():
self.announce = "Hello everyone, what's up?"
In the code above, I created a function that prints “Hello, what’s up?” Let’s do a quick breakdown.
In Solidity, we specify the version by writing pragma solidity 0.8.21;
, but Vyper differs.
First of all, the compiler version is added as a comment, hence the use of hash. You declare the version this way: @version 0.2
or whatever version you want to use.
I created the public variable announce
. Due to the nature of what I want to print, I declared it with the string data type.
I moved down to the constructor
and marked it external
. In Vyper, what we know in Solidity as constructor is called Initialization, written as __init__ . And the def there means definition, which is the same thing as a function in Vyper.
Understanding Data Types in Vyper
# @version 0.3.7
polar: public(bool)
integer: public(int128)
unsigned: public(uint256)
fraction: public(decimal)
wallet: public(address)
word: public(String[100])
@external
def init():
self.polar = False
self.integer = -1
self.unsigned = 123
self.fraction = 4.58
self.wallet = 0x664f0c37A817Cb08B807F213E9576aF21849F25d
self.word = "Now, I am getting better at Vyper as you can see"
There are a few data types you should know in Vyper. It has:
Polar - yes or no, true or false, successful or unsuccessful
Int - integers
Uint - Unsigned Integers
Decimal - Fraction
Address - Wallets
String[100] - Words
You can also create a struct this way:
struct John:
name: String[100]
age: uint256
Functions in Vyper
# @version ^0.3.7
event Wire:
sender: indexed(address)
amount: uint256
@external
@payable
def default():
log Wire(msg.sender, msg.value)
This is a simple function, we defined the visibility to be external
, and we marked it as payable
to take in ether.
Note that default is a global variable which is synonymous with fallback function
. In Vyper, we don’t “emit,” we “log” instead.
Sending Ether in Vyper
There are two ways to send ether in Vyper: send and raw call. But send is the most used method. It is similar to how we use send
in Solidity
@external
def sendEther(to: address, amount: uint256):
send(to, amount)
3 Awesome Resources for Learning Vyper
Apparently, I cannot teach you everything about writing Vyper contracts in this single content (indicate in the comments if you want me to create more tutorials for you to learn the language).
There are mainly 3 resources I am using, and I will recommend them to you.
The Vyper Documentation
Generally, you need to read the documentation of any language to have an in-depth insight into it.
The first time I thought of learning Vyper, I went straight to the documentation. By the way, their doc is interesting to read, and it doesn’t suck.
If you want to go deep, I will encourage you to also study the official documentation.
Vyper by Example
You cannot learn coding by only reading about coding; the only way to become proficient with Vyper is to start writing Vyper contracts.
This is why I love Vyper by Example. It is similar to how Solidity by Example was structured.
This resource will show you concepts in Vyper, and how you can start writing immediately. Using Vyper by Example helped me put my knowledge to work.
Smart Contract Programmer on YouTube
For anyone learning Ethereum smart contract engineering, you should probably know this YouTube channel.
Sometimes, when I don’t understand something in the doc or Vyper by Example, I simply watch how Smart Contract Programmer explains it.
The channel has a Vyper playlist I will encourage you to learn from.
Twitter Chads to Follow for Vyper Content
Sudo: He is a good security researcher and contributes to Vyper development. He also created Snekmate, which is one of the Vyper resources I plan to study next.
Patrick: I mean, this has been the man holding everyone's hand in smart contract development.
These are the two people I noticed are good with Vyper. There are undoubtedly many more.
Get Bitten By Vyper
At the beginning of this discourse, I told you I'll teach you the conceptual underpinnings of Vyper and how you can code with it.
We have achieved that, but there is more.
You need to consume more Vyper resources and write more contracts with it. The more you write contracts, the better you get.
I hope this short handbook gives many developers the push they need to get bitten by Vyper.
I have a Web3 developer-focused marketing company named Blockchain Alpha; you can reach out for revenue-driven marketing for your Web3 Startup.
If you don't need our services now, be kind enough to follow on Twitter.