How to write an Ethereum smart contract using Vyper
Code executing on EVM (Ethereum Virtual Machine) must be highly efficient to minimize the gas needed to execute smart contracts. Any inefficient code in a contract ultimately needs a higher gas fee to get executed, thus becoming prohibitively expensive, which is not ideal, especially in micro-transactions. So here is where Vyper comes in handy; with logical similarity with Solidity and syntactical similarity with Python, Vyper is becoming the choice of many smart contracts developers. So in this article let’s learn about writing an Ethereum smart contract using Vyper.
What is Vyper?
Vyper is a relatively new, pythonic programming language used to write smart contracts. Vyper targets Ethereum Virtual Machine (EVM). Vyper has very simple/intelligible syntax; one of the leading principles of Vyper is to make it virtually impossible for developers to code misleading programs.
One may ask why to use Vyper when we already have Solidity as the primary language to write smart contracts. In a study conducted in 2018, where they analyzed nearly one million deployed Ethereum smart contracts, it was found out that many of these contracts had some severe vulnerabilities; the researchers divided these contracts into three categories to better identify and trace vulnerabilities:
Suicidal Contracts - Smart contracts that arbitrary addresses can kill.
Greedy Contracts - Sometimes, smart contracts can reach a state where they are unable to release ether, smart contracts in this phase are called Greedy contracts.
Prodigal Contracts - Smart contracts that can be made to release ether to arbitrary addresses.
These vulnerabilities are produced in the smart contracts via code, which is unintentional, but regardless of the intentions, undesirable code may result in unexpected loss of funds for users. Vyper tries to eliminate this by letting users write secure code and making it difficult for programmers to write misleading or vulnerable code accidentally.
Following are features of Vyper:
It is strongly typed.
Has support for signed integers and decimal fixed-point numbers.
Small and understandable code like Python.
Has Bounds and Overflow checking.
Decidability: With Vyper, It is possible to compute a precise upper bound for any function call’s gas consumption.
Limited support for pure functions: Any constant in Vyper cannot change state.
Vyper vs Solidity:
Vyper tries to eliminate the possibility of writing unsafe code by omitting some of the Solidity’s features. It is crucial for the developers willing to write smart contracts in Vyper to understand the Solidity features which Vyper does not have; the following are the Solidity features which Vyper omits:
Vyper omits most of the Object-Oriented Programming paradigms - perhaps requires a new paradigm definition for transactional programming.
Getting testnet ETH
We’ll deploy our contract on the Ropsten testnet. To get started, you will need the Metamask browser extension to create an ETH wallet and some test ETH, which you can get by going to the Ropsten faucet. You'll need to select Ropsten Test Network on your Metamask wallet and copy-paste the wallet address into the text field in the faucet, then click Send me test Ether.
How to write and deploy a smart contract in Vyper:
The easiest way to install Vyper on your system is by using Python’s PIP package manager. Vyper requires Python 3.6 or higher installed on your system; you can check if Python is installed on not by typing the following in your terminal/cmd:
Line 1: Specifying that version of Vyper this contract is meant for.
Line 3: Declaring a public variable greet, which will be stored on the blockchain, the data type of greet is a string with a maximum length of the string to be 100.
Line 5-6: Marking our init function as @external means that it can only be called via transactions or other contracts. Defining our __init__ function, a pythonic constructor called upon object creation will initialize the greet variable.
Line 7: Passing a string “Hello World” to our variable greet.
Now go to your terminal/cmd and cd into the directory where you saved your contract and compile your Vyper contract by typing the following:
$ vyper contract.vy
$ vyper -f abi contract.vy
If the contract doesn’t have any errors, it will be compiled to a byte code and abi, which will look something like this::
Connect your Metamask wallet with the MyEtherWallet platform and copy-paste the Byte code and ABI from the terminal/cmd into the respective fields, type in the contract's name, and click on Sign Transaction, confirm the transaction from Metamask.
Now that our contract is deployed let’s interact with it. Go to the Activity section in your Metamask plugin, click on the last transaction (which deployed the contract), click on the Etherscan button from the top-right, and then copy the contract address.
Subscribe to our newsletter for more articles and guides on Ethereum. If you have any feedback, feel free to reach out to us via Twitter. You can always chat with us on our Discord community server, featuring some of the coolest developers you’ll ever meet :)