For Developers

How Does a Smart Contract Work in Blockchain Using Vyper?

smart contract on blockchain using vyper

A smart contract is a program that is stored on the blockchain and executes an agreement when certain conditions are met. It can be created using a programming language like Vyper, which executes on the Ethereum Virtual Machine (EVM), a decentralized computer with millions of executable projects. Vyper is a Python-based language and Vyper smart contracts are saved with .vy extensions. This article will look at how to build secure smart contracts using Vyper.

An overview of smart contracts using blockchain

Smart contracts store rules for negotiating the terms of a contract. The conditions are directly written into the code. They automatically verify the conditions when fulfilled and then execute the agreed-upon terms. There is no third party; those who are involved in the contract can directly sign it. This results in transparency, efficiency, security, speed, and cost reduction. Small wonder that companies are increasingly turning to smart contracts instead of physical ones.

Smart contract blockchain examples

Let’s understand how a smart contract works using a couple of examples.

Example 1:

Consider a crowdfunding platform where anyone can donate money to save patients in need of expensive treatment. Typically, patients’ families must pay the platform to advertise their needs and raise funds. The donated money goes to the platform and then to the patients. Clearly, the platform serves as a third party.

If a smart contract was included in the above scenario, it would include the patients’ health problems, hospital reports for proof, and facilitate donation collection. There would be no need for a third party.

A Vyper smart contract could be programmed to automatically execute all the actions with respect to certain terms and conditions. If enough funds are raised before the deadline, all the contributions are sent to the patients’ families and treatment can be administered quickly. If the treatment cannot be given, the funds collected are returned to the donors. In smart contracts, data is stored in a distributed manner. Hence, in the above example, no one person has control over the money.

Example 2:

Consider a scenario where Tanya buys Maria’s flat. Maria must provide her flat’s address while Tanya must pay the rent. In this case, a smart contract will work in the following manner:

  1. First, independent storage is created where Tanya and Maria can put their values but cannot easily take them out.

  2. Tanya puts her rent in her storage while Maria puts her address.

  3. When Maria receives the payment confirmation, Tanya receives the address.

  4. If Tanya goes to the flat and finds that the address provided by Maria is wrong, Tanya’s rent is returned.

  5. If Tanya suddenly changes her plan and doesn’t want the flat anymore, she will have to pay Maria some penalty as liquidated damages, and the remaining amount of her rent will be transferred back to her.

  6. At the end of the agreement, the smart contract is considered fulfilled and is stored in the blockchain network.

As smart contracts are designed and implemented within blockchains, they inherit their properties.

Features of smart contracts using blockchain.jpg

  • They are immutable, meaning that once they are made, no one can tamper with them.
  • They are distributed. Just like any transaction, the outcomes of the contract are validated by all the participants. It is impossible for an attacker to force control to release funds as any participant can detect the attempt and mark it invalid. This feature always ensures the integrity of the blockchain.

How to implement a smart contract on the blockchain using Vyper

The following steps are used to implement a Vyper smart contract.

Step 1: Install Vyper

Download Python 3.6 or higher versions from Python’s official website.

The pip package manager is essential to install Vyper using the following command:

pip install vyper

Step 2: Write the code

Let’s build a smart contract to store and display a user’s address.

Note: You will need to register with MetaMask to interact with Ethereum.

Vyper code to create a smart contract

Create a folder, vyper_contract, and a file, contract.vy, inside the folder. Contract.vy is the Vyper code file to create the necessary Vyper smart contract.

The corresponding Vyper code is as follows:

# @version ^0.3.1

address: public(String[100])

def __init__(addr: String[100]):
    self.address = addr

def getAddress() -> String[100]:
    return self.address

Code source

The first line is the version pragma. It informs the Vyper compiler to use version 0.3.1 to compile the Vyper smart contract. address is the state variable. As the name suggests, it stores the address of the user in the form of a string. An @external is used to create an external function which can be invoked from the outside. The __init__ function is the constructor of the Vyper smart contract. It comes into play when the contract is deployed.

The @view informs Vyper that the getAddress function does not change the state of the blockchain. It returns the address state variable which holds the address of the user.

Step 3: Compile the Vyper smart contract

This step checks to make sure the contract works as desired.

vyper <filename>.vy, or in this case, vyper contract.vy, is the command to compile the contract.

The output is a hexadecimal string that is the bytecode of the contract.

  • Compile the contract remotely using Remix IDE. In the IDE, create a new file called contract.vy.
  • Include the same Vyper code mentioned above in this remote file.
  • Install the Vyper plugin in the Remix IDE. Click on the socket icon in the sidebar and type “vyper” to search for the same.
  • Click Activate on the item that appears.
  • Click on the Vyper icon and press the Compile contract.vy button.

Step 4: Deploy the contract

Click the Ethereum icon in the sidebar.

From the ENVIRONMENT dropdown, select Injected Web3. A small UI beneath will change to the Ropsten Testnet you are connected to.

Click on Deploy and input a word beside the Deploy button. This word is the user’s address - the desired input.

Clicking the Deploy button will make the MetaMask UI appear.

Click Confirm.

The Vyper smart contract will be deployed to the Ropsten Testnet.

Step 5: Test the deployed contract

Now, interact with the contract deployed in the previous step to call the methods in the Remix browser IDE or view the deployed contract on the Ropsten Ethereum Faucet.

Click on the CONTRACT AT 0x26… dropdown. You will see the functions and state variables, getAddress and address. Choose getAddress.

It will return the word passed to the constructor during deployment in step 4. The action called the getAddress function in the contract.

A Vyper smart contract that can store and display user addresses has successfully been tested and deployed! Try your hand at it by using the codes above. You can also try it with different variables.

In this article, we looked at how to build a smart contract using the pythonic language, Vyper. We also touched on some of the features of a smart contract, which are similar to those of a blockchain.

Web 3.0 is the future of web technologies and blockchain technology is one of its main principles. As trust in blockchain grows, smart contracts - being secure, transparent, fast, and cheap - will soon become a lot more mainstream.


  • Author


    Author is a seasoned writer with a reputation for crafting highly engaging, well-researched, and useful content that is widely read by many of today's skilled programmers and developers.

Frequently Asked Questions

A smart contract is a digital agreement that stores rules for negotiating the terms of the contract, automatically verifies the fulfillment, and then executes the agreed terms.

Remix IDE is used to compile and deploy a Vyper smart contract remotely.

Vyper is a popular programming language used to write smart contracts. It is based on Python.

A smart contract is a program that is stored on the blockchain and executes an agreement after verifying that certain conditions are met. Vyper is often used to create a smart contract. It executes on EVM, which is a decentralized computer.

The various parts of a Vyper smart contract include version pragma, state variables, functions, events, interface, and structs.

Since a smart contract uses the blockchain, it inherits the same properties. These include being immutable, distributed, transparent, and secure.

View more FAQs


What's up with Turing? Get the latest news about us here.


Know more about remote work.
Checkout our blog here.


Have any questions?
We'd love to hear from you.

Hire and manage remote developers

Tell us the skills you need and we'll find the best developer for you in days, not weeks.

Hire Developers